-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_pytest.py
More file actions
321 lines (271 loc) · 9.82 KB
/
test_pytest.py
File metadata and controls
321 lines (271 loc) · 9.82 KB
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
'''
Tests in Pytest
'''
from app import app
def test_client():
'''
Makes a request and checks the message received is the same
'''
response = app.test_client().get('/test')
assert response.status_code == 200
assert response.json['message'] == "Hello, World!"
def test_experience():
'''
Add a new experience and then get all experiences.
Check that it returns the new experience in that list
'''
example_experience = {
"title": "Software Developer",
"company": "A Cooler Company",
"start_date": "October 2022",
"end_date": "Present",
"description": "Writing JavaScript Code",
"logo": "example-logo.png"
}
item_id = app.test_client().post('/resume/experience',
json=example_experience).json['id']
response = app.test_client().get('/resume/experience')
assert response.json[item_id] == example_experience
def test_education():
'''
Add a new education and then get all educations.
Check that it returns the new education in that list
'''
example_education = {
"course": "Engineering",
"school": "NYU",
"start_date": "October 2022",
"end_date": "August 2024",
"grade": "86%",
"logo": "example-logo.png"
}
item_id = app.test_client().post('/resume/education',
json=example_education).json['id']
response = app.test_client().get('/resume/education')
assert response.json[item_id] == example_education
def test_education_by_index():
'''
Get a specific education by index.
Check that it returns the expected education in JSON format
'''
expected_education = {
"course": "Computer Science",
"school": "University of Tech",
"start_date": "September 2019",
"end_date": "July 2022",
"grade": "80%",
"logo": "example-logo.png"
}
response = app.test_client().get('/resume/education/0')
assert response.status_code == 200
assert response.json == expected_education
def test_skill():
'''
Add a new skill and then get all skills.
Check that it returns the new skill in that list
'''
example_skill = {
"name": "JavaScript",
"proficiency": "2-4 years",
"logo": "example-logo.png"
}
item_id = app.test_client().post('/resume/skill',
json=example_skill).json['id']
response = app.test_client().get('/resume/skill')
assert response.json[item_id] == example_skill
def test_delete_skill():
'''
Add new skill and then delete it using the id.
Check that the count of skill entries is the same as before adding and deletion.
And verify that the return does not contain the deleted skill entry.
'''
client = app.test_client()
example_skill = {
"name": "Testing Skill",
"proficiency": "1-2 years",
"logo": "example-logo.png"
}
before = client.get('/resume/skill')
assert before.status_code == 200
before_count = len(before.json)
post_response = client.post('/resume/skill', json=example_skill)
assert post_response.status_code == 200
item_id = post_response.json['id']
delete_response = client.delete('/resume/skill', json={"id": item_id})
assert delete_response.status_code == 200
assert delete_response.json["deleted"] == item_id
after = client.get('/resume/skill')
assert after.status_code == 200
assert len(after.json) == before_count
assert not any(
skill for skill in after.json if skill['name'] == example_skill['name']
and skill['proficiency'] == example_skill['proficiency']
and skill['logo'] == example_skill['logo']
)
def test_delete_experience():
"""
Add an experience, delete it by returned index id,
and verify count goes down and does not contain the specific experience added.
"""
client = app.test_client()
example_experience = {
"title": "Software Developer for deleting",
"company": "Delete Test Co 123",
"start_date": "October 2022",
"end_date": "Present",
"description": "Writing JavaScript Code",
"logo": "example-logo.png"
}
before = client.get('/resume/experience')
assert before.status_code == 200
before_count = len(before.json)
post_response = client.post('/resume/experience', json=example_experience)
assert post_response.status_code == 200
item_id = post_response.json['id']
delete_response = client.delete('/resume/experience', json={"id": item_id})
assert delete_response.status_code == 200
assert delete_response.json["deleted"] == item_id
after = client.get('/resume/experience')
assert after.status_code == 200
assert len(after.json) == before_count
assert not any(
exp for exp in after.json if exp['title'] == example_experience['title'] and
exp['company'] == example_experience['company'] and
exp['start_date'] == example_experience['start_date'] and
exp['end_date'] == example_experience['end_date'] and
exp['description'] == example_experience['description'] and
exp['logo'] == example_experience['logo']
)
def test_get_user_info():
'''
Fetch user info and check if it returns matching data
'''
response = app.test_client().get('/resume/user_info')
assert response.status_code == 200
assert "name" in response.json
assert "phone_number" in response.json
assert "email_address" in response.json
def test_update_user_info():
'''
Update the user info and verify that it changes.
'''
client = app.test_client()
new_user_info = {
"name": "Jane Doe",
"phone_number": "+0987654321",
"email_address": "jane@example.com"
}
response = client.put('/resume/user_info', json=new_user_info)
assert response.status_code == 200
assert response.json["data"] == new_user_info
get_response = client.get('/resume/user_info')
assert get_response.json == new_user_info
def test_user_info_missing_fields():
'''
Test updating user info with missing fields
'''
client = app.test_client()
incomplete_info = {
"name": "No Phone Number"
}
response = client.post('/resume/user_info', json=incomplete_info)
assert response.status_code == 400
assert response.json["error"] == "Missing fields"
def test_user_info_invalid_phone():
'''
Test updating user info with an invalid phone number
'''
client = app.test_client()
invalid_phone_info = {
"name": "Jane",
"phone_number": "12345678", # Missing '+'
"email_address": "jane@example.com"
}
response = client.put('/resume/user_info', json=invalid_phone_info)
assert response.status_code == 400
assert "Phone number must include international country code" in response.json["error"]
def test_experience_missing_field():
'''
Test POST request to experience with missing fields
'''
example_experience = {
"title": "Software Developer",
"company": "A Cooler Company",
"start_date": "October 2022",
"end_date": "Present",
"description": "Writing JavaScript Code"
}
response = app.test_client().post('/resume/experience', json=example_experience)
assert response.status_code == 400
assert response.json['error'] == "Missing required fields"
def test_education_missing_field():
'''
Test POST request to education with missing fields
'''
example_education = {
"course": "Engineering",
"school": "NYU",
"start_date": "October 2022",
"end_date": "August 2024",
"grade": "86%"
}
response = app.test_client().post('/resume/education', json=example_education)
assert response.status_code == 400
assert response.json['error'] == "Missing required fields"
def test_skill_missing_field():
'''
Test POST request to skill with missing fields
'''
example_skill = {
"name": "JavaScript",
"proficiency": "2-4 years"
}
response = app.test_client().post('/resume/skill', json=example_skill)
assert response.status_code == 400
assert response.json['error'] == "Missing required fields"
def test_experience_invalid_format():
'''
Test POST request to experience with invalid format (extra fields)
'''
example_experience = {
"title": "Software Developer",
"company": "A Cooler Company",
"start_date": "October 2022",
"end_date": "Present",
"description": "Writing JavaScript Code",
"logo": "example-logo.png",
"extra_field": "This should fail"
}
response = app.test_client().post('/resume/experience', json=example_experience)
assert response.status_code == 400
assert response.json['error'] == "Invalid format"
def test_education_invalid_format():
'''
Test POST request to education with invalid format (extra fields)
'''
example_education = {
"course": "Engineering",
"school": "NYU",
"start_date": "October 2022",
"end_date": "August 2024",
"grade": "86%",
"logo": "example-logo.png",
"extra_field": "This should fail"
}
response = app.test_client().post('/resume/education', json=example_education)
assert response.status_code == 400
assert response.json['error'] == "Invalid format"
def test_skill_invalid_format():
'''
Test POST request to skill with invalid format (passing a list instead of dict)
'''
example_skill = [
{
"name": "JavaScript",
"proficiency": "2-4 years",
"logo": "example-logo.png"
}
]
response = app.test_client().post('/resume/skill', json=example_skill)
assert response.status_code == 400
assert response.json['error'] == "Missing required fields"