Coverage for toardb/data/models.py: 100%

23 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2025-11-03 20:32 +0000

1# SPDX-FileCopyrightText: 2021 Forschungszentrum Jülich GmbH 

2# SPDX-License-Identifier: MIT 

3 

4from sqlalchemy import PrimaryKeyConstraint, Column, DateTime, Float, ForeignKey, Integer, text, \ 

5 Table, String, CHAR 

6from sqlalchemy.orm import relationship 

7from sqlalchemy.sql.sqltypes import NullType 

8from sqlalchemy.dialects.postgresql import JSONB 

9 

10from toardb.timeseries.models import Timeseries 

11from toardb.base import Base 

12 

13 

14class Data(Base): 

15 """ 

16 Table "public.data"  

17 

18 +---------------+--------------------------+-----------+----------+----------------------------------------+ 

19 |Column |Type |Collation |Nullable |Default | 

20 +===============+==========================+===========+==========+========================================+ 

21 | datetime | timestamp with time zone | | not null | | 

22 +---------------+--------------------------+-----------+----------+----------------------------------------+  

23 | value | double precision | | not null | | 

24 +---------------+--------------------------+-----------+----------+----------------------------------------+  

25 | flags | integer | | not null | | 

26 +---------------+--------------------------+-----------+----------+----------------------------------------+  

27 | version | character(28) | | not null | '000001.000000.00000000000000'::bpchar | 

28 +---------------+--------------------------+-----------+----------+----------------------------------------+  

29 | timeseries_id | integer | | not null | | 

30 +---------------+--------------------------+-----------+----------+----------------------------------------+  

31 

32 Indexes:  

33 "data_pkey" PRIMARY KEY, btree (timeseries_id, datetime)  

34 "data_datetime_idx" btree (datetime)  

35 "data_timeseries_id_idx" btree (timeseries_id)  

36 "data_value_idx" btree (value)  

37 Check constraints:  

38 "data_flags_check" CHECK (flags >= 0)  

39 Foreign-key constraints: 

40 "data_flags_fk_df_vocabulary_enum_val" FOREIGN KEY (flags) REFERENCES df_vocabulary(enum_val) 

41 "data_timeseries_id_a38c5a1a_fk_timeseries_id" FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) DEFERRABLE INITIALLY DEFERRED 

42 """ 

43 

44 __tablename__ = 'data' 

45 __table_args__ = ( 

46 PrimaryKeyConstraint('timeseries_id', 'datetime'), 

47 ) 

48 

49 datetime = Column(DateTime(True), nullable=False, index=True) 

50 value = Column(Float(53), nullable=False, index=True) 

51 flags = Column(ForeignKey('df_vocabulary.enum_val'), nullable=False) 

52 version = Column(CHAR(28), nullable=False, server_default=text("'000001.000000.00000000000000'::bpchar")) 

53# do not use string declaration here (not working for pytest) 

54# use the explicit class name here, 

55# see: https://groups.google.com/forum/#!topic/sqlalchemy/YjGhE4d6K4U 

56 timeseries_id = Column(ForeignKey(Timeseries.id, deferrable=True, initially='DEFERRED'), nullable=False, index=True) 

57 

58class DataArchive(Base): 

59 """ 

60 Table "public.data_archive"  

61 

62 +---------------+--------------------------+-----------+----------+----------------------------------------+ 

63 |Column |Type |Collation |Nullable |Default | 

64 +===============+==========================+===========+==========+========================================+ 

65 | datetime | timestamp with time zone | | not null | | 

66 +---------------+--------------------------+-----------+----------+----------------------------------------+  

67 | value | double precision | | not null | | 

68 +---------------+--------------------------+-----------+----------+----------------------------------------+  

69 | flags | integer | | not null | | 

70 +---------------+--------------------------+-----------+----------+----------------------------------------+  

71 | version | character(28) | | not null | '000001.000000.00000000000000'::bpchar | 

72 +---------------+--------------------------+-----------+----------+----------------------------------------+  

73 | timeseries_id | integer | | not null | | 

74 +---------------+--------------------------+-----------+----------+----------------------------------------+  

75 

76 Indexes:  

77 "data_archive_pkey" PRIMARY KEY, btree (timeseries_id, datetime)  

78 "data_archive_datetime_idx" btree (datetime)  

79 "data_archive_timeseries_id_idx" btree (timeseries_id)  

80 "data_archive_value_idx" btree (value)  

81 Check constraints:  

82 "data_archive_flags_check" CHECK (flags >= 0)  

83 Foreign-key constraints: 

84 "data_archive_flags_fk_df_vocabulary_enum_val" FOREIGN KEY (flags) REFERENCES df_vocabulary(enum_val) 

85 "data_archive_timeseries_id_fk_timeseries_id" FOREIGN KEY (timeseries_id) REFERENCES timeseries(id) DEFERRABLE INITIALLY DEFERRED 

86 """ 

87 

88 __tablename__ = 'data_archive' 

89 __table_args__ = ( 

90 PrimaryKeyConstraint('timeseries_id', 'datetime'), 

91 ) 

92 

93 datetime = Column(DateTime(True), nullable=False, index=True) 

94 value = Column(Float(53), nullable=False, index=True) 

95 flags = Column(ForeignKey('df_vocabulary.enum_val'), nullable=False) 

96 version = Column(CHAR(28), nullable=False, server_default=text("'000001.000000.00000000000000'::bpchar")) 

97 timeseries_id = Column(ForeignKey(Timeseries.id, deferrable=True, initially='DEFERRED'), nullable=False, index=True) 

98 

99# controlled vocabulary 

100 

101# Data Access Rights 

102DF_enum_table = Table("df_vocabulary", 

103 Base.metadata, 

104 Column("enum_val", Integer, primary_key=True), 

105 Column("enum_str", String), 

106 Column("enum_display_str", String) 

107 ) 

108