Skip to content

Commit ef0345a

Browse files
Tests Modification (#17)
* fix : tests modified * fix : test_functions.py updated * fix : test_ipv4.py updated * fix : tests updated * fix : mock added to tests * fix : unused import removed
1 parent e128c11 commit ef0345a

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

ipspot/functions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .params import IPSPOT_OVERVIEW, IPSPOT_REPO, IPSPOT_VERSION
1010

1111

12-
def ipspot_info() -> None: #pragma: no cover
12+
def ipspot_info() -> None: # pragma: no cover
1313
"""Print ipspot details."""
1414
tprint("IPSpot")
1515
tprint("V:" + IPSPOT_VERSION)
@@ -163,7 +163,7 @@ def filter_parameter(parameter: Any) -> Any:
163163

164164

165165
def display_ip_info(ipv4_api: IPv4API = IPv4API.AUTO, geo: bool=False,
166-
timeout: Union[float, Tuple[float, float]]=5) -> None: #pragma: no cover
166+
timeout: Union[float, Tuple[float, float]]=5) -> None: # pragma: no cover
167167
"""
168168
Print collected IP and location data.
169169
@@ -192,7 +192,7 @@ def display_ip_info(ipv4_api: IPv4API = IPv4API.AUTO, geo: bool=False,
192192
print(" Error: {public_result[error]}".format(public_result=public_result))
193193

194194

195-
def main() -> None: #pragma: no cover
195+
def main() -> None: # pragma: no cover
196196
"""CLI main function."""
197197
parser = argparse.ArgumentParser()
198198
parser.add_argument(

tests/test_functions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ def test_filter_parameter3():
1515
assert filter_parameter(" ") == "N/A"
1616

1717

18+
def test_filter_parameter4():
19+
assert filter_parameter("GB") == "GB"
20+
21+
1822

1923

2024

tests/test_ipv4.py

Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
from unittest import mock
23
from ipspot import get_private_ipv4
34
from ipspot import get_public_ipv4, IPv4API
45

@@ -7,39 +8,101 @@
78
DATA_ITEMS = {'country_code', 'latitude', 'longitude', 'api', 'country', 'timezone', 'organization', 'region', 'ip', 'city'}
89

910

10-
def test_private_ipv4():
11+
def test_private_ipv4_success():
1112
result = get_private_ipv4()
1213
assert result["status"]
1314
assert IPV4_REGEX.match(result["data"]["ip"])
1415

1516

16-
def test_public_ipv4_auto():
17+
def test_private_ipv4_error():
18+
with mock.patch("socket.gethostbyname", side_effect=Exception("Test error")):
19+
result = get_private_ipv4()
20+
assert not result["status"]
21+
assert result["error"] == "Test error"
22+
23+
24+
def test_public_ipv4_auto_success():
1725
result = get_public_ipv4(api=IPv4API.AUTO, geo=True)
1826
assert result["status"]
1927
assert IPV4_REGEX.match(result["data"]["ip"])
2028
assert set(result["data"].keys()) == DATA_ITEMS
2129

2230

23-
def test_public_ipv4_ipapi():
31+
def test_public_ipv4_auto_timeout_error():
32+
result = get_public_ipv4(api=IPv4API.AUTO, geo=True, timeout="5")
33+
assert not result["status"]
34+
35+
36+
def test_public_ipv4_auto_net_error():
37+
with mock.patch("requests.get", side_effect=Exception("No Internet")):
38+
result = get_public_ipv4(api=IPv4API.AUTO)
39+
assert not result["status"]
40+
assert result["error"] == "All attempts failed."
41+
42+
43+
def test_public_ipv4_ipapi_success():
2444
result = get_public_ipv4(api=IPv4API.IPAPI, geo=True)
2545
assert result["status"]
2646
assert IPV4_REGEX.match(result["data"]["ip"])
2747
assert set(result["data"].keys()) == DATA_ITEMS
2848
assert result["data"]["api"] == "ip-api.com"
2949

3050

31-
def test_public_ipv4_ipinfo():
51+
def test_public_ipv4_ipapi_timeout_error():
52+
result = get_public_ipv4(api=IPv4API.IPAPI, geo=True, timeout="5")
53+
assert not result["status"]
54+
55+
56+
def test_public_ipv4_ipapi_net_error():
57+
with mock.patch("requests.get", side_effect=Exception("No Internet")):
58+
result = get_public_ipv4(api=IPv4API.IPAPI)
59+
assert not result["status"]
60+
assert result["error"] == "No Internet"
61+
62+
63+
def test_public_ipv4_ipinfo_success():
3264
result = get_public_ipv4(api=IPv4API.IPINFO, geo=True)
3365
assert result["status"]
3466
assert IPV4_REGEX.match(result["data"]["ip"])
3567
assert set(result["data"].keys()) == DATA_ITEMS
3668
assert result["data"]["api"] == "ipinfo.io"
3769

3870

39-
def test_public_ipv4_ipsb():
71+
def test_public_ipv4_ipinfo_timeout_error():
72+
result = get_public_ipv4(api=IPv4API.IPINFO, geo=True, timeout="5")
73+
assert not result["status"]
74+
75+
76+
def test_public_ipv4_ipinfo_net_error():
77+
with mock.patch("requests.get", side_effect=Exception("No Internet")):
78+
result = get_public_ipv4(api=IPv4API.IPINFO)
79+
assert not result["status"]
80+
assert result["error"] == "No Internet"
81+
82+
83+
def test_public_ipv4_ipsb_success():
4084
result = get_public_ipv4(api=IPv4API.IPSB, geo=True)
4185
assert result["status"]
4286
assert IPV4_REGEX.match(result["data"]["ip"])
4387
assert set(result["data"].keys()) == DATA_ITEMS
4488
assert result["data"]["api"] == "ip.sb"
4589

90+
91+
def test_public_ipv4_ipsb_timeout_error():
92+
result = get_public_ipv4(api=IPv4API.IPSB, geo=True, timeout="5")
93+
assert not result["status"]
94+
95+
96+
97+
def test_public_ipv4_ipsb_net_error():
98+
with mock.patch("requests.get", side_effect=Exception("No Internet")):
99+
result = get_public_ipv4(api=IPv4API.IPSB)
100+
assert not result["status"]
101+
assert result["error"] == "No Internet"
102+
103+
104+
def test_public_ipv4_api_error():
105+
result = get_public_ipv4(api="api1", geo=True)
106+
assert not result["status"]
107+
assert result["error"] == "Unsupported API: api1"
108+

0 commit comments

Comments
 (0)