fooder-api/fooder/test/repository/test_base.py

96 lines
3.2 KiB
Python

import pytest
import faker
# ------------------------------------------------------------------ create ---
async def test_create_returns_object_with_id(test_repo, test_model):
model = await test_repo.create(test_model)
assert model.id is not None
assert model.property is not None
# -------------------------------------------------------------------- get ----
async def test_get_returns_existing_record(test_repo, test_model):
created = await test_repo.create(test_model)
found = await test_repo.get(id=created.id)
assert found is not None
assert found.id == created.id
async def test_get_returns_none_for_missing_record(test_repo):
result = await test_repo.get(id=1)
assert result is None
async def test_get_by_field(test_repo, test_model_factory):
await test_repo.create(test_model_factory(1, "value"))
found = await test_repo.get(property="value")
assert found is not None
assert found.id == 1
# ------------------------------------------------------------------- list ----
async def test_list_returns_all_matching(test_repo, test_model_factory):
await test_repo.create(test_model_factory(1))
await test_repo.create(test_model_factory(2))
results = await test_repo.list()
modelnames = {u.id for u in results}
assert {1, 2}.issubset(modelnames)
async def test_list_with_filter(test_repo, test_model_factory):
await test_repo.create(test_model_factory(1, "value"))
await test_repo.create(test_model_factory(2, "value2"))
results = await test_repo.list(property="value")
assert len(results) == 1
assert results[0].id == 1
async def test_list_returns_empty_when_no_match(test_repo, test_model_factory):
await test_repo.create(test_model_factory(1, "value"))
results = await test_repo.list(property="value2")
assert results == []
# ------------------------------------------------------------------ delete ---
async def test_delete_removes_record_and_returns_count(test_repo, test_model):
model = await test_repo.create(test_model)
count = await test_repo.delete(id=model.id)
assert count == 1
assert await test_repo.get(id=model.id) is None
async def test_delete_returns_zero_when_nothing_matched(test_repo, test_model_factory):
count = await test_repo.delete(id=999999)
assert count == 0
# ------------------------------------------------------------------ update ---
async def test_update_modifies_record_and_returns_count(test_repo, test_model):
model = await test_repo.create(test_model)
count = await test_repo.update({"id": model.id}, {"property": "value2"})
assert count == 1
refreshed = await test_repo.get(id=model.id)
assert refreshed is not None
assert refreshed.property == "value2"
async def test_update_returns_zero_when_nothing_matched(test_repo, test_model_factory):
count = await test_repo.update({"id": 999999}, {"property": "value"})
assert count == 0
# ------------------------------------------------------- _build_select ------
def test_build_select_raises_for_unknown_field(test_repo):
with pytest.raises(ValueError, match="has no attribute 'nonexistent'"):
test_repo._build_select(nonexistent="value")