fooder-api/fooder/test/repository/test_base.py
2026-04-07 16:49:30 +02:00

68 lines
2.3 KiB
Python

import pytest
from ..fixtures.db import TestModel
from fooder.exc import NotFound
# ------------------------------------------------------------------ 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(TestModel.id == created.id)
assert found is not None
assert found.id == created.id
async def test_get_raises_not_found_for_missing_record(test_repo):
with pytest.raises(NotFound):
await test_repo._get(TestModel.id == 1)
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(TestModel.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(TestModel.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(TestModel.property == "value2")
assert results == []
# ------------------------------------------------------------------ delete ---
async def test_delete_removes_record(test_repo, test_model):
model = await test_repo.create(test_model)
await test_repo._delete(TestModel.id == model.id)
with pytest.raises(NotFound):
await test_repo._get(TestModel.id == model.id)