-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api_client.py
More file actions
44 lines (37 loc) · 1.53 KB
/
Copy pathtest_api_client.py
File metadata and controls
44 lines (37 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import unittest, requests
import unittest.mock
from src.api_client import get_location
from unittest.mock import patch
class ApiClientTests(unittest.TestCase):
@patch("src.api_client.requests.get")
def test_get_location_returns_expected_data(self, mock_get):
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {
"countryName": "USA",
"regionName": "FLORIDA",
"cityName": "MIAMI",
}
result = get_location("8.8.8.8")
self.assertEqual(result.get("country"), "USA")
self.assertEqual(result.get("region"), "FLORIDA")
self.assertEqual(result.get("city"), "MIAMI")
mock_get.assert_called_once_with("https://freeipapi.com/api/json/8.8.8.8")
@patch("src.api_client.requests.get")
def test_get_location_returns_side_effect(self, mock_get):
mock_get.side_effect = [
requests.exceptions.RequestException("Service Unavailable"),
unittest.mock.Mock(
status_code=200,
json=lambda: {
"countryName": "USA",
"regionName": "FLORIDA",
"cityName": "MIAMI",
},
),
]
with self.assertRaises(requests.exceptions.RequestException):
get_location("8.8.8.8")
result = get_location("8.8.8.8")
self.assertEqual(result.get("country"), "USA")
self.assertEqual(result.get("region"), "FLORIDA")
self.assertEqual(result.get("city"), "MIAMI")