Skip to content

Commit

Permalink
Fix coordinate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janbaykara committed Feb 11, 2025
1 parent 889cea4 commit 0896067
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
19 changes: 10 additions & 9 deletions hub/data_imports/geocoding_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,17 @@ async def geocode_record(
update_data["geocode_data"] = generic_data.geocode_data or {}
update_data["geocode_data"]["skipped"] = True
# Return a complete GeocodeResult to avoid clearing previous data
return GeocodeResult(
kwargs = {
**update_data,
data_type=data_type,
data=id,
geocoder=generic_data.geocoder,
geocode_data=update_data["geocode_data"],
postcode_data=generic_data.postcode_data,
area=generic_data.area,
point=generic_data.point,
)
"data_type": data_type,
"data": id,
"geocoder": generic_data.geocoder,
"geocode_data": update_data["geocode_data"],
"postcode_data": generic_data.postcode_data,
"area": generic_data.area,
"point": generic_data.point,
}
return GeocodeResult(**kwargs)
except GenericData.DoesNotExist:
# logger.debug("Generic Data doesn't exist, no equality check to be done", id)
pass
Expand Down
25 changes: 16 additions & 9 deletions hub/tests/test_external_data_source_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,8 @@ def test_skipping(self):
class TestCoordinateGeocoding(TestCase):
@classmethod
def setUpTestData(cls):
subprocess.call("bin/import_areas_into_test_db.sh")

cls.source = DatabaseJSONSource.objects.create(
name="coordinates_test",
id_field="id",
Expand All @@ -500,41 +502,41 @@ def setUpTestData(cls):
"id": "1",
"longitude": -1.342881,
"latitude": 51.846073,
"expected_postcode": "OX20 1ND",
"expected_constituency": "E14001090",
},
{
# Should work with strings too
"id": "2",
"longitude": "-1.702695",
"latitude": "52.447681",
"expected_postcode": "B92 0HJ",
"expected_constituency": "E14001358",
},
{
"id": "3",
"longitude": " -1.301473",
"latitude": 53.362753,
"expected_postcode": "S26 2GA",
"expected_constituency": "E14001451",
},
{
# Handle failure cases gracefully
"id": "4",
"longitude": -4.2858,
"latitude": None,
"expected_postcode": None,
"expected_constituency": None,
},
# Gracefully handle non-numeric coordinates
{
"id": "5",
"longitude": "invalid",
"latitude": "invalid",
"expected_postcode": None,
"expected_constituency": None,
},
# Gracefully handle crazy big coordinates
{
"id": "6",
"longitude": 0,
"latitude": 1000,
"expected_postcode": None,
"expected_constituency": None,
},
],
# Resulting address query should be something like "Barclays, Victoria Road, Glasgow"
Expand Down Expand Up @@ -563,12 +565,15 @@ def test_geocoding_matches(self):
for d in self.data:
try:
try:
if d.json["expected_postcode"] is not None:
if d.json["expected_constituency"] is not None:
self.assertIsNotNone(d.postcode_data)
self.assertEqual(
d.postcode_data["postcode"], d.json["expected_postcode"]
d.postcode_data["codes"]["parliamentary_constituency"],
d.json["expected_constituency"],
)
self.assertGreaterEqual(len(d.geocode_data["steps"]), 1)
else:
self.assertIsNone(d.postcode_data)
except KeyError:
raise AssertionError("Expected geocoding data was missing.")
except AssertionError as e:
Expand Down Expand Up @@ -600,11 +605,13 @@ def test_skipping(self):
for d in self.data:
try:
try:
if d.json["expected_postcode"] is not None:
if d.json["expected_constituency"] is not None:
self.assertIsNotNone(d.postcode_data)
self.assertTrue(
d.geocode_data["skipped"], "Geocoding should be skipped."
)
else:
self.assertIsNone(d.postcode_data)
except KeyError:
raise AssertionError("Expected geocoding data was missing.")
except AssertionError as e:
Expand Down

0 comments on commit 0896067

Please sign in to comment.