Skip to content

Commit 9fdc704

Browse files
committed
Clean up load_from_thrustcurve_api and improve test_load_from_thrustcurve_api with exception testing
1 parent da39fcb commit 9fdc704

File tree

2 files changed

+70
-12
lines changed

2 files changed

+70
-12
lines changed

rocketpy/motors/motor.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import base64
2+
import logging
23
import re
34
import tempfile
45
import warnings
@@ -9,7 +10,7 @@
910

1011
import numpy as np
1112
import requests
12-
import logging
13+
1314
from ..mathutils.function import Function, funcify_method
1415
from ..plots.motor_plots import _MotorPlots
1516
from ..prints.motor_prints import _MotorPrints
@@ -1918,7 +1919,7 @@ def load_from_rse_file(
19181919
interpolation_method=interpolation_method,
19191920
coordinate_system_orientation=coordinate_system_orientation,
19201921
)
1921-
1922+
19221923
@staticmethod
19231924
def load_from_thrustcurve_api(name: str, **kwargs):
19241925
"""
@@ -1964,7 +1965,7 @@ def load_from_thrustcurve_api(name: str, **kwargs):
19641965
motor_id = motor_info.get("motorId")
19651966
designation = motor_info.get("designation", "").replace("/", "-")
19661967
manufacturer = motor_info.get("manufacturer", "")
1967-
# Logging the fact that the motor was found
1968+
# Logging the fact that the motor was found
19681969
logger.info(f"Motor found: {designation} ({manufacturer})")
19691970

19701971
# Step 2. Download the .eng file
@@ -1976,11 +1977,15 @@ def load_from_thrustcurve_api(name: str, **kwargs):
19761977
dl_data = dl_response.json()
19771978

19781979
if not dl_data.get("results"):
1979-
raise ValueError(f"No .eng file found for motor '{name}' in the ThrustCurve API.")
1980+
raise ValueError(
1981+
f"No .eng file found for motor '{name}' in the ThrustCurve API."
1982+
)
19801983

19811984
data_base64 = dl_data["results"][0].get("data")
19821985
if not data_base64:
1983-
raise ValueError(f"Downloaded .eng data for motor '{name}' is empty or invalid.")
1986+
raise ValueError(
1987+
f"Downloaded .eng data for motor '{name}' is empty or invalid."
1988+
)
19841989

19851990
data_bytes = base64.b64decode(data_base64)
19861991

@@ -1993,9 +1998,7 @@ def load_from_thrustcurve_api(name: str, **kwargs):
19931998
tmp_file.flush()
19941999
tmp_path = tmp_file.name
19952000

1996-
1997-
motor_instance = GenericMotor.load_from_eng_file(tmp_path, **kwargs)
1998-
return motor_instance
2001+
return GenericMotor.load_from_eng_file(tmp_path, **kwargs)
19992002
finally:
20002003
# Ensuring the temporary file is removed
20012004
if tmp_path and path.exists(tmp_path):

tests/unit/motors/test_genericmotor.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import base64
2+
13
import numpy as np
24
import pytest
3-
import scipy.integrate
45
import requests
5-
import base64
6-
6+
import scipy.integrate
77

88
from rocketpy import Function, Motor
99

@@ -215,6 +215,7 @@ def test_load_from_rse_file(generic_motor):
215215
assert thrust_curve[-1][0] == 2.2 # Last point of time
216216
assert thrust_curve[-1][1] == 0.0 # Last thrust point
217217

218+
218219
def test_load_from_thrustcurve_api(monkeypatch, generic_motor):
219220
"""
220221
Tests the GenericMotor.load_from_thrustcurve_api method with mocked ThrustCurve API responses.
@@ -224,7 +225,7 @@ def test_load_from_thrustcurve_api(monkeypatch, generic_motor):
224225
The pytest monkeypatch fixture for mocking.
225226
generic_motor : rocketpy.GenericMotor
226227
The GenericMotor object to be used in the tests.
227-
228+
228229
"""
229230

230231
class MockResponse:
@@ -299,3 +300,57 @@ def mock_get(url, params=None):
299300
assert motor.thrust.y_array == pytest.approx(
300301
Function(points, "Time (s)", "Thrust (N)", "linear", "zero").y_array
301302
)
303+
304+
# 1. No motor found
305+
def mock_get_no_motor(url, params=None):
306+
if "search.json" in url:
307+
return MockResponse({"results": []})
308+
return MockResponse({"results": []})
309+
310+
monkeypatch.setattr(requests, "get", mock_get_no_motor)
311+
with pytest.raises(ValueError, match="No motor found"):
312+
type(generic_motor).load_from_thrustcurve_api("NonexistentMotor")
313+
314+
# 2. No .eng file found
315+
def mock_get_no_eng(url, params=None):
316+
if "search.json" in url:
317+
return MockResponse(
318+
{
319+
"results": [
320+
{
321+
"motorId": "123",
322+
"designation": "Fake",
323+
"manufacturer": "Test",
324+
}
325+
]
326+
}
327+
)
328+
elif "download.json" in url:
329+
return MockResponse({"results": []})
330+
return MockResponse({})
331+
332+
monkeypatch.setattr(requests, "get", mock_get_no_eng)
333+
with pytest.raises(ValueError, match="No .eng file found"):
334+
type(generic_motor).load_from_thrustcurve_api("FakeMotor")
335+
336+
# 3. Empty .eng data
337+
def mock_get_empty_data(url, params=None):
338+
if "search.json" in url:
339+
return MockResponse(
340+
{
341+
"results": [
342+
{
343+
"motorId": "123",
344+
"designation": "Fake",
345+
"manufacturer": "Test",
346+
}
347+
]
348+
}
349+
)
350+
elif "download.json" in url:
351+
return MockResponse({"results": [{"data": ""}]})
352+
return MockResponse({})
353+
354+
monkeypatch.setattr(requests, "get", mock_get_empty_data)
355+
with pytest.raises(ValueError, match="Downloaded .eng data"):
356+
type(generic_motor).load_from_thrustcurve_api("FakeMotor")

0 commit comments

Comments
 (0)