|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import tempfile |
4 | | -from pathlib import Path |
5 | 3 | from typing import TYPE_CHECKING |
6 | 4 |
|
7 | 5 | from pydantic import BaseModel |
8 | | -from tryke import describe, expect, test |
9 | 6 |
|
10 | | -from sapling import MemoryBackend, SQLiteBackend |
11 | | -from sapling.errors import NotFoundError |
12 | | -from sapling.settings import SaplingSettings |
| 7 | +from sapling import SQLiteBackend |
13 | 8 |
|
14 | 9 | if TYPE_CHECKING: |
15 | 10 | from collections.abc import Generator, Iterator |
@@ -247,148 +242,3 @@ def put_many[T: BaseModel]( |
247 | 242 | """ |
248 | 243 | with self.transaction() as txn: |
249 | 244 | return txn.put_many(model_class, models) |
250 | | - |
251 | | - |
252 | | -class _TestModel(BaseModel): |
253 | | - hello: str = "world" |
254 | | - |
255 | | - |
256 | | -@test |
257 | | -def test_basic() -> None: |
258 | | - db = Database() |
259 | | - hello = _TestModel() |
260 | | - with db.transaction() as txn: |
261 | | - pk = "hello" |
262 | | - record = txn.put(_TestModel, pk, hello) |
263 | | - expect(record.model_id).to_equal(pk) |
264 | | - maybe_record = txn.get(_TestModel, pk) |
265 | | - expect(maybe_record).to_be_truthy() |
266 | | - record = txn.fetch(_TestModel, pk) |
267 | | - txn.delete(_TestModel, pk) |
268 | | - expect(lambda: txn.fetch(_TestModel, pk)).to_raise(NotFoundError) |
269 | | - |
270 | | - |
271 | | -@test |
272 | | -def test_all_method() -> None: |
273 | | - db = Database() |
274 | | - with db.transaction() as txn: |
275 | | - txn.put(_TestModel, "1", _TestModel(hello="one")) |
276 | | - txn.put(_TestModel, "2", _TestModel(hello="two")) |
277 | | - txn.put(_TestModel, "3", _TestModel(hello="three")) |
278 | | - |
279 | | - all_hellos = txn.all(_TestModel) |
280 | | - expect(all_hellos).to_have_length(3) |
281 | | - expect({h.model_id for h in all_hellos}).to_equal({"1", "2", "3"}) |
282 | | - expect({h.model.hello for h in all_hellos}).to_equal({"one", "two", "three"}) |
283 | | - |
284 | | - |
285 | | -@test |
286 | | -def test_all_empty() -> None: |
287 | | - db = Database() |
288 | | - with db.transaction() as txn: |
289 | | - all_hellos = txn.all(_TestModel) |
290 | | - expect(all_hellos).to_equal([]) |
291 | | - |
292 | | - |
293 | | -@test |
294 | | -def test_backend_all_method() -> None: |
295 | | - backend = SQLiteBackend() |
296 | | - db = Database(backend=backend) |
297 | | - |
298 | | - with db.transaction() as txn: |
299 | | - txn.put(_TestModel, "a", _TestModel(hello="alpha")) |
300 | | - txn.put(_TestModel, "b", _TestModel(hello="beta")) |
301 | | - |
302 | | - all_docs = txn.all(_TestModel) |
303 | | - expect(all_docs).to_have_length(2) |
304 | | - expect({d.model_id for d in all_docs}).to_equal({"a", "b"}) |
305 | | - |
306 | | - |
307 | | -@test |
308 | | -def test_memory_backend() -> None: |
309 | | - backend = MemoryBackend() |
310 | | - db = Database(backend=backend) |
311 | | - |
312 | | - with db.transaction() as txn: |
313 | | - txn.put(_TestModel, "test", _TestModel(hello="world")) |
314 | | - doc = txn.fetch(_TestModel, "test") |
315 | | - expect(doc.model.hello).to_equal("world") |
316 | | - |
317 | | - txn.put(_TestModel, "1", _TestModel(hello="one")) |
318 | | - txn.put(_TestModel, "2", _TestModel(hello="two")) |
319 | | - |
320 | | - all_docs = txn.all(_TestModel) |
321 | | - expect(all_docs).to_have_length(3) |
322 | | - expect({d.model_id for d in all_docs}).to_equal({"test", "1", "2"}) |
323 | | - |
324 | | - txn.delete(_TestModel, "test") |
325 | | - expect(txn.get(_TestModel, "test")).to_be_none() |
326 | | - |
327 | | - expect(lambda: txn.fetch(_TestModel, "test")).to_raise(NotFoundError) |
328 | | - |
329 | | - |
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") |
340 | | - |
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) |
348 | | - |
349 | | - with db.transaction() as txn: |
350 | | - txn.put(_TestModel, "persistent", _TestModel(hello="saved")) |
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") |
357 | | - |
358 | | - |
359 | | -with describe("initialization"): |
360 | | - |
361 | | - @test |
362 | | - def test_deferred_initialization() -> None: |
363 | | - backend = SQLiteBackend() |
364 | | - db = Database(backend=backend, initialize=False) |
365 | | - |
366 | | - db.initialize() |
367 | | - |
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() |
381 | | - |
382 | | - with db.transaction() as txn: |
383 | | - txn.put(_TestModel, "test", _TestModel(hello="world")) |
384 | | - |
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