-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstories.py
201 lines (170 loc) · 6.16 KB
/
stories.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
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
import django.urls
from bs4 import BeautifulSoup
import pytest
from .locations.factories import Location
from .tasks.factories import Task
from .weather_reports.factories import WeatherReport
@pytest.mark.django_db
def homepage_to_add_tasks_form(client, bs4_matchers):
# GIVEN I am on the TODO list application
tasks_response = client.get(django.urls.reverse("website:tasks"))
assert tasks_response.status_code == 200
tasks = BeautifulSoup(str(tasks_response.content), features="html5lib")
# WHEN I click on the add tasks button
add_task_button = tasks.find(
lambda t: bs4_matchers.button(t) and "add task" in t.text.lower(),
)
assert add_task_button is not None
assert add_task_button.has_attr("href")
# THEN I should be presented with an add tasks form
add_task_response = client.get(add_task_button["href"])
assert add_task_response.status_code == 200
add_task = BeautifulSoup(
str(add_task_response.content),
features="html5lib",
)
assert add_task.find("form") is not None
@pytest.mark.django_db
def homepage_to_edit_tasks_form(client):
task = Task.create()
# GIVEN I am on the TODO list application
tasks_response = client.get(django.urls.reverse("website:tasks"))
assert tasks_response.status_code == 200
tasks = BeautifulSoup(str(tasks_response.content), features="html5lib")
# WHEN I click on an specific task
task_url = (
tasks
.find(lambda t: (
t.has_attr("class")
and "card-header" in t["class"]
and task.name.lower() in t.text.lower()
))
.find_parent(lambda t: (
t.name.strip("\\n") == "a"
and t.has_attr("href")
))
)
assert task_url is not None
task_detail_response = client.get(task_url["href"])
assert task_detail_response.status_code == 200
task_detail = BeautifulSoup(
str(task_detail_response.content),
features="html5lib",
)
# AND click edit this task
task_edit_url = (
task_detail
.find(lambda t: (
t.name.strip("\\n") == "a"
and t.has_attr("href")
and "edit this task" in t.text.lower()
))
)
assert task_edit_url is not None
edit_task_response = client.get(task_edit_url["href"])
assert edit_task_response.status_code == 200
edit_task = BeautifulSoup(
str(edit_task_response.content),
features="html5lib",
)
# THEN I should be presented with an edit tasks form
assert edit_task.find("form") is not None
@pytest.mark.django_db
def add_edit_tasks_location_dropdown(client):
location_count = 3
for _ in range(location_count):
location = Location.create()
task = Task.create(location=location)
# GIVEN I am on the add or edit tasks form
add_task_response = client.get(django.urls.reverse("website:task_create"))
edit_task_response = client.get(django.urls.reverse(
"website:task_update",
kwargs={"pk": task.pk},
))
assert add_task_response.status_code == 200
assert edit_task_response.status_code == 200
add_task = BeautifulSoup(
str(add_task_response.content),
features="html5lib",
)
edit_task = BeautifulSoup(
str(edit_task_response.content),
features="html5lib",
)
# WHEN I click on the location drop-down list
# THEN I should be presented with a list of possible locations
def location_options(soup):
return (
soup
.find(lambda t: (
t.name.strip("\\n") == "label"
and "location" in t.text.lower()
))
.parent
.find("select")
.find_all("option")
)
assert len(location_options(add_task)) == location_count + 1
assert len(location_options(edit_task)) == location_count + 1
@pytest.mark.skip
def weather_reports_dictate_add_edit_task_form_background():
# TODO: PhantomJS with Selenium to test the JS behaves.
# GIVEN I select a location from the drop-down list
# WHEN I view the add/edit tasks form
# THEN I should see the background colour changing
assert False
@pytest.mark.skip
def temperature_shows_in_add_edit_tasks():
# TODO: PhantomJS with Selenium to test the JS behaves.
# GIVEN I select a location from the drop-down list
# WHEN I view the add/edit tasks form
# THEN I should see the current temperature
assert False
@pytest.mark.django_db
def weather_reports_dictate_task_background(client):
weathers_to_classes = {
"Clear": "bg-danger",
"Clouds": "bg-primary",
"Rain": "bg-info",
}
locations = {
weather: Location.create()
for weather in weathers_to_classes
}
tasks = {
weather: Task.create(location=location)
for weather, location in locations.items()
}
tasks_by_name = {
task.name: {"task": task, "weather": weather}
for weather, task in tasks.items()
}
del tasks # Would be overwritten later.
for weather, location in locations.items():
WeatherReport.create(location=location, name=weather)
# GIVEN I am on the TODO list application
tasks_response = client.get(django.urls.reverse("website:tasks"))
assert tasks_response.status_code == 200
tasks = BeautifulSoup(str(tasks_response.content), features="html5lib")
# WHEN I view all of my tasks
# THEN I should see the tasks displayed with respective colours to
# their local weather
#
# Blue for “cold” or “rain”, Yellow-Orange for “warm” or “cloudy”
# and Red for “hot” or “sunny” (the product owner did not specify
# the exact parameters for temperature and colour).
cards = tasks.find_all(lambda t: (
t.has_attr("class")
and "card" in t["class"]
))
for card in cards:
task = tasks_by_name[
card
.find(lambda t: t.has_attr("class") and "card-header" in t["class"])
.text
]
clean_classes = [
class_.strip("\\n") for class_ in card["class"]
if class_.strip("\\n")
]
assert weathers_to_classes[task['weather']] in clean_classes