|
| 1 | +from datetime import datetime, timedelta |
| 2 | +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:') |
| 10 | + |
| 11 | + |
| 12 | +# --------------------------- |
| 13 | +# Module Level Setup/TearDown |
| 14 | +# --------------------------- |
| 15 | +def setup_module(module): |
| 16 | + """setup any state specific to the execution of the given module.""" |
| 17 | + test_db.bind(MODELS, bind_refs=False, bind_backrefs=False) |
| 18 | + test_db.connect() |
| 19 | + |
| 20 | +def teardown_module(module): |
| 21 | + """teardown any state that was previously setup with a setup_module method.""" |
| 22 | + # Not strictly necessary since SQLite in-memory databases only live |
| 23 | + # for the duration of the connection, and in the next step we close |
| 24 | + # the connection...but a good practice all the same. |
| 25 | + test_db.drop_tables(MODELS) |
| 26 | + |
| 27 | + # Close connection to db. |
| 28 | + test_db.close() |
| 29 | + # If we wanted, we could re-bind the models to their original |
| 30 | + # database here. But for tests this is probably not necessary. |
| 31 | + |
| 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 | + |
| 44 | + |
| 45 | +class TestPycampistaIsBusy: |
| 46 | + |
| 47 | + @use_test_database |
| 48 | + def test_return_true_if_assigned_in_slot_equal_to_target_period(self): |
| 49 | + pycamper = Pycampista.create(username="pepe") |
| 50 | + slot = Slot.create(code = "A1", start=datetime.now(), current_wizard=pycamper) |
| 51 | + period = (slot.start, slot.get_end_time()) |
| 52 | + assert pycamper.is_busy(period) |
| 53 | + |
| 54 | + @use_test_database |
| 55 | + def test_return_true_if_assigned_in_slot_starting_at_target_period(self): |
| 56 | + pycamper = Pycampista.create(username="pepe") |
| 57 | + slot_start = datetime.now() |
| 58 | + Slot.create(code = "A1", start=slot_start, current_wizard=pycamper) |
| 59 | + period = (slot_start, slot_start + timedelta(minutes=5)) |
| 60 | + assert pycamper.is_busy(period) |
| 61 | + |
| 62 | + @use_test_database |
| 63 | + def test_return_true_if_assigned_in_slot_around_target_period(self): |
| 64 | + pycamper = Pycampista.create(username="pepe") |
| 65 | + slot_start = datetime.now() |
| 66 | + Slot.create(code = "A1", start=slot_start, current_wizard=pycamper) |
| 67 | + period_start = slot_start + timedelta(minutes=5) |
| 68 | + period = (period_start, period_start + timedelta(minutes=10)) |
| 69 | + assert pycamper.is_busy(period) |
| 70 | + |
| 71 | + @use_test_database |
| 72 | + def test_return_true_if_assigned_in_slot_ending_after_target_period_starts(self): |
| 73 | + pycamper = Pycampista.create(username="pepe") |
| 74 | + slot = Slot.create(code = "A1", start=datetime.now(), current_wizard=pycamper) |
| 75 | + period = ( |
| 76 | + slot.start + timedelta(minutes=5), |
| 77 | + slot.get_end_time() + timedelta(minutes=5), |
| 78 | + ) |
| 79 | + assert pycamper.is_busy(period) |
| 80 | + |
| 81 | + @use_test_database |
| 82 | + def test_return_true_if_assigned_in_slot_starting_before_target_period_ends(self): |
| 83 | + pycamper = Pycampista.create(username="pepe") |
| 84 | + slot = Slot.create(code = "A1", start=datetime.now(), current_wizard=pycamper) |
| 85 | + period = ( |
| 86 | + slot.start - timedelta(minutes=5), |
| 87 | + slot.start + timedelta(minutes=5), |
| 88 | + ) |
| 89 | + assert pycamper.is_busy(period) |
| 90 | + |
| 91 | + @use_test_database |
| 92 | + def test_return_false_if_assigned_in_slot_ending_before_target_period_starts(self): |
| 93 | + pycamper = Pycampista.create(username="pepe") |
| 94 | + slot = Slot.create(code = "A1", start=datetime.now(), current_wizard=pycamper) |
| 95 | + period = ( |
| 96 | + slot.get_end_time() + timedelta(seconds=1), |
| 97 | + slot.get_end_time() + timedelta(seconds=10), |
| 98 | + ) |
| 99 | + assert not pycamper.is_busy(period) |
| 100 | + |
| 101 | + @use_test_database |
| 102 | + def test_return_false_if_assigned_in_slot_start_after_target_period_ends(self): |
| 103 | + pycamper = Pycampista.create(username="pepe") |
| 104 | + slot = Slot.create(code = "A1", start=datetime.now(), current_wizard=pycamper) |
| 105 | + period = ( |
| 106 | + slot.start - timedelta(seconds=10), |
| 107 | + slot.start - timedelta(seconds=1), |
| 108 | + ) |
| 109 | + assert not pycamper.is_busy(period) |
0 commit comments