Passed
Push — master ( 8b3758...32ee86 )
by manny
01:31
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 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
from races_for_testing import get_test_race
7
8
9
def get_test_timer():
10
    timer = Timer()
11
    timer.source_name = "Timer source"
12
    timer.enabled = True
13
    return timer
14
15
16
def test_timer_prerace():
17
    timer = get_test_timer()
18
    race = get_test_race(status_value="open", version=12,
19
                         started_at=None, start_delay=timedelta(seconds=-15))
20
    color, text = timer.get_timer_text(race, "")
21
    assert color is None
22
    assert text == "-0:00:15.0"
23
24
    timer.use_podium_colors = True
25
    color, text = timer.get_timer_text(race, "")
26
    assert color is timer.pre_color
27
    assert text == "-0:00:15.0"
28
29
def test_timer_midrace_no_entrant():
30
    timer = get_test_timer()
31
    race = get_test_race(status_value="in_progress", version=15, entrants_count=2,
32
        started_at=datetime.now(timezone.utc)-timedelta(hours=1, minutes=20))
33
    color, text = timer.get_timer_text(race, "")
34
    assert color is None
35
    # hope this always runs in less than 0.1 seconds XD
36
    assert text == "1:20:00.0"
37
    timer.use_podium_colors = True
38
    color, text = timer.get_timer_text(race, "")
39
    assert color is timer.racing_color
40
41
42 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...
43
    entrant = get_test_entrant(status_value="finished", finished_at=datetime.now(timezone.utc), finish_time=timedelta(hours=1, minutes=42, seconds=6.9))
44
    race = get_test_race(version=16, entrants_count=2, entrant=entrant, entrants=[entrant])
45
    timer = get_test_timer()
46
    color, text = timer.get_timer_text(race, entrant.user.full_name)
47
    assert color is None
48
    assert text == "1:42:06.9"
49
    timer.use_podium_colors = True
50
    color, text = timer.get_timer_text(race, entrant.user.full_name)
51
    assert color == timer.racing_color
52
53
def test_timer_midrace_w_user_not_in_race():
54
    race = get_test_race(version=17, entrants_count=2, started_at=datetime.now(timezone.utc)-timedelta(hours=1, minutes=42, seconds=42.0))
55
    timer = get_test_timer()
56
    color, text = timer.get_timer_text(race, "user_not_in_race#6969")
57
    assert color is None
58
    assert text == "1:42:42.0"
59
    timer.use_podium_colors = True
60
    color, text = timer.get_timer_text(race, "user_not_in_race#6969")
61
    assert color == timer.racing_color
62
63
def test_timer_race_cancelled():
64
    race = get_test_race(status_value="cancelled", cancelled_at=datetime.now(timezone.utc)+timedelta(minutes=20))
65
    timer = get_test_timer()
66
    color, text = timer.get_timer_text(race, "")
67
    assert color is None
68
    assert text == "--:--:--.-"
69
    timer.use_podium_colors = True
70
    color, text = timer.get_timer_text(race, "")
71
    # currently, the timer is always red for canceled or dq races
72
    assert color is timer.cancel_dq_color
73
74
def test_timer_user_dqed():
75
    entrant = get_test_entrant(status_value="dq")
76
    race = get_test_race(entrant=entrant, entrants=[entrant])
77
    timer = get_test_timer()
78
    color, text = timer.get_timer_text(race, entrant.user.full_name)
79
    assert color is None
80
    assert text == "--:--:--.-"
81
    timer.use_podium_colors = True
82
    color, text = timer.get_timer_text(race, entrant.user.full_name)
83
    assert color == timer.cancel_dq_color
84
85 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...
86
    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)
87
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5, entrants=[entrant])
88
    timer = get_test_timer()
89
    color, text = timer.get_timer_text(race, entrant.user.full_name)
90
    assert color is None
91
    assert text == "1:09:42.0"
92
    timer.use_podium_colors = True
93
    color, text = timer.get_timer_text(race, entrant.user.full_name)
94
    assert color == timer.first_color
95
96 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...
97
    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)
98
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5, entrants=[entrant])
99
    timer = get_test_timer()
100
    color, text = timer.get_timer_text(race, entrant.user.full_name)
101
    assert color is None
102
    assert text == "1:09:42.0"
103
    timer.use_podium_colors = True
104
    color, text = timer.get_timer_text(race, entrant.user.full_name)
105
    assert color == timer.second_color
106
107 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...
108
    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)
109
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5, entrants=[entrant])
110
    timer = get_test_timer()
111
    color, text = timer.get_timer_text(race, entrant.user.full_name)
112
    assert color is None
113
    assert text == "1:09:42.0"
114
    timer.use_podium_colors = True
115
    color, text = timer.get_timer_text(race, entrant.user.full_name)
116
    assert color == timer.third_color
117
118 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...
119
    timer = get_test_timer()
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=5)
121
    race = get_test_race(started_at=datetime.now(timezone.utc)-entrant.finish_time, entrant=entrant, entrants_count=5)
122
    color, text = timer.get_timer_text(race, entrant.user.full_name)
123
    assert color is None
124
    assert text == "1:09:42.0"
125
    timer.use_podium_colors = True
126
    color, text = timer.get_timer_text(race, entrant.user.full_name)
127
    assert color == timer.finished_color