Skip to content

Commit 76f9524

Browse files
committed
add some tests for define_wizards_schedule
1 parent 574982d commit 76f9524

File tree

2 files changed

+78
-4
lines changed

2 files changed

+78
-4
lines changed

test/conftest.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os
2+
3+
from datetime import datetime
24
from functools import wraps
35
from peewee import SqliteDatabase
6+
import pytest
47
from telegram import Bot
58

69
from pycamp_bot.models import Pycampista, Slot, Pycamp, WizardAtPycamp
@@ -27,3 +30,7 @@ def inner(self):
2730
finally:
2831
test_db.drop_tables(MODELS)
2932
return inner
33+
34+
35+
TEST_INIT_DATE = datetime(2024, 6, 20, 0, 0, 0)
36+
TEST_END_DATE = datetime(2024, 6, 24, 23, 59, 59, 99999)

test/test_wizard.py

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from datetime import datetime, timedelta
2-
from pycamp_bot.models import Pycamp
1+
from datetime import datetime
2+
from pycamp_bot.models import Pycamp, Pycampista, Slot
33
from pycamp_bot.commands import wizard
4-
from test.conftest import use_test_database, test_db, MODELS
4+
from test.conftest import use_test_database, test_db, MODELS, TEST_END_DATE, TEST_INIT_DATE
55

66

77
# ---------------------------
@@ -99,4 +99,71 @@ def test_no_slot_after_last_day_lunch(self):
9999

100100

101101
class TestDefineWizardsSchedule:
102-
pass
102+
103+
def init_pycamp(self):
104+
self.pycamp = Pycamp.create(
105+
headquarters="Narnia",
106+
init=datetime(2024,6,20),
107+
end=datetime(2024,6,24),
108+
)
109+
110+
# If no wizards, returns {}
111+
@use_test_database
112+
def test_no_wizards_then_return_empty_dict(self):
113+
self.init_pycamp()
114+
sched = wizard.define_wizards_schedule(self.pycamp)
115+
assert sched == {}
116+
117+
# all slots are asigned a wizard
118+
@use_test_database
119+
def test_all_slots_are_signed_a_wizard(self):
120+
self.init_pycamp()
121+
gandalf = Pycampista.create(username="gandalf", wizard=True)
122+
sched = wizard.define_wizards_schedule(self.pycamp)
123+
assert all(
124+
(isinstance(s, Pycampista) and s.wizard) for s in sched.values()
125+
)
126+
127+
# Wizards are not asigned to slots when they are busy
128+
@use_test_database
129+
def test_all_slots_are_signed_a_wizard(self):
130+
self.init_pycamp()
131+
gandalf = Pycampista.create(username="gandalf", wizard=True)
132+
merlin = Pycampista.create(username="merlin", wizard=True)
133+
for h in [9, 10, 11, 12]:
134+
# Create 3 slots where Gandalf is busy
135+
Slot.create(
136+
code = "A1",
137+
start=datetime(2024, 6, 21, h, 30, 0),
138+
current_wizard=gandalf
139+
)
140+
sched = wizard.define_wizards_schedule(self.pycamp)
141+
# Verify Gandalf is not assigned to slots where he is busy
142+
for (ini, end), w in sched.items():
143+
if gandalf.is_busy(ini, end):
144+
print(ini, end, w.username)
145+
assert w != gandalf
146+
147+
# If all wizards are busy in a slot, then one is asigned all the same
148+
@use_test_database
149+
def test_all_slots_are_signed_a_wizard(self):
150+
self.init_pycamp()
151+
gandalf = Pycampista.create(username="gandalf", wizard=True)
152+
merlin = Pycampista.create(username="merlin", wizard=True)
153+
for h in [9, 10, 11, 12]:
154+
# Create 3 slots where Gandalf AND Merlin are busy
155+
Slot.create(
156+
code = "A1",
157+
start=datetime(2024, 6, 21, h, 30, 0),
158+
current_wizard=gandalf
159+
)
160+
Slot.create(
161+
code = "A1",
162+
start=datetime(2024, 6, 21, h, 30, 0),
163+
current_wizard=merlin
164+
)
165+
sched = wizard.define_wizards_schedule(self.pycamp)
166+
167+
assert all(
168+
(isinstance(s, Pycampista) and s.wizard) for s in sched.values()
169+
)

0 commit comments

Comments
 (0)