File size: 2,026 Bytes
9438a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytest
from solution import BookingError, Room


@pytest.fixture
def room():
    return Room("Mavi")


def test_defaults_and_invalid_hours():
    r = Room("Kırmızı")
    assert (r.open_at, r.close_at, r.bookings) == (540, 1080, [])
    with pytest.raises(BookingError):
        Room("X", open_at=600, close_at=600)


def test_book_returns_count_and_keeps_sorted(room):
    assert room.book(720, 780, "b") == 1
    assert room.book(600, 660, "a") == 2
    assert [b[2] for b in room.bookings] == ["a", "b"]


def test_adjacent_bookings_do_not_conflict(room):
    room.book(600, 660, "a")
    room.book(660, 720, "b")
    room.book(540, 600, "c")
    assert len(room.bookings) == 3


def test_overlap_rejected(room):
    room.book(600, 660, "a")
    for s, e in [(630, 690), (570, 610), (600, 660), (610, 620)]:
        with pytest.raises(BookingError):
            room.book(s, e, "x")


def test_invalid_interval_and_hours(room):
    with pytest.raises(BookingError):
        room.book(700, 700, "x")
    with pytest.raises(BookingError):
        room.book(500, 560, "x")
    with pytest.raises(BookingError):
        room.book(1050, 1090, "x")


def test_is_free(room):
    room.book(600, 660, "a")
    assert room.is_free(660, 700) and room.is_free(540, 600)
    assert not room.is_free(650, 700)


def test_cancel_removes_all_of_owner(room):
    room.book(600, 660, "a")
    room.book(700, 760, "a")
    room.book(800, 860, "b")
    assert room.cancel("a") == 2
    assert room.cancel("zz") == 0
    assert [b[2] for b in room.bookings] == ["b"]


def test_free_slots_with_unordered_bookings(room):
    room.book(900, 960, "b")
    room.book(600, 660, "a")
    assert room.free_slots() == [(540, 600), (660, 900), (960, 1080)]


def test_free_slots_min_length_inclusive(room):
    room.book(570, 1050, "a")
    assert room.free_slots(min_length=30) == [(540, 570), (1050, 1080)]
    assert room.free_slots(min_length=31) == []


def test_free_slots_empty_room(room):
    assert room.free_slots() == [(540, 1080)]