Skip to content

Commit

Permalink
Merge pull request #123 from RoboCupJuniorTC/tests
Browse files Browse the repository at this point in the history
Add unittests
  • Loading branch information
Adman authored Jan 18, 2022
2 parents a61799a + 36a298b commit 73bb7e5
Show file tree
Hide file tree
Showing 12 changed files with 542 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: code-style-check
name: code-check
on:
push:
branches:
Expand All @@ -18,3 +18,4 @@ jobs:
- run: isort --check-only .
- run: black --check .
- run: flake8 .
- run: pytest --cov
21 changes: 17 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,23 @@ with `black` and `isort`, and `flake8` issues are fixed.
To do so, follow these steps:

1. Create virtualenv `python3 -m venv venv`
2. Install development modules `pip install -r requirements/development.txt`
3. Setup pre-commit hook `pre-commit install`. This hook will not allow you to
2. Install `pip-tools` by running `pip install pip-tools`
3. Install development modules `pip-sync requirements/development.txt`
4. Setup pre-commit hook `pre-commit install`. This hook will not allow you to
commit in case one of the checkers raises an error.
4. In order format the code, run the formatters in this order
5. In order format the code, run the formatters in this order
* `isort .`
* `black .`
5. Manually fix error raised by `flake8 .`
6. Manually fix error raised by `flake8 .`

### Updating dependencies

Dependencies for development are managed by pip-tools. To compile current
dependencies run

```bash
$ pip-compile -o requirements/development.txt requirements/development.in
```

In order to update dependencies, use `--upgrade` switch.

Empty file.
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")
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()
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()
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")
Loading

0 comments on commit 73bb7e5

Please sign in to comment.