-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
146 lines (109 loc) · 5.3 KB
/
tests.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import app as my_app
import unittest
import json
class AppTestCase(unittest.TestCase):
def setUp(self):
my_app.app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
my_app.app.config['TESTING'] = True
self.app = my_app.app.test_client()
with my_app.app.app_context():
my_app.db.create_all()
def tearDown(self):
with my_app.app.app_context():
my_app.db.drop_all()
def test_index(self):
rv = self.app.get('/')
self.assertIn(b'todo: find an api details generator like swagger?', rv.data)
def test_get_target_by_key_404(self):
# targets should be empty
with my_app.app.app_context():
self.assertEqual(my_app.TargetLocation.query.count(), 0)
# target endpoint should return 404 when the item is missing
rv = self.app.get('/target/key/0')
self.assertEqual(rv.status_code, 404)
def test_get_existing_target_by_key(self):
# add a new target to the database
with my_app.app.app_context():
target = my_app.TargetLocation(lat=1, lng=1)
my_app.db.session.add(target)
my_app.db.session.commit()
target_id = getattr(target, 'key', None)
# retrieve target from api
self.assertIsNotNone(target_id)
rv = self.app.get(f'/target/key/{target.key}')
rv_json = json.loads(rv.get_data())
self.assertDictContainsSubset({'position': {'lat': 1, 'lng': 1}}, rv_json)
def test_create_post(self):
# create a target, then create a post
with my_app.app.app_context():
self.assertEqual(my_app.TargetLocation.query.count(), 0)
self.assertEqual(my_app.Post.query.count(), 0)
target = my_app.TargetLocation(lat=1, lng=1)
my_app.db.session.add(target)
my_app.db.session.commit()
post = my_app.Post(text='test',
image_id=None,
final_distance=1,
location_id=target.key)
my_app.db.session.add(post)
my_app.db.session.commit()
self.assertEqual(my_app.TargetLocation.query.count(), 1)
self.assertEqual(my_app.Post.query.count(), 1)
post_from_db = my_app.Post.query.first()
self.assertDictEqual(post.toSimpleDict(),
post_from_db.toSimpleDict())
def test_get_post_by_page(self):
# create a target, then create a post
with my_app.app.app_context():
self.assertEqual(my_app.TargetLocation.query.count(), 0)
self.assertEqual(my_app.Post.query.count(), 0)
target = my_app.TargetLocation(lat=1, lng=1)
my_app.db.session.add(target)
my_app.db.session.commit()
posts = []
for i in range(0, 100):
post = my_app.Post(text='test',
image_id=None,
final_distance=1,
location_id=target.key)
my_app.db.session.add(post)
my_app.db.session.commit()
posts.append(post.toSimpleDict())
self.assertEqual(my_app.TargetLocation.query.count(), 1)
self.assertEqual(my_app.Post.query.count(), 100)
rv = self.app.get(f'/posts/{target.key}/1')
rv_json = json.loads(rv.get_data())
easy_pagination = my_app.EasyPagination(posts[0:10], 1, False)
self.assertDictEqual(easy_pagination.toSimpleDict(), rv_json)
def test_get_target_by_location(self):
# create a target
with my_app.app.app_context():
self.assertEqual(my_app.TargetLocation.query.count(), 0)
rv = self.app.get('/target/1,1')
self.assertEqual(my_app.TargetLocation.query.count(), 1)
# confirm that the target is within the min, max distance
rv_json = json.loads(rv.get_data())
target_json = rv_json['position']
target_lat, target_lng = target_json['lat'], target_json['lng']
target_location = my_app.TargetLocation(lat=target_lat, lng=target_lng)
source_location = my_app.TargetLocation(lat=1, lng=1)
# test whether the distance is in the allowed range
distance = my_app._haversine(target_location, source_location)
self.assertLess(distance, my_app.app.config['TARGET_MAX_DISTANCE'])
self.assertGreater(distance, my_app.app.config['TARGET_MIN_DISTANCE'])
def test_get_existing_target_by_location(self):
# create a target
with my_app.app.app_context():
self.assertEqual(my_app.TargetLocation.query.count(), 0)
# create the existing location
rv = self.app.get('/target/1,1')
self.assertEqual(my_app.TargetLocation.query.count(), 1)
# now hit the endpoint again, should return same target
rv = self.app.get('/target/1,1')
self.assertEqual(my_app.TargetLocation.query.count(), 1)
@unittest.skip
def test_get_target_returns_metadata(self):
# get target should return total visitors, average visitors
self.fail("finish me!")
if __name__ == '__main__':
unittest.main()