forked from Ada-C15A/task-list-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
88 lines (72 loc) · 2.41 KB
/
conftest.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
import pytest
from app import create_app
from app.models.task import Task
from app.models.goal import Goal
from app import db
from datetime import datetime
@pytest.fixture
def app():
# create the app with a test config dictionary
app = create_app({"TESTING": True})
with app.app_context():
db.create_all()
yield app
# close and remove the temporary database
with app.app_context():
db.drop_all()
@pytest.fixture
def client(app):
return app.test_client()
# This fixture gets called in every test that
# references "one_task"
# This fixture creates a task and saves it in the database
@pytest.fixture
def one_task(app):
new_task = Task(
title="Go on my daily walk 🏞", description="Notice something new every day", completed_at=None)
db.session.add(new_task)
db.session.commit()
# This fixture gets called in every test that
# references "three_tasks"
# This fixture creates three tasks and saves
# them in the database
@pytest.fixture
def three_tasks(app):
db.session.add_all([
Task(
title="Water the garden 🌷", description="", completed_at=None),
Task(
title="Answer forgotten email 📧", description="", completed_at=None),
Task(
title="Pay my outstanding tickets 😭", description="", completed_at=None)
])
db.session.commit()
# This fixture gets called in every test that
# references "completed_task"
# This fixture creates a task with a
# valid completed_at date
@pytest.fixture
def completed_task(app):
new_task = Task(
title="Go on my daily walk 🏞", description="Notice something new every day", completed_at=datetime.utcnow())
db.session.add(new_task)
db.session.commit()
# This fixture gets called in every test that
# references "one_goal"
# This fixture creates a goal and saves it in the database
@pytest.fixture
def one_goal(app):
new_goal = Goal(title="Build a habit of going outside daily")
db.session.add(new_goal)
db.session.commit()
# This fixture gets called in every test that
# references "one_task_belongs_to_one_goal"
# This fixture creates a task and a goal
# It associates the goal and task, so that the
# goal has this task, and the task belongs to one goal
@pytest.fixture
def one_task_belongs_to_one_goal(app, one_goal, one_task):
task = Task.query.first()
goal = Goal.query.first()
goal.tasks.append(task)
db.session.commit()