Skip to content

Commit ae7d4b0

Browse files
refactored tests
1 parent 699236e commit ae7d4b0

3 files changed

Lines changed: 112 additions & 43 deletions

File tree

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,52 @@
11
import pytest
2+
23
from rocketpy.motors.point_mass_motor import PointMassMotor
34

5+
46
def test_init_required_args():
5-
m = PointMassMotor(thrust_source=10, dry_mass=1.0, propellant_initial_mass=0.5, burn_time=1.2)
7+
m = PointMassMotor(
8+
thrust_source=10, dry_mass=1.0, propellant_initial_mass=0.5, burn_time=1.2
9+
)
610
assert isinstance(m, PointMassMotor)
711
assert m.dry_mass == 1.0
812
assert m.propellant_initial_mass == 0.5
13+
assert m.burn_time == 1.2
14+
915

1016
def test_missing_required_args_raises():
17+
# TODO: in the future we would like to capture specific RocketPy Exceptions
1118
with pytest.raises(ValueError):
1219
PointMassMotor(10, 1.0, None, 1)
1320
with pytest.raises(ValueError):
1421
PointMassMotor(10, 1.0, 1.2)
1522
with pytest.raises(TypeError):
1623
PointMassMotor([], 1.0, 1.2, 1)
1724

25+
1826
def test_exhaustvelocity_and_totalmassflowrate():
19-
m = PointMassMotor(thrust_source=10, dry_mass=1.0, propellant_initial_mass=1.0, burn_time=2.0)
27+
m = PointMassMotor(
28+
thrust_source=10, dry_mass=1.0, propellant_initial_mass=1.0, burn_time=2.0
29+
)
2030
ev_func = m.exhaustvelocity()
21-
assert hasattr(ev_func, 'getValue')
31+
assert hasattr(ev_func, "getValue")
2232
tmf_func = m.totalmassflowrate
23-
assert hasattr(tmf_func, 'getValue')
33+
assert hasattr(tmf_func, "getValue")
34+
2435

2536
def test_zero_inertias():
26-
m = PointMassMotor(thrust_source=10, dry_mass=1.0, propellant_initial_mass=1.0, burn_time=2.0)
37+
m = PointMassMotor(
38+
thrust_source=10, dry_mass=1.0, propellant_initial_mass=1.0, burn_time=2.0
39+
)
2740
assert m.propellantI11().getValue(0) == 0
2841
assert m.propellantI22().getValue(0) == 0
2942
assert m.propellantI33().getValue(0) == 0
3043

44+
3145
def test_callable_thrust():
32-
m = PointMassMotor(thrust_source=lambda t: 100*t, dry_mass=2, propellant_initial_mass=2, burn_time=4)
46+
m = PointMassMotor(
47+
thrust_source=lambda t: 100 * t,
48+
dry_mass=2,
49+
propellant_initial_mass=2,
50+
burn_time=4,
51+
)
3352
assert m.thrust_source(0.5) == 50

tests/unit/rocket/test_point_mass_rocket.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
from rocketpy.rocket.rocket import PointMassRocket
21
from rocketpy.motors.point_mass_motor import PointMassMotor
2+
from rocketpy.rocket.rocket import PointMassRocket
3+
34

45
def test_point_mass_rocket_basic_properties():
56
motor = PointMassMotor(10, 1.0, 0.5, 1.0)
@@ -8,14 +9,15 @@ def test_point_mass_rocket_basic_properties():
89
mass=2.0,
910
center_of_mass_without_motor=0.1,
1011
power_off_drag=0.7,
11-
power_on_drag=0.8
12+
power_on_drag=0.8,
1213
)
1314
rocket.addmotor(motor, 0)
1415
assert rocket.radius == 0.05
1516
assert rocket.mass == 2.0
1617
assert rocket.motor is motor
1718
assert rocket.dryI11 == 0.0 # 3-DOF: inertias are forced zero
1819

20+
1921
def test_structural_and_total_mass():
2022
"""Test structural and total mass properties of point mass rocket."""
2123
motor = PointMassMotor(10, 1.0, 1.1, 2.0)
@@ -24,15 +26,16 @@ def test_structural_and_total_mass():
2426
mass=2.5,
2527
center_of_mass_without_motor=0,
2628
power_off_drag=0.3,
27-
power_on_drag=0.4
29+
power_on_drag=0.4,
2830
)
2931
rocket.addmotor(motor, 0)
30-
32+
3133
# Test that structural mass and total mass are calculated correctly
3234
assert rocket.mass == 2.5
3335
expected_total_mass = rocket.mass + motor.propellant_initial_mass
3436
assert abs(rocket.total_mass(0) - expected_total_mass) < 1e-6
3537

