|
| 1 | +import pytest |
| 2 | + |
| 3 | + |
| 4 | +async def test_can_delete_by_instance( |
| 5 | + repository_class, model_class, sa_bind, sync_async_wrapper |
| 6 | +): |
| 7 | + model = model_class( |
| 8 | + model_id=1, |
| 9 | + name="Someone", |
| 10 | + ) |
| 11 | + model2 = model_class( |
| 12 | + model_id=2, |
| 13 | + name="SomeoneElse", |
| 14 | + ) |
| 15 | + repo = repository_class(bind=sa_bind, model_class=model_class) |
| 16 | + await sync_async_wrapper(repo.save_many({model, model2})) |
| 17 | + |
| 18 | + results = [x for x in await sync_async_wrapper(repo.find())] |
| 19 | + assert len(results) == 2 |
| 20 | + |
| 21 | + await sync_async_wrapper(repo.delete(model)) |
| 22 | + results = [x for x in await sync_async_wrapper(repo.find())] |
| 23 | + assert len(results) == 1 |
| 24 | + assert results[0].model_id == 2 |
| 25 | + assert results[0].name == "SomeoneElse" |
| 26 | + |
| 27 | + |
| 28 | +async def test_delete_inexistent_raises_exception( |
| 29 | + repository_class, model_class, sa_bind, sync_async_wrapper |
| 30 | +): |
| 31 | + repo = repository_class(bind=sa_bind, model_class=model_class) |
| 32 | + |
| 33 | + results = [x for x in await sync_async_wrapper(repo.find())] |
| 34 | + assert len(results) == 0 |
| 35 | + |
| 36 | + with pytest.raises(Exception): |
| 37 | + await sync_async_wrapper(repo.delete(4)) |
| 38 | + |
| 39 | + with pytest.raises(Exception): |
| 40 | + await sync_async_wrapper( |
| 41 | + repo.delete( |
| 42 | + model_class( |
| 43 | + model_id=823, |
| 44 | + name="Someone", |
| 45 | + ) |
| 46 | + ) |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +async def test_relationships_are_respected( |
| 51 | + repository_class, model_classes, sa_bind, sync_async_wrapper |
| 52 | +): |
| 53 | + repo = repository_class(bind=sa_bind, model_class=model_classes[0]) |
| 54 | + children_repo = repository_class(bind=sa_bind, model_class=model_classes[1]) |
| 55 | + |
| 56 | + parent = model_classes[0]( |
| 57 | + name="A Parent", |
| 58 | + ) |
| 59 | + child = model_classes[1](name="A Child") |
| 60 | + child2 = model_classes[1](name="Another Child") |
| 61 | + parent.children.append(child) |
| 62 | + parent.children.append(child2) |
| 63 | + |
| 64 | + await sync_async_wrapper(repo.save(parent)) |
| 65 | + |
| 66 | + retrieved_parent = await sync_async_wrapper(repo.get(parent.model_id)) |
| 67 | + assert len(retrieved_parent.children) == 2 |
| 68 | + children_retrieve_using_repo = await sync_async_wrapper(children_repo.find()) |
| 69 | + assert len(children_retrieve_using_repo) == 2 |
| 70 | + |
| 71 | + await sync_async_wrapper(repo.delete(retrieved_parent)) |
| 72 | + |
| 73 | + assert len(await sync_async_wrapper(children_repo.find())) == 0 |
| 74 | + children_retrieve_using_repo = await sync_async_wrapper(children_repo.find()) |
| 75 | + assert len(children_retrieve_using_repo) == 0 |
0 commit comments