Coverage for tests/test_apdbCassandra.py: 46%
71 statements
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-16 01:36 -0700
« prev ^ index » next coverage.py v7.2.5, created at 2023-05-16 01:36 -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/>.
22"""Unit test for `ApdbCassandra` class.
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.:
30 export DAX_APDB_TEST_CASSANDRA_CLUSTER=cassandra.example.com
31 pytest tests/test_apdbCassandra.py
33Individual tests create and destroy unique keyspaces in the cluster, there is
34no need to pre-create a keyspace with predefined name.
35"""
37import logging
38import os
39import unittest
40import uuid
41from typing import TYPE_CHECKING, Any, Optional
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
48TEST_SCHEMA = os.path.join(os.path.abspath(os.path.dirname(__file__)), "config/schema.yaml")
50logging.basicConfig(level=logging.INFO)
53class ApdbCassandraMixin:
54 """Mixin class which defines common methods for unit tests."""
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")
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 = ""
70 config = self.make_config()
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 )
80 apdb = ApdbCassandra(config)
81 apdb._session.execute(query)
82 del apdb
84 def tearDown(self) -> None:
85 config = self.make_config()
86 apdb = ApdbCassandra(config)
87 query = f"DROP KEYSPACE {self.keyspace}"
88 apdb._session.execute(query)
89 del apdb
91 if TYPE_CHECKING: 91 ↛ 93line 91 didn't jump to line 93, because the condition on line 91 was never true
92 # For mypy.
93 def make_config(self, **kwargs: Any) -> ApdbCassandraConfig:
94 ...
97class ApdbCassandraTestCase(ApdbCassandraMixin, unittest.TestCase, ApdbTest):
98 """A test case for ApdbCassandra class"""
100 time_partition_tables = False
101 time_partition_start: Optional[str] = None
102 time_partition_end: Optional[str] = None
104 def make_config(self, **kwargs: Any) -> ApdbCassandraConfig:
105 """Make config class instance used in all tests."""
106 kw = {
107 "contact_points": [self.cluster_host],
108 "keyspace": self.keyspace,
109 "schema_file": TEST_SCHEMA,
110 "time_partition_tables": self.time_partition_tables,
111 "use_insert_id": self.use_insert_id,
112 }
113 if self.time_partition_start:
114 kw["time_partition_start"] = self.time_partition_start
115 if self.time_partition_end:
116 kw["time_partition_end"] = self.time_partition_end
117 kw.update(kwargs)
118 return ApdbCassandraConfig(**kw)
120 def getDiaObjects_table(self) -> ApdbTables:
121 """Return type of table returned from getDiaObjects method."""
122 return ApdbTables.DiaObjectLast
125class ApdbCassandraPerMonthTestCase(ApdbCassandraTestCase):
126 """A test case for ApdbCassandra class with per-month tables."""
128 time_partition_tables = True
129 time_partition_start = "2019-12-01T00:00:00"
130 time_partition_end = "2022-01-01T00:00:00"
133class ApdbCassandraTestCaseInsertIds(ApdbCassandraTestCase):
134 """A test case with use_insert_id."""
136 use_insert_id = True
139class ApdbSchemaUpdateCassandraTestCase(ApdbCassandraMixin, unittest.TestCase, ApdbSchemaUpdateTest):
140 """A test case for schema updates using Cassandra backend."""
142 def make_config(self, **kwargs: Any) -> ApdbCassandraConfig:
143 """Make config class instance used in all tests."""
144 kw = {
145 "contact_points": [self.cluster_host],
146 "keyspace": self.keyspace,
147 "schema_file": TEST_SCHEMA,
148 "time_partition_tables": False,
149 }
150 kw.update(kwargs)
151 return ApdbCassandraConfig(**kw)
154class MyMemoryTestCase(lsst.utils.tests.MemoryTestCase):
155 pass
158def setup_module(module: Any) -> None:
159 lsst.utils.tests.init()
162if __name__ == "__main__": 162 ↛ 163line 162 didn't jump to line 163, because the condition on line 162 was never true
163 lsst.utils.tests.init()
164 unittest.main()