Issues (28)

tests/test_timer.py (8 issues)

1
from datetime import datetime, timedelta, timezone
2
from races_for_testing import time_ago
3
from gadgets.timer import Timer
4
from users_for_testing import get_test_entrant, get_test_entrants
5
from races_for_testing import get_test_race
6
7
8
def get_test_timer():
9
    timer = Timer()
10
    timer.source_name = "Timer source"
11
    timer.enabled = True
12
    return timer
13
14
15 View Code Duplication
def test_timer_prerace(random_users):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
16
    timer = get_test_timer()
17
    race = get_test_race(
18
        status_value="open", version=12,
19
        started_at=None, start_delay=timedelta(seconds=15, microseconds=1),
20
        entrants=get_test_entrants(random_users)
21
    )
22
    color, text = timer.get_timer_text(race, "")
23
    assert color is None
24
    assert text == "-0:00:15.0"
25
26
    timer.use_podium_colors = True
27
    color, text = timer.get_timer_text(race, "")
28
    assert color is timer.pre_color
29
    assert text == "-0:00:15.0"
30
31
32 View Code Duplication
def test_timer_counting_down(random_users):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
33
    timer = get_test_timer()
34
    race = get_test_race(
35
        status_value="pending", version=13, started_at=time_ago(seconds=-5.01),
36
        start_delay=timedelta(seconds=15.0),
37
        entrants=get_test_entrants(random_users)
38
    )
39
    color, text = timer.get_timer_text(race, "")
40
    assert color is None
41
    assert text == "-0:00:05.0"
42
43
    timer.use_podium_colors = True
44
    color, text = timer.get_timer_text(race, "")
45
    assert color is timer.pre_color
46
47
48 View Code Duplication
def test_timer_midrace_no_entrant(random_users):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
49
    timer = get_test_timer()
50
    race = get_test_race(
51
        status_value="in_progress", version=15, entrants_count=2,
52
        started_at=datetime.now(timezone.utc)-timedelta(hours=1, minutes=20),
53
        entrants=get_test_entrants(random_users)
54
    )
55
    color, text = timer.get_timer_text(race, "")
56
    assert color is None
57
    # hope this always runs in less than 0.1 seconds XD
58
    assert text == "1:20:00.0"
59
    timer.use_podium_colors = True
60
    color, text = timer.get_timer_text(race, "")
61
    assert color is timer.racing_color
62
63
64
def test_timer_midrace_w_entrant(random_users):
65
    entrant = get_test_entrant(
66
        next(random_users), status_value="finished",
67
        finished_at=datetime.now(timezone.utc),
68
        finish_time=timedelta(hours=1, minutes=42, seconds=6.9)
69
    )
70
    entrants = get_test_entrants(random_users, entrant)
71
    race = get_test_race(version=16, entrants=entrants)
72
    timer = get_test_timer()
73
    color, text = timer.get_timer_text(race, entrant.user.full_name)
74
    assert color is None
75
    assert text == "1:42:06.9"
76
    timer.use_podium_colors = True
77
    color, text = timer.get_timer_text(race, entrant.user.full_name)
78
    assert color == timer.racing_color
79
80
81 View Code Duplication
def test_timer_midrace_w_user_not_in_race(random_users):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
82
    started_at = (
83
        datetime.now(timezone.utc) -
84
        timedelta(hours=1, minutes=42, seconds=42.0)
85
    )
86
    race = get_test_race(
87
        version=17, entrants=get_test_entrants(random_users),
88
        started_at=started_at
89
    )
90
    timer = get_test_timer()
91
    color, text = timer.get_timer_text(race, "user_not_in_race#6969")
92
    assert color is None
93
    assert text == "1:42:42.0"
94
    timer.use_podium_colors = True
95
    color, text = timer.get_timer_text(race, "user_not_in_race#6969")
96
    assert color == timer.racing_color
97
98
99
def test_timer_race_cancelled(random_users):
100
    race = get_test_race(
101
        status_value="cancelled", entrants=get_test_entrants(random_users),
102
        cancelled_at=datetime.now(timezone.utc)+timedelta(minutes=20)
103
    )
104
    timer = get_test_timer()
105
    color, text = timer.get_timer_text(race, "")
106
    assert color is None
107
    assert text == "--:--:--.-"
108
    timer.use_podium_colors = True
109
    color, text = timer.get_timer_text(race, "")
