This repository was archived by the owner on Feb 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_app.py
149 lines (132 loc) · 6.25 KB
/
test_app.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
from copy import deepcopy
import unittest
import json
import main
from copy import deepcopy
BASE_URL = 'http://127.0.0.1:8000'
valid_input = {"email": "[email protected]", "age": 27, "employee_id": 5,
"position": "Junior Developer", "name": "jansiraj"}
class TestFlaskApi(unittest.TestCase):
def setUp(self):
self.app = main.app.test_client()
self.app.testing = True
def test_post_success_response(self):
# valid: all required fields, age and employee_id takes int
api_url = BASE_URL + "/createEmployee"
response = self.app.post(api_url,
data=json.dumps(valid_input),
content_type='application/json')
self.assertEqual(response.status_code, 201)
data = json.loads(response.get_data())
self.assertEqual(data['message'], "Employee details created Successfully")
delete_url = BASE_URL + "/deleteEmployee/5"
self.app.delete(delete_url)
def test_post_missing_value(self):
# missing value field = bad
employee = {"age": 27, "employee_id": 5, "position": "Junior Developer",
"name": "jansiraja"}
api_url = BASE_URL + "/createEmployee"
response = self.app.post(api_url,
data=json.dumps(employee),
content_type='application/json')
self.assertEqual(response.status_code, 400)
def test_post_already_exits(self):
# check already exists for employee id or email
employee = deepcopy(valid_input)
employee["employee_id"] = 5
employee["email"] = "[email protected]"
api_url = BASE_URL + "/createEmployee"
response = self.app.post(api_url,
data=json.dumps(employee),
content_type='application/json')
employee = {"email": "[email protected]", "age": 27, "employee_id": 5,
"position": "Junior Developer", "name": "jansiraj"}
response = self.app.post(api_url,
data=json.dumps(employee),
content_type='application/json')
self.assertEqual(response.status_code, 200)
data = json.loads(response.get_data())
self.assertEqual(data['message'], "Employee already exists")
delete_url = BASE_URL + "/deleteEmployee/5"
self.app.delete(delete_url)
def test_post_valid_type(self):
# age and employee_id field cannot take str
api_url = BASE_URL + "/createEmployee"
employee = {"email": "[email protected]", "age": "27", "employee_id": "2",
"position": "Junior Developer", "name": "jansiraj"}
response = self.app.post(api_url,
data=json.dumps(employee),
content_type='application/json')
self.assertEqual(response.status_code, 400)
def test_get_all(self):
api_url = BASE_URL + "/getEmployeesAll"
response = self.app.get(api_url)
data = json.loads(response.get_data())
self.assertEqual(response.status_code, 200)
self.assertEqual(data['message'], "Successfully fetched all Employee details")
def test_get_one(self):
employee = deepcopy(valid_input)
employee["employee_id"] = 6
employee["email"] = "[email protected]"
api_url = BASE_URL + "/createEmployee"
response = self.app.post(api_url,
data=json.dumps(employee),
content_type='application/json')
api_url = BASE_URL + "/getEmployee/6"
response = self.app.get(api_url)
data = json.loads(response.get_data())
self.assertEqual(response.status_code, 200)
self.assertEqual(data['message'], "Successfully fetched Employee detail")
delete_url = BASE_URL + "/deleteEmployee/6"
self.app.delete(delete_url)
def test_update(self):
employee = deepcopy(valid_input)
employee["employee_id"] = 7
employee["email"] = "[email protected]"
api_url = BASE_URL + "/createEmployee"
response = self.app.post(api_url,
data=json.dumps(employee),
content_type='application/json')
update_detail = {"age": 40}
api_url = BASE_URL + "/updateEmployee/7"
response = self.app.patch(api_url,
data=json.dumps(update_detail),
content_type='application/json')
self.assertEqual(response.status_code, 200)
delete_url = BASE_URL + "/deleteEmployee/7"
self.app.delete(delete_url)
def test_update_error(self):
# age field cannot take str
employee = deepcopy(valid_input)
employee["employee_id"] = 8
employee["email"] = "[email protected]"
api_url = BASE_URL + "/createEmployee"
response = self.app.post(api_url,
data=json.dumps(employee),
content_type='application/json')
update_detail = {"age": 'string'}
api_url = BASE_URL + "/updateEmployee/8"
response = self.app.patch(api_url,
data=json.dumps(update_detail),
content_type='application/json')
self.assertEqual(response.status_code, 400)
delete_url = BASE_URL + "/deleteEmployee/8"
self.app.delete(delete_url)
def test_delete(self):
employee = deepcopy(valid_input)
employee["employee_id"] = 9
employee["email"] = "[email protected]"
api_url = BASE_URL + "/createEmployee"
response = self.app.post(api_url,
data=json.dumps(employee),
content_type='application/json')
api_url = BASE_URL + "/deleteEmployee/9"
response = self.app.delete(api_url)
self.assertEqual(response.status_code, 200)
def test_delete_error(self):
print("hello")
api_url = BASE_URL + "/deleteEmployee/10"
response = self.app.delete(api_url)
self.assertEqual(response.status_code, 400)
if __name__ == "__main__":
unittest.main()