Coverage for tests/test_postgresql.py: 43%

Shortcuts 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

113 statements  

1# This file is part of daf_butler. 

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 

22import os 

23from contextlib import contextmanager 

24import itertools 

25import secrets 

26import unittest 

27import gc 

28import warnings 

29 

30import astropy.time 

31try: 

32 # It's possible but silly to have testing.postgresql installed without 

33 # having the postgresql server installed (because then nothing in 

34 # testing.postgresql would work), so we use the presence of that module 

35 # to test whether we can expect the server to be available. 

36 import testing.postgresql 

37except ImportError: 

38 testing = None 

39 

40import sqlalchemy 

41 

42from lsst.daf.butler import ddl, Timespan 

43from lsst.daf.butler.registry import Registry 

44from lsst.daf.butler.registry.databases.postgresql import PostgresqlDatabase, _RangeTimespanType 

45from lsst.daf.butler.registry.tests import DatabaseTests, RegistryTests 

46from lsst.daf.butler.tests.utils import makeTestTempDir, removeTestTempDir 

47 

48TESTDIR = os.path.abspath(os.path.dirname(__file__)) 

49 

50 

51def _startServer(root): 

52 """Start a PostgreSQL server and create a database within it, returning 

53 an object encapsulating both. 

54 """ 

55 server = testing.postgresql.Postgresql(base_dir=root) 

56 engine = sqlalchemy.engine.create_engine(server.url()) 

57 with engine.begin() as connection: 

58 connection.execute(sqlalchemy.text("CREATE EXTENSION btree_gist;")) 

59 return server 

60 

61 

62@unittest.skipUnless(testing is not None, "testing.postgresql module not found") 

63class PostgresqlDatabaseTestCase(unittest.TestCase, DatabaseTests): 

64 

65 @classmethod 

66 def setUpClass(cls): 

67 cls.root = makeTestTempDir(TESTDIR) 

68 cls.server = _startServer(cls.root) 

69 

70 @classmethod 

71 def tearDownClass(cls): 

72 # Clean up any lingering SQLAlchemy engines/connections 

73 # so they're closed before we shut down the server. 

74 gc.collect() 

75 cls.server.stop() 

76 removeTestTempDir(cls.root) 

77 

78 def makeEmptyDatabase(self, origin: int = 0) -> PostgresqlDatabase: 

79 namespace = f"namespace_{secrets.token_hex(8).lower()}" 

80 return PostgresqlDatabase.fromUri(origin=origin, uri=self.server.url(), namespace=namespace) 

81 

82 def getNewConnection(self, database: PostgresqlDatabase, *, writeable: bool) -> PostgresqlDatabase: 

83 return PostgresqlDatabase.fromUri(origin=database.origin, uri=self.server.url(), 

84 namespace=database.namespace, writeable=writeable) 

85 

86 @contextmanager 

87 def asReadOnly(self, database: PostgresqlDatabase) -> PostgresqlDatabase: 

88 yield self.getNewConnection(database, writeable=False) 

89 

90 def testNameShrinking(self): 

91 """Test that too-long names for database entities other than tables 

92 and columns (which we preserve, and just expect to fit) are shrunk. 

93 """ 

94 db = self.makeEmptyDatabase(origin=1) 

95 with db.declareStaticTables(create=True) as context: 

96 # Table and field names are each below the 63-char limit even when 

97 # accounting for the prefix, but their combination (which will 

98 # appear in sequences and constraints) is not. 

99 tableName = "a_table_with_a_very_very_long_42_char_name" 

100 fieldName1 = "a_column_with_a_very_very_long_43_char_name" 

101 fieldName2 = "another_column_with_a_very_very_long_49_char_name" 

102 context.addTable( 

103 tableName, 

104 ddl.TableSpec( 

105 fields=[ 

106 ddl.FieldSpec( 

107 fieldName1, 

108 dtype=sqlalchemy.BigInteger, 

109 autoincrement=True, 

110 primaryKey=True 

111 ), 

112 ddl.FieldSpec( 

113 fieldName2, 

114 dtype=sqlalchemy.String, 

115 length=16, 

116 nullable=False, 

117 ), 

118 ], 

119 unique={(fieldName2,)}, 

120 ) 

121 ) 

