Hide keyboard shortcuts

Hot-keys 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

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

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

# 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 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 

 

__all__ = ["DatabaseTests"] 

 

from abc import ABC, abstractmethod 

from collections import namedtuple 

from typing import ContextManager 

 

import sqlalchemy 

 

from lsst.sphgeom import ConvexPolygon, UnitVector3d 

from ..interfaces import ( 

Database, 

ReadOnlyDatabaseError, 

DatabaseConflictError, 

) 

from .. import ddl 

 

StaticTablesTuple = namedtuple("StaticTablesTuple", ["a", "b", "c"]) 

 

STATIC_TABLE_SPECS = StaticTablesTuple( 

a=ddl.TableSpec( 

fields=[ 

ddl.FieldSpec("name", dtype=sqlalchemy.String, length=16, primaryKey=True), 

ddl.FieldSpec("region", dtype=ddl.Base64Region, nbytes=128), 

] 

), 

b=ddl.TableSpec( 

fields=[ 

ddl.FieldSpec("id", dtype=sqlalchemy.BigInteger, autoincrement=True, primaryKey=True), 

ddl.FieldSpec("name", dtype=sqlalchemy.String, length=16, nullable=False), 

ddl.FieldSpec("value", dtype=sqlalchemy.SmallInteger, nullable=True), 

], 

unique=[("name",)], 

), 

c=ddl.TableSpec( 

fields=[ 

ddl.FieldSpec("id", dtype=sqlalchemy.BigInteger, autoincrement=True, primaryKey=True), 

ddl.FieldSpec("origin", dtype=sqlalchemy.BigInteger, primaryKey=True), 

ddl.FieldSpec("b_id", dtype=sqlalchemy.BigInteger, nullable=True), 

], 

foreignKeys=[ 

ddl.ForeignKeySpec("b", source=("b_id",), target=("id",), onDelete="SET NULL"), 

] 

), 

) 

 

DYNAMIC_TABLE_SPEC = ddl.TableSpec( 

fields=[ 

ddl.FieldSpec("c_id", dtype=sqlalchemy.BigInteger, primaryKey=True), 

ddl.FieldSpec("c_origin", dtype=sqlalchemy.BigInteger, primaryKey=True), 

ddl.FieldSpec("a_name", dtype=sqlalchemy.String, length=16, nullable=False), 

], 

foreignKeys=[ 

ddl.ForeignKeySpec("c", source=("c_id", "c_origin"), target=("id", "origin"), onDelete="CASCADE"), 

ddl.ForeignKeySpec("a", source=("a_name",), target=("name",), onDelete="CASCADE"), 

] 

) 

 

 

class DatabaseTests(ABC): 

"""Generic tests for the `Database` interface that can be subclassed to 

generate tests for concrete implementations. 

""" 

 

@abstractmethod 

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

"""Return an empty `Database` with the given origin, or an 

automatically-generated one if ``origin`` is `None`. 

""" 

raise NotImplementedError() 

 

@abstractmethod 

def asReadOnly(self, database: Database) -> ContextManager[Database]: 

"""Return a context manager for a read-only connection into the given 

database. 

 

The original database should be considered unusable within the context 

but safe to use again afterwards (this allows the context manager to 

block write access by temporarily changing user permissions to really 

guarantee that write operations are not performed). 

""" 

raise NotImplementedError() 

 

@abstractmethod 

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

"""Return a new `Database` instance that points to the same underlying 

storage as the given one. 

""" 

raise NotImplementedError() 

 

def checkTable(self, spec: ddl.TableSpec, table: sqlalchemy.schema.Table): 

self.assertCountEqual(spec.fields.names, table.columns.keys()) 

# Checking more than this currently seems fragile, as it might restrict 

# what Database implementations do; we don't care if the spec is 

# actually preserved in terms of types and constraints as long as we 

# can use the returned table as if it was. 

 

def checkStaticSchema(self, tables: StaticTablesTuple): 

