Skip to content

Commit fd5392e

Browse files
committed
Test wizards slots computation
1 parent 6c6a9c7 commit fd5392e

File tree

3 files changed

+123
-19
lines changed

3 files changed

+123
-19
lines changed

test/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
11
import os
2+
from functools import wraps
3+
from peewee import SqliteDatabase
24
from telegram import Bot
35

6+
from pycamp_bot.models import Pycampista, Slot, Pycamp
7+
8+
49

510
bot = Bot(token=os.environ['TOKEN'])
11+
12+
13+
# use an in-memory SQLite for tests.
14+
test_db = SqliteDatabase(':memory:')
15+
16+
MODELS = [Pycampista, Slot, Pycamp]
17+
18+
19+
# Bind the given models to the db for the duration of wrapped block.
20+
def use_test_database(fn):
21+
@wraps(fn)
22+
def inner(self):
23+
with test_db.bind_ctx(MODELS):
24+
test_db.create_tables(MODELS)
25+
try:
26+
fn(self)
27+
finally:
28+
test_db.drop_tables(MODELS)
29+
return inner

test/test_pycampista.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
from datetime import datetime, timedelta
22
from pycamp_bot.models import Pycampista, Slot
3-
from peewee import SqliteDatabase
4-
from functools import wraps
5-
6-
MODELS = [Pycampista, Slot]
7-
8-
# use an in-memory SQLite for tests.
9-
test_db = SqliteDatabase(':memory:')
3+
from test.conftest import use_test_database, test_db, MODELS
104

115

126
# ---------------------------
@@ -29,18 +23,6 @@ def teardown_module(module):
2923
# If we wanted, we could re-bind the models to their original
3024
# database here. But for tests this is probably not necessary.
3125

32-
# Bind the given models to the db for the duration of wrapped block.
33-
def use_test_database(fn):
34-
@wraps(fn)
35-
def inner(self):
36-
with test_db.bind_ctx(MODELS):
37-
test_db.create_tables(MODELS)
38-
try:
39-
fn(self)
40-
finally:
41-
test_db.drop_tables(MODELS)
42-
return inner
43-
4426

4527
class TestPycampistaIsBusy:
4628

test/test_wizard.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
from datetime import datetime, timedelta
2+
from pycamp_bot.models import Pycamp
3+
from pycamp_bot.commands import wizard
4+
from test.conftest import use_test_database, test_db, MODELS
5+
6+
7+
# ---------------------------
8+
# Module Level Setup/TearDown
9+
# ---------------------------
10+
def setup_module(module):
11+
"""setup any state specific to the execution of the given module."""
12+
test_db.bind(MODELS, bind_refs=False, bind_backrefs=False)
13+
test_db.connect()
14+
15+
def teardown_module(module):
16+
"""teardown any state that was previously setup with a setup_module method."""
17+
# Not strictly necessary since SQLite in-memory databases only live
18+
# for the duration of the connection, and in the next step we close
19+
# the connection...but a good practice all the same.
20+
test_db.drop_tables(MODELS)
21+
22+
# Close connection to db.
23+
test_db.close()
24+
# If we wanted, we could re-bind the models to their original
25+
# database here. But for tests this is probably not necessary.
26+
27+
28+
class TestWizardScheduleSlots:
29+
30+
def init_pycamp(self):
31+
self.pycamp = Pycamp.create(
32+
headquarters="Narnia",
33+
init=datetime(2024,6,20),
34+
end=datetime(2024,6,24),
35+
)
36+
37+
@use_test_database
38+
def test_correct_number_of_slots_in_one_day(self):
39+
p = Pycamp.create(
40+
headquarters="Narnia",
41+
init=datetime(2024,6,20),
42+
end=datetime(2024,6,20),
43+
)
44+
slots = wizard.compute_wizards_slots(p)
45+
assert len(slots) == 0
46+
47+
@use_test_database
48+
def test_correct_number_of_slots_in_three_day(self):
49+
p = Pycamp.create(
50+
headquarters="Narnia",
51+
init=datetime(2024,6,20),
52+
end=datetime(2024,6,22,23,59,59,99),
53+
)
54+
slots = wizard.compute_wizards_slots(p)
55+
for i in slots:
56+
print(i)
57+
assert len(slots) == 20
58+
59+
60+
@use_test_database
61+
def test_no_slot_before_first_day_lunch(self):
62+
self.init_pycamp()
63+
lunch_time_end = datetime(
64+
self.pycamp.init.year,
65+
self.pycamp.init.month,
66+
self.pycamp.init.day,
67+
wizard.LUNCH_TIME_END_HOUR
68+
)
69+
for (start, end) in wizard.compute_wizards_slots(self.pycamp):
70+
assert lunch_time_end <= start
71+
72+
@use_test_database
73+
def test_no_slot_after_coding_time(self):
74+
self.init_pycamp()
75+
for (start, _) in wizard.compute_wizards_slots(self.pycamp):
76+
assert start.hour < wizard.WIZARD_TIME_END_HOUR
77+
78+
@use_test_database
79+
def test_no_slot_before_breakfast(self):
80+
self.init_pycamp()
81+
for (start, _) in wizard.compute_wizards_slots(self.pycamp):
82+
assert start.hour >= wizard.WIZARD_TIME_START_HOUR
83+
84+
85+
86+
@use_test_database
87+
def test_no_slot_after_last_day_lunch(self):
88+
self.init_pycamp()
89+
lunch_time_end = datetime(
90+
self.pycamp.end.year,
91+
self.pycamp.end.month,
92+
self.pycamp.end.day,
93+
wizard.LUNCH_TIME_END_HOUR
94+
)
95+
for (start, _) in wizard.compute_wizards_slots(self.pycamp):
96+
print(start)
97+
if start.day == self.pycamp.end.day:
98+
assert start >= lunch_time_end

0 commit comments

Comments
 (0)