122 # Add another table, this time dynamically, with a foreign key to the 

123 # first table. 

124 db.ensureTableExists( 

125 tableName + "_b", 

126 ddl.TableSpec( 

127 fields=[ 

128 ddl.FieldSpec( 

129 fieldName1 + "_b", 

130 dtype=sqlalchemy.BigInteger, 

131 autoincrement=True, 

132 primaryKey=True 

133 ), 

134 ddl.FieldSpec( 

135 fieldName2 + "_b", 

136 dtype=sqlalchemy.String, 

137 length=16, 

138 nullable=False, 

139 ), 

140 ], 

141 foreignKeys=[ 

142 ddl.ForeignKeySpec(tableName, source=(fieldName2 + "_b",), target=(fieldName2,)), 

143 ] 

144 ) 

145 ) 

146 

147 def test_RangeTimespanType(self): 

148 start = astropy.time.Time('2020-01-01T00:00:00', format="isot", scale="tai") 

149 offset = astropy.time.TimeDelta(60, format="sec") 

150 timestamps = [start + offset*n for n in range(3)] 

151 timespans = [Timespan(begin=None, end=None)] 

152 timespans.extend(Timespan(begin=None, end=t) for t in timestamps) 

153 timespans.extend(Timespan(begin=t, end=None) for t in timestamps) 

154 timespans.extend(Timespan(begin=a, end=b) for a, b in itertools.combinations(timestamps, 2)) 

155 db = self.makeEmptyDatabase(origin=1) 

156 with db.declareStaticTables(create=True) as context: 

157 tbl = context.addTable( 

158 "tbl", 

159 ddl.TableSpec( 

160 fields=[ 

161 ddl.FieldSpec(name="id", dtype=sqlalchemy.Integer, primaryKey=True), 

162 ddl.FieldSpec(name="timespan", dtype=_RangeTimespanType), 

163 ], 

164 ) 

165 ) 

166 rows = [{"id": n, "timespan": t} for n, t in enumerate(timespans)] 

167 db.insert(tbl, *rows) 

168 

169 # Test basic round-trip through database. 

170 self.assertEqual( 

171 rows, 

172 [row._asdict() for row in db.query(tbl.select().order_by(tbl.columns.id))] 

173 ) 

174 

175 # Test that Timespan's Python methods are consistent with our usage of 

176 # half-open ranges and PostgreSQL operators on ranges. 

177 def subquery(alias: str) -> sqlalchemy.sql.FromClause: 

178 return sqlalchemy.sql.select( 

179 tbl.columns.id.label("id"), tbl.columns.timespan.label("timespan") 

180 ).select_from( 

181 tbl 

182 ).alias(alias) 

183 sq1 = subquery("sq1") 

184 sq2 = subquery("sq2") 

185 query = sqlalchemy.sql.select( 

186 sq1.columns.id.label("n1"), 

187 sq2.columns.id.label("n2"), 

188 sq1.columns.timespan.overlaps(sq2.columns.timespan).label("overlaps"), 

189 ) 

190 

191 # `columns` is deprecated since 1.4, but 

192 # `selected_columns` method did not exist in 1.3. 

193 if hasattr(query, "selected_columns"): 

194 columns = query.selected_columns 

195 else: 

196 columns = query.columns 

197 

198 # SQLAlchemy issues a warning about cartesian product of two tables, 

199 # which we do intentionally. Disable that warning temporarily. 

200 with warnings.catch_warnings(): 

201 warnings.filterwarnings("ignore", message=".*cartesian product", 

202 category=sqlalchemy.exc.SAWarning) 

203 dbResults = { 

204 (row[columns.n1], row[columns.n2]): row[columns.overlaps] 

205 for row in db.query(query).mappings() 

206 } 

207 

208 pyResults = { 

209 (n1, n2): t1.overlaps(t2) 

210 for (n1, t1), (n2, t2) in itertools.product(enumerate(timespans), enumerate(timespans)) 

211 } 

