-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
33 lines (25 loc) · 1.25 KB
/
test_api.py
File metadata and controls
33 lines (25 loc) · 1.25 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
__author__ = 'HSL'
import unittest
import urllib.request
import json
class TestApi(unittest.TestCase):
def test_api(self):
test_url = 'https://api.tmsandbox.co.nz/v1/Categories/6327/Details.json?catalogue=false'
json_result = self.get_response_json(test_url)
#------------------------criteria 1--------------------------
self.assertIn('Name', json_result)
self.assertEqual(json_result['Name'], 'Carbon credits', 'Criteria 1 failed')
#------------------------criteria 2--------------------------
self.assertIn('CanRelist', json_result)
self.assertEqual(json_result['CanRelist'],True,'Criteria 2 failed')
#------------------------criteria 3--------------------------
for each_element in json_result['Promotions']:
if 'Name' in each_element and each_element['Name'] == 'Gallery':
self.assertIn('Description', each_element, 'Criteria 3 do not have a Description')
self.assertIn('2x larger image', each_element['Description'], 'Criteria 3 failed')
@staticmethod
def get_response_json(get_url):
res = urllib.request.urlopen(get_url).read()
return json.loads(res.decode('utf-8'))
if __name__ == '__main__':
unittest.main()