File size: 7,193 Bytes
bff9db3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""
ๅ˜ไฝ“ใƒ†ใ‚นใƒˆ: utils/shift_patterns.py
ๅ“่ณช็‰นๆ€ง: ๆฉŸ่ƒฝ้ฉๅˆๆ€ง๏ผˆISO/IEC 25010 ยง4.2.1๏ผ‰
ใƒ†ใ‚นใƒˆใƒฌใƒ™ใƒซ: ๅ˜ไฝ“ใƒ†ใ‚นใƒˆ
"""
import pytest
from utils.shift_patterns import (
    ShiftPattern,
    ALL_PATTERNS,
    PATTERN_MAP,
    is_long_breakfast_pattern,
    default_pattern_from_fixed,
    BREAKFAST_START,
    BREAKFAST_END,
    DINNER_START,
    DINNER_END,
    MIN_OVERLAP_HRS,
    LONG_BREAKFAST_END_HOUR,
)


class TestShiftPatternToHour:
    def test_whole_hour(self):
        sp = ShiftPattern("t", "ใƒ†ใ‚นใƒˆ", "06:00", "10:00")
        assert sp.start_hour() == 6.0

    def test_half_hour(self):
        sp = ShiftPattern("t", "ใƒ†ใ‚นใƒˆ", "06:30", "11:30")
        assert sp.start_hour() == 6.5

    def test_quarter_hour(self):
        sp = ShiftPattern("t", "ใƒ†ใ‚นใƒˆ", "05:45", "14:45")
        assert sp.start_hour() == pytest.approx(5.75)
        assert sp.end_hour() == pytest.approx(14.75)

    def test_none_returns_none(self):
        sp = ShiftPattern("t", "ใƒ†ใ‚นใƒˆ", None, None)
        assert sp.start_hour() is None
        assert sp.end_hour() is None


class TestCoversBreakfast:
    """ๆœ้ฃŸๆ™‚้–“ๅธฏ(6:00-11:00)ใ‚ซใƒใƒผๅˆคๅฎš"""

    def test_breakfast_standard_covers(self):
        p = PATTERN_MAP["b_std"]   # 6:30-11:30 โ†’ ้‡่ค‡4.5h
        assert p.covers_breakfast() is True

    def test_breakfast_short_covers(self):
        p = PATTERN_MAP["b_short"]  # 6:00-10:00 โ†’ ้‡่ค‡4h
        assert p.covers_breakfast() is True

    def test_dinner_only_no_cover(self):
        p = PATTERN_MAP["d_std1"]  # 17:00-22:30
        assert p.covers_breakfast() is False

    def test_double_force_both(self):
        p = PATTERN_MAP["double"]
        assert p.covers_breakfast() is True

    def test_custom_no_times_no_cover(self):
        p = PATTERN_MAP["custom"]
        assert p.covers_breakfast() is False

    def test_boundary_exactly_min_overlap(self):
        # 9:00-11:00 โ†’ ้‡่ค‡ exactly 2.0h โ†’ ใ‚ซใƒใƒผ
        sp = ShiftPattern("t", "ใƒ†ใ‚นใƒˆ", "09:00", "11:00")
        assert sp.covers_breakfast() is True

    def test_below_min_overlap_no_cover(self):
        # 9:01-11:00 โ†’ ้‡่ค‡ 1.983h < 2.0h โ†’ ใ‚ซใƒใƒผใชใ—
        sp = ShiftPattern("t", "ใƒ†ใ‚นใƒˆ", "09:01", "11:00")
        assert sp.covers_breakfast() is False

    def test_dinner_full_covers_breakfast_if_start_early_enough(self):
        # ้€šใ—ๆ—ฉ็•ช 13:00-22:30 โ†’ ๆœ้ฃŸใจใฎ้‡่ค‡ใชใ—
        p = PATTERN_MAP["d_full1"]
        assert p.covers_breakfast() is False


class TestCoversDinner:
    """ใƒ‡ใ‚ฃใƒŠใƒผๆ™‚้–“ๅธฏ(17:00-23:00)ใ‚ซใƒใƒผๅˆคๅฎš"""

    def test_dinner_std_covers(self):
        p = PATTERN_MAP["d_std1"]  # 17:00-22:30 โ†’ ้‡่ค‡5.5h
        assert p.covers_dinner() is True

    def test_breakfast_no_cover(self):
        p = PATTERN_MAP["b_std"]
        assert p.covers_dinner() is False

    def test_double_force_both(self):
        p = PATTERN_MAP["double"]
        assert p.covers_dinner() is True

    def test_custom_no_times_no_cover(self):
        p = PATTERN_MAP["custom"]
        assert p.covers_dinner() is False

    def test_boundary_exactly_min_overlap(self):
        # 17:00-19:00 โ†’ ้‡่ค‡ exactly 2.0h โ†’ ใ‚ซใƒใƒผ
        sp = ShiftPattern("t", "ใƒ†ใ‚นใƒˆ", "17:00", "19:00")
        assert sp.covers_dinner() is True

    def test_below_min_overlap_no_cover(self):
        # 17:01-19:00 โ†’ ้‡่ค‡ 1.983h < 2.0h
        sp = ShiftPattern("t", "ใƒ†ใ‚นใƒˆ", "17:01", "19:00")
        assert sp.covers_dinner() is False

    def test_through_shift_covers_dinner(self):
        # ้€šใ—้…็•ช 14:00-23:00
        p = PATTERN_MAP["d_full2"]
        assert p.covers_dinner() is True


class TestDurationHours:
    """ๅฎŸๅ‹คๅ‹™ๆ™‚้–“่จˆ็ฎ—"""

    def test_double_returns_11(self):
        assert PATTERN_MAP["double"].duration_hours() == 11.0

    def test_custom_no_times_returns_zero(self):
        assert PATTERN_MAP["custom"].duration_hours() == 0.0

    def test_b_std_5hours(self):
        p = PATTERN_MAP["b_std"]  # 6:30-11:30
        assert p.duration_hours() == pytest.approx(5.0)

    def test_d_std1_5point5hours(self):
        p = PATTERN_MAP["d_std1"]  # 17:00-22:30
        assert p.duration_hours() == pytest.approx(5.5)

    def test_b_long_9hours(self):
        p = PATTERN_MAP["b_long"]  # 5:45-14:45
        assert p.duration_hours() == pytest.approx(9.0)


class TestTimeRangeLabel:
    """่กจ็คบ็”จๆ™‚ๅˆปๆ–‡ๅญ—ๅˆ—"""

    def test_double(self):
        assert PATTERN_MAP["double"].time_range_label() == "6:00ใ€œ11:00 / 17:00ใ€œ23:00"

    def test_b_std(self):
        assert PATTERN_MAP["b_std"].time_range_label() == "06:30ใ€œ11:30"

    def test_custom_no_times(self):
        assert PATTERN_MAP["custom"].time_range_label() == ""


class TestAllPatternsConsistency:
    """ALL_PATTERNSใฎๆ•ดๅˆๆ€งใƒใ‚งใƒƒใ‚ฏ"""

    def test_all_ids_unique(self):
        ids = [p.id for p in ALL_PATTERNS]
        assert len(ids) == len(set(ids))

    def test_pattern_map_matches_all_patterns(self):
        for p in ALL_PATTERNS:
            assert PATTERN_MAP[p.id] is p

    def test_non_custom_patterns_have_label(self):
        for p in ALL_PATTERNS:
            assert p.label, f"{p.id} has empty label"


class TestIsLongBreakfastPattern:
    """ๆœ้ฃŸใƒญใƒณใ‚ฐๅ‹คๅ‹™ๅˆคๅฎš"""

    def test_b_long_is_long(self):
        # 5:45-14:45 โ†’ ๆœ้ฃŸใ‚ซใƒใƒผใƒป็ต‚ไบ†14.75 >= 14.0
        assert is_long_breakfast_pattern("b_long") is True

    def test_b_8h_is_long(self):
        # 6:00-14:00 โ†’ ็ต‚ไบ†14.0 >= 14.0
        assert is_long_breakfast_pattern("b_8h") is True

    def test_b_half_is_long(self):
        # 6:30-15:30 โ†’ ็ต‚ไบ†15.5 >= 14.0
        assert is_long_breakfast_pattern("b_half") is True

    def test_b_std_not_long(self):
        # 6:30-11:30 โ†’ ็ต‚ไบ†11.5 < 14.0
        assert is_long_breakfast_pattern("b_std") is False

    def test_b_short_not_long(self):
        # 6:00-10:00 โ†’ ็ต‚ไบ†10.0 < 14.0
        assert is_long_breakfast_pattern("b_short") is False

    def test_dinner_not_long(self):
        assert is_long_breakfast_pattern("d_std1") is False

    def test_none_pattern_id(self):
        assert is_long_breakfast_pattern(None) is False

    def test_custom_long(self):
        assert is_long_breakfast_pattern("custom", "06:00", "15:00") is True

    def test_custom_short(self):
        assert is_long_breakfast_pattern("custom", "06:00", "11:00") is False

    def test_custom_missing_times(self):
        assert is_long_breakfast_pattern("custom") is False

    def test_custom_dinner_not_long(self):
        assert is_long_breakfast_pattern("custom", "17:00", "23:00") is False


class TestDefaultPatternFromFixed:
    """ๅ›บๅฎšใƒ•ใƒฉใ‚ฐใ‹ใ‚‰ใƒ‡ใƒ•ใ‚ฉใƒซใƒˆใƒ‘ใ‚ฟใƒผใƒณIDๅค‰ๆ›"""

    def test_both_true_returns_double(self):
        assert default_pattern_from_fixed(True, True) == "double"

    def test_breakfast_only_returns_b_std(self):
        assert default_pattern_from_fixed(True, False) == "b_std"

    def test_dinner_only_returns_d_std1(self):
        assert default_pattern_from_fixed(False, True) == "d_std1"

    def test_both_false_returns_none(self):
        assert default_pattern_from_fixed(False, False) is None