Skip to content

Commit cec0c8f

Browse files
committed
TST: count the reseeds, and cover two air brakes on one spec
The source scan reads create_object for a literal loop over self.collection, so a helper, a local alias or a getattr would hide a collection from it. Counting what each entry actually receives is the check that survives a refactor, and it is the only one that fails when an entry is reseeded twice. The air brakes are a plain list and take a different route through the reseed than the positioned collections, so two of them on one spec are worth their own case. Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
1 parent e8a1bd6 commit cec0c8f

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

‎tests/unit/stochastic/test_stochastic_rocket_seeding.py‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import ast
77
import inspect
88

9+
from rocketpy.rocket.components import Components
910
from rocketpy.stochastic import StochasticAirBrakes, StochasticParachute
1011
from rocketpy.stochastic.stochastic_model import StochasticModel
1112

@@ -30,6 +31,13 @@ def _drawn(component):
3031
return next(component.dict_generator())
3132

3233

34+
def _members_of(collection):
35+
"""Components yields (component, position) pairs; a plain list does not."""
36+
if isinstance(collection, Components):
37+
return [component for component, _ in collection]
38+
return list(collection)
39+
40+
3341
def test_two_components_with_one_spec_do_not_draw_the_same_values(
3442
stochastic_calisto, calisto_main_chute
3543
):
@@ -159,3 +167,71 @@ def parachute_draw():
159167
stochastic_calisto.add_nose(stochastic_nose_cone, position=1.1)
160168

161169
assert parachute_draw() == before
170+
171+
172+
def test_every_entry_is_reseeded_exactly_once(
173+
monkeypatch, stochastic_calisto, calisto_main_chute, calisto_air_brakes_clamp_on
174+
):
175+
"""Counted rather than read off the source.
176+
177+
The scan above reads ``create_object`` for ``for x in self.collection``, so
178+
a helper, a local alias or a ``getattr`` would hide a collection from it.
179+
This counts what actually happens.
180+
"""
181+
stochastic_calisto.add_parachute(
182+
StochasticParachute(parachute=calisto_main_chute, cd_s=0.1, lag=0.2)
183+
)
184+
stochastic_calisto.add_air_brakes(
185+
StochasticAirBrakes(
186+
air_brakes=calisto_air_brakes_clamp_on.air_brakes[0],
187+
deployment_level=(0.5, 0.1),
188+
),
189+
calisto_air_brakes_clamp_on._controllers[0],
190+
)
191+
192+
counted = {}
193+
194+
def recording(self, seed=None):
195+
counted[id(self)] = counted.get(id(self), 0) + 1
196+
return _REAL_SET_STOCHASTIC(self, seed)
197+
198+
monkeypatch.setattr(StochasticModel, "_set_stochastic", recording)
199+
stochastic_calisto._set_stochastic(3)
200+
201+
entries = [
202+
component
203+
for name in type(stochastic_calisto)._stochastic_collections()
204+
for component in _members_of(getattr(stochastic_calisto, name))
205+
]
206+
207+
assert entries, "no components to count"
208+
assert all(counted.get(id(entry)) == 1 for entry in entries), {
209+
type(entry).__name__: counted.get(id(entry)) for entry in entries
210+
}
211+
212+
213+
def test_two_air_brakes_with_one_spec_stay_independent(
214+
stochastic_calisto, calisto_air_brakes_clamp_on
215+
):
216+
"""The air brakes are a plain list, so they take a different route through
217+
the reseed than the positioned collections do."""
218+
for _ in range(2):
219+
stochastic_calisto.add_air_brakes(
220+
StochasticAirBrakes(
221+
air_brakes=calisto_air_brakes_clamp_on.air_brakes[0],
222+
deployment_level=(0.5, 0.1),
223+
),
224+
calisto_air_brakes_clamp_on._controllers[0],
225+
)
226+
227+
def drawn(seed):
228+
stochastic_calisto._set_stochastic(seed)
229+
return [
230+
_drawn(brake)["deployment_level"] for brake in stochastic_calisto.air_brakes
231+
]
232+
233+
first = drawn(808)
234+
235+
assert first[0] != first[1], "two air brakes drew the same value"
236+
assert drawn(808) == first
237+
assert drawn(809) != first

0 commit comments

Comments
 (0)