Coverage for tests/test_postgresql.py: 34%

111 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-23 09:29 +0000

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 gc 

23import itertools 

24import os 

25import secrets 

26import unittest 

27import warnings 

28from contextlib import contextmanager 

29 

30import astropy.time 

31 

32try: 

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

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

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

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

37 import testing.postgresql 

38except ImportError: 

39 testing = None 

40 

41import sqlalchemy 

42from lsst.daf.butler import Timespan, ddl 

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 @classmethod 

65 def setUpClass(cls): 

66 cls.root = makeTestTempDir(TESTDIR) 

67 cls.server = _startServer(cls.root) 

68 

69 @classmethod 

70 def tearDownClass(cls): 

71 # Clean up any lingering SQLAlchemy engines/connections 

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

73 gc.collect() 

74 cls.server.stop() 

75 removeTestTempDir(cls.root) 

76 

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

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

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

80 

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

82 return PostgresqlDatabase.fromUri( 

83 origin=database.origin, uri=self.server.url(), namespace=database.namespace, writeable=writeable 

84 ) 

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, dtype=sqlalchemy.BigInteger, autoincrement=True, primaryKey=True 

108 ), 

109 ddl.FieldSpec( 

110 fieldName2, 

111 dtype=sqlalchemy.String, 

112 length=16, 

113 nullable=False, 

114 ), 

115 ], 

116 unique={(fieldName2,)}, 

117 ), 

118 ) 

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

120 # first table. 

121 db.ensureTableExists( 

122 tableName + "_b", 

123 ddl.TableSpec( 

124 fields=[ 

125 ddl.FieldSpec( 

126 fieldName1 + "_b", dtype=sqlalchemy.BigInteger, autoincrement=True, primaryKey=True 

127 ), 

128 ddl.FieldSpec( 

129 fieldName2 + "_b", 

130 dtype=sqlalchemy.String, 

131 length=16, 

132 nullable=False, 

133 ), 

134 ], 

135 foreignKeys=[ 

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

137 ], 

138 ), 

139 ) 

140 

141 def test_RangeTimespanType(self): 

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

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

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

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

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

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

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

149 db = self.makeEmptyDatabase(origin=1) 

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

151 tbl = context.addTable( 

152 "tbl", 

153 ddl.TableSpec( 

154 fields=[ 

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

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

157 ], 

158 ), 

159 ) 

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

161 db.insert(tbl, *rows) 

162 

163 # Test basic round-trip through database. 

164 with db.query(tbl.select().order_by(tbl.columns.id)) as sql_result: 

165 self.assertEqual(rows, [row._asdict() for row in sql_result]) 

166 

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

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

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

170 return ( 

171 sqlalchemy.sql.select(tbl.columns.id.label("id"), tbl.columns.timespan.label("timespan")) 

172 .select_from(tbl) 

173 .alias(alias) 

174 ) 

175 

176 sq1 = subquery("sq1") 

177 sq2 = subquery("sq2") 

178 query = sqlalchemy.sql.select( 

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

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

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

182 ) 

183 

184 # `columns` is deprecated since 1.4, but 

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

186 if hasattr(query, "selected_columns"): 

187 columns = query.selected_columns 

188 else: 

189 columns = query.columns 

190 

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

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

193 with warnings.catch_warnings(): 

194 warnings.filterwarnings( 

195 "ignore", message=".*cartesian product", category=sqlalchemy.exc.SAWarning 

196 ) 

197 with db.query(query) as sql_result: 

198 dbResults = { 

199 (row[columns.n1], row[columns.n2]): row[columns.overlaps] for row in sql_result.mappings() 

200 } 

201 

202 pyResults = { 

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

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

205 } 

206 self.assertEqual(pyResults, dbResults) 

207 

208 

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

210class PostgresqlRegistryTests(RegistryTests): 

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

212 

213 Note 

214 ---- 

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

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

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

218 """ 

219 

220 @classmethod 

221 def setUpClass(cls): 

222 cls.root = makeTestTempDir(TESTDIR) 

223 cls.server = _startServer(cls.root) 

224 

225 @classmethod 

226 def tearDownClass(cls): 

227 # Clean up any lingering SQLAlchemy engines/connections 

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

229 gc.collect() 

230 cls.server.stop() 

231 removeTestTempDir(cls.root) 

232 

233 @classmethod 

234 def getDataDir(cls) -> str: 

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

236 

237 def makeRegistry(self, share_repo_with: Registry | None = None) -> Registry: 

238 if share_repo_with is None: 

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

240 else: 

241 namespace = share_repo_with._db.namespace 

242 config = self.makeRegistryConfig() 

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

244 config["namespace"] = namespace 

245 if share_repo_with is None: 

246 return Registry.createFromConfig(config) 

247 else: 

248 return Registry.fromConfig(config) 

249 

250 

251class PostgresqlRegistryNameKeyCollMgrUUIDTestCase(PostgresqlRegistryTests, unittest.TestCase): 

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

253 

254 This test case uses NameKeyCollectionManager and 

255 ByDimensionsDatasetRecordStorageManagerUUID. 

256 """ 

257 

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

259 datasetsManager = ( 

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

261 ) 

262 

263 

264class PostgresqlRegistrySynthIntKeyCollMgrUUIDTestCase(PostgresqlRegistryTests, unittest.TestCase): 

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

266 

267 This test case uses SynthIntKeyCollectionManager and 

268 ByDimensionsDatasetRecordStorageManagerUUID. 

269 """ 

270 

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

272 datasetsManager = ( 

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

274 ) 

275 

276 

277if __name__ == "__main__": 

278 unittest.main()