38+
3639
def test_add_motor_overwrites():
3740
"""Test that adding a motor overwrites the previous motor."""
3841
motor1 = PointMassMotor(10, 1, 1.1, 2.0)
Lines changed: 80 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,90 @@
1-
import pytest
21
import numpy as np
3-
from rocketpy.simulation.flight import Flight
4-
from rocketpy.rocket.rocket import PointMassRocket
2+
import pytest
3+
54
from rocketpy.motors.point_mass_motor import PointMassMotor
5+
from rocketpy.rocket.point_mass_rocket import PointMassRocket
6+
from rocketpy.simulation.flight import Flight
7+
8+
9+
@pytest.fixture
10+
def point_mass_motor():
11+
"""Simple PointMassMotor for 3-DOF tests.
12+
13+
Returns
14+
-------
15+
rocketpy.PointMassMotor
16+
"""
17+
return PointMassMotor(
18+
thrust_source=10,
19+
dry_mass=1.0,
20+
propellant_initial_mass=0.5,
21+
burn_time=2.2,
22+
)
623

7-
class DummyEnv:
8-
# Minimal stub, adapt with real Environment in your RocketPy setup
9-
windvelocityx = windvelocityy = speedofsound = pressure = density = dynamicviscosity = lambda self: 0
10-
gravity = lambda self: 9.81
11-
elevation = 0
12-
def __getattr__(self, name):
13-
return lambda *a, **k: 0
14-
15-
def make_simple_3dof_components():
16-
env = DummyEnv()
17-
motor = PointMassMotor(10, dry_mass=1.0, propellant_initial_mass=0.5, burn_time=2.2)
18-
rocket = PointMassRocket(0.05, 2.0, 0.1, 0.5, 0.6)
19-
rocket.addmotor(motor, 0)
20-
return env, rocket
21-
22-
def test_3dof_simulation_mode_autoset():
23-
env, rocket = make_simple_3dof_components()
24-
flight = Flight(rocket=rocket, environment=env, rail_length=1, simulation_mode="3 DOF")
24+
25+
@pytest.fixture
26+
def point_mass_rocket(point_mass_motor):
27+
"""Simple PointMassRocket for 3-DOF tests.
28+
29+
Returns
30+
-------
31+
rocketpy.PointMassRocket
32+
"""
33+
rocket = PointMassRocket(
34+
radius=0.05,
35+
mass=2.0,
36+
center_of_mass_without_motor=0.1,
37+
power_off_drag=0.5,
38+
power_on_drag=0.6,
39+
)
40+
rocket.add_motor(point_mass_motor, position=0)
41+
return rocket
42+
43+
44+
def test_3dof_simulation_mode_autoset(example_plain_env, point_mass_rocket):
45+
"""Test that simulation mode is correctly set to 3 DOF."""
46+
flight = Flight(
47+
rocket=point_mass_rocket,
48+
environment=example_plain_env,
49+
rail_length=1,
50+
simulation_mode="3 DOF",
51+
)
2552
assert flight.simulation_mode == "3 DOF"
2653

27-
def test_3dof_simulation_mode_warning(monkeypatch):
28-
env, rocket = make_simple_3dof_components()
54+
55+
def test_3dof_simulation_mode_warning(
56+
monkeypatch, example_plain_env, point_mass_rocket
57+
):
58+
"""Test that a warning is issued when 6 DOF is requested with PointMassRocket."""
2959
monkeypatch.setattr("warnings.warn", lambda *a, **k: None)
30-
f = Flight(rocket=rocket, environment=env, rail_length=1, simulation_mode="6 DOF")
31-
assert f.simulation_mode == "3 DOF"
60+
flight = Flight(
61+
rocket=point_mass_rocket,
62+
environment=example_plain_env,
63+
rail_length=1,
64+
simulation_mode="6 DOF",
65+
)
66+
assert flight.simulation_mode == "3 DOF"
3267

33-
def test_3dof_equations_of_motion_functions():
34-
env, rocket = make_simple_3dof_components()
35-
flight = Flight(rocket=rocket, environment=env, rail_length=1, simulation_mode="3 DOF")
36-
u = [0]*13 # Proper size for generalized state for 3/6 DOF probably
68+
69+
def test_3dof_equations_of_motion_functions(example_plain_env, point_mass_rocket):
70+
"""Test that 3-DOF equations of motion return valid results."""
71+
flight = Flight(
72+
rocket=point_mass_rocket,
73+
environment=example_plain_env,
74+
rail_length=1,
75+
simulation_mode="3 DOF",
76+
)
77+
u = [0] * 13 # Generalized state vector size
3778
result = flight.udotgeneralized3dof(0, u)
38-
assert isinstance(result, list) or isinstance(result, np.ndarray)
79+
assert isinstance(result, (list, np.ndarray))
80+
3981

40-
def test_invalid_simulation_mode():
41-
env, rocket = make_simple_3dof_components()
82+
def test_invalid_simulation_mode(example_plain_env, point_mass_rocket):
83+
"""Test that invalid simulation mode raises ValueError."""
4284
with pytest.raises(ValueError):
43-
Flight(rocket=rocket, environment=env, rail_length=1, simulation_mode="2 DOF")
85+
Flight(
86+
rocket=point_mass_rocket,
87+
environment=example_plain_env,
88+
rail_length=1,
89+
simulation_mode="2 DOF",
90+
)

0 commit comments

Comments
 (0)