Skip to content

Commit 1a03942

Browse files
authored
Merge pull request #87 from AdaGold/ap_add_tests_for_route_helpers
Ap add tests for route helpers
2 parents a276aa6 + e174e47 commit 1a03942

File tree

4 files changed

+312
-3
lines changed

4 files changed

+312
-3
lines changed

ada-project-docs/wave_07.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
# Wave 7: Deployment
1+
# Wave 7: Deployment and Refactoring
22

33
## Goal
44

5-
Our goal is to make our project accessible online!
5+
Our goal for wave 7 is twofold. We want to implement refactors that will reduce redundancy between our routes and we want our API to be available online!
66

77
## Requirements
88

9+
### Refactoring
10+
Using the Wave 07 tests and the refactoring examples we went over in class with Flasky, refactor your code in a way that removes redundancies across your Task and Goal routes.
11+
12+
Keep in mind that you may need to rework your project file structure and adjust your imports to make the tests work.
13+
14+
### Deployment
915
Deploy this project to Render.
1016

1117
When deploying a web service to Render, it will try to be helpful and set the `Language` field for you, but it doesn't always select the correct option.

tests/test_wave_01.py

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,97 @@
22
from app.db import db
33
import pytest
44

5+
@pytest.mark.skip(reason="No way to test this feature yet")
6+
def test_task_to_dict():
7+
#Arrange
8+
new_task = Task(id = 1, title="Make My Bed",
9+
description="Start the day off right!",
10+
completed_at=None)
11+
12+
#Act
13+
task_dict = new_task.to_dict()
14+
15+
#Assert
16+
assert len(task_dict) == 4
17+
assert task_dict["id"] == 1
18+
assert task_dict["title"] == "Make My Bed"
19+
assert task_dict["description"] == "Start the day off right!"
20+
assert task_dict["is_complete"] == False
21+
22+
@pytest.mark.skip(reason="No way to test this feature yet")
23+
def test_task_to_dict_missing_id():
24+
#Arrange
25+
new_task = Task(title="Make My Bed",
26+
description="Start the day off right!",
27+
completed_at=None)
28+
29+
#Act
30+
task_dict = new_task.to_dict()
31+
32+
#Assert
33+
assert len(task_dict) == 4
34+
assert task_dict["id"] is None
35+
assert task_dict["title"] == "Make My Bed"
36+
assert task_dict["description"] == "Start the day off right!"
37+
assert task_dict["is_complete"] == False
38+
39+
@pytest.mark.skip(reason="No way to test this feature yet")
40+
def test_task_to_dict_missing_title():
41+
#Arrange
42+
new_task = Task(id = 1,
43+
description="Start the day off right!",
44+
completed_at=None)
45+
46+
#Act
47+
task_dict = new_task.to_dict()
48+
49+
#Assert
50+
assert len(task_dict) == 4
51+
assert task_dict["id"] == 1
52+
assert task_dict["title"] is None
53+
assert task_dict["description"] == "Start the day off right!"
54+
assert task_dict["is_complete"] == False
55+
56+
@pytest.mark.skip(reason="No way to test this feature yet")
57+
def test_task_from_dict():
58+
#Arrange
59+
task_dict = {
60+
"title": "Make My Bed",
61+
"description": "Start the day off right!",
62+
"is_complete": False
63+
}
64+
65+
#Act
66+
task_obj = Task.from_dict(task_dict)
67+
68+
#Assert
69+
assert task_obj.title == "Make My Bed"
70+
assert task_obj.description == "Start the day off right!"
71+
assert task_obj.completed_at is None
72+
73+
@pytest.mark.skip(reason="No way to test this feature yet")
74+
def test_task_from_dict_no_title():
75+
#Arrange
76+
task_dict = {
77+
"description": "Start the day off right!",
78+
"is_complete": False
79+
}
80+
81+
#Act & Assert
82+
with pytest.raises(KeyError, match = 'title'):
83+
Task.from_dict(task_dict)
84+
85+
@pytest.mark.skip(reason="No way to test this feature yet")
86+
def test_task_from_dict_no_description():
87+
#Arrange
88+
task_dict = {
89+
"title": "Make My Bed",
90+
"is_complete": False
91+
}
92+
93+
#Act & Assert
94+
with pytest.raises(KeyError, match = 'description'):
95+
Task.from_dict(task_dict)
596

