|
| 1 | +import pytest |
| 2 | + |
| 3 | +from sqlalchemy_bind_manager.exceptions import UnmappedProperty |
| 4 | +from sqlalchemy_bind_manager.repository import SortDirection |
| 5 | + |
| 6 | + |
| 7 | +async def test_find(repository_class, model_class, sa_bind, sync_async_wrapper): |
| 8 | + model = model_class( |
| 9 | + name="Someone", |
| 10 | + ) |
| 11 | + model2 = model_class( |
| 12 | + name="SomeoneElse", |
| 13 | + ) |
| 14 | + model3 = model_class( |
| 15 | + name="StillSomeoneElse", |
| 16 | + ) |
| 17 | + repo = repository_class(bind=sa_bind, model_class=model_class) |
| 18 | + await sync_async_wrapper(repo.save_many({model, model2, model3})) |
| 19 | + |
| 20 | + results = await sync_async_wrapper(repo.find()) |
| 21 | + assert len(results) == 3 |
| 22 | + |
| 23 | + |
| 24 | +async def test_find_filtered(repository_class, model_class, sa_bind, sync_async_wrapper): |
| 25 | + model = model_class( |
| 26 | + name="Someone", |
| 27 | + ) |
| 28 | + model2 = model_class( |
| 29 | + name="SomeoneElse", |
| 30 | + ) |
| 31 | + model3 = model_class( |
| 32 | + name="StillSomeoneElse", |
| 33 | + ) |
| 34 | + repo = repository_class(bind=sa_bind, model_class=model_class) |
| 35 | + await sync_async_wrapper(repo.save_many({model, model2, model3})) |
| 36 | + |
| 37 | + results = await sync_async_wrapper(repo.find(search_params={"name": "Someone"})) |
| 38 | + assert len(results) == 1 |
| 39 | + |
| 40 | + |
| 41 | +async def test_find_filtered_fails_if_invalid_filter(repository_class, model_class, sa_bind, sync_async_wrapper): |
| 42 | + repo = repository_class(bind=sa_bind, model_class=model_class) |
| 43 | + with pytest.raises(UnmappedProperty): |
| 44 | + await sync_async_wrapper(repo.find(search_params={"somename": "Someone"})) |
| 45 | + |
| 46 | + |
| 47 | +async def test_find_ordered(repository_class, model_class, sa_bind, sync_async_wrapper): |
| 48 | + model = model_class( |
| 49 | + name="Abbott", |
| 50 | + ) |
| 51 | + model2 = model_class( |
| 52 | + name="Costello", |
| 53 | + ) |
| 54 | + repo = repository_class(bind=sa_bind, model_class=model_class) |
| 55 | + await sync_async_wrapper(repo.save_many({model, model2})) |
| 56 | + |
| 57 | + results = await sync_async_wrapper(repo.find(order_by=("name",))) |
| 58 | + assert results[0].name == "Abbott" |
| 59 | + assert results[1].name == "Costello" |
| 60 | + |
| 61 | + results2 = await sync_async_wrapper(repo.find(order_by=(("name", SortDirection.ASC),))) |
| 62 | + assert results2[0].name == "Abbott" |
| 63 | + assert results2[1].name == "Costello" |
| 64 | + |
| 65 | + results3 = await sync_async_wrapper(repo.find(order_by=(("name", SortDirection.DESC),))) |
| 66 | + assert results3[1].name == "Abbott" |
| 67 | + assert results3[0].name == "Costello" |
| 68 | + |
| 69 | + |
| 70 | +async def test_find_ordered_fails_if_invalid_column(repository_class, model_class, sa_bind, sync_async_wrapper): |
| 71 | + repo = repository_class(bind=sa_bind, model_class=model_class) |
| 72 | + with pytest.raises(UnmappedProperty): |
| 73 | + await repo.find(order_by=("unexisting",)) |
| 74 | + with pytest.raises(UnmappedProperty): |
| 75 | + await repo.find(order_by=(("unexisting", SortDirection.DESC),)) |
0 commit comments