212 self.assertEqual(pyResults, dbResults) 

213 

214 

215@unittest.skipUnless(testing is not None, "testing.postgresql module not found") 

216class PostgresqlRegistryTests(RegistryTests): 

217 """Tests for `Registry` backed by a PostgreSQL database. 

218 

219 Note 

220 ---- 

221 This is not a subclass of `unittest.TestCase` but to avoid repetition it 

222 defines methods that override `unittest.TestCase` methods. To make this 

223 work sublasses have to have this class first in the bases list. 

224 """ 

225 

226 @classmethod 

227 def setUpClass(cls): 

228 cls.root = makeTestTempDir(TESTDIR) 

229 cls.server = _startServer(cls.root) 

230 

231 @classmethod 

232 def tearDownClass(cls): 

233 # Clean up any lingering SQLAlchemy engines/connections 

234 # so they're closed before we shut down the server. 

235 gc.collect() 

236 cls.server.stop() 

237 removeTestTempDir(cls.root) 

238 

239 @classmethod 

240 def getDataDir(cls) -> str: 

241 return os.path.normpath(os.path.join(os.path.dirname(__file__), "data", "registry")) 

242 

243 def makeRegistry(self) -> Registry: 

244 namespace = f"namespace_{secrets.token_hex(8).lower()}" 

245 config = self.makeRegistryConfig() 

246 config["db"] = self.server.url() 

247 config["namespace"] = namespace 

248 return Registry.createFromConfig(config) 

249 

250 

251class PostgresqlRegistryNameKeyCollMgrTestCase(PostgresqlRegistryTests, unittest.TestCase): 

252 """Tests for `Registry` backed by a PostgreSQL database. 

253 

254 This test case uses NameKeyCollectionManager and 

255 ByDimensionsDatasetRecordStorageManager. 

256 """ 

257 collectionsManager = "lsst.daf.butler.registry.collections.nameKey.NameKeyCollectionManager" 

258 datasetsManager = \ 

259 "lsst.daf.butler.registry.datasets.byDimensions.ByDimensionsDatasetRecordStorageManager" 

260 

261 

262class PostgresqlRegistrySynthIntKeyCollMgrTestCase(PostgresqlRegistryTests, unittest.TestCase): 

263 """Tests for `Registry` backed by a PostgreSQL database. 

264 

265 This test case uses SynthIntKeyCollectionManager and 

266 ByDimensionsDatasetRecordStorageManager. 

267 """ 

268 collectionsManager = "lsst.daf.butler.registry.collections.synthIntKey.SynthIntKeyCollectionManager" 

269 datasetsManager = \ 

270 "lsst.daf.butler.registry.datasets.byDimensions.ByDimensionsDatasetRecordStorageManager" 

271 

272 

273class PostgresqlRegistryNameKeyCollMgrUUIDTestCase(PostgresqlRegistryTests, unittest.TestCase): 

274 """Tests for `Registry` backed by a PostgreSQL database. 

275 

276 This test case uses NameKeyCollectionManager and 

277 ByDimensionsDatasetRecordStorageManagerUUID. 

278 """ 

279 collectionsManager = "lsst.daf.butler.registry.collections.nameKey.NameKeyCollectionManager" 

280 datasetsManager = \ 

281 "lsst.daf.butler.registry.datasets.byDimensions.ByDimensionsDatasetRecordStorageManagerUUID" 

282 

283 

284class PostgresqlRegistrySynthIntKeyCollMgrUUIDTestCase(PostgresqlRegistryTests, unittest.TestCase): 

285 """Tests for `Registry` backed by a PostgreSQL database. 

286 

287 This test case uses SynthIntKeyCollectionManager and 

288 ByDimensionsDatasetRecordStorageManagerUUID. 

289 """ 

290 collectionsManager = "lsst.daf.butler.registry.collections.synthIntKey.SynthIntKeyCollectionManager" 

291 datasetsManager = \ 

292 "lsst.daf.butler.registry.datasets.byDimensions.ByDimensionsDatasetRecordStorageManagerUUID" 

293 

294 

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

296 unittest.main()