Coverage for src/tests/fixtures/club/contacts.py: 98%
40 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 defining fixtures for contacts."""
3import pytest
5from kwai_bc_club.domain.contact import ContactEntity
6from kwai_bc_club.domain.country import CountryEntity
7from kwai_bc_club.domain.value_objects import Address
8from kwai_bc_club.repositories.contact_db_repository import ContactDbRepository
9from kwai_core.db.database import Database
10from kwai_core.db.uow import UnitOfWork
11from kwai_core.domain.value_objects.email_address import EmailAddress
14@pytest.fixture
15def make_address(country_japan):
16 """A factory fixture for an address."""
18 def _make_address(
19 *,
20 address: str = "1-16-30 Kasuga",
21 postal_code: str = "112-0003",
22 city: str = "Tokyo",
23 county: str = "Bunkyo-ku",
24 country: CountryEntity | None = None,
25 ) -> Address:
26 return Address(
27 address=address,
28 postal_code=postal_code,
29 city=city,
30 county=county,
31 country=country or country_japan,
32 )
34 return _make_address
37@pytest.fixture
38def make_emails():
39 """A factory fixture for a contact email."""
41 def _make_emails(email: str | None = None) -> tuple[EmailAddress, ...]:
42 if email is None:
43 return (EmailAddress("jigoro.kano@kwai.com"),)
44 return (EmailAddress(email),)
46 return _make_emails
49@pytest.fixture
50def make_contact(make_emails, make_address):
51 """A factory fixture for a contact."""
53 def _make_contact(
54 *,
55 emails: tuple[EmailAddress, ...] | None = None,
56 address: Address | None = None,
57 ):
58 return ContactEntity(
59 emails=emails or make_emails(),
60 address=address or make_address(),
61 )
63 return _make_contact
66@pytest.fixture
67async def make_contact_in_db(
68 database: Database,
69 make_contact,
70 make_address,
71 make_country_in_db,
72):
73 """A fixture for a contact in the database."""
74 created_entities = []
76 async def _make_contact_in_db(
77 contact: ContactEntity | None = None,
78 ) -> ContactEntity:
79 contact = contact or make_contact(
80 address=make_address(country=await make_country_in_db())
81 )
82 repo = ContactDbRepository(database)
83 async with UnitOfWork(database):
84 contact = await repo.create(contact)
86 created_entities.append(contact)
88 return contact
90 yield _make_contact_in_db
92 repo = ContactDbRepository(database)
94 for entity in created_entities:
95 async with UnitOfWork(database):
96 await repo.delete(entity)