|
5 | 5 | from typing import TYPE_CHECKING |
6 | 6 |
|
7 | 7 | from pydantic import BaseModel |
8 | | -from tryke import expect, test |
| 8 | +from tryke import describe, expect, test |
9 | 9 |
|
10 | 10 | from sapling import MemoryBackend, SQLiteBackend |
11 | 11 | from sapling.errors import NotFoundError |
@@ -290,34 +290,6 @@ def test_all_empty() -> None: |
290 | 290 | expect(all_hellos).to_equal([]) |
291 | 291 |
|
292 | 292 |
|
293 | | -@test |
294 | | -def test_sqlite_backend_memory() -> None: |
295 | | - backend = SQLiteBackend() |
296 | | - db = Database(backend=backend) |
297 | | - with db.transaction() as txn: |
298 | | - txn.put(_TestModel, "test", _TestModel(hello="world")) |
299 | | - doc = txn.fetch(_TestModel, "test") |
300 | | - expect(doc.model.hello).to_equal("world") |
301 | | - |
302 | | - |
303 | | -@test |
304 | | -def test_sqlite_backend_file() -> None: |
305 | | - with tempfile.TemporaryDirectory() as tmp_dir: |
306 | | - db_path = Path(tmp_dir) / "test.db" |
307 | | - settings = SaplingSettings(sqlite_path=str(db_path)) |
308 | | - backend = SQLiteBackend(settings=settings) |
309 | | - db = Database(backend=backend) |
310 | | - |
311 | | - with db.transaction() as txn: |
312 | | - txn.put(_TestModel, "persistent", _TestModel(hello="saved")) |
313 | | - |
314 | | - settings2 = SaplingSettings(sqlite_path=str(db_path)) |
315 | | - db2 = Database(backend=SQLiteBackend(settings=settings2)) |
316 | | - with db2.transaction() as txn: |
317 | | - doc = txn.fetch(_TestModel, "persistent") |
318 | | - expect(doc.model.hello).to_equal("saved") |
319 | | - |
320 | | - |
321 | 293 | @test |
322 | 294 | def test_backend_all_method() -> None: |
323 | 295 | backend = SQLiteBackend() |
@@ -355,39 +327,68 @@ def test_memory_backend() -> None: |
355 | 327 | expect(lambda: txn.fetch(_TestModel, "test")).to_raise(NotFoundError) |
356 | 328 |
|
357 | 329 |
|
358 | | -@test |
359 | | -def test_deferred_initialization() -> None: |
360 | | - backend = SQLiteBackend() |
361 | | - db = Database(backend=backend, initialize=False) |
| 330 | +with describe("sqlite"): |
| 331 | + |
| 332 | + @test |
| 333 | + def test_sqlite_backend_memory() -> None: |
| 334 | + backend = SQLiteBackend() |
| 335 | + db = Database(backend=backend) |
| 336 | + with db.transaction() as txn: |
| 337 | + txn.put(_TestModel, "test", _TestModel(hello="world")) |
| 338 | + doc = txn.fetch(_TestModel, "test") |
| 339 | + expect(doc.model.hello).to_equal("world") |
362 | 340 |
|
363 | | - db.initialize() |
| 341 | + @test |
| 342 | + def test_sqlite_backend_file() -> None: |
| 343 | + with tempfile.TemporaryDirectory() as tmp_dir: |
| 344 | + db_path = Path(tmp_dir) / "test.db" |
| 345 | + settings = SaplingSettings(sqlite_path=str(db_path)) |
| 346 | + backend = SQLiteBackend(settings=settings) |
| 347 | + db = Database(backend=backend) |
364 | 348 |
|
365 | | - with db.transaction() as txn: |
366 | | - txn.put(_TestModel, "test", _TestModel(hello="world")) |
367 | | - doc = txn.fetch(_TestModel, "test") |
368 | | - expect(doc.model.hello).to_equal("world") |
| 349 | + with db.transaction() as txn: |
| 350 | + txn.put(_TestModel, "persistent", _TestModel(hello="saved")) |
369 | 351 |
|
| 352 | + settings2 = SaplingSettings(sqlite_path=str(db_path)) |
| 353 | + db2 = Database(backend=SQLiteBackend(settings=settings2)) |
| 354 | + with db2.transaction() as txn: |
| 355 | + doc = txn.fetch(_TestModel, "persistent") |
| 356 | + expect(doc.model.hello).to_equal("saved") |
370 | 357 |
|
371 | | -@test |
372 | | -def test_idempotent_initialization() -> None: |
373 | | - backend = SQLiteBackend() |
374 | | - db = Database(backend=backend, initialize=False) |
375 | 358 |
|
376 | | - db.initialize() |
377 | | - db.initialize() |
378 | | - db.initialize() |
| 359 | +with describe("initialization"): |
379 | 360 |
|
380 | | - with db.transaction() as txn: |
381 | | - txn.put(_TestModel, "test", _TestModel(hello="world")) |
| 361 | + @test |
| 362 | + def test_deferred_initialization() -> None: |
| 363 | + backend = SQLiteBackend() |
| 364 | + db = Database(backend=backend, initialize=False) |
382 | 365 |
|
| 366 | + db.initialize() |
383 | 367 |
|
384 | | -@test |
385 | | -def test_uninitialized_error() -> None: |
386 | | - backend = SQLiteBackend() |
387 | | - db = Database(backend=backend, initialize=False) |
| 368 | + with db.transaction() as txn: |
| 369 | + txn.put(_TestModel, "test", _TestModel(hello="world")) |
| 370 | + doc = txn.fetch(_TestModel, "test") |
| 371 | + expect(doc.model.hello).to_equal("world") |
| 372 | + |
| 373 | + @test |
| 374 | + def test_idempotent_initialization() -> None: |
| 375 | + backend = SQLiteBackend() |
| 376 | + db = Database(backend=backend, initialize=False) |
| 377 | + |
| 378 | + db.initialize() |
| 379 | + db.initialize() |
| 380 | + db.initialize() |
388 | 381 |
|
389 | | - def try_uninitialized() -> None: |
390 | 382 | with db.transaction() as txn: |
391 | 383 | txn.put(_TestModel, "test", _TestModel(hello="world")) |
392 | 384 |
|
393 | | - expect(try_uninitialized).to_raise(ValueError, match="not initialized") |
| 385 | + @test |
| 386 | + def test_uninitialized_error() -> None: |
| 387 | + backend = SQLiteBackend() |
| 388 | + db = Database(backend=backend, initialize=False) |
| 389 | + |
| 390 | + def try_uninitialized() -> None: |
| 391 | + with db.transaction() as txn: |
| 392 | + txn.put(_TestModel, "test", _TestModel(hello="world")) |
| 393 | + |
| 394 | + expect(try_uninitialized).to_raise(ValueError, match="not initialized") |
0 commit comments