Coverage for tests/test_apdbCassandra.py: 46%

71 statements  

« prev     ^ index     » next       coverage.py v6.5.0, created at 2023-01-19 02:07 -0800

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 

39import unittest 

40import uuid 

41from typing import TYPE_CHECKING, Any, Optional 

42 

43import lsst.utils.tests 

44from lsst.dax.apdb import ApdbCassandra, ApdbCassandraConfig, ApdbTables 

45from lsst.dax.apdb.apdbCassandra import CASSANDRA_IMPORTED 

46from lsst.dax.apdb.tests import ApdbSchemaUpdateTest, ApdbTest 

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 ApdbCassandraMixin: 

54 """Mixin class which defines common methods for unit tests.""" 

55 

56 @classmethod 

57 def setUpClass(cls) -> None: 

58 """Prepare config for server connection.""" 

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

60 if not cluster_host: 

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

62 if not CASSANDRA_IMPORTED: 

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

64 

65 def setUp(self) -> None: 

66 """Prepare config for server connection.""" 

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

68 self.keyspace = "" 

69 

70 config = self.make_config() 

71 

72 # create dedicated keyspace for each test 

73 key = uuid.uuid4() 

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

75 query = ( 

76 f"CREATE KEYSPACE {self.keyspace}" 

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

78 ) 

79 

80 apdb = ApdbCassandra(config) 

81 apdb._session.execute(query) 

82 del apdb 

83 

84 def tearDown(self) -> None: 

85 

86 config = self.make_config() 

87 apdb = ApdbCassandra(config) 

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

89 apdb._session.execute(query) 

90 del apdb 

91 

92 if TYPE_CHECKING: 92 ↛ 94line 92 didn't jump to line 94, because the condition on line 92 was never true

93 # For mypy. 

94 def make_config(self, **kwargs: Any) -> ApdbCassandraConfig: 

95 ... 

96 

97 

98class ApdbCassandraTestCase(ApdbCassandraMixin, unittest.TestCase, ApdbTest): 

99 """A test case for ApdbCassandra class""" 

100 

101 time_partition_tables = False 

102 time_partition_start: Optional[str] = None 

103 time_partition_end: Optional[str] = None 

104 

105 def make_config(self, **kwargs: Any) -> ApdbCassandraConfig: 

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

107 kw = { 

108 "contact_points": [self.cluster_host], 

109 "keyspace": self.keyspace, 

110 "schema_file": TEST_SCHEMA, 

111 "time_partition_tables": self.time_partition_tables, 

112 "use_insert_id": self.use_insert_id, 

113 } 

114 if self.time_partition_start: 

115 kw["time_partition_start"] = self.time_partition_start 

116 if self.time_partition_end: 

117 kw["time_partition_end"] = self.time_partition_end 

118 kw.update(kwargs) 

119 return ApdbCassandraConfig(**kw) 

120 

121 def getDiaObjects_table(self) -> ApdbTables: 

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

123 return ApdbTables.DiaObjectLast 

124 

125 

126class ApdbCassandraPerMonthTestCase(ApdbCassandraTestCase): 

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

128 

129 time_partition_tables = True 

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

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

132 

133 

134class ApdbCassandraTestCaseInsertIds(ApdbCassandraTestCase): 

135 """A test case with use_insert_id.""" 

136 

137 use_insert_id = True 

138 

139 

140class ApdbSchemaUpdateCassandraTestCase(ApdbCassandraMixin, unittest.TestCase, ApdbSchemaUpdateTest): 

141 """A test case for schema updates using Cassandra backend.""" 

142 

143 def make_config(self, **kwargs: Any) -> ApdbCassandraConfig: 

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

145 kw = { 

146 "contact_points": [self.cluster_host], 

147 "keyspace": self.keyspace, 

148 "schema_file": TEST_SCHEMA, 

149 "time_partition_tables": False, 

150 } 

151 kw.update(kwargs) 

152 return ApdbCassandraConfig(**kw) 

153 

154 

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

156 pass 

157 

158 

159def setup_module(module: Any) -> None: 

160 lsst.utils.tests.init() 

161 

162 

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

164 lsst.utils.tests.init() 

165 unittest.main()