-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_validator.py
58 lines (39 loc) · 2.08 KB
/
test_validator.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from validator import get_ids
import unittest
import json
from unittest.mock import patch, Mock
from validator import get_json_listings, get_ids_from_json
class BasicTests(unittest.TestCase):
def test_get_ids(self):
mock_get_patcher = patch('validator.requests.get')
with open('listing_validator\json_listings.txt', 'r') as file:
listings = json.loads(file.read())
mock_get = mock_get_patcher.start()
mock_get.return_value = Mock(status_code = 200)
mock_get.return_value.json.return_value = listings
ids = get_ids("http://jsonplaceholder.listingtest.com/listings")
mock_get_patcher.stop()
self.assertEqual(ids, ["01FA61T8NJXR9SFEW53FBS7XK2", "22FA61T8NJXR9SFEW53FBS7XK2"])
def test_get_ids_from_json(self):
with open('listing_validator\json_listings.txt', 'r') as file:
listings = json.loads(file.read())
self.assertEqual(get_ids_from_json(listings), ["01FA61T8NJXR9SFEW53FBS7XK2", "22FA61T8NJXR9SFEW53FBS7XK2"])
def test_get_json_listings(self):
mock_get_patcher = patch('validator.requests.get')
with open('listing_validator\json_listings.txt', 'r') as file:
listings = json.loads(file.read())
# Start patching 'requests.get'.
mock_get = mock_get_patcher.start()
# Configure the mock to return a response with status code 200 and a list of listings.
mock_get.return_value = Mock(status_code = 200)
mock_get.return_value.json.return_value = listings
# Call the service, which will send a request to the server.
response = get_json_listings("http://jsonplaceholder.listingtest.com/listings")
# Stop patching 'requests'.
mock_get_patcher.stop()
# Assert that the request-response cycle completed successfully.
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json()[0]['id'], "01FA61T8NJXR9SFEW53FBS7XK2")
self.assertEqual(response.json()[1]['id'], "22FA61T8NJXR9SFEW53FBS7XK2")
if __name__ == "__main__":
unittest.main()