Hide keyboard shortcuts

Hot-keys 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

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 

60 @classmethod 

61 def setUpClass(cls): 

62 """Prepare config for server connection. 

63 """ 

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

65 if not cluster_host: 

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

67 if not CASSANDRA_IMPORTED: 

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

69 

70 def setUp(self): 

71 """Prepare config for server connection. 

72 """ 

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

74 self.keyspace = "" 

75 

76 config = self.make_config() 

77 

78 # create dedicated keyspace for each test 

79 key = uuid.uuid4() 

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

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

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

83 

84 apdb = ApdbCassandra(config) 

85 apdb._session.execute(query) 

86 del apdb 

87 

88 def tearDown(self): 

89 

90 config = self.make_config() 

91 apdb = ApdbCassandra(config) 

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

93 apdb._session.execute(query) 

94 del apdb 

95 

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

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

98 kw = { 

99 "contact_points": [self.cluster_host], 

100 "keyspace": self.keyspace, 

101 "time_partition_tables": self.time_partition_tables, 

102 } 

103 if self.time_partition_start: 

104 kw["time_partition_start"] = self.time_partition_start 

105 if self.time_partition_end: 

106 kw["time_partition_end"] = self.time_partition_end 

107 kw.update(kwargs) 

108 return ApdbCassandraConfig(**kw) 

109 

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

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

112 

113 if table is ApdbTables.DiaObject: 

114 # DiaObjectLast is used 

115 table = ApdbTables.DiaObjectLast 

116 

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

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 

135 

136class ApdbCassandraPerMonthTestCase(ApdbCassandraTestCase): 

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

138 """ 

139 

140 time_partition_tables = True 

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

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

143 

144 

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

146 pass 

147 

148 

149def setup_module(module): 

150 lsst.utils.tests.init() 

151 

152 

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

154 lsst.utils.tests.init() 

155 unittest.main()