-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_routes.py
85 lines (61 loc) · 2.46 KB
/
test_routes.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
from app import app
import json
from typing import Any
import uuid
from werkzeug.test import TestResponse
def get_last_contact_id() -> int:
with open("contacts.json", "r") as contacts_file:
contacts: Any = json.load(contacts_file)
sorted_contacts: Any = sorted(contacts, key=lambda x: x["id"])
return sorted_contacts[-1]["id"]
def test_index() -> None:
response: TestResponse = app.test_client().get("/")
assert response.status_code == 302
def test_contacts_all() -> None:
response: TestResponse = app.test_client().get("/contacts")
assert b"<td>Carson</td>" in response.data
def test_contacts_search() -> None:
response: TestResponse = app.test_client().get("/contacts?q=carson")
assert b"<td>Carson</td>" in response.data
def test_contacts_new_get() -> None:
response: TestResponse = app.test_client().get("/contacts/new")
assert b"<legend>Contact Values</legend>" in response.data
def test_contacts_new_post() -> None:
response: TestResponse = app.test_client().post(
"/contacts/new",
data={
"first_name": "Test",
"last_name": "Contact",
"phone": "555-555-5555",
"email": f"{str(uuid.uuid4())[:8]}@example.com",
},
)
assert response.status_code == 302
def test_contacts_view() -> None:
response: TestResponse = app.test_client().get("/contacts/2")
assert b"<h1>Carson Gross</h1>" in response.data
def test_contact_view_not_exist() -> None:
response: TestResponse = app.test_client().get("/contacts/200")
assert b"<h1> </h1>" in response.data
def test_contacts_edit() -> None:
response: TestResponse = app.test_client().get("/contacts/2/edit")
assert b'hx-get="/contacts/2/email"' in response.data
def test_contacts_edit_not_exists() -> None:
response: TestResponse = app.test_client().get("/contacts/200/edit")
assert response.status_code == 500
def test_contacts_edit_post() -> None:
response: TestResponse = app.test_client().post(
f"/contacts/{get_last_contact_id()}/edit",
data={
"first_name": "Test",
"last_name": "Contact",
"phone": "666-666-6666",
"email": f"{str(uuid.uuid4())[:8]}@example.com",
},
)
assert response.status_code == 302
def test_contacts_delete_post() -> None:
response: TestResponse = app.test_client().delete(
f"/contacts/{get_last_contact_id()}"
)
assert response.status_code == 303