Coverage for src/tests/modules/club/repositories/test_contact_db_repository.py: 100%

30 statements  

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

1"""Module for testing the contact repository for a database.""" 

2 

3from dataclasses import replace 

4 

5import pytest 

6 

7from kwai_bc_club.repositories.contact_db_repository import ContactDbRepository 

8from kwai_bc_club.repositories.contact_repository import ( 

9 ContactNotFoundException, 

10 ContactRepository, 

11) 

12from kwai_core.db.database import Database 

13from kwai_core.db.uow import UnitOfWork 

14 

15 

16pytestmark = pytest.mark.db 

17 

18 

19@pytest.fixture 

20def contact_repo(database: Database) -> ContactRepository: 

21 """A fixture for a contact repository.""" 

22 return ContactDbRepository(database) 

23 

24 

25async def test_create_contact(make_contact_in_db): 

26 """Test creating a contact.""" 

27 contact = await make_contact_in_db() 

28 assert not contact.id.is_empty(), "Contact should be saved" 

29 

30 

31async def test_get_contact_by_id(contact_repo: ContactRepository, make_contact_in_db): 

32 """Test getting the contact with the id.""" 

33 contact = await make_contact_in_db() 

34 contact = await contact_repo.get(contact.id) 

35 assert contact is not None, "Contact should be returned" 

36 

37 

38async def test_update_contact( 

39 database, contact_repo: ContactRepository, make_contact_in_db 

40): 

41 """Test updating a contact.""" 

42 contact = await make_contact_in_db() 

43 contact = replace(contact, remark="This is an update") 

44 async with UnitOfWork(database): 

45 await contact_repo.update(contact) 

46 contact = await contact_repo.get(contact.id) 

47 assert contact.remark == "This is an update", "The contact should be updated." 

48 

49 

50async def test_delete_contact( 

51 database: Database, contact_repo: ContactRepository, make_contact_in_db 

52): 

53 """Test delete of a contact.""" 

54 contact = await make_contact_in_db() 

55 async with UnitOfWork(database): 

56 await contact_repo.delete(contact) 

57 with pytest.raises(ContactNotFoundException): 

58 await contact_repo.get(contact.id)