-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_postgresql.py
258 lines (220 loc) · 10.2 KB
/
test_postgresql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# This file is part of daf_butler.
#
# Developed for the LSST Data Management System.
# This product includes software developed by the LSST Project
# (http://www.lsst.org).
# See the COPYRIGHT file at the top-level directory of this distribution
# for details of code ownership.
#
# This software is dual licensed under the GNU General Public License and also
# under a 3-clause BSD license. Recipients may choose which of these licenses
# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
# respectively. If you choose the GPL option then the following text applies
# (but note that there is still no warranty even if you opt for BSD instead):
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import annotations
import itertools
import os
import unittest
import warnings
from contextlib import contextmanager
import astropy.time
import sqlalchemy
from lsst.daf.butler import Butler, ButlerConfig, StorageClassFactory, Timespan, ddl
from lsst.daf.butler.datastore import NullDatastore
from lsst.daf.butler.direct_butler import DirectButler
from lsst.daf.butler.registry import _RegistryFactory
from lsst.daf.butler.tests.postgresql import setup_postgres_test_db
try:
from lsst.daf.butler.registry.databases.postgresql import PostgresqlDatabase, _RangeTimespanType
except ImportError:
PostgresqlDatabase = None
from lsst.daf.butler.registry.tests import DatabaseTests, RegistryTests
TESTDIR = os.path.abspath(os.path.dirname(__file__))
@unittest.skipUnless(PostgresqlDatabase is not None, "Couldn't load PostgresqlDatabase")
class PostgresqlDatabaseTestCase(unittest.TestCase, DatabaseTests):
"""Test a postgres Registry."""
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.postgres = cls.enterClassContext(setup_postgres_test_db())
def makeEmptyDatabase(self, origin: int = 0) -> PostgresqlDatabase:
return PostgresqlDatabase.fromUri(
origin=origin, uri=self.postgres.url, namespace=self.postgres.generate_namespace_name()
)
def getNewConnection(self, database: PostgresqlDatabase, *, writeable: bool) -> PostgresqlDatabase:
return PostgresqlDatabase.fromUri(
origin=database.origin, uri=self.postgres.url, namespace=database.namespace, writeable=writeable
)
@contextmanager
def asReadOnly(self, database: PostgresqlDatabase) -> PostgresqlDatabase:
yield self.getNewConnection(database, writeable=False)
def testNameShrinking(self):
"""Test that too-long names for database entities other than tables
and columns (which we preserve, and just expect to fit) are shrunk.
"""
db = self.makeEmptyDatabase(origin=1)
with db.declareStaticTables(create=True) as context:
# Table and field names are each below the 63-char limit even when
# accounting for the prefix, but their combination (which will
# appear in sequences and constraints) is not.
tableName = "a_table_with_a_very_very_long_42_char_name"
fieldName1 = "a_column_with_a_very_very_long_43_char_name"
fieldName2 = "another_column_with_a_very_very_long_49_char_name"
context.addTable(
tableName,
ddl.TableSpec(
fields=[
ddl.FieldSpec(
fieldName1, dtype=sqlalchemy.BigInteger, autoincrement=True, primaryKey=True
),
ddl.FieldSpec(
fieldName2,
dtype=sqlalchemy.String,
length=16,
nullable=False,
),
],
unique={(fieldName2,)},
),
)
# Add another table, this time dynamically, with a foreign key to the
# first table.
db.ensureTableExists(
tableName + "_b",
ddl.TableSpec(
fields=[
ddl.FieldSpec(
fieldName1 + "_b", dtype=sqlalchemy.BigInteger, autoincrement=True, primaryKey=True
),
ddl.FieldSpec(
fieldName2 + "_b",
dtype=sqlalchemy.String,
length=16,
nullable=False,
),
],
foreignKeys=[
ddl.ForeignKeySpec(tableName, source=(fieldName2 + "_b",), target=(fieldName2,)),
],
),
)
def test_RangeTimespanType(self):
start = astropy.time.Time("2020-01-01T00:00:00", format="isot", scale="tai")
offset = astropy.time.TimeDelta(60, format="sec")
timestamps = [start + offset * n for n in range(3)]
timespans = [Timespan(begin=None, end=None)]
timespans.extend(Timespan(begin=None, end=t) for t in timestamps)
timespans.extend(Timespan(begin=t, end=None) for t in timestamps)
timespans.extend(Timespan(begin=a, end=b) for a, b in itertools.combinations(timestamps, 2))
db = self.makeEmptyDatabase(origin=1)
with db.declareStaticTables(create=True) as context:
tbl = context.addTable(
"tbl",
ddl.TableSpec(
fields=[
ddl.FieldSpec(name="id", dtype=sqlalchemy.Integer, primaryKey=True),
ddl.FieldSpec(name="timespan", dtype=_RangeTimespanType),
],
),
)
rows = [{"id": n, "timespan": t} for n, t in enumerate(timespans)]
db.insert(tbl, *rows)
# Test basic round-trip through database.
with db.query(tbl.select().order_by(tbl.columns.id)) as sql_result:
self.assertEqual(rows, [row._asdict() for row in sql_result])
# Test that Timespan's Python methods are consistent with our usage of
# half-open ranges and PostgreSQL operators on ranges.
def subquery(alias: str) -> sqlalchemy.sql.FromClause:
return (
sqlalchemy.sql.select(tbl.columns.id.label("id"), tbl.columns.timespan.label("timespan"))
.select_from(tbl)
.alias(alias)
)
sq1 = subquery("sq1")
sq2 = subquery("sq2")
query = sqlalchemy.sql.select(
sq1.columns.id.label("n1"),
sq2.columns.id.label("n2"),
sq1.columns.timespan.overlaps(sq2.columns.timespan).label("overlaps"),
)
# `columns` is deprecated since 1.4, but
# `selected_columns` method did not exist in 1.3.
if hasattr(query, "selected_columns"):
columns = query.selected_columns
else:
columns = query.columns
# SQLAlchemy issues a warning about cartesian product of two tables,
# which we do intentionally. Disable that warning temporarily.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore", message=".*cartesian product", category=sqlalchemy.exc.SAWarning
)
with db.query(query) as sql_result:
dbResults = {
(row[columns.n1], row[columns.n2]): row[columns.overlaps] for row in sql_result.mappings()
}
pyResults = {
(n1, n2): t1.overlaps(t2)
for (n1, t1), (n2, t2) in itertools.product(enumerate(timespans), enumerate(timespans))
}
self.assertEqual(pyResults, dbResults)
class PostgresqlRegistryTests(RegistryTests):
"""Tests for `Registry` backed by a PostgreSQL database.
Notes
-----
This is not a subclass of `unittest.TestCase` but to avoid repetition it
defines methods that override `unittest.TestCase` methods. To make this
work subclasses have to have this class first in the bases list.
"""
sometimesHasDuplicateQueryRows = True
supportsCalibrationCollectionInFindFirst = False
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.postgres = cls.enterClassContext(setup_postgres_test_db())
@classmethod
def getDataDir(cls) -> str:
return os.path.normpath(os.path.join(os.path.dirname(__file__), "data", "registry"))
def make_butler(self) -> Butler:
config = self.makeRegistryConfig()
self.postgres.patch_registry_config(config)
registry = _RegistryFactory(config).create_from_config()
return DirectButler(
config=ButlerConfig(),
registry=registry,
datastore=NullDatastore(None, None),
storageClasses=StorageClassFactory(),
)
class PostgresqlRegistryNameKeyCollMgrUUIDTestCase(PostgresqlRegistryTests, unittest.TestCase):
"""Tests for `Registry` backed by a PostgreSQL database.
This test case uses NameKeyCollectionManager and
ByDimensionsDatasetRecordStorageManagerUUID.
"""
collectionsManager = "lsst.daf.butler.registry.collections.nameKey.NameKeyCollectionManager"
datasetsManager = (
"lsst.daf.butler.registry.datasets.byDimensions.ByDimensionsDatasetRecordStorageManagerUUID"
)
class PostgresqlRegistrySynthIntKeyCollMgrUUIDTestCase(PostgresqlRegistryTests, unittest.TestCase):
"""Tests for `Registry` backed by a PostgreSQL database.
This test case uses SynthIntKeyCollectionManager and
ByDimensionsDatasetRecordStorageManagerUUID.
"""
collectionsManager = "lsst.daf.butler.registry.collections.synthIntKey.SynthIntKeyCollectionManager"
datasetsManager = (
"lsst.daf.butler.registry.datasets.byDimensions.ByDimensionsDatasetRecordStorageManagerUUID"
)
if __name__ == "__main__":
unittest.main()