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
26 changes: 10 additions & 16 deletions lib/controllers/motor.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@ class MotorController:
- 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 +39,19 @@ 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
"""
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 Down Expand Up @@ -173,8 +165,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 +182,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
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
5 changes: 2 additions & 3 deletions lib/views/environment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Optional, Any
from datetime import datetime, timedelta
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict
from lib.models.environment import AtmosphericModelTypes
from lib.utils import to_python_primitive

Expand Down Expand Up @@ -49,8 +49,7 @@ class EnvSummary(BaseModel):
geodesic_to_utm: Optional[Any] = None
utm_to_geodesic: Optional[Any] = None

class Config:
json_encoders = {Any: to_python_primitive}
model_config = ConfigDict(json_encoders={Any: to_python_primitive})


class EnvCreated(BaseModel):
Expand Down
5 changes: 2 additions & 3 deletions lib/views/flight.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Optional, Any
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict
from lib.models.flight import Flight
from lib.views.rocket import RocketView, RocketSummary
from lib.views.environment import EnvSummary
Expand Down Expand Up @@ -151,8 +151,7 @@ class FlightSummary(RocketSummary, EnvSummary):
z_impact: Optional[Any] = None
flight_phases: Optional[Any] = None

class Config:
json_encoders = {Any: to_python_primitive}
model_config = ConfigDict(json_encoders={Any: to_python_primitive})


class FlightCreated(BaseModel):
Expand Down
5 changes: 2 additions & 3 deletions lib/views/motor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import List, Any, Optional
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict
from lib.models.motor import Motor, MotorKinds, CoordinateSystemOrientation
from lib.utils import to_python_primitive

Expand Down Expand Up @@ -69,8 +69,7 @@ class MotorSummary(BaseModel):
total_mass_flow_rate: Optional[Any] = None
thrust: Optional[Any] = None

class Config:
json_encoders = {Any: to_python_primitive}
model_config = ConfigDict(json_encoders={Any: to_python_primitive})


class MotorCreated(BaseModel):
Expand Down
5 changes: 2 additions & 3 deletions lib/views/rocket.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from typing import Any, Optional
from pydantic import BaseModel
from pydantic import BaseModel, ConfigDict
from lib.models.rocket import Rocket, CoordinateSystemOrientation
from lib.views.motor import MotorView, MotorSummary
from lib.utils import to_python_primitive
Expand Down Expand Up @@ -38,8 +38,7 @@ class RocketSummary(MotorSummary):
thrust_to_weight: Optional[Any] = None
total_lift_coeff_der: Optional[Any] = None

class Config:
json_encoders = {Any: to_python_primitive}
model_config = ConfigDict(json_encoders={Any: to_python_primitive})


class RocketCreated(BaseModel):
Expand Down
Loading
Loading