From d7dafddafeee9bc5394d54f7d0014285999b585d Mon Sep 17 00:00:00 2001 From: Arthur-Prince Date: Sat, 6 Dec 2025 19:27:06 -0300 Subject: [PATCH 1/2] fix score 2 --- src/core/services/trip_service.py | 2 +- tests/adapters/test_sptrans_adapter.py | 11 ----------- tests/core/test_trip_service_basic.py | 11 +++++++---- tests/core/test_trip_service_example.py | 14 +++++++++----- 4 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/core/services/trip_service.py b/src/core/services/trip_service.py index 5e5db8d..f8af966 100644 --- a/src/core/services/trip_service.py +++ b/src/core/services/trip_service.py @@ -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, diff --git a/tests/adapters/test_sptrans_adapter.py b/tests/adapters/test_sptrans_adapter.py index 55a9e3f..ecb708c 100644 --- a/tests/adapters/test_sptrans_adapter.py +++ b/tests/adapters/test_sptrans_adapter.py @@ -1,17 +1,10 @@ -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: """ @@ -25,7 +18,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: """ @@ -44,7 +36,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: """ @@ -58,7 +49,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: """ @@ -84,7 +74,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: """ diff --git a/tests/core/test_trip_service_basic.py b/tests/core/test_trip_service_basic.py index bd7e44c..6de22a4 100644 --- a/tests/core/test_trip_service_basic.py +++ b/tests/core/test_trip_service_basic.py @@ -11,6 +11,9 @@ 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) @@ -48,7 +51,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", @@ -87,9 +90,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 @@ -137,7 +140,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) diff --git a/tests/core/test_trip_service_example.py b/tests/core/test_trip_service_example.py index d0b4bd8..45584d6 100644 --- a/tests/core/test_trip_service_example.py +++ b/tests/core/test_trip_service_example.py @@ -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) @@ -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" @@ -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 From 7dc92eb6a744987bebb73cf81f2d257967b70ec3 Mon Sep 17 00:00:00 2001 From: Arthur-Prince Date: Sat, 6 Dec 2025 19:31:26 -0300 Subject: [PATCH 2/2] ruff format --- tests/adapters/test_sptrans_adapter.py | 1 - tests/core/test_trip_service_basic.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/adapters/test_sptrans_adapter.py b/tests/adapters/test_sptrans_adapter.py index ecb708c..c69f26f 100644 --- a/tests/adapters/test_sptrans_adapter.py +++ b/tests/adapters/test_sptrans_adapter.py @@ -1,4 +1,3 @@ - import pytest from src.adapters.external.sptrans_adapter import SpTransAdapter diff --git a/tests/core/test_trip_service_basic.py b/tests/core/test_trip_service_basic.py index 6de22a4..51d058b 100644 --- a/tests/core/test_trip_service_basic.py +++ b/tests/core/test_trip_service_basic.py @@ -14,6 +14,7 @@ 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)