Passed
Push — master ( 6de23a...679ff4 )
by manny
01:42 queued 11s
created

test_timer.get_test_race()   B

Complexity

Conditions 3

Size

Total Lines 42
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 41
dl 0
loc 42
rs 8.896
c 0
b 0
f 0
cc 3
nop 9

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
from datetime import datetime, 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
5
from categories_for_testing import get_test_race_category
6
7
8
def get_test_entrant(status_value="joined", finished_at: datetime = None, finish_time: timedelta = None) -> Entrant:
9
    return Entrant(get_test_user(), status=get_test_status(status_value), has_comment=False, stream_live=True, stream_override=False, actions=[], finished_at=finished_at, finish_time=finish_time)
10
11
def get_test_status(status_value):
12
    Status(value=status_value, verbose_value="", help_text="")
13
14
def get_test_race(status_value="in_progress", version=1, entrants_count=2, started_at=datetime.now(timezone.utc), start_delay=timedelta(seconds=-15), 
15
                opened_at=datetime.now(timezone.utc), ended_at=None, cancelled_at: datetime = None, entrant: Entrant = None) -> Race:
16
    test_race= Race(name="",
17
                status=Status(value=status_value,
18
                              verbose_value="", help_text=""),
19
                category=get_test_race_category(),
20
                goal=Goal("", False),
21
                info="A test race",
22
                url="",
23
                data_url="",
24
                websocket_url="",
25
                websocket_bot_url="",
26
                websocket_oauth_url="",
27
                entrants_count=entrants_count,
28
                entrants_count_inactive=0,
29
                entrants_count_finished=0,
30
                entrants=(list(get_test_entrant()
31
                          for x in range(0, entrants_count))),
32
                version=version,
33
                started_at=started_at,
34
                start_delay=start_delay,
35
                ended_at=ended_at,
36
                cancelled_at=cancelled_at,
37
                unlisted=False,
38
                time_limit=timedelta(days=1),
39
                streaming_required=True,
40
                auto_start=True,
41
                opened_by=get_test_user(),
42
                opened_at=opened_at,
43
                monitors=[],
44
                recordable=True,
45
                recorded=False,
46
                recorded_by=None,
47
                allow_comments=True,
48
                hide_comments=False,
49
                allow_midrace_chat=True
50
                )
51
    if entrant is not None:
52
        if not entrant in test_race.entrants:
53
            test_race.entrants.pop()
54
            test_race.entrants.append(entrant)
55
    return test_race
56
57
def get_test_timer():
58
    timer = Timer()
59
    timer.source_name = "Timer source"
60
    timer.enabled = True
61
    return timer
62
63
64
def test_timer_prerace():
65
    timer = get_test_timer()
66
    race = get_test_race(status_value="open", version=12,
67
                         started_at=None, start_delay=timedelta(seconds=-15))
68
    color, text = timer.get_timer_text(race, "")
69
    assert color is None
70
    assert text == "-0:00:15.0"
71
72
    timer.use_podium_colors = True
73
    color, text = timer.get_timer_text(race, "")
74
    assert color is timer.pre_color
75
    assert text == "-0:00:15.0"
76
77
def test_timer_midrace():
78
    timer = get_test_timer()
79
    race1 = get_test_race(status_value="in_progress", version=15, entrants_count=2,
80
        started_at=datetime.now(timezone.utc)-timedelta(hours=1, minutes=20))
81
    color, text = timer.get_timer_text(race1, "")
82
    assert color is None
83
    # hope this always runs in less than 0.1 seconds XD
84
    assert text == "1:20:00.0"
85
    timer.use_podium_colors = True
86
    color, text = timer.get_timer_text(race1, "")
87
    assert color is timer.racing_color
88
89
    timer.use_podium_colors = False
90
    entrant = get_test_entrant(status_value="finished", finished_at=datetime.now(timezone.utc), finish_time=timedelta(hours=1, minutes=42, seconds=6.9))
91
    race2 = get_test_race(version=16, entrants_count=2, entrant=entrant)
92
    color, text = timer.get_timer_text(race2, entrant.user.full_name)
93
    assert color is None
94
    assert text == "1:42:06.9"
95
    timer.use_podium_colors = True
96
    color, text = timer.get_timer_text(race2, entrant.user.full_name)
97
    assert color == timer.racing_color
98
99
    timer.use_podium_colors = False
100
    race3 = get_test_race(version=17, entrants_count=2, started_at=datetime.now(timezone.utc)-timedelta(hours=1, minutes=42, seconds=42.0))
101
    color, text = timer.get_timer_text(race3, "user_not_in_race#6969")
102
    assert color is None
103
    assert text == "1:42:42.0"
104
    timer.use_podium_colors = True
105
    color, text = timer.get_timer_text(race3, "user_not_in_race#6969")
106
    assert color == timer.racing_color
107
108
    
109
110