Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements motor route tests #38

Merged
merged 11 commits into from
Nov 16, 2024
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ flake8:
flake8 --ignore E501,E402,F401,W503,C0414 ./tests || true

pylint:
pylint --extension-pkg-whitelist='pydantic' ./lib || true
pylint --extension-pkg-whitelist='pydantic' --disable=E0401,W0621 ./tests || true
pylint ./lib || true
pylint --disable=E0401,W0621 ./tests || true

ruff:
ruff check --fix ./lib || true
Expand Down
11 changes: 5 additions & 6 deletions lib/controllers/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ class EnvController:
"""
Controller for the Environment model.

Init Attributes:
env: models.Env

Enables:
- Simulation of a RocketPy Environment from models.Env
- CRUD operations over models.Env on the database
Expand All @@ -35,6 +32,7 @@ async def create_env(env: Env) -> Union[EnvCreated, HTTPException]:
Returns:
views.EnvCreated
"""
env_repo = None
try:
async with EnvRepository(env) as env_repo:
await env_repo.create_env()
Expand All @@ -58,9 +56,10 @@ async def create_env(env: Env) -> Union[EnvCreated, HTTPException]:
else:
return EnvCreated(env_id=env_repo.env_id)
finally:
logger.info(
f"Call to controllers.environment.create_env completed for Env {env_repo.env_id}"
)
if env_repo:
logger.info(
f"Call to controllers.environment.create_env completed for Env {env_repo.env_id}"
)

@staticmethod
async def get_env_by_id(env_id: str) -> Union[Env, HTTPException]:
Expand Down
36 changes: 13 additions & 23 deletions lib/controllers/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class FlightController:
"""
Controller for the Flight model.

Init Attributes:
flight (models.Flight): Flight model object.

Enables:
- Create a RocketPyFlight object from a Flight model object.
- Generate trajectory simulation from a RocketPyFlight object.
Expand All @@ -39,34 +36,24 @@ class FlightController:

"""

def __init__(
self,
flight: Flight,
):
self.guard(flight)
self._flight = flight

@property
def flight(self) -> Flight:
return self._flight

@flight.setter
def flight(self, flight: Flight):
self._flight = flight

@staticmethod
def guard(flight: Flight):
RocketController.guard(flight.rocket)

async def create_flight(self) -> Union[FlightCreated, HTTPException]:
@classmethod
async def create_flight(
cls, flight: Flight
) -> Union[FlightCreated, HTTPException]:
"""
Create a flight in the database.

Returns:
views.FlightCreated
"""
flight_repo = None
try:
async with FlightRepository(self.flight) as flight_repo:
cls.guard(flight)
async with FlightRepository(flight) as flight_repo:
await flight_repo.create_flight()
except PyMongoError as e:
logger.error(f"controllers.flight.create_flight: PyMongoError {e}")
Expand All @@ -86,8 +73,9 @@ async def create_flight(self) -> Union[FlightCreated, HTTPException]:
else:
return FlightCreated(flight_id=flight_repo.flight_id)
finally:
flight_id = getattr(flight_repo, 'flight_id', 'unknown') if flight_repo else 'unknown'
logger.info(
f"Call to controllers.flight.create_flight completed for Flight {flight_repo.flight_id}"
f"Call to controllers.flight.create_flight completed for Flight {flight_id}"
)

@staticmethod
Expand Down Expand Up @@ -189,8 +177,9 @@ async def get_rocketpy_flight_binary(
f"Call to controllers.flight.get_rocketpy_flight_binary completed for Flight {flight_id}"
)

@classmethod
async def update_flight_by_id(
self, flight_id: str
cls, flight: Flight, flight_id: str
) -> Union[FlightUpdated, HTTPException]:
"""
Update a models.Flight in the database.
Expand All @@ -205,7 +194,8 @@ async def update_flight_by_id(
HTTP 404 Not Found: If the flight is not found in the database.
"""
try:
async with FlightRepository(self.flight) as flight_repo:
cls.guard(flight)
async with FlightRepository(flight) as flight_repo:
await flight_repo.update_flight_by_id(flight_id)
except PyMongoError as e:
logger.error(f"controllers.flight.update_flight: PyMongoError {e}")
Expand Down
37 changes: 15 additions & 22 deletions lib/controllers/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,10 @@ class MotorController:
"""
Controller for the motor model.

Init Attributes:
motor (models.Motor): Motor model object.

Enables:
- Create a rocketpy.Motor object from a Motor model object.
"""

