Coverage for tests/test_apdbCassandra.py: 36%

78 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2022-08-18 11:51 -0700

1# This file is part of dax_apdb. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# This program is free software: you can redistribute it and/or modify 

10# it under the terms of the GNU General Public License as published by 

11# the Free Software Foundation, either version 3 of the License, or 

12# (at your option) any later version. 

13# 

14# This program is distributed in the hope that it will be useful, 

15# but WITHOUT ANY WARRANTY; without even the implied warranty of 

16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

17# GNU General Public License for more details. 

18# 

19# You should have received a copy of the GNU General Public License 

20# along with this program. If not, see <http://www.gnu.org/licenses/>. 

21 

22"""Unit test for `ApdbCassandra` class. 

23 

24Notes 

25----- 

26For now this test can only run against actual Cassandra cluster, to specify 

27cluster location use ``DAX_APDB_TEST_CASSANDRA_CLUSTER`` environment variable, 

28e.g.: 

29 

30 export DAX_APDB_TEST_CASSANDRA_CLUSTER=cassandra.example.com 

31 pytest tests/test_apdbCassandra.py 

32 

33Individual tests create and destroy unique keyspaces in the cluster, there is 

34no need to pre-create a keyspace with predefined name. 

35""" 

36 

37import logging 

38import os 

39from typing import Any 

40import unittest 

41import uuid 

42 

43from lsst.dax.apdb import ApdbCassandra, ApdbCassandraConfig, ApdbConfig, ApdbTables 

44from lsst.dax.apdb.apdbCassandra import CASSANDRA_IMPORTED 

45from lsst.dax.apdb.tests import ApdbTest 

46import lsst.utils.tests 

47 

48TEST_SCHEMA = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config/schema.yaml") 

49 

50logging.basicConfig(level=logging.INFO) 

51 

52 

53class ApdbCassandraTestCase(unittest.TestCase, ApdbTest): 

54 """A test case for ApdbCassandra class 

55 """ 

56 

57 time_partition_tables = False 

58 time_partition_start = None 

59 time_partition_end = None 

60 fsrc_history_region_filtering = True 

61 

62 @classmethod 

63 def setUpClass(cls): 

64 """Prepare config for server connection. 

65 """ 

66 cluster_host = os.environ.get("DAX_APDB_TEST_CASSANDRA_CLUSTER") 

67 if not cluster_host: 

68 raise unittest.SkipTest("DAX_APDB_TEST_CASSANDRA_CLUSTER is not set") 

69 if not CASSANDRA_IMPORTED: 

70 raise unittest.SkipTest("cassandra_driver cannot be imported") 

71 

72 def setUp(self): 

73 """Prepare config for server connection. 

74 """ 

75 self.cluster_host = os.environ.get("DAX_APDB_TEST_CASSANDRA_CLUSTER") 

76 self.keyspace = "" 

77 

78 config = self.make_config() 

79 

80 # create dedicated keyspace for each test 

81 key = uuid.uuid4() 

82 self.keyspace = f"apdb_{key.hex}" 

83 query = f"CREATE KEYSPACE {self.keyspace}" \ 

84 " WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}" 

85 

86 apdb = ApdbCassandra(config) 

87 apdb._session.execute(query) 

88 del apdb 

89 

90 def tearDown(self): 

91 

92 config = self.make_config() 

93 apdb = ApdbCassandra(config) 

94 query = f"DROP KEYSPACE {self.keyspace}" 

95 apdb._session.execute(query) 

96 del apdb 

97 

98 def make_config(self, **kwargs: Any) -> ApdbConfig: 

99 """Make config class instance used in all tests.""" 

100 kw = { 

101 "contact_points": [self.cluster_host], 

102 "keyspace": self.keyspace, 

103 "schema_file": TEST_SCHEMA, 

104 "time_partition_tables": self.time_partition_tables, 

105 } 

106 if self.time_partition_start: 

107 kw["time_partition_start"] = self.time_partition_start 

108 if self.time_partition_end: 

109 kw["time_partition_end"] = self.time_partition_end 

110 kw.update(kwargs) 

111 return ApdbCassandraConfig(**kw) 

112 

113 def n_columns(self, table: ApdbTables) -> int: 

114 """Return number of columns for a specified table.""" 

115 

116 # Tables add one or two partitioning columns depending on config 

117 n_part_columns = 0 

118 if table is ApdbTables.DiaObjectLast: 

119 n_part_columns = 1 

120 else: 

121 if self.time_partition_tables: 

122 n_part_columns = 1 

123 else: 

124 n_part_columns = 2 

125 

126 if table is ApdbTables.DiaObject: 

127 return self.n_obj_columns + n_part_columns 

128 elif table is ApdbTables.DiaObjectLast: 

129 return self.n_obj_last_columns + n_part_columns 

130 elif table is ApdbTables.DiaSource: 

131 return self.n_src_columns + n_part_columns 

132 elif table is ApdbTables.DiaForcedSource: 

133 return self.n_fsrc_columns + n_part_columns 

134 elif table is ApdbTables.SSObject: 

135 return self.n_ssobj_columns 

136 

137 def getDiaObjects_table(self) -> ApdbTables: 

138 """Return type of table returned from getDiaObjects method.""" 

139 return ApdbTables.DiaObjectLast 

140 

141 

142class ApdbCassandraPerMonthTestCase(ApdbCassandraTestCase): 

143 """A test case for ApdbCassandra class with per-month tables. 

144 """ 

145 

146 time_partition_tables = True 

147 time_partition_start = "2019-12-01T00:00:00" 

148 time_partition_end = "2022-01-01T00:00:00" 

149 

150 

151class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase): 

152 pass 

153 

154 

155def setup_module(module): 

156 lsst.utils.tests.init() 

157 

158 

159if __name__ == "__main__": 159 ↛ 160line 159 didn't jump to line 160, because the condition on line 159 was never true

160 lsst.utils.tests.init() 

161 unittest.main()