sw7 detect laps from coordinates
This commit is contained in:
1 parent
5d0aa9c876
commit
45921bfe17
14 files changed
+1121
No files matched your search
@@ -0,0 +1,57 @@
|
||||
import pytest
|
||||
|
||||
from lap_detection.api import Collection, Feature, Geometry, Coordinate
|
||||
import lap_detection.main as main
|
||||
|
||||
|
||||
# arrange
|
||||
@pytest.fixture
|
||||
def sample_collection():
|
||||
return Collection(
|
||||
"FeatureCollection",
|
||||
[
|
||||
Feature(
|
||||
"Feature",
|
||||
{},
|
||||
Geometry([[123.456, 654.321], [987.654, 101.010]], "LineString"),
|
||||
)
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
# arrange
|
||||
@pytest.mark.parametrize(
|
||||
"path,expected_laps",
|
||||
[
|
||||
("src/lap_detection/data/usecase1.json", 3),
|
||||
("src/lap_detection/data/usecase2.json", 3),
|
||||
("src/lap_detection/data/usecase3.json", 0),
|
||||
("src/lap_detection/data/usecase4.json", 2),
|
||||
],
|
||||
)
|
||||
def test_main(path, expected_laps, capsys):
|
||||
# act
|
||||
main.main(path)
|
||||
|
||||
# assert
|
||||
captured = capsys.readouterr()
|
||||
assert captured.out.strip() == f"Anzahl Runden: {expected_laps}"
|
||||
|
||||
|
||||
def test_check_invalid_path():
|
||||
# assert
|
||||
with pytest.raises(FileNotFoundError, match="invalid_path.json does not exist"):
|
||||
# act
|
||||
main._check_path("invalid_path.json")
|
||||
|
||||
|
||||
def test_build_coordinates(sample_collection):
|
||||
# act
|
||||
coordinates = main._build_coordinates(sample_collection)
|
||||
|
||||
# assert
|
||||
assert isinstance(coordinates, list)
|
||||
assert len(coordinates) == 2
|
||||
assert isinstance(coordinates[1], Coordinate)
|
||||
assert coordinates[1].lon == 987.654
|
||||
assert coordinates[1].lat == 101.010
|
||||
@@ -0,0 +1,54 @@
|
||||
from lap_detection.api import Coordinate
|
||||
from lap_detection.lap_detector import count_laps
|
||||
|
||||
|
||||
def test_no_laps():
|
||||
# arrange
|
||||
start = Coordinate(lat=47.0, lon=8.0)
|
||||
coords = [
|
||||
Coordinate(lat=47.0001, lon=8.0001),
|
||||
Coordinate(lat=47.0002, lon=8.0002),
|
||||
Coordinate(lat=47.0003, lon=8.0003),
|
||||
]
|
||||
|
||||
# act
|
||||
result = count_laps(coords, start, radius=20)
|
||||
|
||||
# assert
|
||||
assert result == 0
|
||||
|
||||
|
||||
def test_single_lap():
|
||||
# arrange
|
||||
start = Coordinate(lat=47.0, lon=8.0)
|
||||
coords = [
|
||||
Coordinate(lat=47.0030, lon=8.0000), # far from start
|
||||
Coordinate(lat=47.0020, lon=8.0010),
|
||||
Coordinate(lat=47.0010, lon=8.0020),
|
||||
Coordinate(lat=47.0001, lon=8.0001), # back near start -> lap counted
|
||||
Coordinate(lat=47.0030, lon=8.0000), # far again -> no second lap
|
||||
]
|
||||
|
||||
# act
|
||||
result = count_laps(coords, start, radius=20)
|
||||
|
||||
# assert
|
||||
assert result == 1
|
||||
|
||||
|
||||
def test_two_laps():
|
||||
# arrange
|
||||
start = Coordinate(lat=47.0, lon=8.0)
|
||||
coords = [
|
||||
Coordinate(lat=47.0030, lon=8.0000), # far from start
|
||||
Coordinate(lat=47.0001, lon=8.0001), # back near start -> lap 1
|
||||
Coordinate(lat=47.0002, lon=8.0002), # still near start -> don't count twice
|
||||
Coordinate(lat=47.0030, lon=8.0000), # far again
|
||||
Coordinate(lat=47.0001, lon=8.0001), # back near start -> lap 2
|
||||
]
|
||||
|
||||
# act
|
||||
result = count_laps(coords, start, radius=20)
|
||||
|
||||
# assert
|
||||
assert result == 2
|
||||
Reference in new issue
Block a user