Coverage for tests/test_apdbCassandra.py: 36%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

77 statements  

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 

48 

49logging.basicConfig(level=logging.INFO) 

50 

51 

52class ApdbCassandraTestCase(unittest.TestCase, ApdbTest): 

53 """A test case for ApdbCassandra class 

54 """ 

55 

56 time_partition_tables = False 

57 time_partition_start = None 

58 time_partition_end = None 

59 fsrc_history_region_filtering = True 

60 

61 @classmethod 

62 def setUpClass(cls): 

63 """Prepare config for server connection. 

64 """ 

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

66 if not cluster_host: 

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

68 if not CASSANDRA_IMPORTED: 

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

70 

71 def setUp(self): 

72 """Prepare config for server connection. 

73 """ 

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

75 self.keyspace = "" 

76 

77 config = self.make_config() 

78 

79 # create dedicated keyspace for each test 

80 key = uuid.uuid4() 

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

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

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

84 

85 apdb = ApdbCassandra(config) 

86 apdb._session.execute(query) 

87 del apdb 

88 

89 def tearDown(self): 

90 

91 config = self.make_config() 

92 apdb = ApdbCassandra(config) 

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

94 apdb._session.execute(query) 

95 del apdb 

96 

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

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

99 kw = { 

100 "contact_points": [self.cluster_host], 

101 "keyspace": self.keyspace, 

102 "time_partition_tables": self.time_partition_tables, 

103 } 

104 if self.time_partition_start: 

105 kw["time_partition_start"] = self.time_partition_start 

106 if self.time_partition_end: 

107 kw["time_partition_end"] = self.time_partition_end 

108 kw.update(kwargs) 

109 return ApdbCassandraConfig(**kw) 

110 

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

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

113 

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

115 n_part_columns = 0 

116 if table is ApdbTables.DiaObjectLast: 

117 n_part_columns = 1 

118 else: 

119 if self.time_partition_tables: 

120 n_part_columns = 1 

121 else: 

122 n_part_columns = 2 

123 

124 if table is ApdbTables.DiaObject: 

125 return self.n_obj_columns + n_part_columns 

126 elif table is ApdbTables.DiaObjectLast: 

127 return self.n_obj_last_columns + n_part_columns 

128 elif table is ApdbTables.DiaSource: 

129 return self.n_src_columns + n_part_columns 

130 elif table is ApdbTables.DiaForcedSource: 

131 return self.n_fsrc_columns + n_part_columns 

132 elif table is ApdbTables.SSObject: 

133 return self.n_ssobj_columns 

134 

135 def getDiaObjects_table(self) -> ApdbTables: 

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

137 return ApdbTables.DiaObjectLast 

138 

139 

140class ApdbCassandraPerMonthTestCase(ApdbCassandraTestCase): 

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

142 """ 

143 

144 time_partition_tables = True 

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

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

147 

148 

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

150 pass 

151 

152 

153def setup_module(module): 

154 lsst.utils.tests.init() 

155 

156 

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

158 lsst.utils.tests.init() 

159 unittest.main()