def geocode(self): geocoder = AddressBaseGeocoder(self.postcode) geocoder.centroid try: geocoder.get_code("lad") except MultipleCodesException as e: # re-raise as a more specific MultipleCouncilsException # because that is what the calling code expects to handle raise MultipleCouncilsException(str(e)) return geocoder
def test_get_code_by_uprn_valid(self): """ valid get_code() by UPRN queries """ with self.assertNumQueries(FuzzyInt(0, 4)): addressbase = AddressBaseGeocoder("CC1 1CC") self.assertEqual("B01000001", addressbase.get_code("lad", "00000008")) self.assertIsInstance(addressbase.get_point("00000008"), Point) self.assertEqual("B01000002", addressbase.get_code("lad", "00000009")) self.assertIsInstance(addressbase.get_point("00000009"), Point)
def test_strict_mode(self): """ We find records for the given postcode in the AddressBase table There are some corresponding records in the ONSUD for the UPRNs we found Note that in this case, the ONSUD table does not contain corresponding records for *all* of the UPRNs we found, and we are passing strict=True so we raise a StrictMatchException """ with self.assertNumQueries(FuzzyInt(0, 4)): addressbase = AddressBaseGeocoder("BB11BB") with self.assertRaises(StrictMatchException): addressbase.get_code("lad", strict=True)
def test_multiple_codes(self): """ We find records for the given postcode in the AddressBase table There are corresponding records in the ONSUD for the UPRNs we found The UPRNs described by this postcode map to more than one 'lad' but they all map to the same 'cty' """ with self.assertNumQueries(FuzzyInt(0, 5)): addressbase = AddressBaseGeocoder("CC1 1CC") with self.assertRaises(MultipleCodesException): result = addressbase.get_code("lad") self.assertEqual("A01000001", addressbase.get_code("cty")) self.assertIsInstance(addressbase.centroid, Point)
def test_get_code_by_uprn_no_onsud(self): """ '00000006' is a valid UPRN in AddressBase but not in ONSUD """ with self.assertNumQueries(FuzzyInt(0, 4)): addressbase = AddressBaseGeocoder("BB1 1BB") with self.assertRaises(get_onsud_model().DoesNotExist): result = addressbase.get_code("lad", "00000006") self.assertIsInstance(addressbase.get_point("00000006"), Point)
def test_get_code_by_uprn_invalid_uprn(self): """ 'foo' is not a valid UPRN in our DB """ with self.assertNumQueries(FuzzyInt(0, 4)): addressbase = AddressBaseGeocoder("CC1 1CC") with self.assertRaises(get_address_model().DoesNotExist): result = addressbase.get_code("lad", "foo") with self.assertRaises(get_address_model().DoesNotExist): result = addressbase.get_point("foo")
def test_no_codes(self): """ We find records for the given postcode in the AddressBase table but there are no corresponding records in the ONSUD for the UPRNs we found """ with self.assertNumQueries(FuzzyInt(0, 5)): addressbase = AddressBaseGeocoder("AA11AA") with self.assertRaises(CodesNotFoundException): result = addressbase.get_code("lad") self.assertIsInstance(addressbase.centroid, Point)
def test_get_code_by_uprn_invalid_uprn_for_postcode(self): """ '00000001' is a valid UPRN in our DB, but for a different postcode than the one we constructed with """ with self.assertNumQueries(FuzzyInt(0, 4)): addressbase = AddressBaseGeocoder("CC1 1CC") with self.assertRaises(get_address_model().DoesNotExist): result = addressbase.get_code("lad", "00000001") with self.assertRaises(get_address_model().DoesNotExist): result = addressbase.get_point("00000001")
def test_valid(self): """ We find records for the given postcode in the AddressBase table There are some corresponding records in the ONSUD for the UPRNs we found Valid result should be returned Note that in this case, the ONSUD table does not contain corresponding records for *all* of the UPRNs we found, but we accept the result anyway """ with self.assertNumQueries(FuzzyInt(0, 5)): addressbase = AddressBaseGeocoder( "bb 1 1B B") # intentionally spurious whitespace and case self.assertEqual("B01000001", addressbase.get_code("lad")) self.assertIsInstance(addressbase.centroid, Point)
def test_invalid_code_type(self): with self.assertNumQueries(FuzzyInt(0, 4)): addressbase = AddressBaseGeocoder("CC1 1CC") with self.assertRaises(FieldDoesNotExist): result = addressbase.get_code("foo") # not a real code type