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

1"""Module for defining fixtures for contacts.""" 

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.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 

12 

13 

14@pytest.fixture 

15def make_address(country_japan): 

16 """A factory fixture for an address.""" 

17 

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 ) 

33 

34 return _make_address 

35 

36 

37@pytest.fixture 

38def make_emails(): 

39 """A factory fixture for a contact email.""" 

40 

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),) 

45 

46 return _make_emails 

47 

48 

49@pytest.fixture 

50def make_contact(make_emails, make_address): 

51 """A factory fixture for a contact.""" 

52 

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 ) 

62 

63 return _make_contact 

64 

65 

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 = [] 

75 

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) 

85 

86 created_entities.append(contact) 

87 

88 return contact 

89 

90 yield _make_contact_in_db 

91 

92 repo = ContactDbRepository(database) 

93 

94 for entity in created_entities: 

95 async with UnitOfWork(database): 

96 await repo.delete(entity)