self.checkTable(STATIC_TABLE_SPECS.a, tables.a) 

self.checkTable(STATIC_TABLE_SPECS.b, tables.b) 

self.checkTable(STATIC_TABLE_SPECS.c, tables.c) 

 

def testDeclareStaticTables(self): 

"""Tests for `Database.declareStaticSchema` and the methods it 

delegates to. 

""" 

# Create the static schema in a new, empty database. 

newDatabase = self.makeEmptyDatabase() 

with newDatabase.declareStaticTables(create=True) as context: 

tables = context.addTableTuple(STATIC_TABLE_SPECS) 

self.checkStaticSchema(tables) 

# Check that we can load that schema even from a read-only connection. 

with self.asReadOnly(newDatabase) as existingReadOnlyDatabase: 

with existingReadOnlyDatabase.declareStaticTables(create=False) as context: 

tables = context.addTableTuple(STATIC_TABLE_SPECS) 

self.checkStaticSchema(tables) 

 

def testDynamicTables(self): 

"""Tests for `Database.ensureTableExists` and 

`Database.getExistingTable`. 

""" 

# Need to start with the static schema. 

newDatabase = self.makeEmptyDatabase() 

with newDatabase.declareStaticTables(create=True) as context: 

context.addTableTuple(STATIC_TABLE_SPECS) 

# Try to ensure the dyamic table exists in a read-only version of that 

# database, which should fail because we can't create it. 

with self.asReadOnly(newDatabase) as existingReadOnlyDatabase: 

with existingReadOnlyDatabase.declareStaticTables(create=False) as context: 

context.addTableTuple(STATIC_TABLE_SPECS) 

with self.assertRaises(ReadOnlyDatabaseError): 

existingReadOnlyDatabase.ensureTableExists("d", DYNAMIC_TABLE_SPEC) 

# Just getting the dynamic table before it exists should return None. 

self.assertIsNone(newDatabase.getExistingTable("d", DYNAMIC_TABLE_SPEC)) 

# Ensure the new table exists back in the original database, which 

# should create it. 

table = newDatabase.ensureTableExists("d", DYNAMIC_TABLE_SPEC) 

self.checkTable(DYNAMIC_TABLE_SPEC, table) 

# Ensuring that it exists should just return the exact same table 

# instance again. 

self.assertIs(newDatabase.ensureTableExists("d", DYNAMIC_TABLE_SPEC), table) 

# Try again from the read-only database. 

with self.asReadOnly(newDatabase) as existingReadOnlyDatabase: 

with existingReadOnlyDatabase.declareStaticTables(create=False) as context: 

context.addTableTuple(STATIC_TABLE_SPECS) 

# Just getting the dynamic table should now work... 

self.assertIsNotNone(existingReadOnlyDatabase.getExistingTable("d", DYNAMIC_TABLE_SPEC)) 

# ...as should ensuring that it exists, since it now does. 

existingReadOnlyDatabase.ensureTableExists("d", DYNAMIC_TABLE_SPEC) 

self.checkTable(DYNAMIC_TABLE_SPEC, table) 

# Trying to get the table with a different specification (at least 

# in terms of what columns are present) should raise. 

with self.assertRaises(DatabaseConflictError): 

newDatabase.ensureTableExists( 

"d", 

ddl.TableSpec( 

fields=[ddl.FieldSpec("name", dtype=sqlalchemy.String, length=4, primaryKey=True)] 

) 

) 

# Calling ensureTableExists inside a transaction block is an error, 

# even if it would do nothing. 

with newDatabase.transaction(): 

with self.assertRaises(AssertionError): 

newDatabase.ensureTableExists("d", DYNAMIC_TABLE_SPEC) 

 

def testSchemaSeparation(self): 

"""Test that creating two different `Database` instances allows us 

to create different tables with the same name in each. 

""" 

db1 = self.makeEmptyDatabase(origin=1) 

with db1.declareStaticTables(create=True) as context: 