697
@pytest.mark.skip(reason="No way to test this feature yet")
798
def test_get_tasks_no_saved_tasks(client):
@@ -96,7 +187,6 @@ def test_create_task(client):
96187
assert new_task.description == "Test Description"
97188
assert new_task.completed_at == None
98189

99-
100190
@pytest.mark.skip(reason="No way to test this feature yet")
101191
def test_update_task(client, one_task):
102192
# Act

tests/test_wave_05.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,67 @@
1+
from app.models.goal import Goal
12
import pytest
23

4+
@pytest.mark.skip(reason="No way to test this feature yet")
5+
def test_goal_to_dict():
6+
#Arrange
7+
new_goal = Goal(id=1, title="Seize the Day!")
8+
9+
#Act
10+
goal_dict = new_goal.to_dict()
11+
12+
#Assert
13+
assert goal_dict["id"] == 1
14+
assert goal_dict["title"] == "Seize the Day!"
15+
16+
@pytest.mark.skip(reason="No way to test this feature yet")
17+
def test_goal_to_dict_no_id():
18+
#Arrange
19+
new_goal = Goal(title="Seize the Day!")
20+
21+
#Act
22+
goal_dict = new_goal.to_dict()
23+
24+
#Assert
25+
assert goal_dict["id"] is None
26+
assert goal_dict["title"] == "Seize the Day!"
27+
28+
@pytest.mark.skip(reason="No way to test this feature yet")
29+
def test_goal_to_dict_no_title():
30+
#Arrange
31+
new_goal = Goal(id=1)
32+
33+
#Act
34+
goal_dict = new_goal.to_dict()
35+
36+
#Assert
37+
assert goal_dict["id"] == 1
38+
assert goal_dict["title"] is None
39+
40+
41+
42+
@pytest.mark.skip(reason="No way to test this feature yet")
43+
def test_goal_from_dict():
44+
#Arrange
45+
goal_dict = {
46+
"title": "Seize the Day!",
47+
}
48+
49+
#Act
50+
goal_obj = Goal.from_dict(goal_dict)
51+
52+
#Assert
53+
assert goal_obj.title == "Seize the Day!"
54+
55+
@pytest.mark.skip(reason="No way to test this feature yet")
56+
def test_goal_from_dict_no_title():
57+
#Arrange
58+
goal_dict = {
59+
}
60+
61+
#Act & Assert
62+
with pytest.raises(KeyError, match = 'title'):
63+
Goal.from_dict(goal_dict)
64+
365

466
@pytest.mark.skip(reason="No way to test this feature yet")
567
def test_get_goals_no_saved_goals(client):

