31 lines
1.9 KiB
Python
31 lines
1.9 KiB
Python
import unittest
|
|||
|
|
from app.domain import normalize_phone, normalize_location, normalize_business, match_businesses
|
||
|
|
|
||
|
|
class Phase6DomainTests(unittest.TestCase):
|
||
|
|
def test_south_african_phone_formats_share_canonical_value(self):
|
||
|
|
self.assertEqual(normalize_phone("082 555 1234"), "+27825551234")
|
||
|
|
self.assertEqual(normalize_phone("0027 82 555 1234"), "+27825551234")
|
||
|
|
self.assertEqual(normalize_phone("+27 (82) 555-1234"), "+27825551234")
|
||
|
|
|
||
|
|
def test_unknown_international_phone_is_not_rewritten(self):
|
||
|
|
self.assertEqual(normalize_phone("+44 (20) 1234 5678"), "+442012345678")
|
||
|
|
self.assertEqual(normalize_phone("555-1234"), "5551234")
|
||
|
|
|
||
|
|
def test_location_normalization_and_business_fields(self):
|
||
|
|
self.assertEqual(normalize_location({"province": " Gauteng ", "city": " Johannesburg ", "suburb": " Sandton "}), {"province": "gauteng", "city": "johannesburg", "suburb": "sandton"})
|
||
|
|
b = normalize_business({"name":" Acme ", "province":" Gauteng ", "city":" Johannesburg ", "suburb":" Sandton "})
|
||
|
|
self.assertEqual((b["province"], b["city"], b["suburb"]), ("gauteng", "johannesburg", "sandton"))
|
||
|
|
|
||
|
|
def test_matching_has_deterministic_confidence_and_reasons(self):
|
||
|
|
a = {"name":"Acme Consulting", "email":"hello@acme.co.za", "city":"Johannesburg"}
|
||
|
|
b = {"name":"Acme Consultng", "email":"hello@acme.co.za", "city":"Johannesburg"}
|
||
|
|
first = match_businesses(a, [dict(b, id=2), {"id":3,"name":"Unrelated Shop"}], threshold=0.5)
|
||
|
|
second = match_businesses(a, [dict(b, id=2), {"id":3,"name":"Unrelated Shop"}], threshold=0.5)
|
||
|
|
self.assertEqual(first, second)
|
||
|
|
self.assertEqual(first[0]["id"], 2)
|
||
|
|
self.assertGreaterEqual(first[0]["confidence"], 0.5)
|
||
|
|
self.assertTrue(first[0]["reasons"])
|
||
|
|
self.assertEqual([x["id"] for x in match_businesses(a, [{"id":3,"name":"Unrelated Shop"}], threshold=0.8)], [])
|
||
|
|
|
||
|
|
if __name__ == "__main__": unittest.main()
|