tables = context.addTableTuple(STATIC_TABLE_SPECS) 

self.checkStaticSchema(tables) 

 

db2 = self.makeEmptyDatabase(origin=2) 

# Make the DDL here intentionally different so we'll definitely 

# notice if db1 and db2 are pointing at the same schema. 

spec = ddl.TableSpec(fields=[ddl.FieldSpec("id", dtype=sqlalchemy.Integer, primaryKey=True)]) 

with db2.declareStaticTables(create=True) as context: 

# Make the DDL here intentionally different so we'll definitely 

# notice if db1 and db2 are pointing at the same schema. 

table = context.addTable("a", spec) 

self.checkTable(spec, table) 

 

def testInsertQueryDelete(self): 

"""Test the `Database.insert`, `Database.query`, and `Database.delete` 

methods, as well as the `Base64Region` type and the ``onDelete`` 

argument to `ddl.ForeignKeySpec`. 

""" 

db = self.makeEmptyDatabase(origin=1) 

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

tables = context.addTableTuple(STATIC_TABLE_SPECS) 

# Insert a single, non-autoincrement row that contains a region and 

# query to get it back. 

region = ConvexPolygon((UnitVector3d(1, 0, 0), UnitVector3d(0, 1, 0), UnitVector3d(0, 0, 1))) 

row = {"name": "a1", "region": region} 

db.insert(tables.a, row) 

self.assertEqual([dict(r) for r in db.query(tables.a.select()).fetchall()], [row]) 

# Insert multiple autoincrement rows but do not try to get the IDs 

# back immediately. 

db.insert(tables.b, {"name": "b1", "value": 10}, {"name": "b2", "value": 20}) 

results = [dict(r) for r in db.query(tables.b.select().order_by("id")).fetchall()] 

self.assertEqual(len(results), 2) 

for row in results: 

self.assertIn(row["name"], ("b1", "b2")) 

self.assertIsInstance(row["id"], int) 

self.assertGreater(results[1]["id"], results[0]["id"]) 

# Insert multiple autoincrement rows and get the IDs back from insert. 

rows = [{"name": "b3", "value": 30}, {"name": "b4", "value": 40}] 

ids = db.insert(tables.b, *rows, returnIds=True) 

results = [ 

dict(r) for r in db.query( 

tables.b.select().where(tables.b.columns.id > results[1]["id"]) 

).fetchall() 

] 

expected = [dict(row, id=id) for row, id in zip(rows, ids)] 

self.assertCountEqual(results, expected) 

self.assertTrue(all(result["id"] is not None for result in results)) 

# Insert multiple rows into a table with an autoincrement+origin 

# primary key, then use the returned IDs to insert into a dynamic 

# table. 

rows = [{"origin": db.origin, "b_id": results[0]["id"]}, 

{"origin": db.origin, "b_id": None}] 

ids = db.insert(tables.c, *rows, returnIds=True) 

results = [dict(r) for r in db.query(tables.c.select()).fetchall()] 

expected = [dict(row, id=id) for row, id in zip(rows, ids)] 

self.assertCountEqual(results, expected) 

self.assertTrue(all(result["id"] is not None for result in results)) 

# Add the dynamic table. 

d = db.ensureTableExists("d", DYNAMIC_TABLE_SPEC) 

# Insert into it. 

rows = [{"c_origin": db.origin, "c_id": id, "a_name": "a1"} for id in ids] 

db.insert(d, *rows) 

results = [dict(r) for r in db.query(d.select()).fetchall()] 

self.assertCountEqual(rows, results) 

# Insert multiple rows into a table with an autoincrement+origin 

# primary key (this is especially tricky for SQLite, but good to test 

# for all DBs), but pass in a value for the autoincrement key. 

# For extra complexity, we re-use the autoincrement value with a 

# different value for origin. 

rows2 = [{"id": 700, "origin": db.origin, "b_id": None}, 

{"id": 700, "origin": 60, "b_id": None}, 

{"id": 1, "origin": 60, "b_id": None}] 

