-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
66 lines (57 loc) · 2.61 KB
/
test.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
59
60
61
62
63
64
65
66
import json, urllib, unittest
#global variable for API address
api_address = 'http://54.186.126.107:5000'
class TestCartAPI(unittest.TestCase):
def setup(self):
pass
def test01ValidCallByLocation(self):
"""
test valid call to the API using a proper latitude,longitude variable
"""
url = api_address + '/location/37.7823601210175,-122.402095994563'
try:
results = json.load(urllib.urlopen(url))['data']
self.assertTrue('applicant' in results[0] and 'fooditems' in results[0] and 'latitude' in results[0])
except ValueError:
self.fail(msg='JSON response not received from API')
except Exception as e:
self.fail(msg='Unexpected error received: %s' % str(e.message))
def test02InvalidCallByLocation(self):
"""
test invalid call to the API - attempt passing non-numeric value as longitude
"""
url = api_address + '/location/7823601210175,Tex-mex'
try:
results = json.load(urllib.urlopen(url))
self.fail(msg="Result should not have been JSON (HTTP 500 expected)")
except ValueError as e:
#we want a value error, JSON should not be returned
pass
except Exception as e:
self.fail(msg='Unexpected error received: '+e.message)
def test03ValidCallWithCategory(self):
"""
test valid call to the API using latitude,longitude variable and specify filter category
"""
url = api_address + '/location/37.7823601210175,-122.402095994563/Beverages'
try:
results = json.load(urllib.urlopen(url))['data']
self.assertTrue('applicant' in results[0] and 'fooditems' in results[0] and 'latitude' in results[0])
except ValueError:
self.fail(msg='JSON response not received from API')
except Exception as e:
self.fail(msg='Unexpected error received: %s' % e.message)
def test04InvalidCallWithCategory(self):
"""
test invalid call to the API using proper latitue,longitude variable and use an invalid filter category
"""
url = api_address + '/location/37.7823601210175,-122.402095994563/Eats'
try:
results = json.load(urllib.urlopen(url))['data']
self.assertTrue('Valid categories' in results)
except ValueError:
self.fail(msg='JSON response not received from API')
except Exception as e:
self.fail(msg='Unexpected error received: %s' % e.message)
if __name__ == '__main__':
unittest.main()