Passed
Push — master ( 73c582...10e257 )
by manny
01:41
created

test_timer.test_user_finished_other()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
eloc 10
dl 10
loc 10
rs 9.9
c 0
b 0
f 0
cc 1
nop 0
1
from datetime import datetime, time, timedelta, timezone
2
from tests.races_for_testing import time_ago
3
from gadgets.timer import Timer
4
from models.race import Entrant, Goal, Race, Status
5
from users_for_testing import get_test_user, get_test_entrant
6
from categories_for_testing import get_test_race_category
7
from races_for_testing import get_test_race
8
9
10
def get_test_timer():
11
    timer = Timer()
12
    timer.source_name = "Timer source"
13
    timer.enabled = True
14
    return timer
15
16
17
def test_timer_prerace():
18
    timer = get_test_timer()
19
    race = get_test_race(status_value="open", version=12,
20
                         started_at=None, start_delay=timedelta(seconds=-15))
21
    color, text = timer.get_timer_text(race, "")
22
    assert color is None
23
    assert text == "-0:00:15.0"
24
25
    timer.use_podium_colors = True
26
    color, text = timer.get_timer_text(race, "")
27
    assert color is timer.pre_color
28
    assert text == "-0:00:15.0"
29
30
def test_timer_counting_down():
31
    timer = get_test_timer()
32
    race = get_test_race(status_value="pending", version=13, started_at=time_ago(seconds=-5), start_delay=timedelta(seconds=-15))
33
    color, text = timer.get_timer_text(race, "")
34
    assert color is None
35
    assert text == "-0:00:05.0"
36
    
37
    timer.use_podium_colors = True
38
    color, text = timer.get_timer_text(race, "")
39
    assert color is timer.pre_color
40
41
def test_timer_midrace_no_entrant():
42
    timer = get_test_timer()
43
    race = get_test_race(status_value="in_progress", version=15, entrants_count=2,
44
        started_at=datetime.now(timezone.utc)-timedelta(hours=1, minutes=20))
45
    color, text = timer.get_timer_text(race, "")
46
    assert color is None
47
    # hope this always runs in less than 0.1 seconds XD
48
    assert text == "1:20:00.0"
49
    timer.use_podium_colors = True
50
    color, text = timer.get_timer_text(race, "")
51
    assert color is timer.racing_color
52
53
54 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...
55
    entrant = get_test_entrant(status_value="finished", finished_at=datetime.now(timezone.utc), finish_time=timedelta(hours=1, minutes=42, seconds=6.9))
56
    race = get_test_race(version=16, entrants_count=2, entrant=entrant, entrants=[entrant])
57
    timer = get_test_timer()
58
    color, text = timer.get_timer_text(race, entrant.user.full_name)
59
    assert color is None
60
    assert text == "1:42:06.9"
61
    timer.use_podium_colors = True
62
    color, text = timer.get_timer_text(race, entrant.user.full_name)
63
    assert color == timer.racing_color
64
65
def test_timer_midrace_w_user_not_in_race():
66
    race = get_test_race(version=17, entrants_count=2, started_at=datetime.now(timezone.utc)-timedelta(hours=1, minutes=42, seconds=42.0))
67
    timer = get_test_timer()
68
    color, text = timer.get_timer_text(race, "user_not_in_race#6969")
69
    assert color is None
70
    assert text == "1:42:42.0"
71
    timer.use_podium_colors = True
72
    color, text = timer.get_timer_text(race, "user_not_in_race#6969")
73
    assert color == timer.racing_color
74
75
def test_timer_race_cancelled():
76
    race = get_test_race(status_value="cancelled", cancelled_at=datetime.now(timezone.utc)+timedelta(minutes=20))
77
    timer = get_test_timer()
78
    color, text = timer.get_timer_text(race, "")
79
    assert color is None
80
    assert text == "--:--:--.-"
81
    timer.use_podium_colors = True
82
    color, text = timer.get_timer_text(race, "")
83
    # currently, the timer is always red for canceled or dq races
84
    assert color is timer.cancel_dq_color
85
86
def test_timer_user_dqed():
87
    entrant = get_test_entrant(status_value="dq")
88
    race = get_test_race(entrant=entrant, entrants=[entrant])
89
    timer = get_test_timer()
90
    color, text = timer.get_timer_text(race, entrant.user.full_name)
91
    assert color is None
92
    assert text == "--:--:--.-"
93
    timer.use_podium_colors = True
94
    color, text = timer.get_timer_text(race, entrant.user.full_name)
95
    assert color == timer.cancel_dq_color
96
97 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...
98
    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)
99
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5, entrants=[entrant])
100
    timer = get_test_timer()
101
    color, text = timer.get_timer_text(race, entrant.user.full_name)
102
    assert color is None
103
    assert text == "1:09:42.0"
104
    timer.use_podium_colors = True
105
    color, text = timer.get_timer_text(race, entrant.user.full_name)
106
    assert color == timer.first_color
107
108 View Code Duplication
def test_user_finished_second():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
109
    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)
110
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5, entrants=[entrant])
111
    timer = get_test_timer()
112
    color, text = timer.get_timer_text(race, entrant.user.full_name)
113
    assert color is None
114
    assert text == "1:09:42.0"
115
    timer.use_podium_colors = True
116
    color, text = timer.get_timer_text(race, entrant.user.full_name)
117
    assert color == timer.second_color
118
119 View Code Duplication
def test_user_finished_third():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
120
    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)
121
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5, entrants=[entrant])
122
    timer = get_test_timer()
123
    color, text = timer.get_timer_text(race, entrant.user.full_name)
124
    assert color is None
125
    assert text == "1:09:42.0"
126
    timer.use_podium_colors = True
127
    color, text = timer.get_timer_text(race, entrant.user.full_name)
128
    assert color == timer.third_color
129
130 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...
131
    timer = get_test_timer()
132
    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)
133
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5)
134
    color, text = timer.get_timer_text(race, entrant.user.full_name)
135
    assert color is None
136
    assert text == "1:09:42.0"
137
    timer.use_podium_colors = True
138
    color, text = timer.get_timer_text(race, entrant.user.full_name)
139
    assert color == timer.finished_color