110
    # currently, the timer is always red for canceled or dq races
111
    assert color is timer.cancel_dq_color
112
113
114
def test_timer_user_dqed(random_users):
115
    entrant = get_test_entrant(next(random_users), status_value="dq")
116
    entrants = get_test_entrants(random_users, entrant)
117
    race = get_test_race(entrants=entrants)
118
    timer = get_test_timer()
119
    color, text = timer.get_timer_text(race, entrant.user.full_name)
120
    assert color is None
121
    assert text == "--:--:--.-"
122
    timer.use_podium_colors = True
123
    color, text = timer.get_timer_text(race, entrant.user.full_name)
124
    assert color == timer.cancel_dq_color
125
126
127 View Code Duplication
def test_user_finished_first(random_users):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
128
    entrant = get_test_entrant(
129
        next(random_users), status_value="finished",
130
        finished_at=datetime.now(timezone.utc), place=1,
131
        finish_time=timedelta(hours=1, minutes=9, seconds=42, microseconds=1)
132
    )
133
    entrants = get_test_entrants(random_users, entrant)
134
    race = get_test_race(
135
        started_at=datetime.now(timezone.utc)-entrant.finish_time,
136
        entrants_count=5, entrants=entrants
137
    )
138
    timer = get_test_timer()
139
    color, text = timer.get_timer_text(race, entrant.user.full_name)
140
    assert color is None
141
    assert text == "1:09:42.0"
142
    timer.use_podium_colors = True
143
    color, text = timer.get_timer_text(race, entrant.user.full_name)
144
    assert color == timer.first_color
145
146
147 View Code Duplication
def test_user_finished_second(random_users):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
148
    entrant = get_test_entrant(
149
        next(random_users), status_value="finished",
150
        finished_at=datetime.now(timezone.utc), place=2,
151
        finish_time=timedelta(hours=1, minutes=9, seconds=42, microseconds=1)
152
    )
153
    entrants = get_test_entrants(random_users, entrant)
154
    race = get_test_race(
155
        started_at=datetime.now(timezone.utc)-entrant.finish_time,
156
        entrants_count=5, entrants=entrants
157
    )
158
    timer = get_test_timer()
159
    color, text = timer.get_timer_text(race, entrant.user.full_name)
160
    assert color is None
161
    assert text == "1:09:42.0"
162
    timer.use_podium_colors = True
163
    color, text = timer.get_timer_text(race, entrant.user.full_name)
164
    assert color == timer.second_color
165
166
167 View Code Duplication
def test_user_finished_third(random_users):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
168
    entrant = get_test_entrant(
169
        next(random_users), status_value="finished",
170
        finished_at=datetime.now(timezone.utc), place=3,
171
        finish_time=timedelta(hours=1, minutes=9, seconds=42, microseconds=1)
172
    )
173
    entrants = get_test_entrants(random_users, entrant)
174
    race = get_test_race(
175
        started_at=datetime.now(timezone.utc)-entrant.finish_time,
176
        entrants_count=5, entrants=entrants
177
    )
178
    timer = get_test_timer()
179
    color, text = timer.get_timer_text(race, entrant.user.full_name)
180
    assert color is None
181
    assert text == "1:09:42.0"
182
    timer.use_podium_colors = True
183
    color, text = timer.get_timer_text(race, entrant.user.full_name)
184
    assert color == timer.third_color
185
186
187 View Code Duplication
def test_user_finished_other(random_users):
0 ignored issues
show
This code seems to be duplicated in your project.
Loading history...
188
    entrant = get_test_entrant(
189
        next(random_users), status_value="finished",
190
        finished_at=datetime.now(timezone.utc), place=5,
191
        finish_time=timedelta(hours=1, minutes=9, seconds=42, microseconds=1)
192
    )
193
    entrants = get_test_entrants(random_users, entrant)
194
    race = get_test_race(
195
        started_at=datetime.now(timezone.utc)-entrant.finish_time,
196
        entrants_count=5, entrants=entrants
197
    )
198
    timer = get_test_timer()
199
    color, text = timer.get_timer_text(race, entrant.user.full_name)
200
    assert color is None
201
    assert text == "1:09:42.0"
202
    timer.use_podium_colors = True
203
    color, text = timer.get_timer_text(race, entrant.user.full_name)
204
    assert color == timer.finished_color
205