Skip to content

Commit 45e4ce2

Browse files
committed
pytest: test model.Break and model.BreakQueue
1 parent bf4a04a commit 45e4ce2

File tree

3 files changed

+359
-0
lines changed

3 files changed

+359
-0
lines changed

pyproject.toml

+5
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,8 @@ build-backend = "setuptools.build_meta"
4747
[tool.setuptools]
4848
packages=["safeeyes"]
4949
include-package-data = true
50+
51+
[tool.pytest.ini_options]
52+
addopts = [
53+
"--import-mode=importlib",
54+
]

safeeyes/tests/__init__.py

Whitespace-only changes.

safeeyes/tests/test_model.py

+354
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,354 @@
1+
# Safe Eyes is a utility to remind you to take break frequently
2+
# to protect your eyes from eye strain.
3+
4+
# Copyright (C) 2017 Gobinath
5+
6+
# This program is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
19+
import random
20+
from .. import model
21+
22+
class TestBreak:
23+
def test_break_short(self):
24+
b = model.Break(
25+
model.BreakType.SHORT_BREAK,
26+
"test break",
27+
15,
28+
15,
29+
None,
30+
None
31+
)
32+
33+
assert b.is_short_break()
34+
assert not b.is_long_break()
35+
36+
def test_break_long(self):
37+
b = model.Break(
38+
model.BreakType.LONG_BREAK,
39+
"long break",
40+
75,
41+
60,
42+
None,
43+
None
44+
)
45+
46+
assert not b.is_short_break()
47+
assert b.is_long_break()
48+
49+
50+
class TestBreakQueue:
51+
def test_create_empty(self):
52+
config = {
53+
"short_breaks": [],
54+
"long_breaks": [],
55+
"short_break_interval": 15,
56+
"long_break_interval": 75,
57+
"long_break_duration": 60,
58+
"short_break_duration": 15,
59+
"random_order": False,
60+
}
61+
62+
context = {}
63+
64+
bq = model.BreakQueue(
65+
config,
66+
context
67+
)
68+
69+
assert bq.is_empty()
70+
assert bq.is_empty(model.BreakType.LONG_BREAK)
71+
assert bq.is_empty(model.BreakType.SHORT_BREAK)
72+
assert bq.next() is None
73+
assert bq.get_break() is None
74+
75+
76+
def get_bq_only_short(self, monkeypatch, random_seed=None):
77+
if random_seed is not None:
78+
random.seed(random_seed)
79+
80+
monkeypatch.setattr(model, "_", lambda message: "translated!: " + message, raising=False)
81+
82+
config = {
83+
"short_breaks": [
84+
{"name": "break 1"},
85+
{"name": "break 2"},
86+
{"name": "break 3"},
87+
],
88+
"long_breaks": [],
89+
"short_break_interval": 15,
90+
"long_break_interval": 75,
91+
"long_break_duration": 60,
92+
"short_break_duration": 15,
93+
"random_order": random_seed is not None,
94+
}
95+
96+
context = {
97+
"session": {},
98+
}
99+
100+
return model.BreakQueue(
101+
config,
102+
context
103+
)
104+
105+
106+
def get_bq_only_long(self, monkeypatch, random_seed=None):
107+
if random_seed is not None:
108+
random.seed(random_seed)
109+
110+
monkeypatch.setattr(model, "_", lambda message: "translated!: " + message, raising=False)
111+
112+
config = {
113+
"short_breaks": [],
114+
"long_breaks": [
115+
{"name": "long break 1"},
116+
{"name": "long break 2"},
117+
{"name": "long break 3"},
118+
],
119+
"short_break_interval": 15,
120+
"long_break_interval": 75,
121+
"long_break_duration": 60,
122+
"short_break_duration": 15,
123+
"random_order": random_seed is not None,
124+
}
125+
126+
context = {
127+
"session": {},
128+
}
129+
130+
return model.BreakQueue(
131+
config,
132+
context
133+
)
134+
135+
136+
def get_bq_full(self, monkeypatch, random_seed=None):
137+
if random_seed is not None:
138+
random.seed(random_seed)
139+
140+
monkeypatch.setattr(model, "_", lambda message: "translated!: " + message, raising=False)
141+
142+
config = {
143+
"short_breaks": [
144+
{"name": "break 1"},
145+
{"name": "break 2"},
146+
{"name": "break 3"},
147+
{"name": "break 4"},
148+
],
149+
"long_breaks": [
150+
{"name": "long break 1"},
151+
{"name": "long break 2"},
152+
{"name": "long break 3"},
153+
],
154+
"short_break_interval": 15,
155+
"long_break_interval": 75,
156+
"long_break_duration": 60,
157+
"short_break_duration": 15,
158+
"random_order": random_seed is not None,
159+
}
160+
161+
context = {
162+
"session": {},
163+
}
164+
165+
return model.BreakQueue(
166+
config,
167+
context
168+
)
169+
170+
171+
def test_create_only_short(self, monkeypatch):
172+
bq = self.get_bq_only_short(monkeypatch)
173+
174+
assert not bq.is_empty()
175+
assert not bq.is_empty(model.BreakType.SHORT_BREAK)
176+
assert bq.is_empty(model.BreakType.LONG_BREAK)
177+
178+
179+
def test_only_short_repeat_get_break_no_change(self, monkeypatch):
180+
bq = self.get_bq_only_short(monkeypatch)
181+
182+
next = bq.get_break()
183+
assert next.name == "translated!: break 1"
184+
185+
next = bq.get_break()
186+
assert next.name == "translated!: break 1"
187+
188+
assert not bq.is_long_break()
189+
190+
191+
def test_only_short_next_break(self, monkeypatch):
192+
bq = self.get_bq_only_short(monkeypatch)
193+
194+
next = bq.get_break()
195+
assert next.name == "translated!: break 1"
196+
197+
assert bq.next().name == "translated!: break 2"
198+
assert bq.next().name == "translated!: break 3"
199+
assert bq.next().name == "translated!: break 1"
200+
assert bq.next().name == "translated!: break 2"
201+
assert bq.next().name == "translated!: break 3"
202+
assert bq.next().name == "translated!: break 1"
203+
assert bq.next().name == "translated!: break 2"
204+
assert bq.next().name == "translated!: break 3"
205+
206+
207+
def test_only_short_next_break_random(self, monkeypatch):
208+
random_seed = 5
209+
bq = self.get_bq_only_short(monkeypatch, random_seed)
210+
211+
next = bq.get_break()
212+
assert next.name == "translated!: break 3"
213+
214+
assert bq.next().name == "translated!: break 2"
215+
assert bq.next().name == "translated!: break 1"
216+
assert bq.next().name == "translated!: break 3"
217+
assert bq.next().name == "translated!: break 1"
218+
219+
220+
def test_create_only_long(self, monkeypatch):
221+
bq = self.get_bq_only_long(monkeypatch)
222+
223+
assert not bq.is_empty()
224+
assert not bq.is_empty(model.BreakType.LONG_BREAK)
225+
assert bq.is_empty(model.BreakType.SHORT_BREAK)
226+
227+
228+
def test_only_long_repeat_get_break_no_change(self, monkeypatch):
229+
bq = self.get_bq_only_long(monkeypatch)
230+
231+
next = bq.get_break()
232+
assert next.name == "translated!: long break 1"
233+
234+
next = bq.get_break()
235+
assert next.name == "translated!: long break 1"
236+
237+
assert bq.is_long_break()
238+
239+
240+
def test_only_long_next_break(self, monkeypatch):
241+
bq = self.get_bq_only_long(monkeypatch)
242+
243+
next = bq.get_break()
244+
assert next.name == "translated!: long break 1"
245+
246+
assert bq.next().name == "translated!: long break 2"
247+
assert bq.next().name == "translated!: long break 3"
248+
assert bq.next().name == "translated!: long break 1"
249+
assert bq.next().name == "translated!: long break 2"
250+
assert bq.next().name == "translated!: long break 3"
251+
assert bq.next().name == "translated!: long break 1"
252+
assert bq.next().name == "translated!: long break 2"
253+
assert bq.next().name == "translated!: long break 3"
254+
255+
256+
def test_only_long_next_break_random(self, monkeypatch):
257+
random_seed = 5
258+
bq = self.get_bq_only_long(monkeypatch, random_seed)
259+
260+
next = bq.get_break()
261+
assert next.name == "translated!: long break 3"
262+
263+
assert bq.next().name == "translated!: long break 2"
264+
assert bq.next().name == "translated!: long break 1"
265+
assert bq.next().name == "translated!: long break 3"
266+
assert bq.next().name == "translated!: long break 1"
267+
268+
269+
def test_create_full(self, monkeypatch):
270+
bq = self.get_bq_full(monkeypatch)
271+
272+
assert not bq.is_empty()
273+
assert not bq.is_empty(model.BreakType.LONG_BREAK)
274+
assert not bq.is_empty(model.BreakType.SHORT_BREAK)
275+
276+
277+
def test_full_repeat_get_break_no_change(self, monkeypatch):
278+
bq = self.get_bq_full(monkeypatch)
279+
280+
next = bq.get_break()
281+
assert next.name == "translated!: break 1"
282+
283+
next = bq.get_break()
284+
assert next.name == "translated!: break 1"
285+
286+
assert not bq.is_long_break()
287+
288+
289+
def test_full_next_break(self, monkeypatch):
290+
bq = self.get_bq_full(monkeypatch)
291+
292+
next = bq.get_break()
293+
assert next.name == "translated!: break 1"
294+
assert not bq.is_long_break()
295+
296+
assert bq.next().name == "translated!: break 2"
297+
assert bq.next().name == "translated!: break 3"
298+
assert bq.next().name == "translated!: break 4"
299+
assert bq.next().name == "translated!: long break 1"
300+
assert bq.is_long_break()
301+
assert bq.next().name == "translated!: break 1"
302+
assert not bq.is_long_break()
303+
assert bq.next().name == "translated!: break 2"
304+
assert bq.next().name == "translated!: break 3"
305+
assert bq.next().name == "translated!: break 4"
306+
assert bq.next().name == "translated!: long break 2"
307+
assert bq.next().name == "translated!: break 1"
308+
assert bq.next().name == "translated!: break 2"
309+
assert bq.next().name == "translated!: break 3"
310+
assert bq.next().name == "translated!: break 4"
311+
assert bq.next().name == "translated!: long break 3"
312+
assert bq.next().name == "translated!: break 1"
313+
assert bq.next().name == "translated!: break 2"
314+
assert bq.next().name == "translated!: break 3"
315+
assert bq.next().name == "translated!: break 4"
316+
assert bq.next().name == "translated!: long break 1"
317+
assert bq.next().name == "translated!: break 1"
318+
assert bq.next().name == "translated!: break 2"
319+
assert bq.next().name == "translated!: break 3"
320+
assert bq.next().name == "translated!: break 4"
321+
assert bq.next().name == "translated!: long break 2"
322+
assert bq.next().name == "translated!: break 1"
323+
assert bq.next().name == "translated!: break 2"
324+
assert bq.next().name == "translated!: break 3"
325+
assert bq.next().name == "translated!: break 4"
326+
assert bq.next().name == "translated!: long break 3"
327+
assert bq.next().name == "translated!: break 1"
328+
assert bq.next().name == "translated!: break 2"
329+
assert bq.next().name == "translated!: break 3"
330+
assert bq.next().name == "translated!: break 4"
331+
assert bq.next().name == "translated!: long break 1"
332+
333+
334+
def test_full_next_break_random(self, monkeypatch):
335+
random_seed = 5
336+
bq = self.get_bq_full(monkeypatch, random_seed)
337+
338+
next = bq.get_break()
339+
assert next.name == "translated!: break 1"
340+
341+
assert bq.next().name == "translated!: break 2"
342+
assert bq.next().name == "translated!: break 4"
343+
assert bq.next().name == "translated!: break 3"
344+
assert bq.next().name == "translated!: long break 3"
345+
assert bq.next().name == "translated!: break 2"
346+
assert bq.next().name == "translated!: break 1"
347+
assert bq.next().name == "translated!: break 4"
348+
assert bq.next().name == "translated!: break 3"
349+
assert bq.next().name == "translated!: long break 2"
350+
assert bq.next().name == "translated!: break 2"
351+
assert bq.next().name == "translated!: break 4"
352+
assert bq.next().name == "translated!: break 1"
353+
assert bq.next().name == "translated!: break 3"
354+
assert bq.next().name == "translated!: long break 1"

0 commit comments

Comments
 (0)