Passed
Branch master (d83e06)
by manny
01:38
created

test_timer.test_timer_midrace()   A

Complexity

Conditions 1

Size

Total Lines 30
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 27
dl 0
loc 30
rs 9.232
c 0
b 0
f 0
cc 1
nop 0
1
from datetime import datetime, time, timedelta, timezone
2
from gadgets.timer import Timer
3
from models.race import Entrant, Goal, Race, Status
4
from users_for_testing import get_test_user, get_test_entrant
5
from categories_for_testing import get_test_race_category
6
7
8
def get_test_race(status_value="in_progress", version=1, entrants_count=2, started_at=datetime.now(timezone.utc),
9
                start_delay=timedelta(seconds=-15), opened_at=datetime.now(timezone.utc), ended_at=None, 
10
                cancelled_at: datetime = None, entrant: Entrant = None) -> Race:
11
    test_race= Race(name="",
12
                status=Status(value=status_value,
13
                              verbose_value="", help_text=""),
14
                category=get_test_race_category(),
15
                goal=Goal("", False),
16
                info="A test race",
17
                url="",
18
                data_url="",
19
                websocket_url="",
20
                websocket_bot_url="",
21
                websocket_oauth_url="",
22
                entrants_count=entrants_count,
23
                entrants_count_inactive=0,
24
                entrants_count_finished=0,
25
                entrants=(list(get_test_entrant()
26
                          for x in range(0, entrants_count))),
27
                version=version,
28
                started_at=started_at,
29
                start_delay=start_delay,
30
                ended_at=ended_at,
31
                cancelled_at=cancelled_at,
32
                unlisted=False,
33
                time_limit=timedelta(days=1),
34
                streaming_required=True,
35
                auto_start=True,
36
                opened_by=get_test_user(),
37
                opened_at=opened_at,
38
                monitors=[],
39
                recordable=True,
40
                recorded=False,
41
                recorded_by=None,
42
                allow_comments=True,
43
                hide_comments=False,
44
                allow_midrace_chat=True
45
                )
46
    if entrant is not None:
47
        if not entrant in test_race.entrants:
48
            test_race.entrants.pop()
49
            test_race.entrants.append(entrant)
50
    return test_race
51
52
def get_test_timer():
53
    timer = Timer()
54
    timer.source_name = "Timer source"
55
    timer.enabled = True
56
    return timer
57
58
59
def test_timer_prerace():
60
    timer = get_test_timer()
61
    race = get_test_race(status_value="open", version=12,
62
                         started_at=None, start_delay=timedelta(seconds=-15))
63
    color, text = timer.get_timer_text(race, "")
64
    assert color is None
65
    assert text == "-0:00:15.0"
66
67
    timer.use_podium_colors = True
68
    color, text = timer.get_timer_text(race, "")
69
    assert color is timer.pre_color
70
    assert text == "-0:00:15.0"
71
72
def test_timer_midrace_no_entrant():
73
    timer = get_test_timer()
74
    race = get_test_race(status_value="in_progress", version=15, entrants_count=2,
75
        started_at=datetime.now(timezone.utc)-timedelta(hours=1, minutes=20))
76
    color, text = timer.get_timer_text(race, "")
77
    assert color is None
78
    # hope this always runs in less than 0.1 seconds XD
79
    assert text == "1:20:00.0"
80
    timer.use_podium_colors = True
81
    color, text = timer.get_timer_text(race, "")
82
    assert color is timer.racing_color
83
84
85 View Code Duplication
def test_timer_midrace_w_entrant():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
86
    timer = get_test_timer()
87
    entrant = get_test_entrant(status_value="finished", finished_at=datetime.now(timezone.utc), finish_time=timedelta(hours=1, minutes=42, seconds=6.9))
88
    race = get_test_race(version=16, entrants_count=2, entrant=entrant)
89
    color, text = timer.get_timer_text(race, entrant.user.full_name)
90
    assert color is None
91
    assert text == "1:42:06.9"
92
    timer.use_podium_colors = True
93
    color, text = timer.get_timer_text(race, entrant.user.full_name)
