Refs #35841 -- Adjusted GeoIP2 tests for easier test case extension.

These changes will make it easier to introduce tests for alternate
databases that may have different results without the need to duplicate
lots of the tests definition.
This commit is contained in:
Nick Pope 2024-10-15 09:58:19 +01:00 committed by nessita
parent c37f249ffa
commit 5873f10177
1 changed files with 49 additions and 55 deletions

View File

@ -32,6 +32,33 @@ class GeoLite2Test(SimpleTestCase):
ipv6_addr = ipaddress.ip_address(ipv6_str)
query_values = (fqdn, ipv4_str, ipv6_str, ipv4_addr, ipv6_addr)
expected_city = {
"accuracy_radius": 100,
"city": "Boxford",
"continent_code": "EU",
"continent_name": "Europe",
"country_code": "GB",
"country_name": "United Kingdom",
"is_in_european_union": False,
"latitude": 51.75,
"longitude": -1.25,
"metro_code": None,
"postal_code": "OX1",
"region_code": "ENG",
"region_name": "England",
"time_zone": "Europe/London",
# Kept for backward compatibility.
"dma_code": None,
"region": "ENG",
}
expected_country = {
"continent_code": "EU",
"continent_name": "Europe",
"country_code": "GB",
"country_name": "United Kingdom",
"is_in_european_union": False,
}
@classmethod
def setUpClass(cls):
# Avoid referencing __file__ at module level.
@ -100,85 +127,52 @@ class GeoLite2Test(SimpleTestCase):
self.assertIs(g._metadata.database_type.endswith("Country"), True)
for query in self.query_values:
with self.subTest(query=query):
self.assertEqual(g.country(query), self.expected_country)
self.assertEqual(
g.country(query),
{
"continent_code": "EU",
"continent_name": "Europe",
"country_code": "GB",
"country_name": "United Kingdom",
"is_in_european_union": False,
},
g.country_code(query), self.expected_country["country_code"]
)
self.assertEqual(
g.country_name(query), self.expected_country["country_name"]
)
self.assertEqual(g.country_code(query), "GB")
self.assertEqual(g.country_name(query), "United Kingdom")
def test_country_using_city_database(self):
g = GeoIP2(country="<invalid>")
self.assertIs(g._metadata.database_type.endswith("City"), True)
for query in self.query_values:
with self.subTest(query=query):
self.assertEqual(g.country(query), self.expected_country)
self.assertEqual(
g.country(query),
{
"continent_code": "EU",
"continent_name": "Europe",
"country_code": "GB",
"country_name": "United Kingdom",
"is_in_european_union": False,
},
g.country_code(query), self.expected_country["country_code"]
)
self.assertEqual(
g.country_name(query), self.expected_country["country_name"]
)
self.assertEqual(g.country_code(query), "GB")
self.assertEqual(g.country_name(query), "United Kingdom")
def test_city(self):
g = GeoIP2(country="<invalid>")
self.assertIs(g._metadata.database_type.endswith("City"), True)
for query in self.query_values:
with self.subTest(query=query):
self.assertEqual(
g.city(query),
{
"accuracy_radius": 100,
"city": "Boxford",
"continent_code": "EU",
"continent_name": "Europe",
"country_code": "GB",
"country_name": "United Kingdom",
"is_in_european_union": False,
"latitude": 51.75,
"longitude": -1.25,
"metro_code": None,
"postal_code": "OX1",
"region_code": "ENG",
"region_name": "England",
"time_zone": "Europe/London",
# Kept for backward compatibility.
"dma_code": None,
"region": "ENG",
},
)
self.assertEqual(g.city(query), self.expected_city)
geom = g.geos(query)
self.assertIsInstance(geom, GEOSGeometry)
self.assertEqual(geom.srid, 4326)
self.assertEqual(geom.tuple, (-1.25, 51.75))
self.assertEqual(g.lat_lon(query), (51.75, -1.25))
self.assertEqual(g.lon_lat(query), (-1.25, 51.75))
expected_lat = self.expected_city["latitude"]
expected_lon = self.expected_city["longitude"]
self.assertEqual(geom.tuple, (expected_lon, expected_lat))
self.assertEqual(g.lat_lon(query), (expected_lat, expected_lon))
self.assertEqual(g.lon_lat(query), (expected_lon, expected_lat))
# Country queries should still work.
self.assertEqual(g.country(query), self.expected_country)
self.assertEqual(
g.country(query),
{
"continent_code": "EU",
"continent_name": "Europe",
"country_code": "GB",
"country_name": "United Kingdom",
"is_in_european_union": False,
},
g.country_code(query), self.expected_country["country_code"]
)
self.assertEqual(
g.country_name(query), self.expected_country["country_name"]
)
self.assertEqual(g.country_code(query), "GB")
self.assertEqual(g.country_name(query), "United Kingdom")
def test_not_found(self):
g1 = GeoIP2(city="<invalid>")