-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #123 from RoboCupJuniorTC/tests
Add unittests
- Loading branch information
Showing
12 changed files
with
542 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
38 changes: 38 additions & 0 deletions
38
controllers/rcj_soccer_referee_supervisor/referee/tests/test_eventer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
|
||
from referee.event_handlers import EventHandler | ||
from referee.eventer import Eventer | ||
|
||
|
||
@pytest.fixture | ||
def eventer() -> Eventer: | ||
return Eventer() | ||
|
||
|
||
def test_no_subscribers(eventer: Eventer): | ||
assert eventer.subscribers == [] | ||
|
||
|
||
def test_subscribers_exist(eventer: Eventer): | ||
subscriber1 = EventHandler() | ||
subscriber2 = EventHandler() | ||
eventer.subscribe(subscriber1) | ||
eventer.subscribe(subscriber2) | ||
|
||
assert eventer.subscribers == [subscriber1, subscriber2] | ||
|
||
|
||
def test_event(eventer: Eventer): | ||
subscriber1 = EventHandler() | ||
subscriber2 = EventHandler() | ||
subscriber1.handle = MagicMock() | ||
subscriber2.handle = MagicMock() | ||
eventer.subscribe(subscriber1) | ||
eventer.subscribe(subscriber2) | ||
|
||
eventer.event("arg", kwarg="test") | ||
|
||
subscriber1.handle.assert_called_with("arg", kwarg="test") | ||
subscriber2.handle.assert_called_with("arg", kwarg="test") |
146 changes: 146 additions & 0 deletions
146
controllers/rcj_soccer_referee_supervisor/referee/tests/test_penalty_area_checker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
from typing import List | ||
|
||
import pytest | ||
|
||
from referee.consts import BLUE_PENALTY_AREA, YELLOW_PENALTY_AREA | ||
from referee.penalty_area_checker import PenaltyAreaChecker | ||
|
||
|
||
@pytest.fixture | ||
def checker() -> PenaltyAreaChecker: | ||
return PenaltyAreaChecker(time_allowed=15, reset_after=2) | ||
|
||
|
||
@pytest.fixture | ||
def outside_penalty_pos() -> List[float]: | ||
return [0.0, 0.0, 0.0] | ||
|
||
|
||
@pytest.fixture | ||
def in_yellow_penalty_pos() -> List[float]: | ||
return [ | ||
YELLOW_PENALTY_AREA[2] - 0.001, | ||
YELLOW_PENALTY_AREA[0] - 0.001, | ||
0.0, | ||
] | ||
|
||
|
||
@pytest.fixture | ||
def in_blue_penalty_pos() -> List[float]: | ||
return [ | ||
BLUE_PENALTY_AREA[2] - 0.001, | ||
BLUE_PENALTY_AREA[0] + 0.001, | ||
0.0, | ||
] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"x,y,expected", | ||
[ | ||
(0.0, 0.0, False), | ||
(YELLOW_PENALTY_AREA[2], YELLOW_PENALTY_AREA[0], False), | ||
(YELLOW_PENALTY_AREA[2] - 0.001, YELLOW_PENALTY_AREA[0] - 0.001, True), | ||
], | ||
) | ||
def test_is_in_yellow_penalty( | ||
x: float, y: float, expected: bool, checker: PenaltyAreaChecker | ||
): | ||
assert checker.is_in_yellow_penalty(x, y) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"x,y,expected", | ||
[ | ||
(0.0, 0.0, False), | ||
(BLUE_PENALTY_AREA[2], BLUE_PENALTY_AREA[0], False), | ||
(BLUE_PENALTY_AREA[2] - 0.001, BLUE_PENALTY_AREA[0] + 0.001, True), | ||
], | ||
) | ||
def test_is_in_blue_penalty( | ||
x: float, y: float, expected: bool, checker: PenaltyAreaChecker | ||
): | ||
assert checker.is_in_blue_penalty(x, y) == expected | ||
|
||
|
||
def test_outside_penalty_area(checker: PenaltyAreaChecker): | ||
checker.track([0.0, 0.0, 0.0], 60) | ||
|
||
assert not checker.has_entered | ||
assert not checker.has_left | ||
assert checker.time_entered_penalty is None | ||
assert checker.time_left_penalty is None | ||
assert not checker.is_violating() | ||
|
||
|
||
def test_enter_yellow_penalty_area( | ||
checker: PenaltyAreaChecker, in_yellow_penalty_pos: List[float] | ||
): | ||
checker.track(in_yellow_penalty_pos, 60) | ||
|
||
assert checker.has_entered | ||
assert not checker.has_left | ||
assert checker.time_entered_penalty == 60 | ||
|
||
|
||
def test_enter_blue_penalty_area( | ||
checker: PenaltyAreaChecker, in_blue_penalty_pos: List[float] | ||
): | ||
checker.track(in_blue_penalty_pos, 60) | ||
|
||
assert checker.has_entered | ||
assert not checker.has_left | ||
assert checker.time_entered_penalty == 60 | ||
|
||
|
||
def test_left_penalty_area( | ||
checker: PenaltyAreaChecker, | ||
in_yellow_penalty_pos: List[float], | ||
outside_penalty_pos: List[float], | ||
): | ||
checker.track(in_yellow_penalty_pos, 60) | ||
checker.track(outside_penalty_pos, 59) | ||
|
||
assert checker.has_entered | ||
assert checker.has_left | ||
assert checker.time_left_penalty == 59 | ||
assert not checker.has_been_outside_penalty_for_longer | ||
|
||
|
||
def test_left_penalty_area_for_longer( | ||
checker: PenaltyAreaChecker, | ||
in_yellow_penalty_pos: List[float], | ||
outside_penalty_pos: List[float], | ||
): | ||
checker.track(in_yellow_penalty_pos, 60) | ||
checker.track(outside_penalty_pos, 59) | ||
checker.track(outside_penalty_pos, 56) | ||
|
||
assert not checker.has_entered | ||
assert not checker.has_left | ||
|
||
|
||
def test_reenter_penalty_area( | ||
checker: PenaltyAreaChecker, | ||
in_yellow_penalty_pos: List[float], | ||
outside_penalty_pos: List[float], | ||
): | ||
checker.track(in_yellow_penalty_pos, 60) | ||
checker.track(outside_penalty_pos, 59) | ||
checker.track(in_yellow_penalty_pos, 58) | ||
|
||
assert checker.time_entered_penalty == 60 | ||
assert not checker.has_left | ||
assert not checker.is_violating() | ||
|
||
|
||
def test_violating( | ||
checker: PenaltyAreaChecker, | ||
in_yellow_penalty_pos: List[float], | ||
outside_penalty_pos: List[float], | ||
): | ||
checker.track(in_yellow_penalty_pos, 60) | ||
checker.track(outside_penalty_pos, 59) | ||
checker.track(in_yellow_penalty_pos, 57) | ||
checker.track(in_yellow_penalty_pos, 40) | ||
|
||
assert checker.is_violating() |
71 changes: 71 additions & 0 deletions
71
controllers/rcj_soccer_referee_supervisor/referee/tests/test_progress_checker.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import pytest | ||
|
||
from referee.progress_checker import ProgressChecker | ||
|
||
|
||
@pytest.fixture | ||
def checker() -> ProgressChecker: | ||
return ProgressChecker(steps=235, threshold=0.5) | ||
|
||
|
||
def test_initialize(checker: ProgressChecker): | ||
assert len(checker.samples) == 235 | ||
assert checker.iterator == 0 | ||
assert checker.prev_position is None | ||
|
||
|
||
def test_first_track(checker: ProgressChecker): | ||
position = [0.0, 0.0, 0.0] | ||
checker.track(position) | ||
|
||
assert checker.prev_position == position | ||
assert checker.samples[0] == 0 | ||
|
||
|
||
def test_track_multiple_times(checker: ProgressChecker): | ||
checker.track([0.0, 0.0, 0.0]) | ||
checker.track([0.01, 0.0, 0.0]) | ||
checker.track([0.02, 0.0, 0.0]) | ||
|
||
assert checker.iterator == 2 | ||
assert checker.samples[0] == 0.01 | ||
assert checker.samples[1] == 0.01 | ||
assert checker.samples[2] == 0 | ||
assert checker.prev_position == [0.02, 0.0, 0.0] | ||
|
||
|
||
def test_progress_after_initialize(checker: ProgressChecker): | ||
assert checker.is_progress() | ||
|
||
|
||
def test_progress_not_enough_steps(checker: ProgressChecker): | ||
checker.iterator = 234 | ||
|
||
assert checker.is_progress() | ||
|
||
|
||
def test_progress_enough_steps(checker: ProgressChecker): | ||
checker.iterator = 235 | ||
|
||
assert not checker.is_progress() | ||
|
||
|
||
def test_no_progress(checker: ProgressChecker): | ||
x = 0.0 | ||
checker.track([x, 0.0, 0.0]) | ||
for _ in range(234): | ||
x += 0.002 | ||
checker.track([x, 0.0, 0.0]) | ||
assert checker.is_progress() | ||
|
||
checker.track([x + 0.002, 0.0, 0.0]) | ||
assert not checker.is_progress() | ||
|
||
|
||
def test_progress_ok(checker: ProgressChecker): | ||
for _ in range(235): | ||
checker.track([0.0, 0.0, 0.0]) | ||
assert checker.is_progress() | ||
|
||
checker.track([0.5, 0.0, 0.0]) | ||
assert checker.is_progress() |
61 changes: 61 additions & 0 deletions
61
controllers/rcj_soccer_referee_supervisor/referee/tests/test_referee.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import sys | ||
from unittest.mock import MagicMock | ||
|
||
sys.modules["controller"] = MagicMock() | ||
|
||
import pytest | ||
|
||
from referee.consts import MAX_EVENT_MESSAGES_IN_QUEUE | ||
from referee.referee import RCJSoccerReferee | ||
|
||
|
||
@pytest.fixture | ||
def referee() -> RCJSoccerReferee: | ||
supervisor = MagicMock() | ||
return RCJSoccerReferee( | ||
supervisor=supervisor, | ||
match_time=600, | ||
progress_check_steps=235, | ||
progress_check_threshold=0.5, | ||
ball_progress_check_steps=235, | ||
ball_progress_check_threshold=0.5, | ||
team_name_blue="Blues", | ||
team_name_yellow="Yellows", | ||
initial_score_blue=0, | ||
initial_score_yellow=0, | ||
penalty_area_allowed_time=15, | ||
penalty_area_reset_after=2, | ||
match_id=1, | ||
half_id=1, | ||
initial_position_noise=0.15, | ||
) | ||
|
||
|
||
def test_pack_packet(referee: RCJSoccerReferee): | ||
assert referee._pack_packet() == b"\x00" | ||
|
||
|
||
def test_add_initial_position_noise(referee: RCJSoccerReferee): | ||
position = [0.0, 0.0, 0.0] | ||
new_position = referee._add_initial_position_noise(position) | ||
|
||
assert -0.075 <= new_position[0] < 0.075 | ||
assert -0.075 <= new_position[1] < 0.075 | ||
assert new_position[2] == 0.0 | ||
|
||
|
||
def test_add_event_message_to_queue(referee: RCJSoccerReferee): | ||
assert referee.event_messages_to_draw == [] | ||
|
||
for i in range(1, MAX_EVENT_MESSAGES_IN_QUEUE + 1): | ||
referee.add_event_message_to_queue(str(i)) | ||
assert len(referee.event_messages_to_draw) == i | ||
assert referee.event_messages_to_draw[-1] == (referee.time, str(i)) | ||
|
||
referee.add_event_message_to_queue(str(MAX_EVENT_MESSAGES_IN_QUEUE + 1)) | ||
assert len(referee.event_messages_to_draw) == MAX_EVENT_MESSAGES_IN_QUEUE | ||
assert referee.event_messages_to_draw[-1] == ( | ||
referee.time, | ||
str(MAX_EVENT_MESSAGES_IN_QUEUE + 1), | ||
) | ||
assert referee.event_messages_to_draw[0] == (referee.time, "2") |
Oops, something went wrong.