from dataclasses import replace import pytest from apps.customers.application.dto import BillingUnitData, ContactData, CustomerDetailsData from apps.customers.application.use_cases import ( AddBillingUnit, AddContact, DeleteContact, DeleteCustomer, RegisterCustomer, UpdateBillingUnit, UpdateContact, UpdateCustomer, ) from apps.customers.domain.enums import CustomerType from apps.customers.domain.exceptions import ( BillingUnitNotFound, BusinessIdTaken, CompanyCannotHavePersonalId, ContactNotFound, CustomerInUse, CustomerNotFound, InternalCustomerCannotBeDeleted, PersonalIdRemovalNotAllowed, PersonalIdTaken, ) from apps.customers.tests.fakes import ( InMemoryBillingUnitRepository, InMemoryContactRepository, InMemoryCustomerRepository, ) from apps.shared.tests.fakes import FakeUnitOfWork PIC = "131052-308T" PIC2 = "010594Y9032" @pytest.fixture def repo() -> InMemoryCustomerRepository: return InMemoryCustomerRepository() def company(**kw: object) -> CustomerDetailsData: return CustomerDetailsData(**{"name": "Oy Esimerkki Ab", "type": CustomerType.COMPANY, **kw}) # type: ignore[arg-type] def consumer(**kw: object) -> CustomerDetailsData: return CustomerDetailsData(**{"name": "Kalle Kuluttaja", "type": CustomerType.CONSUMER, **kw}) # type: ignore[arg-type] def register(repo: InMemoryCustomerRepository, data: CustomerDetailsData) -> int: return RegisterCustomer(repo, FakeUnitOfWork()).execute(data) class TestRegister: def test_business_id_is_unique(self, repo: InMemoryCustomerRepository) -> None: register(repo, company(business_id="0112038-9")) with pytest.raises(BusinessIdTaken): register(repo, company(name="Toinen", business_id="01120389")) def test_personal_id_is_stored_and_unique(self, repo: InMemoryCustomerRepository) -> None: cid = register(repo, consumer(personal_identity_code=PIC)) assert repo.personal_ids[cid] == PIC assert repo.get(cid).has_personal_id # type: ignore[union-attr] with pytest.raises(PersonalIdTaken): register(repo, consumer(name="Toinen", personal_identity_code=PIC.lower())) def test_company_cannot_get_personal_id(self, repo: InMemoryCustomerRepository) -> None: with pytest.raises(CompanyCannotHavePersonalId): register(repo, company(personal_identity_code=PIC)) class TestUpdatePersonalId: def update( self, repo: InMemoryCustomerRepository, cid: int, data: CustomerDetailsData, manage: bool = False, ) -> None: UpdateCustomer(repo, FakeUnitOfWork()).execute(cid, data, can_manage_personal_id=manage) def test_empty_field_keeps_code(self, repo: InMemoryCustomerRepository) -> None: cid = register(repo, consumer(personal_identity_code=PIC)) self.update(repo, cid, consumer(name="Kalle K.")) assert repo.personal_ids[cid] == PIC def test_new_value_replaces_code(self, repo: InMemoryCustomerRepository) -> None: cid = register(repo, consumer(personal_identity_code=PIC)) self.update(repo, cid, consumer(personal_identity_code=PIC2)) assert repo.personal_ids[cid] == PIC2 def test_only_admin_or_management_may_remove(self, repo: InMemoryCustomerRepository) -> None: cid = register(repo, consumer(personal_identity_code=PIC)) with pytest.raises(PersonalIdRemovalNotAllowed): self.update(repo, cid, consumer(remove_personal_id=True)) self.update(repo, cid, consumer(remove_personal_id=True), manage=True) assert cid not in repo.personal_ids def test_becoming_company_removes_code(self, repo: InMemoryCustomerRepository) -> None: cid = register(repo, consumer(personal_identity_code=PIC)) self.update(repo, cid, company(name="Kalle Oy")) assert cid not in repo.personal_ids assert repo.get(cid).type is CustomerType.COMPANY # type: ignore[union-attr] def test_unknown_customer(self, repo: InMemoryCustomerRepository) -> None: with pytest.raises(CustomerNotFound): self.update(repo, 99, company()) class TestDelete: def test_internal_customer_cannot_be_deleted(self, repo: InMemoryCustomerRepository) -> None: cid = register(repo, company()) repo.customers[cid] = replace(repo.customers[cid], is_internal=True) with pytest.raises(InternalCustomerCannotBeDeleted): DeleteCustomer(repo, FakeUnitOfWork()).execute(cid) def test_referenced_customer_cannot_be_deleted(self, repo: InMemoryCustomerRepository) -> None: cid = register(repo, company()) repo.references.add(cid) with pytest.raises(CustomerInUse): DeleteCustomer(repo, FakeUnitOfWork()).execute(cid) def test_delete(self, repo: InMemoryCustomerRepository) -> None: cid = register(repo, company()) DeleteCustomer(repo, FakeUnitOfWork()).execute(cid) assert repo.get(cid) is None class TestContactsAndBillingUnits: def test_contact_lifecycle_and_ownership(self, repo: InMemoryCustomerRepository) -> None: contacts = InMemoryContactRepository() a = register(repo, company()) b = register(repo, company(name="Toinen")) contact_id = AddContact(repo, contacts, FakeUnitOfWork()).execute( a, ContactData(name="Maija", email="MAIJA@example.test") ) assert contacts.items[contact_id].email.value == "maija@example.test" # type: ignore[union-attr] with pytest.raises(ContactNotFound): # belongs to customer a, not b UpdateContact(repo, contacts, FakeUnitOfWork()).execute( b, contact_id, ContactData(name="X") ) with pytest.raises(ContactNotFound): DeleteContact(repo, contacts, FakeUnitOfWork()).execute(b, contact_id) DeleteContact(repo, contacts, FakeUnitOfWork()).execute(a, contact_id) assert contacts.items == {} def test_contact_for_unknown_customer(self, repo: InMemoryCustomerRepository) -> None: with pytest.raises(CustomerNotFound): AddContact(repo, InMemoryContactRepository(), FakeUnitOfWork()).execute( 5, ContactData(name="Maija") ) def test_billing_unit(self, repo: InMemoryCustomerRepository) -> None: units = InMemoryBillingUnitRepository() a = register(repo, company()) unit_id = AddBillingUnit(repo, units, FakeUnitOfWork()).execute( a, BillingUnitData( name="Pääkonttori", e_invoice_address="0037 0112 0389", e_invoice_operator="003721291126", ), ) assert units.items[unit_id].e_invoice_address.value == "003701120389" # type: ignore[union-attr] with pytest.raises(BillingUnitNotFound): UpdateBillingUnit(repo, units, FakeUnitOfWork()).execute( a + 1, unit_id, BillingUnitData(name="X") )