forked from dualboot-partners/eu-python-learn-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_flask_excercises.py
More file actions
81 lines (60 loc) · 2.67 KB
/
test_flask_excercises.py
File metadata and controls
81 lines (60 loc) · 2.67 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
import json
from http import HTTPStatus
from flask import Flask
from flask.testing import FlaskClient
from .flask_excercises import FlaskExercise
def create_flask_client() -> FlaskClient:
app = Flask(__name__)
FlaskExercise.configure_routes(app)
return app.test_client()
class TestFlaskExercise:
flask_client = create_flask_client()
def create_user(self, user_data: dict) -> dict:
response = self.flask_client.post(
"/user", data=json.dumps(user_data), content_type="application/json"
)
assert response.status_code == HTTPStatus.CREATED
return response.get_json()
def retrieve_user(self, username: str) -> dict:
response = self.flask_client.get(f"/user/{username}")
assert response.status_code == HTTPStatus.OK
return response.get_json()
def update_user(self, user_name: str, user_data: dict) -> dict:
response = self.flask_client.patch(
f"user/{user_name}",
data=json.dumps(user_data),
content_type="application/json",
)
assert response.status_code == HTTPStatus.OK
return response.get_json()
def delete_user(self, username: str) -> dict:
response = self.flask_client.delete(f"/user/{username}")
assert response.status_code == HTTPStatus.NO_CONTENT
return response.get_json()
def test_create(self) -> None:
response = self.create_user({"name": "Heisenberg"})
assert response == {"data": "User Heisenberg is created!"}
def test_unprocessable_entity(self) -> None:
response = self.flask_client.post(
"/user",
data=json.dumps({"profession": "Chemistry teacher"}),
content_type="application/json",
)
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY
assert response.get_json() == {"errors": {"name": "This field is required"}}
def test_get(self) -> None:
self.create_user({"name": "Heisenberg"})
response = self.retrieve_user("Heisenberg")
assert response == {"data": "My name is Heisenberg"}
def test_update(self) -> None:
self.create_user({"name": "Heisenberg"})
response = self.update_user("Heisenberg", {"name": "Jesse"})
assert response == {"data": "My name is Jesse"}
def test_delete(self) -> None:
self.create_user({"name": "Heisenberg"})
self.delete_user("Heisenberg")
response = self.flask_client.get("/user/Heisenberg")
assert response.status_code == HTTPStatus.NOT_FOUND
def test_not_found(self) -> None:
response = self.flask_client.get("/404")
assert response.status_code == HTTPStatus.NOT_FOUND