Model/tests/domain/addresses/test_user_address.py
Jun-te Kim 6198d7a46d postcode_splitter: pure domain (UserAddress, sanitise_postcode, postcode_batching)
Slice 1/6 of the postcode_splitter refactor (Hestia-Homes/Model#1100).
Introduces the pure-domain foundation under domain/, with no AWS, Postgres,
or pandas. UserAddress is a frozen dataclass that sanitises its postcode in
__post_init__ via the canonical sanitise_postcode helper, and
iter_postcode_grouped_batches preserves the legacy splitter's batching
invariants (group-by-postcode in insertion order, never split a group,
oversize single-postcode groups dispatched whole, final flush). Updates
UBIQUITOUS_LANGUAGE.md so the User Address term covers both the dataclass
sense (preferred in domain code) and the raw upstream-string sense.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-19 16:45:47 +00:00

45 lines
1.6 KiB
Python

import dataclasses
import pytest
from domain.addresses.user_address import UserAddress
def test_user_address_sanitises_postcode_on_construction() -> None:
addr = UserAddress(user_address="1 The Street", postcode="sw1a 1aa")
assert addr.postcode == "SW1A1AA"
def test_user_address_preserves_user_address_verbatim() -> None:
# The free-text user_address string is intentionally NOT normalised --
# only the postcode is canonicalised at the boundary.
addr = UserAddress(user_address=" 1 The Street ", postcode="sw1a 1aa")
assert addr.user_address == " 1 The Street "
def test_user_address_internal_reference_defaults_to_none() -> None:
addr = UserAddress(user_address="1 The Street", postcode="SW1A1AA")
assert addr.internal_reference is None
def test_user_address_internal_reference_accepted() -> None:
addr = UserAddress(
user_address="1 The Street",
postcode="SW1A1AA",
internal_reference="cust-42",
)
assert addr.internal_reference == "cust-42"
def test_user_address_is_frozen() -> None:
addr = UserAddress(user_address="1 The Street", postcode="SW1A1AA")
with pytest.raises(dataclasses.FrozenInstanceError):
addr.postcode = "OTHER" # type: ignore[misc]
def test_user_address_equality_uses_sanitised_postcode() -> None:
# Two instances constructed with different surface forms of the same
# postcode must compare equal because sanitisation runs eagerly.
a = UserAddress(user_address="1 The Street", postcode="sw1a 1aa")
b = UserAddress(user_address="1 The Street", postcode="SW1A1AA")
assert a == b