db.insert(tables.c, *rows2) 

results = [dict(r) for r in db.query(tables.c.select()).fetchall()] 

self.assertCountEqual(results, expected + rows2) 

self.assertTrue(all(result["id"] is not None for result in results)) 

 

# Define 'SELECT COUNT(*)' query for later use. 

count = sqlalchemy.sql.select([sqlalchemy.sql.func.count()]) 

# Get the values we inserted into table b. 

bValues = [dict(r) for r in db.query(tables.b.select()).fetchall()] 

# Remove two row from table b by ID. 

n = db.delete(tables.b, ["id"], {"id": bValues[0]["id"]}, {"id": bValues[1]["id"]}) 

self.assertEqual(n, 2) 

# Remove the other two rows from table b by name. 

n = db.delete(tables.b, ["name"], {"name": bValues[2]["name"]}, {"name": bValues[3]["name"]}) 

self.assertEqual(n, 2) 

# There should now be no rows in table b. 

self.assertEqual( 

db.query(count.select_from(tables.b)).scalar(), 

0 

) 

# All b_id values in table c should now be NULL, because there's an 

# onDelete='SET NULL' foreign key. 

self.assertEqual( 

db.query(count.select_from(tables.c).where(tables.c.columns.b_id != None)).scalar(), # noqa:E711 

0 

) 

# Remove all rows in table a (there's only one); this should remove all 

# rows in d due to onDelete='CASCADE'. 

n = db.delete(tables.a, []) 

self.assertEqual(n, 1) 

self.assertEqual(db.query(count.select_from(tables.a)).scalar(), 0) 

self.assertEqual(db.query(count.select_from(d)).scalar(), 0) 

 

def testUpdate(self): 

"""Tests for `Database.update`. 

""" 

db = self.makeEmptyDatabase(origin=1) 

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

tables = context.addTableTuple(STATIC_TABLE_SPECS) 

# Insert two rows into table a, both without regions. 

db.insert(tables.a, {"name": "a1"}, {"name": "a2"}) 

# Update one of the rows with a region. 

region = ConvexPolygon((UnitVector3d(1, 0, 0), UnitVector3d(0, 1, 0), UnitVector3d(0, 0, 1))) 

n = db.update(tables.a, {"name": "k"}, {"k": "a2", "region": region}) 

self.assertEqual(n, 1) 

sql = sqlalchemy.sql.select([tables.a.columns.name, tables.a.columns.region]).select_from(tables.a) 

self.assertCountEqual( 

[dict(r) for r in db.query(sql).fetchall()], 

[{"name": "a1", "region": None}, {"name": "a2", "region": region}] 

) 

 

def testSync(self): 

"""Tests for `Database.sync`. 

""" 

db = self.makeEmptyDatabase(origin=1) 

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

tables = context.addTableTuple(STATIC_TABLE_SPECS) 

# Insert a row with sync, because it doesn't exist yet. 

values, inserted = db.sync(tables.b, keys={"name": "b1"}, extra={"value": 10}, returning=["id"]) 

self.assertTrue(inserted) 

self.assertEqual([{"id": values[0], "name": "b1", "value": 10}], 

[dict(r) for r in db.query(tables.b.select()).fetchall()]) 

# Repeat that operation, which should do nothing but return the 

# requested values. 

values, inserted = db.sync(tables.b, keys={"name": "b1"}, extra={"value": 10}, returning=["id"]) 

self.assertFalse(inserted) 

self.assertEqual([{"id": values[0], "name": "b1", "value": 10}], 

[dict(r) for r in db.query(tables.b.select()).fetchall()]) 

# Repeat the operation without the 'extra' arg, which should also just 

# return the existing row. 

values, inserted = db.sync(tables.b, keys={"name": "b1"}, returning=["id"]) 

self.assertFalse(inserted) 

self.assertEqual([{"id": values[0], "name": "b1", "value": 10}], 

[dict(r) for r in db.query(tables.b.select()).fetchall()]) 

