-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
564 lines (491 loc) · 20.6 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
import os
import sys
from app import create_app, db
import unittest
from threading import Thread
from flask.ext.sqlalchemy import SQLAlchemy
import json
from itertools import izip, islice
from random import randint
DEVICEID = 'abc123'
NAMES = ['alice','bob','charlie','doug','erica','francis','greg','henry','ian','john','karen','lauren','megan','nick','oliver','peter','quentin','ryan','steven','tracy','uriel','veronica','wendy','young','zane']
class ApiTestCases(unittest.TestCase):
def setUp(self):
self.flapp = create_app('testing')
self.app = self.flapp.test_client()
# self.db.session.expire_all()
# self.db.drop_all()
db.init_app(self.flapp)
with self.flapp.app_context():
db.create_all()
def tearDown(self):
with self.flapp.app_context():
db.drop_all()
# os.unlink(self.sqlitedb)
def test_passing(self):
"""
At least one unit test should pass
"""
self.assertEqual(1,1)
# def test_failing(self):
# """
# At least one unit test should fail
# """
# self.assertEqual(1,2)
###########################################################################
# ROOT TEST CASES
###########################################################################
# def test_get_root(self):
# """
# The root of the website should return some statically specified data.
# """
# rv = self.app.get('/')
# self.assertIn('Hello, world!', rv.data)
###########################################################################
# PLAYERS TEST CASES
###########################################################################
def test_players_is_initially_empty(self):
"""
On initial app, there should be no players.
"""
rv = self.app.get('/players')
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('players', data)
self.assertEqual(data['players'], [])
def test_bad_add_player1(self):
"""
The app should make sure the proper parameters are passed in when creating a player.
"""
rv = self.app.post('/players')
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_add_player2(self):
"""
The app should make sure the proper parameters are passed in when creating a player.
"""
rv = self.app.post('/players?name=testuser')
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_add_player3(self):
"""
The app should make sure the proper parameters are passed in when creating a player.
"""
rv = self.app.post('/players?device_id=%s' % DEVICEID)
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_add_player(self):
"""
The app should create a player when the proper parameters are passed in.
This test can be called by other tests to insert and return a new player into the database with name 'testuser' and device_id `DEVICEID`
"""
rv = self.app.post('/players?name=testuser&device_id=%s' % DEVICEID)
self.assertEqual(rv.status_code, 200)
player = json.loads(rv.data)
self.assertIn('id', player)
self.assertIn('name', player)
self.assertEqual(player['name'], 'testuser')
return player
def test_add_check_all_players(self):
"""
The app should maintain information about players after they are created.
"""
# create the user first
self.test_add_player()
rv = self.app.get('/players')
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('players', data)
self.assertEqual(len(data['players']), 1)
player = data['players'][0]
self.assertEqual(player['name'], 'testuser')
self.assertEqual(player['hifive_count'], 0)
self.assertEqual(player['characters'], 1)
self.assertEqual(player['powerup_lvl'], 1)
def test_add_check_player(self):
"""
The app should maintain information about players after they are created.
"""
# create the user first
player = self.test_add_player()
_id = player['id']
rv = self.app.get('/players/%d' % _id)
self.assertEqual(rv.status_code, 200)
retplayer = json.loads(rv.data)
self.assertIn('id', retplayer)
self.assertIn('name', retplayer)
self.assertIn('hifive_count', retplayer)
self.assertIn('characters', retplayer)
self.assertIn('powerup_lvl', retplayer)
self.assertEqual(retplayer['id'], player['id'])
self.assertEqual(retplayer['name'], player['name'])
self.assertEqual(retplayer['hifive_count'], player['hifive_count'])
self.assertEqual(retplayer['characters'], player['characters'])
self.assertEqual(retplayer['powerup_lvl'], player['powerup_lvl'])
def test_player_id_not_found(self):
"""
The app should return 404 if a player is not found when retrieving by id.
"""
rv = self.app.get('/players/1')
self.assertEqual(rv.status_code, 404)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_find_player1(self):
"""
The app should make sure the proper parameters are passed in when finding a player.
"""
rv = self.app.get('/players/find')
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_find_player2(self):
"""
The app should make sure the proper parameters are passed in when finding a player.
"""
rv = self.app.get('/players/find?name=%s' % 'test')
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_find_player3(self):
"""
The app should make sure the proper parameters are passed in when finding a player.
"""
rv = self.app.get('/players/find?device_id=%s' % DEVICEID)
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_not_found_player(self):
"""
The app should make sure the proper parameters are passed in when finding a player.
"""
rv = self.app.get('/players/find?name=%s&device_id=%s' % ('testuser',DEVICEID))
self.assertEqual(rv.status_code, 404)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_find_player(self):
"""
If a player exists, the app should return that player when searching with the player's name and device_id.
"""
player = self.test_add_player()
name = player['name']
rv = self.app.get('/players/find?name=%s&device_id=%s' % (name, DEVICEID))
self.assertEqual(rv.status_code, 200)
retplayer = json.loads(rv.data)
self.assertIn('id', retplayer)
self.assertIn('name', retplayer)
self.assertIn('hifive_count', retplayer)
self.assertIn('characters', retplayer)
self.assertIn('powerup_lvl', retplayer)
self.assertEqual(retplayer['id'], player['id'])
self.assertEqual(retplayer['name'], player['name'])
self.assertEqual(retplayer['hifive_count'], player['hifive_count'])
self.assertEqual(retplayer['characters'], player['characters'])
self.assertEqual(retplayer['powerup_lvl'], player['powerup_lvl'])
def test_bad_set_player_hifives1(self):
"""
The app should make sure the proper parameters are passed in when updating the hifive_count
"""
player = self.test_add_player()
_id = player['id']
rv = self.app.post('/players/%d/hifives' % _id)
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_set_player_hifives2(self):
"""
The app should make sure the proper parameters are passed in when updating the hifive_count
"""
player = self.test_add_player()
_id = player['id']
rv = self.app.post('/players/%d/hifives?hifives=abc' % _id)
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_set_player_hifives3(self):
"""
The app should return 404 if the player is not found when updating the hifive_count.
"""
rv = self.app.post('/players/1/hifives')
self.assertEqual(rv.status_code, 404)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_set_player_hifives(self):
"""
The app should update a player's hifive_count if the proper parameters are passed in.
"""
player = self.test_add_player()
_id = player['id']
new_val = 42
rv = self.app.post('/players/%d/hifives?hifives=%d' % (_id, new_val))
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('id', data)
self.assertEqual(data['id'], _id)
self.assertIn('hifive_count', data)
self.assertEqual(data['hifive_count'], new_val)
def test_bad_set_player_characters1(self):
"""
The app should make sure the proper parameters are passed in when updating the characters
"""
player = self.test_add_player()
_id = player['id']
rv = self.app.post('/players/%d/characters' % _id)
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_set_player_characters2(self):
"""
The app should make sure the proper parameters are passed in when updating the characters
"""
player = self.test_add_player()
_id = player['id']
rv = self.app.post('/players/%d/characters?hifives=abc' % _id)
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_set_player_characters3(self):
"""
The app should return 404 if the player is not found when updating the characters.
"""
rv = self.app.post('/players/1/characters')
self.assertEqual(rv.status_code, 404)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_set_player_characters(self):
"""
The app should update a player's characters if the proper parameters are passed in.
"""
player = self.test_add_player()
_id = player['id']
new_val = 42
rv = self.app.post('/players/%d/characters?characters=%d' % (_id, new_val))
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('id', data)
self.assertEqual(data['id'], _id)
self.assertIn('characters', data)
self.assertEqual(data['characters'], new_val)
def test_bad_set_player_poweruplvl1(self):
"""
The app should make sure the proper parameters are passed in when updating the powerup_lvl
"""
player = self.test_add_player()
_id = player['id']
rv = self.app.post('/players/%d/powerup_lvl' % _id)
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_set_player_poweruplvl2(self):
"""
The app should make sure the proper parameters are passed in when updating the powerup_lvl
"""
player = self.test_add_player()
_id = player['id']
rv = self.app.post('/players/%d/powerup_lvl?powerup_lvl=abc' % _id)
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_set_player_poweruplvl3(self):
"""
The app should return 404 if the player is not found when updating the powerup_lvl.
"""
rv = self.app.post('/players/1/powerup_lvl')
self.assertEqual(rv.status_code, 404)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_set_player_poweruplvl(self):
"""
The app should update a player's powerup_lvl if the proper parameters are passed in.
"""
player = self.test_add_player()
_id = player['id']
new_val = 42
rv = self.app.post('/players/%d/powerup_lvl?powerup_lvl=%d' % (_id, new_val))
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('id', data)
self.assertEqual(data['id'], _id)
self.assertIn('powerup_lvl', data)
self.assertEqual(data['powerup_lvl'], new_val)
def test_bad_get_player_scores1(self):
"""
The app should return 404 if the player is not found when retrieving their scores.
"""
rv = self.app.get('/players/1/scores')
self.assertEqual(rv.status_code, 404)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_player_scores_initially_empty(self):
"""
On initial player creation, there should be no scores.
"""
player = self.test_add_player()
_id = player['id']
rv = self.app.get('/players/1/scores')
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('scores', data)
self.assertEqual(data['scores'],[])
###########################################################################
# SCORES TEST CASES
###########################################################################
def test_get_scores_initially_empty(self):
"""
On initial app, there should be no scores.
"""
player = self.test_add_player()
_id = player['id']
rv = self.app.get('/scores')
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('scores', data)
self.assertEqual(data['scores'],[])
def test_bad_add_score1(self):
"""
The app should make sure the proper parameters are passed in when creating a new score.
"""
rv = self.app.post('/scores')
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_add_score2(self):
"""
The app should make sure the proper parameters are passed in when creating a new score.
"""
rv = self.app.post('/scores?device_id=%s' % DEVICEID)
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_add_score3(self):
"""
The app should make sure the proper parameters are passed in when creating a new score.
"""
rv = self.app.post('/scores?name=%s' % 'testuser')
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_add_score4(self):
"""
The app should make sure the proper parameters are passed in when creating a new score.
"""
rv = self.app.post('/scores?score=%d' % 1)
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_add_score5(self):
"""
The app should make sure the proper parameters are passed in when creating a new score.
"""
rv = self.app.post('/scores?device_id=%s&name=%s' % (DEVICEID, 'testuser'))
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_add_score6(self):
"""
The app should make sure the proper parameters are passed in when creating a new score.
"""
rv = self.app.post('/scores?score=%d&name=%s' % (2, 'testuser'))
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_bad_add_score7(self):
"""
The app should make sure the proper parameters are passed in when creating a new score.
"""
rv = self.app.post('/scores?device_id=%s&score=%d' % (DEVICEID, 3))
self.assertEqual(rv.status_code, 400)
data = json.loads(rv.data)
self.assertIn('error', data)
def test_add_score(self, name='testuser', score=1):
"""
The app should create a new score when all the proper parameters are passed in. If the player with the given name did not exist yet, it should exist after the create-score call.
"""
rv = self.app.post('/scores?device_id=%s&name=%s&score=%d' % (DEVICEID, name, score))
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('rank', data)
self.assertIn('score', data)
self.assertIn('player', data['score'])
self.assertIn('name', data['score']['player'])
self.assertIn('score', data['score'])
self.assertEqual(data['score']['score'], score)
self.assertEqual(data['score']['player']['name'], name)
rv = self.app.get('/players')
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('players', data)
self.assertGreaterEqual(len(data['players']), 1)
found = False
for player in data['players']:
self.assertIn('name', player)
self.assertIn('scores', player)
if player['name'] == name:
found = True
foundScore = False
for s in player['scores']:
self.assertIn('score', s)
if s['score'] == score:
foundScore = True
self.assertTrue(foundScore)
self.assertTrue(found)
def test_add_multiple_scores(self):
"""
The app should maintain information about multiple scores.
"""
for i in range(50):
name = NAMES[randint(0,len(NAMES)-1)]
self.test_add_score(name=name,score=randint(10,10000))
rv = self.app.get('/scores')
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('scores', data)
self.assertLessEqual(len(data['scores']), 15)
for fst, snd in izip(data['scores'], islice(data['scores'], 1, None)):
self.assertLessEqual(snd['score'], fst['score'], msg="Scores not in decreasing order")
def test_limit_get_scores(self):
"""
The app should maintain information about multiple scores.
"""
for i in range(50):
name = NAMES[randint(0,len(NAMES)-1)]
self.test_add_score(name=name,score=randint(10,10000))
rv = self.app.get('/scores?limit=3')
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('scores', data)
self.assertLessEqual(len(data['scores']), 3)
for fst, snd in izip(data['scores'], islice(data['scores'], 1, None)):
self.assertLessEqual(snd['score'], fst['score'], msg="Scores not in decreasing order")
def test_multiple_player_scores(self):
"""
The app should return data about player scores and should be limited with default of 15.
"""
for i in range(50):
name = 'testuser'
self.test_add_score(name=name,score=randint(10,10000))
rv = self.app.get('/scores')
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('scores', data)
self.assertLessEqual(len(data['scores']), 15)
for fst, snd in izip(data['scores'], islice(data['scores'], 1, None)):
self.assertLessEqual(snd['score'], fst['score'], msg="Scores not in decreasing order")
def test_limit_player_scores(self):
"""
The app should return data about player scores and should be limited with default of 15.
"""
for i in range(50):
name = 'testuser'
self.test_add_score(name=name,score=randint(10,10000))
rv = self.app.get('/scores?limit=3')
self.assertEqual(rv.status_code, 200)
data = json.loads(rv.data)
self.assertIn('scores', data)
self.assertLessEqual(len(data['scores']), 3)
for fst, snd in izip(data['scores'], islice(data['scores'], 1, None)):
self.assertLessEqual(snd['score'], fst['score'], msg="Scores not in decreasing order")
if __name__ == '__main__':
unittest.main()