Skip to content

Commit c61543e

Browse files
refactor test rockets route
1 parent bcca778 commit c61543e

11 files changed

+449
-460
lines changed

lib/controllers/flight.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ def __init__(self):
2222
super().__init__(models=[FlightModel])
2323

2424
@controller_exception_handler
25-
async def update_env_by_flight_id(
26-
self, flight_id: str, *, env: EnvironmentModel
25+
async def update_environment_by_flight_id(
26+
self, flight_id: str, *, environment: EnvironmentModel
2727
) -> FlightUpdated:
2828
"""
29-
Update a models.Flight.env in the database.
29+
Update a models.Flight.environment in the database.
3030
3131
Args:
3232
flight_id: str
33-
env: models.Env
33+
environment: models.Environment
3434
3535
Returns:
3636
views.FlightUpdated
@@ -39,7 +39,7 @@ async def update_env_by_flight_id(
3939
HTTP 404 Not Found: If the flight is not found in the database.
4040
"""
4141
flight = await self.get_flight_by_id(flight_id)
42-
flight.environment = env
42+
flight.environment = environment
4343
self.update_flight_by_id(flight_id, flight)
4444
return FlightUpdated(flight_id=flight_id)
4545

lib/models/motor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from enum import Enum
2-
from typing import Optional, Tuple, List, Union, Self, ClassVar
2+
from typing import Optional, Tuple, List, Union, Self, ClassVar, Literal
33
from pydantic import PrivateAttr, model_validator
44

55
from lib.models.interface import ApiBaseModel

lib/routes/environment.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,15 @@ async def delete_environment(environment_id: str) -> EnvironmentDeleted:
9494
status_code=203,
9595
response_class=Response,
9696
)
97-
async def read_rocketpy_env(environment_id: str):
97+
async def get_rocketpy_environment_binary(environment_id: str):
9898
"""
99-
Loads rocketpy.environment as a dill binary
99+
Loads rocketpy.environment as a dill binary.
100+
Currently only amd64 architecture is supported.
100101
101102
## Args
102103
``` environment_id: str ```
103104
"""
104-
with tracer.start_as_current_span("read_rocketpy_env"):
105+
with tracer.start_as_current_span("get_rocketpy_environment_binary"):
105106
headers = {
106107
'Content-Disposition': f'attachment; filename="rocketpy_environment_{environment_id}.dill"'
107108
}

lib/routes/flight.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,15 @@ async def delete_flight(flight_id: str) -> FlightDeleted:
9999
status_code=203,
100100
response_class=Response,
101101
)
102-
async def read_rocketpy_flight(flight_id: str):
102+
async def get_rocketpy_flight_binary(flight_id: str):
103103
"""
104-
Loads rocketpy.flight as a dill binary
104+
Loads rocketpy.flight as a dill binary.
105+
Currently only amd64 architecture is supported.
105106
106107
## Args
107108
``` flight_id: str ```
108109
"""
109-
with tracer.start_as_current_span("read_rocketpy_flight"):
110+
with tracer.start_as_current_span("get_rocketpy_flight_binary"):
110111
controller = FlightController()
111112
headers = {
112113
'Content-Disposition': f'attachment; filename="rocketpy_flight_{flight_id}.dill"'
@@ -120,21 +121,21 @@ async def read_rocketpy_flight(flight_id: str):
120121
)
121122

122123

123-
@router.put("/{flight_id}/env")
124-
async def update_flight_env(flight_id: str, env: EnvironmentModel) -> FlightUpdated:
124+
@router.put("/{flight_id}/environment")
125+
async def update_flight_environment(flight_id: str, environment: EnvironmentModel) -> FlightUpdated:
125126
"""
126127
Updates flight environment
127128
128129
## Args
129130
```
130131
flight_id: Flight ID
131-
env: env object as JSON
132+
environment: env object as JSON
132133
```
133134
"""
134-
with tracer.start_as_current_span("update_flight_env"):
135+
with tracer.start_as_current_span("update_flight_environment"):
135136
controller = FlightController()
136-
return await controller.update_env_by_flight_id(
137-
flight_id, env=env
137+
return await controller.update_environment_by_flight_id(
138+
flight_id, environment=environment
138139
)
139140

140141

lib/routes/motor.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,15 @@ async def delete_motor(motor_id: str) -> MotorDeleted:
9696
status_code=203,
9797
response_class=Response,
9898
)
99-
async def read_rocketpy_motor(motor_id: str):
99+
async def get_rocketpy_motor_binary(motor_id: str):
100100
"""
101-
Loads rocketpy.motor as a dill binary
101+
Loads rocketpy.motor as a dill binary.
102+
Currently only amd64 architecture is supported.
102103
103104
## Args
104105
``` motor_id: str ```
105106
"""
106-
with tracer.start_as_current_span("read_rocketpy_motor"):
107+
with tracer.start_as_current_span("get_rocketpy_motor_binary"):
107108
headers = {
108109
'Content-Disposition': f'attachment; filename="rocketpy_motor_{motor_id}.dill"'
109110
}

