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
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
1"""Module for fixtures related to countries."""
3import pytest
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
12@pytest.fixture
13def make_country():
14 """A factory fixture for a country."""
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)
19 return _make_country
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")
28@pytest.fixture
29async def make_country_in_db(database: Database, make_country):
30 """A fixture for a country in the database.
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 = []
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)
49 created_entities.append(country)
51 return country
53 yield _make_country_in_db
55 repo = CountryDbRepository(database)
56 for entity in created_entities:
57 async with UnitOfWork(database):
58 await repo.delete(entity)