-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_sqlite.py
321 lines (256 loc) · 13.2 KB
/
test_sqlite.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# 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/>.
import os
import os.path
import stat
import tempfile
import unittest
from contextlib import contextmanager
import sqlalchemy
from lsst.daf.butler import Butler, Config, ddl
from lsst.daf.butler.registry import _RegistryFactory
from lsst.daf.butler.registry.databases.sqlite import SqliteDatabase
from lsst.daf.butler.registry.tests import DatabaseTests, RegistryTests
from lsst.daf.butler.tests import makeTestRepo
from lsst.daf.butler.tests.utils import create_populated_sqlite_registry, makeTestTempDir, removeTestTempDir
TESTDIR = os.path.abspath(os.path.dirname(__file__))
@contextmanager
def removeWritePermission(filename):
"""Remove the write permission on a file."""
mode = os.stat(filename).st_mode
try:
os.chmod(filename, stat.S_IREAD)
yield
finally:
os.chmod(filename, mode)
def isEmptyDatabaseActuallyWriteable(database: SqliteDatabase) -> bool:
"""Check whether we really can modify a database.
This intentionally allows any exception to be raised (not just
`ReadOnlyDatabaseError`) to deal with cases where the file is read-only
but the Database was initialized (incorrectly) with writeable=True.
"""
try:
with database.declareStaticTables(create=True) as context:
table = context.addTable(
"a", ddl.TableSpec(fields=[ddl.FieldSpec("b", dtype=sqlalchemy.Integer, primaryKey=True)])
)
# Drop created table so that schema remains empty.
database._metadata._metadata.drop_all(database._engine, tables=[table])
return True
except Exception:
return False
class SqliteFileDatabaseTestCase(unittest.TestCase, DatabaseTests):
"""Tests for `SqliteDatabase` using a standard file-based database."""
def setUp(self):
self.root = makeTestTempDir(TESTDIR)
def tearDown(self):
removeTestTempDir(self.root)
def makeEmptyDatabase(self, origin: int = 0) -> SqliteDatabase:
_, filename = tempfile.mkstemp(dir=self.root, suffix=".sqlite3")
engine = SqliteDatabase.makeEngine(filename=filename)
return SqliteDatabase.fromEngine(engine=engine, origin=origin)
def getNewConnection(self, database: SqliteDatabase, *, writeable: bool) -> SqliteDatabase:
engine = SqliteDatabase.makeEngine(filename=database.filename, writeable=writeable)
return SqliteDatabase.fromEngine(origin=database.origin, engine=engine, writeable=writeable)
@contextmanager
def asReadOnly(self, database: SqliteDatabase) -> SqliteDatabase:
with removeWritePermission(database.filename):
yield self.getNewConnection(database, writeable=False)
def testConnection(self):
"""Test that different ways of connecting to a SQLite database
are equivalent.
"""
_, filename = tempfile.mkstemp(dir=self.root, suffix=".sqlite3")
# Create a read-write database by passing in the filename.
rwFromFilename = SqliteDatabase.fromEngine(SqliteDatabase.makeEngine(filename=filename), origin=0)
self.assertEqual(os.path.realpath(rwFromFilename.filename), os.path.realpath(filename))
self.assertEqual(rwFromFilename.origin, 0)
self.assertTrue(rwFromFilename.isWriteable())
self.assertTrue(isEmptyDatabaseActuallyWriteable(rwFromFilename))
# Create a read-write database via a URI.
rwFromUri = SqliteDatabase.fromUri(f"sqlite:///{filename}", origin=0)
self.assertEqual(os.path.realpath(rwFromUri.filename), os.path.realpath(filename))
self.assertEqual(rwFromUri.origin, 0)
self.assertTrue(rwFromUri.isWriteable())
self.assertTrue(isEmptyDatabaseActuallyWriteable(rwFromUri))
# We don't support SQLite URIs inside SQLAlchemy URIs.
with self.assertRaises(NotImplementedError):
SqliteDatabase.makeEngine(uri=f"sqlite:///file:{filename}?uri=true")
# Test read-only connections against a read-only file.
with removeWritePermission(filename):
# Create a read-only database by passing in the filename.
roFromFilename = SqliteDatabase.fromEngine(
SqliteDatabase.makeEngine(filename=filename), origin=0, writeable=False
)
self.assertEqual(os.path.realpath(roFromFilename.filename), os.path.realpath(filename))
self.assertEqual(roFromFilename.origin, 0)
self.assertFalse(roFromFilename.isWriteable())
self.assertFalse(isEmptyDatabaseActuallyWriteable(roFromFilename))
# Create a read-write database via a URI.
roFromUri = SqliteDatabase.fromUri(f"sqlite:///{filename}", origin=0, writeable=False)
self.assertEqual(os.path.realpath(roFromUri.filename), os.path.realpath(filename))
self.assertEqual(roFromUri.origin, 0)
self.assertFalse(roFromUri.isWriteable())
self.assertFalse(isEmptyDatabaseActuallyWriteable(roFromUri))
def testTransactionLocking(self):
# This (inherited) test can't run on SQLite because of our use of an
# aggressive locking strategy there.
pass
class SqliteMemoryDatabaseTestCase(unittest.TestCase, DatabaseTests):
"""Tests for `SqliteDatabase` using an in-memory database."""
def makeEmptyDatabase(self, origin: int = 0) -> SqliteDatabase:
engine = SqliteDatabase.makeEngine(filename=None)
return SqliteDatabase.fromEngine(engine=engine, origin=origin)
def getNewConnection(self, database: SqliteDatabase, *, writeable: bool) -> SqliteDatabase:
return SqliteDatabase.fromEngine(origin=database.origin, engine=database._engine, writeable=writeable)
@contextmanager
def asReadOnly(self, database: SqliteDatabase) -> SqliteDatabase:
yield self.getNewConnection(database, writeable=False)
def testConnection(self):
"""Test that different ways of connecting to a SQLite database
are equivalent.
"""
# Create an in-memory database by passing filename=None.
memFromFilename = SqliteDatabase.fromEngine(SqliteDatabase.makeEngine(filename=None), origin=0)
self.assertIsNone(memFromFilename.filename)
self.assertEqual(memFromFilename.origin, 0)
self.assertTrue(memFromFilename.isWriteable())
self.assertTrue(isEmptyDatabaseActuallyWriteable(memFromFilename))
# Create an in-memory database via a URI.
memFromUri = SqliteDatabase.fromUri("sqlite://", origin=0)
self.assertIsNone(memFromUri.filename)
self.assertEqual(memFromUri.origin, 0)
self.assertTrue(memFromUri.isWriteable())
self.assertTrue(isEmptyDatabaseActuallyWriteable(memFromUri))
# We don't support SQLite URIs inside SQLAlchemy URIs.
with self.assertRaises(NotImplementedError):
SqliteDatabase.makeEngine(uri="sqlite:///:memory:?uri=true")
# We don't support read-only in-memory databases.
with self.assertRaises(NotImplementedError):
SqliteDatabase.makeEngine(filename=None, writeable=False)
def testTransactionLocking(self):
# This (inherited) test can't run on SQLite because of our use of an
# aggressive locking strategy there.
pass
class SqliteFileRegistryTests(RegistryTests):
"""Tests for `Registry` backed by a SQLite file-based 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 sublasses have to have this class first in the bases list.
"""
sometimesHasDuplicateQueryRows = True
supportsCalibrationCollectionInFindFirst = False
def setUp(self):
self.root = makeTestTempDir(TESTDIR)
def tearDown(self):
removeTestTempDir(self.root)
@classmethod
def getDataDir(cls) -> str:
return os.path.normpath(os.path.join(os.path.dirname(__file__), "data", "registry"))
def make_butler(self) -> Butler:
config = Config()
config["registry"] = self.makeRegistryConfig()
return makeTestRepo(self.root, config=config)
class SqliteFileRegistryNameKeyCollMgrUUIDTestCase(SqliteFileRegistryTests, unittest.TestCase):
"""Tests for `Registry` backed by a SQLite file-based 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 ClonedSqliteFileRegistryNameKeyCollMgrUUIDTestCase(
SqliteFileRegistryNameKeyCollMgrUUIDTestCase, unittest.TestCase
):
"""Test that NameKeyCollectionManager still works after cloning."""
def make_butler(self) -> Butler:
original = super().make_butler()
return original.clone()
class SqliteFileRegistrySynthIntKeyCollMgrUUIDTestCase(SqliteFileRegistryTests, unittest.TestCase):
"""Tests for `Registry` backed by a SQLite file-based database.
This test case uses SynthIntKeyCollectionManager and
ByDimensionsDatasetRecordStorageManagerUUID.
"""
collectionsManager = "lsst.daf.butler.registry.collections.synthIntKey.SynthIntKeyCollectionManager"
datasetsManager = (
"lsst.daf.butler.registry.datasets.byDimensions.ByDimensionsDatasetRecordStorageManagerUUID"
)
class SqliteMemoryRegistryTests(RegistryTests):
"""Tests for `Registry` backed by a SQLite in-memory database."""
sometimesHasDuplicateQueryRows = True
supportsCalibrationCollectionInFindFirst = False
@classmethod
def getDataDir(cls) -> str:
return os.path.normpath(os.path.join(os.path.dirname(__file__), "data", "registry"))
def make_butler(self) -> Butler:
# This helper function always return in-memory registry
# with default managers.
return create_populated_sqlite_registry()
def testMissingAttributes(self):
"""Test for instantiating a registry against outdated schema which
misses butler_attributes table.
"""
# TODO: Once we have stable gen3 schema everywhere this test can be
# dropped (DM-27373).
config = self.makeRegistryConfig()
config["db"] = "sqlite://"
with self.assertRaises(LookupError):
_RegistryFactory(config).from_config()
class SqliteMemoryRegistryNameKeyCollMgrUUIDTestCase(unittest.TestCase, SqliteMemoryRegistryTests):
"""Tests for `Registry` backed by a SQLite in-memory 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 SqliteMemoryRegistrySynthIntKeyCollMgrUUIDTestCase(unittest.TestCase, SqliteMemoryRegistryTests):
"""Tests for `Registry` backed by a SQLite in-memory database.
This test case uses SynthIntKeyCollectionManager and
ByDimensionsDatasetRecordStorageManagerUUID.
"""
collectionsManager = "lsst.daf.butler.registry.collections.synthIntKey.SynthIntKeyCollectionManager"
datasetsManager = (
"lsst.daf.butler.registry.datasets.byDimensions.ByDimensionsDatasetRecordStorageManagerUUID"
)
class SqliteMemoryRegistryAstropyIngestDateTestCase(unittest.TestCase, SqliteMemoryRegistryTests):
"""Tests for `Registry` backed by a SQLite in-memory database.
This test case uses version schema with ingest_date as nanoseconds instead
of DATETIME. This tests can be removed when/if we switch to nanoseconds as
default.
"""
collectionsManager = "lsst.daf.butler.registry.collections.synthIntKey.SynthIntKeyCollectionManager"
datasetsManager = {
"cls": "lsst.daf.butler.registry.datasets.byDimensions.ByDimensionsDatasetRecordStorageManagerUUID",
"schema_version": "2.0.0",
}
if __name__ == "__main__":
unittest.main()