Coverage for src/tests/modules/club/repositories/test_person_db_repository.py: 100%
30 statements
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
1"""Module for testing the person repository for a database."""
3from dataclasses import replace
5import pytest
7from kwai_bc_club.repositories.person_db_repository import PersonDbRepository
8from kwai_bc_club.repositories.person_repository import (
9 PersonNotFoundException,
10 PersonRepository,
11)
12from kwai_core.db.database import Database
13from kwai_core.db.uow import UnitOfWork
16pytestmark = pytest.mark.db
19@pytest.fixture
20def person_repo(database: Database) -> PersonRepository:
21 """A fixture for a person repository."""
22 return PersonDbRepository(database)
25async def test_create_person(person_repo: PersonRepository, make_person_in_db):
26 """Test the creation of a person."""
27 person = await make_person_in_db()
28 assert not person.id.is_empty(), "There should be a person in the database."
31async def test_get_person_by_id(person_repo: PersonRepository, make_person_in_db):
32 """Test getting a person with the id."""
33 person = await make_person_in_db()
34 person = await person_repo.get(person.id)
35 assert person is not None, "There should be a person."
38async def test_update_person(
39 database: Database, person_repo: PersonRepository, make_person_in_db
40):
41 """Test updating a person."""
42 person = await make_person_in_db()
43 person = replace(person, remark="This is updated.")
44 async with UnitOfWork(database):
45 await person_repo.update(person)
46 person = await person_repo.get(person.id)
47 assert person.remark == "This is updated.", "The person should be updated."
50async def test_delete_person(
51 database: Database, person_repo: PersonRepository, make_person_in_db
52):
53 """Test delete of a person."""
54 person = await make_person_in_db()
55 async with UnitOfWork(database):
56 await person_repo.delete(person)
57 with pytest.raises(PersonNotFoundException):
58 await person_repo.get(person.id)