From ece22c0d19436f63a64cbcbda17b3c4f1b0878f3 Mon Sep 17 00:00:00 2001 From: Gabriel Barberini Date: Sun, 2 Feb 2025 12:48:45 -0300 Subject: [PATCH] improve error semantics on simulation route crash; make fins and nosecone mandatory parameters --- lib/controllers/environment.py | 2 +- lib/controllers/flight.py | 2 +- lib/controllers/motor.py | 2 +- lib/controllers/rocket.py | 2 +- lib/models/rocket.py | 4 +-- requirements-dev.txt | 2 ++ tests/test_routes/conftest.py | 33 +++++++++++++++++++++++- tests/test_routes/test_rockets_route.py | 34 ------------------------- 8 files changed, 40 insertions(+), 41 deletions(-) diff --git a/lib/controllers/environment.py b/lib/controllers/environment.py index 40bd7bd..2da51c2 100644 --- a/lib/controllers/environment.py +++ b/lib/controllers/environment.py @@ -264,7 +264,7 @@ async def simulate_env( logger.error(f"controllers.environment.simulate: {exc_str}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - detail=f"Failed to simulate environment: {exc_str}", + detail=f"Failed to simulate environment, parameters may contain data that is not physically coherent: {exc_str}", ) from e else: return env_summary diff --git a/lib/controllers/flight.py b/lib/controllers/flight.py index d112aa4..42fe456 100644 --- a/lib/controllers/flight.py +++ b/lib/controllers/flight.py @@ -384,7 +384,7 @@ async def simulate_flight( logger.error(f"controllers.flight.simulate_flight: {exc_str}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - detail=f"Failed to simulate flight: {exc_str}", + detail=f"Failed to simulate flight, parameters may contain data that is not physically coherent: {exc_str}", ) from e else: return flight_summary diff --git a/lib/controllers/motor.py b/lib/controllers/motor.py index 824dfd1..81304fe 100644 --- a/lib/controllers/motor.py +++ b/lib/controllers/motor.py @@ -279,7 +279,7 @@ async def simulate_motor( logger.error(f"controllers.motor.simulate_motor: {exc_str}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - detail=f"Failed to simulate motor: {exc_str}", + detail=f"Failed to simulate motor, parameters may contain data that is not physically coherent: {exc_str}", ) from e else: return motor_summary diff --git a/lib/controllers/rocket.py b/lib/controllers/rocket.py index e3817d6..0bfea11 100644 --- a/lib/controllers/rocket.py +++ b/lib/controllers/rocket.py @@ -279,7 +279,7 @@ async def simulate_rocket( logger.error(f"controllers.rocket.simulate: {exc_str}") raise HTTPException( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, - detail=f"Failed to simulate rocket: {exc_str}", + detail=f"Failed to simulate rocket, parameters may contain data that is not physically coherent: {exc_str}", ) from e else: return rocket_summary diff --git a/lib/models/rocket.py b/lib/models/rocket.py index 2f35096..ab45419 100644 --- a/lib/models/rocket.py +++ b/lib/models/rocket.py @@ -33,10 +33,10 @@ class Rocket(BaseModel): coordinate_system_orientation: CoordinateSystemOrientation = ( CoordinateSystemOrientation.TAIL_TO_NOSE ) + nose: NoseCone + fins: List[Fins] # Optional parameters parachutes: Optional[List[Parachute]] = None rail_buttons: Optional[RailButtons] = None - nose: Optional[NoseCone] = None - fins: Optional[List[Fins]] = None tail: Optional[Tail] = None diff --git a/requirements-dev.txt b/requirements-dev.txt index 00564b5..9b83bee 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -1,3 +1,5 @@ flake8 pylint ruff +pytest +httpx diff --git a/tests/test_routes/conftest.py b/tests/test_routes/conftest.py index deecd99..e8f6fb1 100644 --- a/tests/test_routes/conftest.py +++ b/tests/test_routes/conftest.py @@ -4,6 +4,7 @@ from lib.models.rocket import Rocket from lib.models.motor import Motor, MotorTank, TankFluids, TankKinds from lib.models.environment import Env +from lib.models.aerosurfaces import Fins, NoseCone @pytest.fixture @@ -79,7 +80,35 @@ def stub_mass_tank(stub_tank): @pytest.fixture -def stub_rocket(stub_motor): +def stub_nose_cone(): + nose_cone = NoseCone( + name='nose', + length=0, + kind='kind', + position=0, + base_radius=0, + rocket_radius=0, + ) + nose_cone_json = nose_cone.model_dump_json() + return json.loads(nose_cone_json) + + +@pytest.fixture +def stub_fins(): + fins = Fins( + fins_kind='TRAPEZOIDAL', + name='fins', + n=0, + root_chord=0, + span=0, + position=0, + ) + fins_json = fins.model_dump_json() + return json.loads(fins_json) + + +@pytest.fixture +def stub_rocket(stub_motor, stub_nose_cone, stub_fins): rocket = Rocket( motor=stub_motor, radius=0, @@ -89,6 +118,8 @@ def stub_rocket(stub_motor): inertia=[0, 0, 0], power_off_drag=[(0, 0)], power_on_drag=[(0, 0)], + nose=stub_nose_cone, + fins=[stub_fins], coordinate_system_orientation='TAIL_TO_NOSE', ) rocket_json = rocket.model_dump_json() diff --git a/tests/test_routes/test_rockets_route.py b/tests/test_routes/test_rockets_route.py index 852a012..f689b98 100644 --- a/tests/test_routes/test_rockets_route.py +++ b/tests/test_routes/test_rockets_route.py @@ -4,8 +4,6 @@ from fastapi.testclient import TestClient from fastapi import HTTPException, status from lib.models.aerosurfaces import ( - Fins, - NoseCone, Tail, RailButtons, Parachute, @@ -36,34 +34,6 @@ def stub_rocket_summary(): return json.loads(rocket_summary_json) -@pytest.fixture -def stub_nose_cone(): - nose_cone = NoseCone( - name='nose', - length=0, - kind='kind', - position=0, - base_radius=0, - rocket_radius=0, - ) - nose_cone_json = nose_cone.model_dump_json() - return json.loads(nose_cone_json) - - -@pytest.fixture -def stub_fins(): - fins = Fins( - fins_kind='TRAPEZOIDAL', - name='fins', - n=0, - root_chord=0, - span=0, - position=0, - ) - fins_json = fins.model_dump_json() - return json.loads(fins_json) - - @pytest.fixture def stub_tail(): tail = Tail( @@ -126,8 +96,6 @@ def test_create_rocket(stub_rocket): def test_create_rocket_optional_params( stub_rocket, - stub_nose_cone, - stub_fins, stub_tail, stub_rail_buttons, stub_parachute, @@ -136,8 +104,6 @@ def test_create_rocket_optional_params( { 'parachutes': [stub_parachute], 'rail_buttons': stub_rail_buttons, - 'nose': stub_nose_cone, - 'fins': [stub_fins], 'tail': stub_tail, } )