def __init__(self, motor: Motor):
self.guard(motor)
self._motor = motor

@property
def motor(self) -> Motor:
return self._motor

@motor.setter
def motor(self, motor: Motor):
self._motor = motor

@staticmethod
def guard(motor: Motor):
if (
Expand All @@ -51,15 +36,20 @@ def guard(motor: Motor):

# TODO: extend guard to check motor kinds and tank kinds specifics

async def create_motor(self) -> Union[MotorCreated, HTTPException]:
@classmethod
async def create_motor(
cls, motor: Motor
) -> Union[MotorCreated, HTTPException]:
GabrielBarberini marked this conversation as resolved.
Show resolved Hide resolved
"""
Create a models.Motor in the database.

Returns:
views.MotorCreated
"""
motor_repo = None
try:
async with MotorRepository(self.motor) as motor_repo:
cls.guard(motor)
async with MotorRepository(motor) as motor_repo:
GabrielBarberini marked this conversation as resolved.
Show resolved Hide resolved
await motor_repo.create_motor()
except PyMongoError as e:
logger.error(f"controllers.motor.create_motor: PyMongoError {e}")
Expand All @@ -79,9 +69,10 @@ async def create_motor(self) -> Union[MotorCreated, HTTPException]:
else:
return MotorCreated(motor_id=motor_repo.motor_id)
finally:
logger.info(
f"Call to controllers.motor.create_motor completed for Motor {motor_repo.motor_id}"
)
if motor_repo:
logger.info(
f"Call to controllers.motor.create_motor completed for Motor {motor_repo.motor_id}"
)

@staticmethod
async def get_motor_by_id(
Expand Down Expand Up @@ -173,8 +164,9 @@ async def get_rocketpy_motor_binary(
f"Call to controllers.motor.get_rocketpy_motor_binary completed for Motor {motor_id}"
)

@classmethod
async def update_motor_by_id(
self, motor_id: str
cls, motor_id: str, motor: Motor
) -> Union[MotorUpdated, HTTPException]:
"""
Update a motor in the database.
Expand All @@ -189,7 +181,8 @@ async def update_motor_by_id(
HTTP 404 Not Found: If the motor is not found in the database.
"""
try:
async with MotorRepository(self.motor) as motor_repo:
cls.guard(motor)
async with MotorRepository(motor) as motor_repo:
GabrielBarberini marked this conversation as resolved.
Show resolved Hide resolved
await motor_repo.update_motor_by_id(motor_id)
except PyMongoError as e:
logger.error(f"controllers.motor.update_motor: PyMongoError {e}")
Expand Down
40 changes: 15 additions & 25 deletions lib/controllers/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,28 @@ class RocketController:
"""
Controller for the Rocket model.

Init Attributes:
rocket: models.Rocket.

Enables:
- CRUD operations over models.Rocket on the database.
"""

def __init__(
self,
rocket: Rocket,
):
self.guard(rocket)
self._rocket = rocket

@property
def rocket(self) -> Rocket:
return self._rocket

@rocket.setter
def rocket(self, rocket: Rocket):
self._rocket = rocket

@staticmethod
def guard(rocket: Rocket):
MotorController.guard(rocket.motor)

async def create_rocket(self) -> Union[RocketCreated, HTTPException]:
@classmethod
async def create_rocket(
cls, rocket: Rocket
) -> Union[RocketCreated, HTTPException]:
"""
Create a models.Rocket in the database.

Returns:
views.RocketCreated
"""
rocket_repo = None
try:
async with RocketRepository(self.rocket) as rocket_repo:
cls.guard(rocket)
async with RocketRepository(rocket) as rocket_repo:
await rocket_repo.create_rocket()
except PyMongoError as e:
logger.error(f"controllers.rocket.create_rocket: PyMongoError {e}")
Expand All @@ -76,9 +63,10 @@ async def create_rocket(self) -> Union[RocketCreated, HTTPException]:
else:
return RocketCreated(rocket_id=rocket_repo.rocket_id)
finally:
logger.info(
f"Call to controllers.rocket.create_rocket completed for Rocket {rocket_repo.rocket_id}"
)
if rocket_repo:
logger.info(
f"Call to controllers.rocket.create_rocket completed for Rocket {rocket_repo.rocket_id}"
)

@staticmethod
async def get_rocket_by_id(
Expand Down Expand Up @@ -175,8 +163,9 @@ async def get_rocketpy_rocket_binary(
f"Call to controllers.rocket.get_rocketpy_rocket_binary completed for Rocket {rocket_id}"
)

@classmethod
async def update_rocket_by_id(
self, rocket_id: str
cls, rocket: Rocket, rocket_id: str
) -> Union[RocketUpdated, HTTPException]:
"""
Update a models.Rocket in the database.
Expand All @@ -191,7 +180,8 @@ async def update_rocket_by_id(
HTTP 404 Not Found: If the rocket is not found in the database.
"""
try:
async with RocketRepository(self.rocket) as rocket_repo:
cls.guard(rocket)
async with RocketRepository(rocket) as rocket_repo:
await rocket_repo.update_rocket_by_id(rocket_id)
except PyMongoError as e:
logger.error(f"controllers.rocket.update_rocket: PyMongoError {e}")
Expand Down
22 changes: 11 additions & 11 deletions lib/models/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,25 @@ class MotorTank(BaseModel):
discretize: int

# Level based tank parameters
liquid_height: Optional[float]
liquid_height: Optional[float] = None

# Mass based tank parameters
liquid_mass: Optional[float]
gas_mass: Optional[float]
liquid_mass: Optional[float] = None
gas_mass: Optional[float] = None

# Mass flow based tank parameters
gas_mass_flow_rate_in: Optional[float]
gas_mass_flow_rate_out: Optional[float]
liquid_mass_flow_rate_in: Optional[float]
liquid_mass_flow_rate_out: Optional[float]
initial_liquid_mass: Optional[float]
initial_gas_mass: Optional[float]
gas_mass_flow_rate_in: Optional[float] = None
gas_mass_flow_rate_out: Optional[float] = None
liquid_mass_flow_rate_in: Optional[float] = None
liquid_mass_flow_rate_out: Optional[float] = None
initial_liquid_mass: Optional[float] = None
initial_gas_mass: Optional[float] = None

# Ullage based tank parameters
ullage: Optional[float]
ullage: Optional[float] = None

# Optional parameters
name: Optional[str]
name: Optional[str] = None

# Computed parameters
tank_kind: TankKinds = TankKinds.MASS_FLOW
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/flight.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def create_flight(
"""
with tracer.start_as_current_span("create_flight"):
flight.rocket.motor.set_motor_kind(motor_kind)
return await FlightController(flight).create_flight()
return await FlightController.create_flight(flight)


@router.get("/{flight_id}")
Expand Down Expand Up @@ -146,7 +146,7 @@ async def update_flight(
"""
with tracer.start_as_current_span("update_flight"):
flight.rocket.motor.set_motor_kind(motor_kind)
return await FlightController(flight).update_flight_by_id(flight_id)
return await FlightController.update_flight_by_id(flight, flight_id)


@router.get("/{flight_id}/summary")
Expand Down
6 changes: 3 additions & 3 deletions lib/routes/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def create_motor(motor: Motor, motor_kind: MotorKinds) -> MotorCreated:
"""
with tracer.start_as_current_span("create_motor"):
motor.set_motor_kind(motor_kind)
return await MotorController(motor).create_motor()
return await MotorController.create_motor(motor)


@router.get("/{motor_id}")
Expand Down Expand Up @@ -68,11 +68,11 @@ async def update_motor(
"""
with tracer.start_as_current_span("update_motor"):
motor.set_motor_kind(motor_kind)
return await MotorController(motor).update_motor_by_id(motor_id)
return await MotorController.update_motor_by_id(motor_id, motor)


@router.get(
"/rocketpy/{motor_id}",
"/{motor_id}/rocketpy",
GabrielBarberini marked this conversation as resolved.
Show resolved Hide resolved
responses={
203: {
"description": "Binary file download",
Expand Down
4 changes: 2 additions & 2 deletions lib/routes/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def create_rocket(
"""
with tracer.start_as_current_span("create_rocket"):
rocket.motor.set_motor_kind(motor_kind)
return await RocketController(rocket).create_rocket()
return await RocketController.create_rocket(rocket)


@router.get("/{rocket_id}")
Expand Down Expand Up @@ -73,7 +73,7 @@ async def update_rocket(
"""
with tracer.start_as_current_span("update_rocket"):
rocket.motor.set_motor_kind(motor_kind)
return await RocketController(rocket).update_rocket_by_id(rocket_id)
return await RocketController.update_rocket_by_id(rocket, rocket_id)


@router.get(
Expand Down
Loading
Loading