94
    assert color == timer.racing_color
95
96
def test_timer_midrace_w_user_not_in_race():
97
    timer = get_test_timer()
98
    race = get_test_race(version=17, entrants_count=2, started_at=datetime.now(timezone.utc)-timedelta(hours=1, minutes=42, seconds=42.0))
99
    color, text = timer.get_timer_text(race, "user_not_in_race#6969")
100
    assert color is None
101
    assert text == "1:42:42.0"
102
    timer.use_podium_colors = True
103
    color, text = timer.get_timer_text(race, "user_not_in_race#6969")
104
    assert color == timer.racing_color
105
106
def test_timer_race_cancelled():
107
    timer = get_test_timer()
108
    race = get_test_race(status_value="cancelled", cancelled_at=datetime.now(timezone.utc)+timedelta(minutes=20))
109
    color, text = timer.get_timer_text(race, "")
110
    assert color is None
111
    assert text == "--:--:--.-"
112
    timer.use_podium_colors = True
113
    color, text = timer.get_timer_text(race, "")
114
    # currently, the timer is always red for canceled or dq races
115
    assert color is timer.cancel_dq_color
116
117
def test_timer_user_dqed():
118
    timer = get_test_timer()
119
    entrant = get_test_entrant(status_value="dq")
120
    race = get_test_race(entrant=entrant)
121
    color, text = timer.get_timer_text(race, entrant.user.full_name)
122
    assert color is None
123
    assert text == "--:--:--.-"
124
    timer.use_podium_colors = True
125
    color, text = timer.get_timer_text(race, entrant.user.full_name)
126
    assert color == timer.cancel_dq_color
127
128 View Code Duplication
def test_user_finished_first():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
129
    timer = get_test_timer()
130
    entrant = get_test_entrant(status_value="finished", finished_at=datetime.now(timezone.utc), finish_time=timedelta(hours=1, minutes=9, seconds=42, microseconds=1), place=1)
131
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5)
132
    color, text = timer.get_timer_text(race, entrant.user.full_name)
133
    assert color is None
134
    assert text == "1:09:42.0"
135
    timer.use_podium_colors = True
136
    color, text = timer.get_timer_text(race, entrant.user.full_name)
137
    assert color == timer.first_color
138
139 View Code Duplication
def test_user_finished_first():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
140
    timer = get_test_timer()
141
    entrant = get_test_entrant(status_value="finished", finished_at=datetime.now(timezone.utc), finish_time=timedelta(hours=1, minutes=9, seconds=42, microseconds=1), place=2)
142
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5)
143
    color, text = timer.get_timer_text(race, entrant.user.full_name)
144
    assert color is None
145
    assert text == "1:09:42.0"
146
    timer.use_podium_colors = True
147
    color, text = timer.get_timer_text(race, entrant.user.full_name)
148
    assert color == timer.second_color
149
150 View Code Duplication
def test_user_finished_first():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
151
    timer = get_test_timer()
152
    entrant = get_test_entrant(status_value="finished", finished_at=datetime.now(timezone.utc), finish_time=timedelta(hours=1, minutes=9, seconds=42, microseconds=1), place=3)
153
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5)
154
    color, text = timer.get_timer_text(race, entrant.user.full_name)
155
    assert color is None
156
    assert text == "1:09:42.0"
157
    timer.use_podium_colors = True
158
    color, text = timer.get_timer_text(race, entrant.user.full_name)
159
    assert color == timer.third_color
160
161 View Code Duplication
def test_user_finished_other():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
162
    timer = get_test_timer()
163
    entrant = get_test_entrant(status_value="finished", finished_at=datetime.now(timezone.utc), finish_time=timedelta(hours=1, minutes=9, seconds=42, microseconds=1), place=5)
164
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5)
165
    color, text = timer.get_timer_text(race, entrant.user.full_name)
166
    assert color is None
167
    assert text == "1:09:42.0"
168
    timer.use_podium_colors = True
169
    color, text = timer.get_timer_text(race, entrant.user.full_name)
170
    assert color == timer.finished_color