tests/test_wave_07.py

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import pytest
2+
from werkzeug.exceptions import HTTPException
3+
from app.models.goal import Goal
4+
from app.models.task import Task
5+
from app.routes.route_utilities import create_model, validate_model
6+
7+
@pytest.mark.skip(reason="No way to test this feature yet")
8+
def test_route_utilities_validate_model_with_task(client, three_tasks):
9+
#Act
10+
task_1 = validate_model(Task, 1)
11+
task_2 = validate_model(Task, 2)
12+
task_3 = validate_model(Task, 3)
13+
14+
#Assert
15+
assert task_1.id == 1
16+
assert task_1.title == "Water the garden 🌷"
17+
assert task_1.description == ""
18+
assert task_1.completed_at is None
19+
20+
assert task_2.id == 2
21+
assert task_2.title == "Answer forgotten email 📧"
22+
23+
assert task_3.id == 3
24+
assert task_3.title == "Pay my outstanding tickets 😭"
25+
26+
27+
@pytest.mark.skip(reason="No way to test this feature yet")
28+
def test_route_utilities_validate_model_with_task_invalid_id(client, three_tasks):
29+
#Act & Assert
30+
# Calling `validate_model` without being invoked by a route will
31+
# cause an `HTTPException` when an `abort` statement is reached
32+
with pytest.raises(HTTPException) as e:
33+
result_task = validate_model(Task, "One")
34+
35+
# Test that the correct status code and response message are returned
36+
response = e.value.get_response()
37+
assert response.status_code == 400
38+
39+
raise Exception("Complete test with an assertion about the response body")
40+
# *****************************************************************************
41+
# ** Complete test with an assertion about the response body ****************
42+
# *****************************************************************************
43+
44+
@pytest.mark.skip(reason="No way to test this feature yet")
45+
def test_route_utilities_validate_model_with_task_missing_id(client, three_tasks):
46+
#Act & Assert
47+
with pytest.raises(HTTPException) as e:
48+
result_task = validate_model(Task, 4)
49+
50+
raise Exception("Complete test with assertion status code and response body")
51+
# *****************************************************************************
52+
# **Complete test with assertion about status code response body***************
53+
# *****************************************************************************
54+
55+
56+
@pytest.mark.skip(reason="No way to test this feature yet")
57+
def test_route_utilities_validate_model_with_goal(client, one_goal):
58+
#Act
59+
goal_1 = validate_model(Goal, 1)
60+
61+
#Assert
62+
assert goal_1.id == 1
63+
assert goal_1.title == "Build a habit of going outside daily"
64+
65+
@pytest.mark.skip(reason="No way to test this feature yet")
66+
def test_route_utilities_validate_model_with_goal_invalid_id(client, one_goal):
67+
#Act & Assert
68+
with pytest.raises(HTTPException) as e:
69+
result_task = validate_model(Goal, "One")
70+
71+
raise Exception("Complete test with assertion status code and response body")
72+
# *****************************************************************************
73+
# **Complete test with assertion about status code response body***************
74+
# *****************************************************************************
75+
76+
@pytest.mark.skip(reason="No way to test this feature yet")
77+
def test_route_utilities_validate_model_with_goal_missing_id(client, one_goal):
78+
#Act & Assert
79+
with pytest.raises(HTTPException) as e:
80+
result_task = validate_model(Goal, 4)
81+
82+
raise Exception("Complete test with assertion status code and response body")
83+
# *****************************************************************************
84+
# **Complete test with assertion about status code response body***************
85+
# *****************************************************************************
86+
87+
@pytest.mark.skip(reason="No way to test this feature yet")
88+
def test_route_utilities_create_model_with_task(client):
89+
#Arrange
90+
request_body = {
91+
"title": "Make the bed",
92+
"description": "",
93+
"completed_at": None
94+
}
95+
96+
#Act
97+
response = create_model(Task, request_body)
98+
99+
#Assert
100+
assert response[0]["id"] == 1 #create_model returns a tuple
101+
assert response[0]["title"] == "Make the bed"
102+
assert response[0]["description"] == ""
103+
assert response[0]["is_complete"] == False
104+
assert response[1] == 201
105+
106+
@pytest.mark.skip(reason="No way to test this feature yet")
107+
def test_route_utilities_create_model_with_task_missing_title(client):
108+
#Arrange
109+
request_body = {
110+
"description": "",
111+
"completed_at": None
112+
}
113+
114+
#Act
115+
with pytest.raises(HTTPException) as e:
116+
create_model(Task, request_body)
117+
118+
response = e.value.get_response()
119+
assert response.status_code == 400
120+
assert response.get_json() == {"details": "Invalid data"}
121+
122+
123+
@pytest.mark.skip(reason="No way to test this feature yet")
124+
def test_route_utilities_create_model_with_goal(client):
125+
#Arrange
126+
request_body = {
127+
"title": "Seize the Day!"
128+
}
129+
130+
#Act
131+
response = create_model(Goal, request_body)
132+
133+
#Assert
134+
assert response[0]["id"] == 1 #create_model returns a tuple
135+
assert response[0]["title"] == "Seize the Day!"
136+
assert response[1] == 201
137+
138+
@pytest.mark.skip(reason="No way to test this feature yet")
139+
def test_route_utilities_create_model_with_goal_missing_title(client):
140+
#Arrange
141+
request_body = {
142+
}
143+
144+
#Act
145+
with pytest.raises(HTTPException) as e:
146+
create_model(Goal, request_body)
147+
148+
raise Exception("Complete test with assertion status code and response body")
149+
# *****************************************************************************
150+
# **Complete test with assertion about status code response body***************
151+
# *****************************************************************************

0 commit comments

Comments
 (0)