2
2
3
3
4
4
import ipaddress
5
+ import re
5
6
import unittest
6
7
from unittest .mock import patch , MagicMock
7
8
9
+ import pytest
10
+
8
11
import geoip2 .database
9
12
import geoip2 .errors
10
13
import maxminddb
@@ -28,38 +31,34 @@ def test_language_list(self) -> None:
28
31
29
32
def test_unknown_address (self ) -> None :
30
33
reader = geoip2 .database .Reader ("tests/data/test-data/GeoIP2-City-Test.mmdb" )
31
- with self . assertRaisesRegex (
34
+ with pytest . raises (
32
35
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." ,
34
37
):
35
38
reader .city ("10.10.10.10" )
36
39
reader .close ()
37
40
38
41
def test_unknown_address_network (self ) -> None :
39
42
reader = geoip2 .database .Reader ("tests/data/test-data/GeoIP2-City-Test.mmdb" )
40
- try :
43
+ with pytest . raises ( geoip2 . errors . AddressNotFoundError ) as ei :
41
44
reader .city ("10.10.10.10" )
42
- self .fail ("Expected AddressNotFoundError" )
43
- except geoip2 .errors .AddressNotFoundError as e :
44
- assert e .network == ipaddress .ip_network ("10.0.0.0/8" )
45
- except Exception as e :
46
- self .fail (f"Expected AddressNotFoundError, got { type (e )} : { str (e )} " )
47
- finally :
48
- reader .close ()
45
+ assert ei .value .network == ipaddress .ip_network ("10.0.0.0/8" )
46
+ reader .close ()
49
47
50
48
def test_wrong_database (self ) -> None :
51
49
reader = geoip2 .database .Reader ("tests/data/test-data/GeoIP2-City-Test.mmdb" )
52
- with self . assertRaisesRegex (
50
+ with pytest . raises (
53
51
TypeError ,
54
- "The country method cannot be used with " " the GeoIP2-City database" ,
52
+ match = "The country method cannot be used with the GeoIP2-City database" ,
55
53
):
56
54
reader .country ("1.1.1.1" )
57
55
reader .close ()
58
56
59
57
def test_invalid_address (self ) -> None :
60
58
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"
59
+ with pytest .raises (
60
+ ValueError ,
61
+ match = "u?'invalid' does not appear to be an " "IPv4 or IPv6 address" ,
63
62
):
64
63
reader .city ("invalid" )
65
64
reader .close ()
@@ -111,11 +110,9 @@ def test_asn(self) -> None:
111
110
assert record .ip_address == ip_address
112
111
assert record .network == ipaddress .ip_network ("1.128.0.0/11" )
113
112
114
- self .assertRegex (
115
- str (record ),
116
- r"geoip2.models.ASN\(.*1\.128\.0\.0.*\)" ,
117
- "str representation is correct" ,
118
- )
113
+ assert re .search (
114
+ r"geoip2.models.ASN\(.*1\.128\.0\.0.*\)" , str (record )
115
+ ), "str representation is correct"
119
116
120
117
reader .close ()
121
118
@@ -149,13 +146,9 @@ def test_connection_type(self) -> None:
149
146
assert record .connection_type == "Cellular"
150
147
assert record .ip_address == ip_address
151
148
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
-
149
+ assert re .search (
150
+ r"ConnectionType\(\{.*Cellular.*\}\)" , str (record )
151
+ ), "ConnectionType str representation is reasonable"
159
152
reader .close ()
160
153
161
154
def test_country (self ) -> None :
@@ -183,12 +176,9 @@ def test_domain(self) -> None:
183
176
assert record .domain == "maxmind.com"
184
177
assert record .ip_address == ip_address
185
178
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
- )
179
+ assert re .search (
180
+ r"Domain\(\{.*maxmind.com.*\}\)" , str (record )
181
+ ), "Domain str representation is reasonable"
192
182
193
183
reader .close ()
194
184
@@ -231,13 +221,9 @@ def test_isp(self) -> None:
231
221
assert record .organization == "Telstra Internet"
232
222
assert record .ip_address == ip_address
233
223
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
-
224
+ assert re .search (
225
+ r"ISP\(\{.*Telstra.*\}\)" , str (record )
226
+ ), "ISP str representation is reasonable"
241
227
record = reader .isp ("149.101.100.0" )
242
228
243
229
assert record .mobile_country_code == "310"
0 commit comments