lib/routes/rocket.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,15 @@ async def delete_rocket(rocket_id: str) -> RocketDeleted:
9797
status_code=203,
9898
response_class=Response,
9999
)
100-
async def read_rocketpy_rocket(rocket_id: str):
100+
async def get_rocketpy_rocket_binary(rocket_id: str):
101101
"""
102-
Loads rocketpy.rocket as a dill binary
102+
Loads rocketpy.rocket as a dill binary.
103+
Currently only amd64 architecture is supported.
103104
104105
## Args
105106
``` rocket_id: str ```
106107
"""
107-
with tracer.start_as_current_span("read_rocketpy_rocket"):
108+
with tracer.start_as_current_span("get_rocketpy_rocket_binary"):
108109
headers = {
109110
'Content-Disposition': f'attachment; filename="rocketpy_rocket_{rocket_id}.dill"'
110111
}

tests/test_routes/conftest.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ def stub_tank_dump():
4545

4646

4747
@pytest.fixture
48-
def stub_level_tank(stub_tank):
49-
stub_tank.update({'tank_kind': TankKinds.LEVEL, 'liquid_height': 0})
50-
return stub_tank
48+
def stub_level_tank_dump(stub_tank_dump):
49+
stub_tank_dump.update({'tank_kind': TankKinds.LEVEL, 'liquid_height': 0})
50+
return stub_tank_dump
5151

5252

