import pytest from apps.customers.domain.entities import BillingUnit, Contact, Customer from apps.customers.domain.enums import CustomerType from apps.customers.domain.exceptions import ( ConsumerCannotHaveBusinessId, EInvoiceOperatorRequired, InternalCustomerMustBeCompany, InvalidBusinessId, InvalidContactName, InvalidCustomerName, InvalidEInvoiceAddress, InvalidPersonalIdentityCode, ) from apps.customers.domain.value_objects import ( BusinessId, CustomerName, EInvoiceAddress, PersonalIdentityCode, ) class TestBusinessId: @pytest.mark.parametrize( ("value", "expected"), [ ("0112038-9", "0112038-9"), # Nokia ("01120389", "0112038-9"), ("112038-9", "0112038-9"), # leading zero missing (" 1572860-0 ", "1572860-0"), # remainder 0 → check digit 0 ], ) def test_valid(self, value: str, expected: str) -> None: assert BusinessId(value).value == expected @pytest.mark.parametrize("value", ["0112038-8", "1234567", "abcdefg-1", "", "12345678-9"]) def test_invalid(self, value: str) -> None: with pytest.raises(InvalidBusinessId): BusinessId(value) def test_remainder_one_is_never_valid(self) -> None: weights = (7, 9, 10, 5, 8, 4, 2) body = next( f"{n:07d}" for n in range(1, 10_000) if sum(int(d) * w for d, w in zip(f"{n:07d}", weights, strict=True)) % 11 == 1 ) for check in range(10): with pytest.raises(InvalidBusinessId): BusinessId(f"{body}-{check}") class TestPersonalIdentityCode: @pytest.mark.parametrize( "value", ["131052-308T", "131052-308t", "010594Y9032", "010594Y9021", "020300A123N"] ) def test_valid(self, value: str) -> None: assert PersonalIdentityCode(value).value == value.upper() @pytest.mark.parametrize( "value", [ "131052-308X", # wrong check character "310252-308T", # 31 February "131052Z308T", # unknown century sign "131052-001T", # individual number below 002 "13105-308T", "", ], ) def test_invalid(self, value: str) -> None: with pytest.raises(InvalidPersonalIdentityCode): PersonalIdentityCode(value) def test_masked_and_repr_hide_birth_date(self) -> None: code = PersonalIdentityCode("131052-308T") assert code.masked == "••••••-308T" assert "131052" not in repr(code) assert "131052" not in str(code) class TestOtherValueObjects: def test_customer_name(self) -> None: assert CustomerName(" Oy Esimerkki Ab ").value == "Oy Esimerkki Ab" with pytest.raises(InvalidCustomerName): CustomerName(" ") def test_e_invoice_address(self) -> None: assert EInvoiceAddress("0037 1234 5678").value == "003712345678" for bad in ["12345678901", "x" * 36, "FI12-3456-7890"]: with pytest.raises(InvalidEInvoiceAddress): EInvoiceAddress(bad) class TestCustomer: def test_consumer_cannot_have_business_id(self) -> None: with pytest.raises(ConsumerCannotHaveBusinessId): Customer.register( name=CustomerName("Kalle"), type=CustomerType.CONSUMER, business_id=BusinessId("0112038-9"), ) def test_becoming_company_drops_personal_id(self) -> None: consumer = Customer.reconstitute( id=1, name=CustomerName("Kalle"), type=CustomerType.CONSUMER, business_id=None, is_internal=False, has_personal_id=True, ) company = consumer.change_details( name=CustomerName("Kalle Oy"), type=CustomerType.COMPANY, business_id=None ) assert not company.has_personal_id def test_internal_customer_stays_company(self) -> None: internal = Customer.reconstitute( id=1, name=CustomerName("Oma yritys"), type=CustomerType.COMPANY, business_id=None, is_internal=True, has_personal_id=False, ) with pytest.raises(InternalCustomerMustBeCompany): internal.change_details( name=CustomerName("Oma"), type=CustomerType.CONSUMER, business_id=None ) class TestChildren: def test_contact_requires_name(self) -> None: with pytest.raises(InvalidContactName): Contact(id=None, customer_id=1, name=" ") def test_e_invoice_needs_operator(self) -> None: with pytest.raises(EInvoiceOperatorRequired): BillingUnit( id=None, customer_id=1, name="Laskutus", e_invoice_address=EInvoiceAddress("003712345678"), ) unit = BillingUnit( id=None, customer_id=1, name="Laskutus", e_invoice_address=EInvoiceAddress("003712345678"), e_invoice_operator=" 003721291126 ", ) assert unit.e_invoice_operator == "003721291126"