Skip to content

Commit 9a89c11

Browse files
committed
Rewrite remaining unittest assertions
1 parent fabcc1d commit 9a89c11

File tree

3 files changed

+47
-66
lines changed

3 files changed

+47
-66
lines changed

tests/database_test.py

+22-31
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33

44
import ipaddress
5+
import re
56
import unittest
67
from unittest.mock import patch, MagicMock
78

9+
import pytest
10+
811
import geoip2.database
912
import geoip2.errors
1013
import maxminddb
@@ -28,9 +31,9 @@ def test_language_list(self) -> None:
2831

2932
def test_unknown_address(self) -> None:
3033
reader = geoip2.database.Reader("tests/data/test-data/GeoIP2-City-Test.mmdb")
31-
with self.assertRaisesRegex(
34+
with pytest.raises(
3235
geoip2.errors.AddressNotFoundError,
33-
"The address 10.10.10.10 is not in the " "database.",
36+
match="The address 10.10.10.10 is not in the database.",
3437
):
3538
reader.city("10.10.10.10")
3639
reader.close()
@@ -49,17 +52,18 @@ def test_unknown_address_network(self) -> None:
4952

5053
def test_wrong_database(self) -> None:
5154
reader = geoip2.database.Reader("tests/data/test-data/GeoIP2-City-Test.mmdb")
52-
with self.assertRaisesRegex(
55+
with pytest.raises(
5356
TypeError,
54-
"The country method cannot be used with " "the GeoIP2-City database",
57+
match="The country method cannot be used with the GeoIP2-City database",
5558
):
5659
reader.country("1.1.1.1")
5760
reader.close()
5861

5962
def test_invalid_address(self) -> None:
6063
reader = geoip2.database.Reader("tests/data/test-data/GeoIP2-City-Test.mmdb")
61-
with self.assertRaisesRegex(
62-
ValueError, "u?'invalid' does not appear to be an " "IPv4 or IPv6 address"
64+
with pytest.raises(
65+
ValueError,
66+
match="u?'invalid' does not appear to be an " "IPv4 or IPv6 address",
6367
):
6468
reader.city("invalid")
6569
reader.close()
@@ -111,11 +115,9 @@ def test_asn(self) -> None:
111115
assert record.ip_address == ip_address
112116
assert record.network == ipaddress.ip_network("1.128.0.0/11")
113117

114-
self.assertRegex(
115-
str(record),
116-
r"geoip2.models.ASN\(.*1\.128\.0\.0.*\)",
117-
"str representation is correct",
118-
)
118+
assert re.search(
119+
r"geoip2.models.ASN\(.*1\.128\.0\.0.*\)", str(record)
120+
), "str representation is correct"
119121

120122
reader.close()
121123

@@ -149,13 +151,9 @@ def test_connection_type(self) -> None:
149151
assert record.connection_type == "Cellular"
150152
assert record.ip_address == ip_address
151153
assert record.network == ipaddress.ip_network("1.0.1.0/24")
152-
153-
self.assertRegex(
154-
str(record),
155-
r"ConnectionType\(\{.*Cellular.*\}\)",
156-
"ConnectionType str representation is reasonable",
157-
)
158-
154+
assert re.search(
155+
r"ConnectionType\(\{.*Cellular.*\}\)", str(record)
156+
), "ConnectionType str representation is reasonable"
159157
reader.close()
160158

161159
def test_country(self) -> None:
@@ -183,12 +181,9 @@ def test_domain(self) -> None:
183181
assert record.domain == "maxmind.com"
184182
assert record.ip_address == ip_address
185183
assert record.network == ipaddress.ip_network("1.2.0.0/16")
186-
187-
self.assertRegex(
188-
str(record),
189-
r"Domain\(\{.*maxmind.com.*\}\)",
190-
"Domain str representation is reasonable",
191-
)
184+
assert re.search(
185+
r"Domain\(\{.*maxmind.com.*\}\)", str(record)
186+
), "Domain str representation is reasonable"
192187

193188
reader.close()
194189

@@ -231,13 +226,9 @@ def test_isp(self) -> None:
231226
assert record.organization == "Telstra Internet"
232227
assert record.ip_address == ip_address
233228
assert record.network == ipaddress.ip_network("1.128.0.0/11")
234-
235-
self.assertRegex(
236-
str(record),
237-
r"ISP\(\{.*Telstra.*\}\)",
238-
"ISP str representation is reasonable",
239-
)
240-
229+
assert re.search(
230+
r"ISP\(\{.*Telstra.*\}\)", str(record)
231+
), "ISP str representation is reasonable"
241232
record = reader.isp("149.101.100.0")
242233

243234
assert record.mobile_country_code == "310"

tests/models_test.py

+11-18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import re
2+
13
import pytest
24
from typing import Dict
35
import unittest
@@ -134,20 +136,13 @@ def test_insights_full(self) -> None:
134136
assert model.location.longitude == 93.2636, "correct longitude"
135137
assert model.location.metro_code == 765, "correct metro_code"
136138
assert model.location.population_density == 1341, "correct population_density"
137-
138-
self.assertRegex(
139-
str(model),
140-
r"^geoip2.models.Insights\(\{.*geoname_id.*\}, \[.*en.*\]\)",
141-
"Insights str representation looks reasonable",
142-
)
143-
139+
assert re.search(
140+
r"^geoip2.models.Insights\(\{.*geoname_id.*\}, \[.*en.*\]\)", str(model)
141+
), "Insights str representation looks reasonable"
144142
assert model == eval(repr(model)), "Insights repr can be eval'd"
145-
146-
self.assertRegex(
147-
str(model.location),
148-
r"^geoip2.records.Location\(.*longitude=.*\)",
149-
"Location str representation is reasonable",
150-
)
143+
assert re.search(
144+
r"^geoip2.records.Location\(.*longitude=.*\)", str(model.location)
145+
), "Location str representation is reasonable"
151146

152147
assert model.location == eval(
153148
repr(model.location)
@@ -268,11 +263,9 @@ def test_city_full(self) -> None:
268263
model.traits.is_satellite_provider is True
269264
), "traits is_setellite_provider is True"
270265
assert model.raw == raw, "raw method produces raw output"
271-
272-
self.assertRegex(
273-
str(model), r"^geoip2.models.City\(\{.*geoname_id.*\}, \[.*en.*\]\)"
274-
)
275-
266+
assert re.search(
267+
r"^geoip2.models.City\(\{.*geoname_id.*\}, \[.*en.*\]\)", str(model)
268+
), "City str representation looks reasonable"
276269
assert not (model == True), "__eq__ does not blow up on weird input"
277270

278271
def test_unknown_keys(self) -> None:

tests/webservice_test.py

+14-17
Original file line numberDiff line numberDiff line change
@@ -123,14 +123,12 @@ def test_200_error(self):
123123
content_type=self._content_type("country"),
124124
)
125125

126-
with self.assertRaisesRegex(
127-
GeoIP2Error, "could not decode the response as JSON"
128-
):
126+
with pytest.raises(GeoIP2Error, match="could not decode the response as JSON"):
129127
self.run_client(self.client.country("1.1.1.1"))
130128

131129
def test_bad_ip_address(self):
132-
with self.assertRaisesRegex(
133-
ValueError, "'1.2.3' does not appear to be an IPv4 " "or IPv6 address"
130+
with pytest.raises(
131+
ValueError, match="'1.2.3' does not appear to be an IPv4 or IPv6 address"
134132
):
135133
self.run_client(self.client.country("1.2.3"))
136134

@@ -142,9 +140,7 @@ def test_no_body_error(self):
142140
status=400,
143141
content_type=self._content_type("country"),
144142
)
145-
with self.assertRaisesRegex(
146-
HTTPError, "Received a 400 error for .* with no body"
147-
):
143+
with pytest.raises(HTTPError, match="Received a 400 error for .* with no body"):
148144
self.run_client(self.client.country("1.2.3.7"))
149145

150146
def test_weird_body_error(self):
@@ -157,9 +153,10 @@ def test_weird_body_error(self):
157153
content_type=self._content_type("country"),
158154
)
159155

160-
with self.assertRaisesRegex(
156+
with pytest.raises(
161157
HTTPError,
162-
"Response contains JSON but it does not " "specify code or error keys",
158+
match="Response contains JSON but it does not "
159+
"specify code or error keys",
163160
):
164161
self.run_client(self.client.country("1.2.3.8"))
165162

@@ -172,8 +169,8 @@ def test_bad_body_error(self):
172169
status=400,
173170
content_type=self._content_type("country"),
174171
)
175-
with self.assertRaisesRegex(
176-
HTTPError, "it did not include the expected JSON body"
172+
with pytest.raises(
173+
HTTPError, match="it did not include the expected JSON body"
177174
):
178175
self.run_client(self.client.country("1.2.3.9"))
179176

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

191188
def test_300_error(self):
@@ -197,8 +194,8 @@ def test_300_error(self):
197194
status=300,
198195
content_type=self._content_type("country"),
199196
)
200-
with self.assertRaisesRegex(
201-
HTTPError, r"Received a very surprising HTTP status \(300\) for"
197+
with pytest.raises(
198+
HTTPError, match=r"Received a very surprising HTTP status \(300\) for"
202199
):
203200
self.run_client(self.client.country("1.2.3.11"))
204201

@@ -245,7 +242,7 @@ def _test_error(self, status, error_code, error_class):
245242
status=status,
246243
content_type=self._content_type("country"),
247244
)
248-
with self.assertRaisesRegex(error_class, msg):
245+
with pytest.raises(error_class, match=msg):
249246
self.run_client(self.client.country("1.2.3.18"))
250247

251248
def test_unknown_error(self):
@@ -259,7 +256,7 @@ def test_unknown_error(self):
259256
status=400,
260257
content_type=self._content_type("country"),
261258
)
262-
with self.assertRaisesRegex(InvalidRequestError, msg):
259+
with pytest.raises(InvalidRequestError, match=msg):
263260
self.run_client(self.client.country(ip))
264261

265262
def test_request(self):

0 commit comments

Comments
 (0)