57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
"""Tests for trip stats."""
|
|
|
|
from datetime import date
|
|
|
|
import agenda
|
|
import pytest
|
|
from agenda.stats import calculate_yearly_stats
|
|
from agenda.types import Trip
|
|
|
|
|
|
def make_trip(start: date, country_code: str) -> Trip:
|
|
"""Build a trip with a single country visit."""
|
|
return Trip(start=start, accommodation=[{"country": country_code}])
|
|
|
|
|
|
def test_new_country_only_first_year() -> None:
|
|
"""Ensure new countries are only counted on their first visit year."""
|
|
trips = [
|
|
make_trip(date(2024, 5, 1), "CZ"),
|
|
make_trip(date(2026, 6, 1), "CZ"),
|
|
]
|
|
|
|
yearly_stats = calculate_yearly_stats(trips)
|
|
czechia = agenda.get_country("CZ")
|
|
assert czechia is not None
|
|
|
|
assert czechia in yearly_stats[2024]["countries"]
|
|
assert czechia in yearly_stats[2024]["new_countries"]
|
|
assert czechia in yearly_stats[2026]["countries"]
|
|
assert "new_countries" not in yearly_stats[2026]
|
|
|
|
|
|
def test_new_country_respects_previously_visited() -> None:
|
|
"""Ensure previously visited countries are excluded from new-country stats."""
|
|
trips = [make_trip(date(2024, 5, 1), "CZ")]
|
|
|
|
yearly_stats = calculate_yearly_stats(trips, {"CZ"})
|
|
assert "new_countries" not in yearly_stats[2024]
|
|
|
|
|
|
def test_yearly_stats_tracks_co2_by_transport_type() -> None:
|
|
"""CO₂ stats should include a per-transport-type breakdown."""
|
|
trip = Trip(
|
|
start=date(2024, 5, 1),
|
|
travel=[
|
|
{"type": "car", "distance": 100.0, "co2_kg": 21.8},
|
|
{"type": "bus", "distance": 50.0, "co2_kg": 5.0},
|
|
],
|
|
)
|
|
|
|
yearly_stats = calculate_yearly_stats([trip])
|
|
|
|
assert yearly_stats[2024]["co2_kg"] == pytest.approx(26.8)
|
|
assert yearly_stats[2024]["co2_by_transport_type"] == {
|
|
"car": 21.8,
|
|
"bus": 5.0,
|
|
}
|