5353
@pytest.fixture
54-
def stub_mass_flow_tank(stub_tank):
55-
stub_tank.update(
54+
def stub_mass_flow_tank_dump(stub_tank_dump):
55+
stub_tank_dump.update(
5656
{
5757
'tank_kind': TankKinds.MASS_FLOW,
5858
'gas_mass_flow_rate_in': 0,
@@ -63,21 +63,21 @@ def stub_mass_flow_tank(stub_tank):
6363
'initial_gas_mass': 0,
6464
}
6565
)
66-
return stub_tank
66+
return stub_tank_dump
6767

6868

6969
@pytest.fixture
70-
def stub_ullage_tank(stub_tank):
71-
stub_tank.update({'tank_kind': TankKinds.ULLAGE, 'ullage': 0})
72-
return stub_tank
70+
def stub_ullage_tank_dump(stub_tank_dump):
71+
stub_tank_dump.update({'tank_kind': TankKinds.ULLAGE, 'ullage': 0})
72+
return stub_tank_dump
7373

7474

7575
@pytest.fixture
76-
def stub_mass_tank(stub_tank):
77-
stub_tank.update(
76+
def stub_mass_tank_dump(stub_tank_dump):
77+
stub_tank_dump.update(
7878
{'tank_kind': TankKinds.MASS, 'liquid_mass': 0, 'gas_mass': 0}
7979
)
80-
return stub_tank
80+
return stub_tank_dump
8181

8282

8383
@pytest.fixture
@@ -109,18 +109,18 @@ def stub_fins_dump():
109109

110110

111111
@pytest.fixture
112-
def stub_rocket_dump(stub_motor, stub_nose_cone, stub_fins):
112+
def stub_rocket_dump(stub_motor_dump, stub_nose_cone_dump, stub_fins_dump):
113113
rocket = RocketModel(
114-
motor=stub_motor,
114+
motor=stub_motor_dump,
115115
radius=0,
116116
mass=0,
117117
motor_position=0,
118118
center_of_mass_without_motor=0,
119119
inertia=[0, 0, 0],
120120
power_off_drag=[(0, 0)],
121121
power_on_drag=[(0, 0)],
122-
nose=stub_nose_cone,
123-
fins=[stub_fins],
122+
nose=stub_nose_cone_dump,
123+
fins=[stub_fins_dump],
124124
coordinate_system_orientation='tail_to_nose',
125125
)
126126
rocket_json = rocket.model_dump_json()

tests/test_routes/test_environments_route.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
from unittest.mock import patch
1+
from unittest.mock import patch, Mock, AsyncMock
22
import json
33
import pytest
44
from fastapi.testclient import TestClient
5-
from fastapi import HTTPException, status
5+
from fastapi import HTTPException
66
from lib.models.environment import EnvironmentModel
7-
from lib.controllers.environment import EnvironmentController
87
from lib.views.environment import (
8+
EnvironmentView,
99
EnvironmentCreated,
1010
EnvironmentUpdated,
11+
EnvironmentRetrieved,
1112
EnvironmentDeleted,
1213
EnvironmentSummary,
1314
)
@@ -22,6 +23,7 @@ def stub_environment_summary_dump():
2223
env_summary_json = env_summary.model_dump_json()
2324
return json.loads(env_summary_json)
2425

26+
2527
@pytest.fixture(autouse=True)
2628
def mock_controller_instance():
2729
with patch(
@@ -34,6 +36,7 @@ def mock_controller_instance():
3436
mock_controller_instance.delete_environment_by_id = Mock()
3537
yield mock_controller_instance
3638

39+
3740
def test_create_environment(stub_environment_dump, mock_controller_instance):
3841
mock_response = AsyncMock(return_value=EnvironmentCreated(environment_id='123'))
3942
mock_controller_instance.post_environment = mock_response
@@ -47,6 +50,7 @@ def test_create_environment(stub_environment_dump, mock_controller_instance):
4750
EnvironmentModel(**stub_environment_dump)
4851
)
4952

53+
5054
def test_create_environment_optional_params(stub_environment_dump, mock_controller_instance):
5155
stub_environment_dump.update({
5256
'latitude': 0,
@@ -65,12 +69,14 @@ def test_create_environment_optional_params(stub_environment_dump, mock_controll
6569
'message': 'Environment successfully created',
6670
}
6771

72+
6873
def test_create_environment_invalid_input():
6974
response = client.post(
7075
'/environments/', json={'latitude': 'foo', 'longitude': 'bar'}
7176
)
7277
assert response.status_code == 422
7378

79+
7480
def test_create_environment_server_error(
7581
stub_environment_dump, mock_controller_instance
7682
):
@@ -80,8 +86,9 @@ def test_create_environment_server_error(
8086
assert response.status_code == 500
8187
assert response.json() == {'detail': 'Internal Server Error'}
8288

83-
def test_read_environment(stub_env):
84-
stub_environment_out = EnvironmentModelOut(environment_id='123', **stub_environment_dump)
89+
90+
def test_read_environment(stub_environment_dump, mock_controller_instance):
91+
stub_environment_out = EnvironmentView(environment_id='123', **stub_environment_dump)
8592
mock_response = AsyncMock(
8693
return_value=EnvironmentRetrieved(environment=stub_environment_out)
8794
)
@@ -92,6 +99,8 @@ def test_read_environment(stub_env):
9299
'message': 'Environment successfully retrieved',
93100
'environment': json.loads(stub_environment_out.model_dump_json()),
94101
}
102+
mock_controller_instance.get_environment_by_id.assert_called_once_with('123')
103+
95104

96105
def test_read_environment_not_found(mock_controller_instance):
97106
mock_response = AsyncMock(side_effect=HTTPException(status_code=404))
@@ -101,13 +110,15 @@ def test_read_environment_not_found(mock_controller_instance):
101110
assert response.json() == {'detail': 'Not Found'}
102111
mock_controller_instance.get_environment_by_id.assert_called_once_with('123')
103112

113+
104114
def test_read_environment_server_error(mock_controller_instance):
105115
mock_response = AsyncMock(side_effect=HTTPException(status_code=500))
106116
mock_controller_instance.get_environment_by_id = mock_response
107117
response = client.get('/environments/123')
108118
assert response.status_code == 500
109119
assert response.json() == {'detail': 'Internal Server Error'}
110120

121+
111122
def test_update_environment(stub_environment_dump, mock_controller_instance):
112123
mock_reponse = AsyncMock(return_value=EnvironmentUpdated(environment_id='123'))
113124
mock_controller_instance.put_environment_by_id = mock_reponse
@@ -120,12 +131,14 @@ def test_update_environment(stub_environment_dump, mock_controller_instance):
120131
'123', EnvironmentModel(**stub_environment_dump)
121132
)
122133

134+
123135
def test_update_environment_invalid_input():
124136
response = client.put(
125137
'/environments/123', json={'consignment': 'foo', 'delivery': 'bar'}
126138
)
127139
assert response.status_code == 422
128140

141+
129142
def test_update_environment_not_found(stub_environment_dump, mock_controller_instance):
130143
mock_reponse = AsyncMock(side_effect=HTTPException(status_code=404))
131144
mock_controller_instance.put_environment_by_id = mock_reponse
@@ -136,6 +149,7 @@ def test_update_environment_not_found(stub_environment_dump, mock_controller_ins
136149
'123', EnvironmentModel(**stub_environment_dump)
137150
)
138151

152+
139153
def test_update_environment_server_error(
140154
stub_environment_dump, mock_controller_instance
141155
):
@@ -145,6 +159,7 @@ def test_update_environment_server_error(
145159
assert response.status_code == 500
146160
assert response.json() == {'detail': 'Internal Server Error'}
147161

162+
148163
def test_delete_environment(mock_controller_instance):
149164
mock_reponse = AsyncMock(return_value=EnvironmentDeleted(environment_id='123'))
150165
mock_controller_instance.delete_environment_by_id = mock_reponse
@@ -155,13 +170,15 @@ def test_delete_environment(mock_controller_instance):
155170
}
156171
mock_controller_instance.delete_environment_by_id.assert_called_once_with('123')
157172

173+
158174
def test_delete_environment_server_error(mock_controller_instance):
159175
mock_response = AsyncMock(side_effect=HTTPException(status_code=500))
160176
mock_controller_instance.delete_environment_by_id = mock_response
161177
response = client.delete('/environments/123')
162178
assert response.status_code == 500
163179
assert response.json() == {'detail': 'Internal Server Error'}
164180

181+
165182
def test_simulate_environment_success(
166183
stub_environment_summary_dump, mock_controller_instance
167184
):
@@ -172,6 +189,7 @@ def test_simulate_environment_success(
172189
assert response.json() == stub_environment_summary_dump
173190
mock_controller_instance.get_environment_simulation.assert_called_once_with('123')
174191

192+
175193
def test_simulate_environment_not_found(mock_controller_instance):
176194
mock_response = AsyncMock(side_effect=HTTPException(status_code=404))
177195
mock_controller_instance.get_environment_simulation = mock_response
@@ -180,14 +198,16 @@ def test_simulate_environment_not_found(mock_controller_instance):
180198
assert response.json() == {'detail': 'Not Found'}
181199
mock_controller_instance.get_environment_simulation.assert_called_once_with('123')
182200

201+
183202
def test_simulate_environment_server_error(mock_controller_instance):
184203
mock_response = AsyncMock(side_effect=HTTPException(status_code=500))
185204
mock_controller_instance.get_environment_simulation = mock_response
186205
response = client.get('/environments/123/summary')
187206
assert response.status_code == 500
188207
assert response.json() == {'detail': 'Internal Server Error'}
189208

190-
def test_read_rocketpy_environment(mock_controller_instance):
209+
210+
def test_read_rocketpy_environment_binary(mock_controller_instance):
191211
mock_response = AsyncMock(return_value=b'rocketpy')
192212
mock_controller_instance.get_rocketpy_environment_binary = mock_response
193213
response = client.get('/environments/123/rocketpy')
@@ -198,7 +218,8 @@ def test_read_rocketpy_environment(mock_controller_instance):
198218
'123'
199219
)
200220

201-
def test_read_rocketpy_environment_not_found(mock_controller_instance):
221+
222+
def test_read_rocketpy_environment_binary_not_found(mock_controller_instance):
202223
mock_response = AsyncMock(side_effect=HTTPException(status_code=404))
203224
mock_controller_instance.get_rocketpy_environment_binary = mock_response
204225
response = client.get('/environments/123/rocketpy')
@@ -208,7 +229,8 @@ def test_read_rocketpy_environment_not_found(mock_controller_instance):
208229
'123'
209230
)
210231

211-
def test_read_rocketpy_environment_server_error(mock_controller_instance):
232+
233+
def test_read_rocketpy_environment_binary_server_error(mock_controller_instance):
212234
mock_response = AsyncMock(side_effect=HTTPException(status_code=500))
213235
mock_controller_instance.get_rocketpy_environment_binary = mock_response
214236
response = client.get('/environments/123/rocketpy')

0 commit comments

Comments
 (0)