Coverage for src/tests/fixtures/club/persons.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module for defining fixtures for persons.""" 

2 

3import pytest 

4 

5from kwai_bc_club.domain.contact import ContactEntity 

6from kwai_bc_club.domain.country import CountryEntity 

7from kwai_bc_club.domain.person import PersonEntity 

8from kwai_bc_club.domain.value_objects import Birthdate, Gender 

9from kwai_bc_club.repositories.person_db_repository import PersonDbRepository 

10from kwai_core.db.database import Database 

11from kwai_core.db.uow import UnitOfWork 

12from kwai_core.domain.value_objects.date import Date 

13from kwai_core.domain.value_objects.name import Name 

14 

15 

16@pytest.fixture 

17def make_person(make_contact, country_japan): 

18 """A factory fixture for a person.""" 

19 

20 def _make_person( 

21 name: Name | None = None, 

22 gender: Gender | None = None, 

23 birthdate: Birthdate | None = None, 

24 nationality: CountryEntity | None = None, 

25 contact: ContactEntity | None = None, 

26 ) -> PersonEntity: 

27 return PersonEntity( 

28 name=name or Name(first_name="Jigoro", last_name="Kano"), 

29 gender=gender or Gender.MALE, 

30 birthdate=birthdate or Birthdate(Date.create(year=1860, month=10, day=28)), 

31 nationality=nationality or country_japan, 

32 contact=contact or make_contact(), 

33 ) 

34 

35 return _make_person 

36 

37 

38@pytest.fixture 

39async def make_person_in_db( 

40 database: Database, 

41 make_person, 

42 make_contact_in_db, 

43 make_country_in_db, 

44 country_japan, 

45): 

46 """A factory fixture for a person in a database.""" 

47 created_entities = [] 

48 

49 async def _make_person_in_db(person: PersonEntity | None = None) -> PersonEntity: 

50 person = person or make_person( 

51 contact=await make_contact_in_db(), 

52 nationality=await make_country_in_db(country_japan), 

53 ) 

54 repo = PersonDbRepository(database) 

55 async with UnitOfWork(database): 

56 person = await repo.create(person) 

57 

58 created_entities.append(person) 

59 

60 return person 

61 

62 yield _make_person_in_db 

63 

64 repo = PersonDbRepository(database) 

65 for entity in created_entities: 

66 async with UnitOfWork(database): 

67 await repo.delete(entity)