Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/core/services/trip_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ async def create_trip(
if distance < 0:
raise ValueError("distance must be non-negative")

score = (distance // 1000) * 77
score = round(distance * 0.077)

trip = Trip(
email=email,
Expand Down
12 changes: 0 additions & 12 deletions tests/adapters/test_sptrans_adapter.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import os

import pytest

from src.adapters.external.sptrans_adapter import SpTransAdapter
from src.core.models.bus import BusPosition, BusRoute

skip_if_no_token = pytest.mark.skipif(
not os.getenv("SPTRANS_API_TOKEN"),
reason="SPTRANS_API_TOKEN not set - skipping integration test",
)


@skip_if_no_token
@pytest.mark.asyncio
async def test_automatic_authentication() -> None:
"""
Expand All @@ -25,7 +17,6 @@ async def test_automatic_authentication() -> None:
assert len(routes) > 0


@skip_if_no_token
@pytest.mark.asyncio
async def test_search_routes_number() -> None:
"""
Expand All @@ -44,7 +35,6 @@ async def test_search_routes_number() -> None:
assert "8075" in bus_route.route.bus_line


@skip_if_no_token
@pytest.mark.asyncio
async def test_search_routes_by_destination() -> None:
"""
Expand All @@ -58,7 +48,6 @@ async def test_search_routes_by_destination() -> None:
assert len(bus_routes) > 0, "Nenhuma rota retornada para Lapa"


@skip_if_no_token
@pytest.mark.asyncio
async def test_get_bus_positions() -> None:
"""
Expand All @@ -84,7 +73,6 @@ async def test_get_bus_positions() -> None:
assert isinstance(pos.position.longitude, float | int)


@skip_if_no_token
@pytest.mark.asyncio
async def test_search_routes_returns_empty_for_unknown() -> None:
"""
Expand Down
12 changes: 8 additions & 4 deletions tests/core/test_trip_service_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
from src.core.services.trip_service import TripService


def calculate_expected_score(distance: int) -> float:
return round(distance * 0.077)


@pytest.mark.asyncio
async def test_create_trip_no_user() -> None:
user_repo = create_autospec(UserRepository, instance=True)
Expand Down Expand Up @@ -48,7 +52,7 @@ async def test_create_trip_single_user() -> None:
service = TripService(trip_repo, user_repo)

distance = 1500
expected_score = (distance // 1000) * 77
expected_score = calculate_expected_score(distance)

trip = await service.create_trip(
email="user@example.com",
Expand Down Expand Up @@ -87,9 +91,9 @@ async def test_create_trip_zero_distance() -> None:
)

assert isinstance(trip, Trip)
assert trip.score == 0
assert trip.score == 0.0
assert trip.route.bus_line == "0000"
user_repo.add_user_score.assert_awaited_once_with("zero@example.com", 0)
user_repo.add_user_score.assert_not_awaited()


@pytest.mark.asyncio
Expand Down Expand Up @@ -137,7 +141,7 @@ async def test_create_trip_very_large_distance() -> None:
trip_datetime=datetime(2025, 11, 15, 12, 0, 0),
)

expected_score = (big_distance // 1000) * 77
expected_score = calculate_expected_score(big_distance)
assert trip.score == expected_score
assert trip.route.bus_line == "BIG"
user_repo.add_user_score.assert_awaited_once_with("big@example.com", expected_score)
Expand Down
14 changes: 9 additions & 5 deletions tests/core/test_trip_service_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
from pytest_mock import MockerFixture


def calculate_expected_score(distance: int) -> float:
return round(distance * 0.077)


@pytest.mark.asyncio
async def test_create_trip_calculates_score_correctly() -> None:
user_repo = create_autospec(UserRepository, instance=True)
Expand All @@ -40,7 +44,7 @@ async def test_create_trip_calculates_score_correctly() -> None:
trip_datetime=datetime(2025, 10, 16, 10, 0, 0),
)

expected_score = (distance // 1000) * 77
expected_score = 77.0
assert trip.score == expected_score
assert trip.email == "test@example.com"
assert trip.route.bus_line == "8000"
Expand Down Expand Up @@ -105,14 +109,14 @@ async def test_multiple_trips(mocker: "MockerFixture") -> None:
trip_datetime=datetime.now(),
)

assert trip1.score == 0
assert trip1.score == calculate_expected_score(trip1.distance)
assert trip1.route.bus_line == "8000"
assert trip2.score == 77
assert trip2.score == calculate_expected_score(trip2.distance)
assert trip2.route.bus_direction == 2
assert trip_repo.save_trip.await_count == 2
assert user_repo.add_user_score.await_count == 2
user_repo.add_user_score.assert_any_await("bob@example.com", 0)
user_repo.add_user_score.assert_any_await("bob@example.com", 77)
user_repo.add_user_score.assert_any_await("bob@example.com", trip1.score)
user_repo.add_user_score.assert_any_await("bob@example.com", trip2.score)


@pytest.mark.asyncio
Expand Down