8000 Modernize tests by akx · Pull Request #180 · maxmind/GeoIP2-python · GitHub
[go: up one dir, main page]

Skip to content

Modernize tests #180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
Rewrite remaining unittest assertions
  • Loading branch information
akx committed Aug 28, 2024
commit fc4049e218246e541998c7ef0e84d771f0a4f7b0
64 changes: 25 additions & 39 deletions tests/database_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@


import ipaddress
import re
import unittest
from unittest.mock import patch, MagicMock

import pytest

import geoip2.database
import geoip2.errors
import maxminddb
Expand All @@ -28,38 +31,34 @@ def test_language_list(self) -> None:

def test_unknown_address(self) -> None:
reader = geoip2.database.Reader("tests/data/test-data/GeoIP2-City-Test.mmdb")
with self.assertRaisesRegex(
with pytest.raises(
geoip2.errors.AddressNotFoundError,
"The address 10.10.10.10 is not in the " "database.",
match="The address 10.10.10.10 is not in the database.",
):
reader.city("10.10.10.10")
reader.close()

def test_unknown_address_network(self) -> None:
reader = geoip2.database.Reader("tests/data/test-data/GeoIP2-City-Test.mmdb")
try:
with pytest.raises(geoip2.errors.AddressNotFoundError) as ei:
reader.city("10.10.10.10")
self.fail("Expected AddressNotFoundError")
except geoip2.errors.AddressNotFoundError as e:
assert e.network == ipaddress.ip_network("10.0.0.0/8")
except Exception as e:
self.fail(f"Expected AddressNotFoundError, got {type(e)}: {str(e)}")
finally:
reader.close()
assert ei.value.network == ipaddress.ip_network("10.0.0.0/8")
reader.close()

def test_wrong_database(self) -> None:
reader = geoip2.database.Reader("tests/data/test-data/GeoIP2-City-Test.mmdb")
with self.assertRaisesRegex(
with pytest.raises(
TypeError,
"The country method cannot be used with " "the GeoIP2-City database",
match="The country method cannot be used with the GeoIP2-City database",
):
reader.country("1.1.1.1")
reader.close()

def test_invalid_address(self) -> None:
reader = geoip2.database.Reader("tests/data/test-data/GeoIP2-City-Test.mmdb")
with self.assertRaisesRegex(
ValueError, "u?'invalid' does not appear to be an " "IPv4 or IPv6 address"
with pytest.raises(
ValueError,
match="u?'invalid' does not appear to be an " "IPv4 or IPv6 address",
):
reader.city("invalid")
reader.close()
Expand Down Expand Up @@ -111,11 +110,9 @@ def test_asn(self) -> None:
assert record.ip_address == ip_address
assert record.network == ipaddress.ip_network("1.128.0.0/11")

self.assertRegex(
str(record),
r"geoip2.models.ASN\(.*1\.128\.0\.0.*\)",
"str representation is correct",
)
assert re.search(
r"geoip2.models.ASN\(.*1\.128\.0\.0.*\)", str(record)
), "str representation is correct"

reader.close()

Expand Down Expand Up @@ -149,13 +146,9 @@ def test_connection_type(self) -> None:
assert record.connection_type == "Cellular"
assert record.ip_address == ip_address
assert record.network == ipaddress.ip_network("1.0.1.0/24")

self.assertRegex(
str(record),
r"ConnectionType\(\{.*Cellular.*\}\)",
"ConnectionType str representation is reasonable",
)

assert re.search(
r"ConnectionType\(\{.*Cellular.*\}\)", str(record)
), "ConnectionType str representation is reasonable"
reader.close()

def test_country(self) -> None:
Expand Down Expand Up @@ -183,12 +176,9 @@ def test_domain(self) -> None:
assert record.domain == "maxmind.com"
assert record.ip_address == ip_address
assert record.network == ipaddress.ip_network("1.2.0.0/16")

self.assertRegex(
str(record),
r"Domain\(\{.*maxmind.com.*\}\)",
"Domain str representation is reasonable",
)
assert re.search(
r"Domain\(\{.*maxmind.com.*\}\)", str(record)
), "Domain str representation is reasonable"

reader.close()

Expand Down Expand Up @@ -231,13 +221,9 @@ def test_isp(self) -> None:
assert record.organization == "Telstra Internet"
assert record.ip_address == ip_address
assert record.network == ipaddress.ip_network("1.128.0.0/11")

self.assertRegex(
str(record),
r"ISP\(\{.*Telstra.*\}\)",
"ISP str representation is reasonable",
)

assert re.search(
r"ISP\(\{.*Telstra.*\}\)", str(record)
), "ISP str representation is reasonable"
record = reader.isp("149.101.100.0")

assert record.mobile_country_code == "310"
Expand Down
29 changes: 11 additions & 18 deletions tests/models_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import pytest
from typing import Dict
import unittest
Expand Down Expand Up @@ -134,20 +136,13 @@ def test_insights_full(self) -> None:
assert model.location.longitude == 93.2636, "correct longitude"
assert model.location.metro_code == 765, "correct metro_code"
assert model.location.population_density == 1341, "correct population_density"

self.assertRegex(
str(model),
r"^geoip2.models.Insights\(\{.*geoname_id.*\}, \[.*en.*\]\)",
"Insights str representation looks reasonable",
)

assert re.search(
r"^geoip2.models.Insights\(\{.*geoname_id.*\}, \[.*en.*\]\)", str(model)
), "Insights str representation looks reasonable"
assert model == eval(repr(model)), "Insights repr can be eval'd"

self.assertRegex(
str(model.location),
r"^geoip2.records.Location\(.*longitude=.*\)",
"Location str representation is reasonable",
)
assert re.search(
r"^geoip2.records.Location\(.*longitude=.*\)", str(model.location)
), "Location str representation is reasonable"

assert model.location == eval(
repr(model.location)
Expand Down Expand Up @@ -268,11 +263,9 @@ def test_city_full(self) -> None:
model.traits.is_satellite_provider is True
), "traits is_setellite_provider is True"
assert model.raw == raw, "raw method produces raw output"

self.assertRegex(
str(model), r"^geoip2.models.City\(\{.*geoname_id.*\}, \[.*en.*\]\)"
)

assert re.search(
r"^geoip2.models.City\(\{.*geoname_id.*\}, \[.*en.*\]\)", str(model)
), "City str representation looks reasonable"
assert not (model == True), "__eq__ does not blow up on weird input"

def test_unknown_keys(self) -> None:
Expand Down
31 changes: 14 additions & 17 deletions tests/webservice_test.py
A3E2
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,12 @@ def test_200_error(self):
content_type=self._content_type("country"),
)

with self.assertRaisesRegex(
GeoIP2Error, "could not decode the response as JSON"
):
with pytest.raises(GeoIP2Error, match="could not decode the response as JSON"):
self.run_client(self.client.country("1.1.1.1"))

def test_bad_ip_address(self):
with self.assertRaisesRegex(
ValueError, "'1.2.3' does not appear to be an IPv4 " "or IPv6 address"
with pytest.raises(
ValueError, match="'1.2.3' does not appear to be an IPv4 or IPv6 address"
):
self.run_client(self.client.country("1.2.3"))

Expand All @@ -142,9 +140,7 @@ def test_no_body_error(self):
status=400,
content_type=self._content_type("country"),
)
with self.assertRaisesRegex(
HTTPError, "Received a 400 error for .* with no body"
):
with pytest.raises(HTTPError, match="Received a 400 error for .* with no body"):
self.run_client(self.client.country("1.2.3.7"))

def test_weird_body_error(self):
Expand All @@ -157,9 +153,10 @@ def test_weird_body_error(self):
content_type=self._content_type("country"),
)

with self.assertRaisesRegex(
with pytest.raises(
HTTPError,
"Response contains JSON but it does not " "specify code or error keys",
match="Response contains JSON but it does not "
"specify code or error keys",
):
self.run_client(self.client.country("1.2.3.8"))

Expand All @@ -172,8 +169,8 @@ def test_bad_body_error(self):
status=400,
content_type=self._content_type("country"),
)
with self.assertRaisesRegex(
HTTPError, "it did not include the expected JSON body"
with pytest.raises(
HTTPError, match="it did not include the expected JSON body"
):
self.run_client(self.client.country("1.2.3.9"))

Expand All @@ -185,7 +182,7 @@ def test_500_error(self):
status=500,
content_type=self._content_type("country"),
)
with self.assertRaisesRegex(HTTPError, r"Received a server error \(500\) for"):
with pytest.raises(HTTPError, match=r"Received a server error \(500\) for"):
self.run_client(self.client.country("1.2.3.10"))

def test_300_error(self):
Expand All @@ -197,8 +194,8 @@ def test_300_error(self):
status=300,
content_type=self._content_type("country"),
)
with self.assertRaisesRegex(
HTTPError, r"Received a very surprising HTTP status \(300\) for"
with pytest.raises(
HTTPError, match=r"Received a very surprising HTTP status \(300\) for"
):
self.run_client(self.client.country("1.2.3.11"))

Expand Down Expand Up @@ -245,7 +242,7 @@ def _test_error(self, status, error_code, error_class):
status=status,
content_type=self._content_type("country"),
)
with self.assertRaisesRegex(error_class, msg):
with pytest.raises(error_class, match=msg):
self.run_client(self.client.country("1.2.3.18"))

def test_unknown_error(self):
Expand All @@ -259,7 +256,7 @@ def test_unknown_error(self):
status=400,
content_type=self._content_type("country"),
)
with self.assertRaisesRegex(InvalidRequestError, msg):
with pytest.raises(InvalidRequestError, match=msg):
self.run_client(self.client.country(ip))

def test_request(self):
Expand Down
0