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

33 statements  

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

1"""Module for fixtures related to countries.""" 

2 

3import pytest 

4 

5from kwai_bc_club.domain.country import CountryEntity 

6from kwai_bc_club.repositories.country_db_repository import CountryDbRepository 

7from kwai_bc_club.repositories.country_repository import CountryNotFoundException 

8from kwai_core.db.database import Database 

9from kwai_core.db.uow import UnitOfWork 

10 

11 

12@pytest.fixture 

13def make_country(): 

14 """A factory fixture for a country.""" 

15 

16 def _make_country(iso_2="XX", iso_3="XXX", name="Test Country"): 

17 return CountryEntity(iso_2=iso_2, iso_3=iso_3, name=name) 

18 

19 return _make_country 

20 

21 

22@pytest.fixture 

23def country_japan(): 

24 """A factory fixture for the country Japan.""" 

25 return CountryEntity(iso_2="JP", iso_3="JPN", name="Japan") 

26 

27 

28@pytest.fixture 

29async def make_country_in_db(database: Database, make_country): 

30 """A fixture for a country in the database. 

31 

32 When the country is already in the database, it will be returned. 

33 If not it will be saved in the database, returned and deleted afterward. 

34 """ 

35 created_entities = [] 

36 

37 async def _make_country_in_db( 

38 country: CountryEntity | None = None, 

39 ) -> CountryEntity: 

40 country = country or make_country() 

41 repo = CountryDbRepository(database) 

42 try: 

43 country = await repo.get_by_iso_2(country.iso_2) 

44 return country 

45 except CountryNotFoundException: 

46 async with UnitOfWork(database): 

47 country = await repo.create(country) 

48 

49 created_entities.append(country) 

50 

51 return country 

52 

53 yield _make_country_in_db 

54 

55 repo = CountryDbRepository(database) 

56 for entity in created_entities: 

57 async with UnitOfWork(database): 

58 await repo.delete(entity)