-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
89 lines (74 loc) · 3.37 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
import unittest
import json
from api import create_app
from api.models import db
class UserTestCase(unittest.TestCase):
"""This class represents the user test case"""
def setUp(self):
"""Define test variables and initialize app."""
self.app = create_app(config_name="testing")
self.client = self.app.test_client
self.user = {'name': 'Test user', 'email': '[email protected]',
'password': '123', 'gender': 'male'}
self.userweight = {'weight': 80.0}
# binds the app to the current context
with self.app.app_context():
# create all tables
db.create_all()
def test_user_creation(self):
res = self.client().post('/user/signup/', data=self.user)
self.assertEqual(res.status_code, 201)
self.assertIn('User created successfully!', str(res.data))
def test_api_can_get_all_users(self):
res = self.client().post('/user/signup/', data=self.user)
self.assertEqual(res.status_code, 201)
res = self.client().get('/user/')
self.assertEqual(res.status_code, 200)
def test_api_can_get_user_by_id(self):
res = self.client().post('/user/signup/', data=self.user)
self.assertEqual(res.status_code, 201)
result_in_json = json.loads(res.data.decode('utf-8').replace("'", "\""))
result = self.client().post('/user/', data={'user_id': int(result_in_json['user_id'])})
self.assertEqual(result.status_code, 200)
def test_user_can_be_edited(self):
res = self.client().post(
'/user/signup/',
data=self.user)
self.assertEqual(res.status_code, 201)
res = self.client().put(
'/user/1',
data={
"name": "John"
})
self.assertEqual(res.status_code, 200)
def test_user_deletion(self):
res = self.client().post(
'/user/signup/',
data=self.user)
self.assertEqual(res.status_code, 201)
res = self.client().delete('/user/1')
self.assertEqual(res.status_code, 200)
def test_create_weight(self):
res = self.client().post('/user/signup/', data=self.user)
self.assertEqual(res.status_code, 201)
result_in_json = json.loads(res.data.decode('utf-8').replace("'", "\""))
result = self.client().post('/userweight/', data={'weight': 89, 'user_id':int(result_in_json['user_id'])})
self.assertEqual(result.status_code, 201)
def test_get_user_weight(self):
res = self.client().post('/user/signup/', data=self.user)
self.assertEqual(res.status_code, 201)
result_in_json = json.loads(res.data.decode('utf-8').replace("'", "\""))
result = self.client().post('/userweight/', data={'weight': 89, 'user_id':int(result_in_json['user_id'])})
self.assertEqual(result.status_code, 201)
weights = self.client().get('/user/{}/weight'.format(int(result_in_json['user_id'])),
headers={'Authorization': 'Bearer '+ result_in_json['access_token']})
self.assertEqual(weights.status_code, 200)
def tearDown(self):
"""teardown all initialized variables."""
with self.app.app_context():
# drop all tables
db.session.remove()
db.drop_all()
# Make the tests conveniently executable
if __name__ == "__main__":
unittest.main()