# Repeat the operation with a different value in 'extra'. That still 

# shouldn't be an error, because 'extra' is only used if we really do 

# insert. Also drop the 'returning' argument. 

_, inserted = db.sync(tables.b, keys={"name": "b1"}, extra={"value": 20}) 

self.assertFalse(inserted) 

self.assertEqual([{"id": values[0], "name": "b1", "value": 10}], 

[dict(r) for r in db.query(tables.b.select()).fetchall()]) 

# Repeat the operation with the correct value in 'compared' instead of 

# 'extra'. 

_, inserted = db.sync(tables.b, keys={"name": "b1"}, compared={"value": 10}) 

self.assertFalse(inserted) 

self.assertEqual([{"id": values[0], "name": "b1", "value": 10}], 

[dict(r) for r in db.query(tables.b.select()).fetchall()]) 

# Repeat the operation with an incorrect value in 'compared'; this 

# should raise. 

with self.assertRaises(DatabaseConflictError): 

db.sync(tables.b, keys={"name": "b1"}, compared={"value": 20}) 

# Try to sync inside a transaction. That's always an error, regardless 

# of whether there would be an insertion or not. 

with self.assertRaises(AssertionError): 

with db.transaction(): 

db.sync(tables.b, keys={"name": "b1"}, extra={"value": 10}) 

with self.assertRaises(AssertionError): 

with db.transaction(): 

db.sync(tables.b, keys={"name": "b2"}, extra={"value": 20}) 

# Try to sync in a read-only database. This should work if and only 

# if the matching row already exists. 

with self.asReadOnly(db) as rodb: 

with rodb.declareStaticTables(create=False) as context: 

tables = context.addTableTuple(STATIC_TABLE_SPECS) 

_, inserted = rodb.sync(tables.b, keys={"name": "b1"}) 

self.assertFalse(inserted) 

self.assertEqual([{"id": values[0], "name": "b1", "value": 10}], 

[dict(r) for r in rodb.query(tables.b.select()).fetchall()]) 

with self.assertRaises(ReadOnlyDatabaseError): 

rodb.sync(tables.b, keys={"name": "b2"}, extra={"value": 20}) 

 

def testReplace(self): 

"""Tests for `Database.replace`. 

""" 

db = self.makeEmptyDatabase(origin=1) 

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

tables = context.addTableTuple(STATIC_TABLE_SPECS) 

# Use 'replace' to insert a single row that contains a region and 

# query to get it back. 

region = ConvexPolygon((UnitVector3d(1, 0, 0), UnitVector3d(0, 1, 0), UnitVector3d(0, 0, 1))) 

row1 = {"name": "a1", "region": region} 

db.replace(tables.a, row1) 

self.assertEqual([dict(r) for r in db.query(tables.a.select()).fetchall()], [row1]) 

# Insert another row without a region. 

row2 = {"name": "a2", "region": None} 

db.replace(tables.a, row2) 

self.assertCountEqual([dict(r) for r in db.query(tables.a.select()).fetchall()], [row1, row2]) 

# Use replace to re-insert both of those rows again, which should do 

# nothing. 

db.replace(tables.a, row1, row2) 

self.assertCountEqual([dict(r) for r in db.query(tables.a.select()).fetchall()], [row1, row2]) 

# Replace row1 with a row with no region, while reinserting row2. 

row1a = {"name": "a1", "region": None} 

db.replace(tables.a, row1a, row2) 

self.assertCountEqual([dict(r) for r in db.query(tables.a.select()).fetchall()], [row1a, row2]) 

# Replace both rows, returning row1 to its original state, while adding 

# a new one. Pass them in in a different order. 

row2a = {"name": "a2", "region": region} 

row3 = {"name": "a3", "region": None} 

db.replace(tables.a, row3, row2a, row1) 

self.assertCountEqual([dict(r) for r in db.query(tables.a.select()).fetchall()], [row1, row2a, row3])