1313# Helper functions
1414
1515
16- def assert_approx_equal (test_value , expected_value , atol = 1e-4 , label = "" ):
17- """Asserts if two values are approximately equal. If the values are not
18- approximately equal, an AssertionError is raised, with a message containing
19- the label and the values.
20-
21- Parameters
22- ----------
23- test_value : float
24- Value to be tested
25- expected_value : float
26- Expected value
27- atol : float, optional
28- Absolute tolerance error, by default 1e-4
29- label : str, optional
30- Label to be printed in the error message, by default ""
31- """
32- assert (
33- pytest .approx (test_value , abs = atol ) == expected_value
34- ), f"Assertion failed at { label } : { test_value } != { expected_value } "
35-
36-
3716def setup_rocket_with_given_static_margin (rocket , static_margin ):
3817 """Takes any rocket, removes its aerodynamic surfaces and adds a set of
3918 nose, fins and tail specially designed to have a given static margin.
@@ -731,7 +710,16 @@ def test_rail_buttons_forces(flight_calisto_custom_wind):
731710 assert pytest .approx (0.51337 , abs = atol ) == test .max_rail_button2_shear_force
732711
733712
734- def test_accelerations (flight_calisto_custom_wind ):
713+ @pytest .mark .parametrize (
714+ "params" ,
715+ [
716+ ("t_initial" , 0 , 0 , 0 ),
717+ ("out_of_rail_time" , 0 , 7.8068 , 89.2325 ),
718+ ("apogee_time" , 0.07534 , - 0.058127 , - 9.614386 ),
719+ ("t_final" , 0 , 0 , 0.0017346294117130806 ),
720+ ],
721+ )
722+ def test_accelerations (flight_calisto_custom_wind , params ):
735723 """Tests if the acceleration in some particular points of the trajectory is
736724 correct. The expected values were NOT calculated by hand, it was just
737725 copied from the test results. The results are not expected to change,
@@ -744,23 +732,28 @@ def test_accelerations(flight_calisto_custom_wind):
744732 atol : float, optional
745733 The absolute tolerance error, by default 5e-3
746734 """
735+ expected_attr , * expected_acc = params
736+
747737 test = flight_calisto_custom_wind
738+ t = getattr (test , expected_attr )
748739 atol = 5e-3
749740
750- points = {
751- "t_initial" : (test .t_initial , 0 , 0 , 0 ),
752- "out_of_rail_time" : (test .out_of_rail_time , 0 , 7.8068 , 89.2325 ),
753- "apogee_time" : (test .apogee_time , 0.07534 , - 0.058127 , - 9.614386 ),
754- "t_final" : (test .t_final , 0 , 0 , 0.0017346294117130806 ),
755- }
756-
757- for label , (t , expected_ax , expected_ay , expected_az ) in points .items ():
758- assert_approx_equal (test .ax (t ), expected_ax , atol , label )
759- assert_approx_equal (test .ay (t ), expected_ay , atol , label )
760- assert_approx_equal (test .az (t ), expected_az , atol , label )
741+ assert (
742+ pytest .approx (expected_acc , abs = atol ) == (test .ax (t ), test .ay (t ), test .az (t )),
743+ f"Assertion error for acceleration vector at { expected_attr } ." ,
744+ )
761745
762746
763- def test_velocities (flight_calisto_custom_wind ):
747+ @pytest .mark .parametrize (
748+ "params" ,
749+ [
750+ ("t_initial" , 0 , 0 , 0 ),
751+ ("out_of_rail_time" , 0 , 2.248727 , 25.703072 ),
752+ ("apogee_time" , - 13.209436 , 16.05115 , - 0.000257 ),
753+ ("t_final" , 5 , 2 , - 5.334289 ),
754+ ],
755+ )
756+ def test_velocities (flight_calisto_custom_wind , params ):
764757 """Tests if the velocity in some particular points of the trajectory is
765758 correct. The expected values were NOT calculated by hand, it was just
766759 copied from the test results. The results are not expected to change,
@@ -773,23 +766,28 @@ def test_velocities(flight_calisto_custom_wind):
773766 atol : float, optional
774767 The absolute tolerance error, by default 5e-3
775768 """
769+ expected_attr , * expected_vel = params
770+
776771 test = flight_calisto_custom_wind
772+ t = getattr (test , expected_attr )
777773 atol = 5e-3
778774
779- points = {
780- "t_initial" : (test .t_initial , 0 , 0 , 0 ),
781- "out_of_rail_time" : (test .out_of_rail_time , 0 , 2.248727 , 25.703072 ),
782- "apogee_time" : (test .apogee_time , - 13.209436 , 16.05115 , - 0.000257 ),
783- "t_final" : (test .t_final , 5 , 2 , - 5.334289 ),
784- }
785-
786- for label , (t , expected_vx , expected_vy , expected_vz ) in points .items ():
787- assert_approx_equal (test .vx (t ), expected_vx , atol , label )
788- assert_approx_equal (test .vy (t ), expected_vy , atol , label )
789- assert_approx_equal (test .vz (t ), expected_vz , atol , label )
775+ assert (
776+ pytest .approx (expected_vel , abs = atol ) == (test .vx (t ), test .vy (t ), test .vz (t )),
777+ f"Assertion error for velocity vector at { expected_attr } ." ,
778+ )
790779
791780
792- def test_aerodynamic_forces (flight_calisto_custom_wind ):
781+ @pytest .mark .parametrize (
782+ "params" ,
783+ [
784+ ("t_initial" , 1.6542528 , 0.65918 , - 0.067107 ),
785+ ("out_of_rail_time" , 5.05334 , 2.01364 , - 1.7541 ),
786+ ("apogee_time" , 2.35291 , - 1.8275 , - 0.87851 ),
787+ ("t_final" , 0 , 0 , 141.42421 ),
788+ ],
789+ )
790+ def test_aerodynamic_forces (flight_calisto_custom_wind , params ):
793791 """Tests if the aerodynamic forces in some particular points of the
794792 trajectory is correct. The expected values were NOT calculated by hand, it
795793 was just copied from the test results. The results are not expected to
@@ -802,23 +800,28 @@ def test_aerodynamic_forces(flight_calisto_custom_wind):
802800 atol : float, optional
803801 The absolute tolerance error, by default 5e-3
804802 """
803+ expected_attr , * expected_R = params
804+
805805 test = flight_calisto_custom_wind
806+ t = getattr (test , expected_attr )
806807 atol = 5e-3
807808
808- points = {
809- "t_initial" : (test .t_initial , 1.6542528 , 0.65918 , - 0.067107 ),
810- "out_of_rail_time" : (test .out_of_rail_time , 5.05334 , 2.01364 , - 1.7541 ),
811- "apogee_time" : (test .apogee_time , 2.35291 , - 1.8275 , - 0.87851 ),
812- "t_final" : (test .t_final , 0 , 0 , 141.42421 ),
813- }
814-
815- for label , (t , expected_R1 , expected_R2 , expected_R3 ) in points .items ():
816- assert_approx_equal (test .R1 (t ), expected_R1 , atol , label )
817- assert_approx_equal (test .R2 (t ), expected_R2 , atol , label )
818- assert_approx_equal (test .R3 (t ), expected_R3 , atol , label )
809+ assert (
810+ pytest .approx (expected_R , abs = atol ) == (test .R1 (t ), test .R2 (t ), test .R3 (t )),
811+ f"Assertion error for aerodynamic forces vector at { expected_attr } ." ,
812+ )
819813
820814
821- def test_aerodynamic_moments (flight_calisto_custom_wind ):
815+ @pytest .mark .parametrize (
816+ "params" ,
817+ [
818+ ("t_initial" , 0.17179073815516033 , - 0.431117 , 0 ),
819+ ("out_of_rail_time" , 0.547026 , - 1.3727895 , 0 ),
820+ ("apogee_time" , - 0.5874848151271623 , - 0.7563596 , 0 ),
821+ ("t_final" , 0 , 0 , 0 ),
822+ ],
823+ )
824+ def test_aerodynamic_moments (flight_calisto_custom_wind , params ):
822825 """Tests if the aerodynamic moments in some particular points of the
823826 trajectory is correct. The expected values were NOT calculated by hand, it
824827 was just copied from the test results. The results are not expected to
@@ -831,17 +834,13 @@ def test_aerodynamic_moments(flight_calisto_custom_wind):
831834 atol : float, optional
832835 The absolute tolerance error, by default 5e-3
833836 """
837+ expected_attr , * expected_M = params
838+
834839 test = flight_calisto_custom_wind
840+ t = getattr (test , expected_attr )
835841 atol = 5e-3
836842
837- points = {
838- "t_initial" : (test .t_initial , 0.17179073815516033 , - 0.431117 , 0 ),
839- "out_of_rail_time" : (test .out_of_rail_time , 0.547026 , - 1.3727895 , 0 ),
840- "apogee_time" : (test .apogee_time , - 0.5874848151271623 , - 0.7563596 , 0 ),
841- "t_final" : (test .t_final , 0 , 0 , 0 ),
842- }
843-
844- for label , (t , expected_M1 , expected_M2 , expected_M3 ) in points .items ():
845- assert_approx_equal (test .M1 (t ), expected_M1 , atol , label )
846- assert_approx_equal (test .M2 (t ), expected_M2 , atol , label )
847- assert_approx_equal (test .M3 (t ), expected_M3 , atol , label )
843+ assert (
844+ pytest .approx (expected_M , abs = atol ) == (test .M1 (t ), test .M2 (t ), test .M3 (t )),
845+ f"Assertion error for moment vector at { expected_attr } ." ,
846+ )
0 commit comments