99plt .rcParams .update ({"figure.max_open_warning" : 0 })
1010
1111
12+ def _post_test_file_cleanup ():
13+ """Clean monte carlo files after test session if they exist."""
14+ if os .path .exists ("monte_carlo_class_example.kml" ):
15+ os .remove ("monte_carlo_class_example.kml" )
16+ if os .path .exists ("monte_carlo_test.errors.txt" ):
17+ os .remove ("monte_carlo_test.errors.txt" )
18+ if os .path .exists ("monte_carlo_test.inputs.txt" ):
19+ os .remove ("monte_carlo_test.inputs.txt" )
20+ if os .path .exists ("monte_carlo_test.outputs.txt" ):
21+ os .remove ("monte_carlo_test.outputs.txt" )
22+
23+
1224@pytest .mark .slow
1325@pytest .mark .parametrize ("parallel" , [False , True ])
1426def test_monte_carlo_simulate (monte_carlo_calisto , parallel ):
@@ -19,27 +31,29 @@ def test_monte_carlo_simulate(monte_carlo_calisto, parallel):
1931 monte_carlo_calisto : MonteCarlo
2032 The MonteCarlo object, this is a pytest fixture.
2133 """
22- # NOTE: this is really slow, it runs 10 flight simulations
23- monte_carlo_calisto .simulate (
24- number_of_simulations = 10 , append = False , parallel = parallel
25- )
26-
27- assert monte_carlo_calisto .num_of_loaded_sims == 10
28- assert monte_carlo_calisto .number_of_simulations == 10
29- assert str (monte_carlo_calisto .filename .name ) == "monte_carlo_test"
30- assert str (monte_carlo_calisto .error_file .name ) == "monte_carlo_test.errors.txt"
31- assert str (monte_carlo_calisto .output_file .name ) == "monte_carlo_test.outputs.txt"
32- assert np .isclose (
33- monte_carlo_calisto .processed_results ["apogee" ][0 ], 4711 , rtol = 0.2
34- )
35- assert np .isclose (
36- monte_carlo_calisto .processed_results ["impact_velocity" ][0 ],
37- - 5.234 ,
38- rtol = 0.2 ,
39- )
40- os .remove ("monte_carlo_test.errors.txt" )
41- os .remove ("monte_carlo_test.outputs.txt" )
42- os .remove ("monte_carlo_test.inputs.txt" )
34+ try :
35+ # NOTE: this is really slow, it runs 10 flight simulations
36+ monte_carlo_calisto .simulate (
37+ number_of_simulations = 10 , append = False , parallel = parallel
38+ )
39+
40+ assert monte_carlo_calisto .num_of_loaded_sims == 10
41+ assert monte_carlo_calisto .number_of_simulations == 10
42+ assert str (monte_carlo_calisto .filename .name ) == "monte_carlo_test"
43+ assert str (monte_carlo_calisto .error_file .name ) == "monte_carlo_test.errors.txt"
44+ assert (
45+ str (monte_carlo_calisto .output_file .name ) == "monte_carlo_test.outputs.txt"
46+ )
47+ assert np .isclose (
48+ monte_carlo_calisto .processed_results ["apogee" ][0 ], 4711 , rtol = 0.2
49+ )
50+ assert np .isclose (
51+ monte_carlo_calisto .processed_results ["impact_velocity" ][0 ],
52+ - 5.234 ,
53+ rtol = 0.2 ,
54+ )
55+ finally :
56+ _post_test_file_cleanup ()
4357
4458
4559def test_monte_carlo_set_inputs_log (monte_carlo_calisto ):
@@ -50,14 +64,17 @@ def test_monte_carlo_set_inputs_log(monte_carlo_calisto):
5064 monte_carlo_calisto : MonteCarlo
5165 The MonteCarlo object, this is a pytest fixture.
5266 """
53- monte_carlo_calisto .input_file = "tests/fixtures/monte_carlo/example.inputs.txt"
54- monte_carlo_calisto .set_inputs_log ()
55- assert len (monte_carlo_calisto .inputs_log ) == 100
56- assert all (isinstance (item , dict ) for item in monte_carlo_calisto .inputs_log )
57- assert all (
58- "gravity" in item and "elevation" in item
59- for item in monte_carlo_calisto .inputs_log
60- )
67+ try :
68+ monte_carlo_calisto .input_file = "tests/fixtures/monte_carlo/example.inputs.txt"
69+ monte_carlo_calisto .set_inputs_log ()
70+ assert len (monte_carlo_calisto .inputs_log ) == 100
71+ assert all (isinstance (item , dict ) for item in monte_carlo_calisto .inputs_log )
72+ assert all (
73+ "gravity" in item and "elevation" in item
74+ for item in monte_carlo_calisto .inputs_log
75+ )
76+ finally :
77+ _post_test_file_cleanup ()
6178
6279
6380def test_monte_carlo_set_outputs_log (monte_carlo_calisto ):
@@ -68,14 +85,19 @@ def test_monte_carlo_set_outputs_log(monte_carlo_calisto):
6885 monte_carlo_calisto : MonteCarlo
6986 The MonteCarlo object, this is a pytest fixture.
7087 """
71- monte_carlo_calisto .output_file = "tests/fixtures/monte_carlo/example.outputs.txt"
72- monte_carlo_calisto .set_outputs_log ()
73- assert len (monte_carlo_calisto .outputs_log ) == 100
74- assert all (isinstance (item , dict ) for item in monte_carlo_calisto .outputs_log )
75- assert all (
76- "apogee" in item and "impact_velocity" in item
77- for item in monte_carlo_calisto .outputs_log
78- )
88+ try :
89+ monte_carlo_calisto .output_file = (
90+ "tests/fixtures/monte_carlo/example.outputs.txt"
91+ )
92+ monte_carlo_calisto .set_outputs_log ()
93+ assert len (monte_carlo_calisto .outputs_log ) == 100
94+ assert all (isinstance (item , dict ) for item in monte_carlo_calisto .outputs_log )
95+ assert all (
96+ "apogee" in item and "impact_velocity" in item
97+ for item in monte_carlo_calisto .outputs_log
98+ )
99+ finally :
100+ _post_test_file_cleanup ()
79101
80102
81103# def test_monte_carlo_set_errors_log(monte_carlo_calisto):
@@ -86,22 +108,30 @@ def test_monte_carlo_set_outputs_log(monte_carlo_calisto):
86108
87109def test_monte_carlo_prints (monte_carlo_calisto ):
88110 """Tests the prints methods of the MonteCarlo class."""
89- monte_carlo_calisto .info ()
90- monte_carlo_calisto .compare_info (monte_carlo_calisto )
111+ try :
112+ monte_carlo_calisto .info ()
113+ monte_carlo_calisto .compare_info (monte_carlo_calisto )
114+ finally :
115+ _post_test_file_cleanup ()
91116
92117
93118@patch ("matplotlib.pyplot.show" ) # pylint: disable=unused-argument
94119def test_monte_carlo_plots (mock_show , monte_carlo_calisto_pre_loaded ):
95120 """Tests the plots methods of the MonteCarlo class."""
96- assert monte_carlo_calisto_pre_loaded .all_info () is None
97- assert (
98- monte_carlo_calisto_pre_loaded .compare_plots (monte_carlo_calisto_pre_loaded )
99- is None
100- )
101- assert (
102- monte_carlo_calisto_pre_loaded .compare_ellipses (monte_carlo_calisto_pre_loaded )
103- is None
104- )
121+ try :
122+ assert monte_carlo_calisto_pre_loaded .all_info () is None
123+ assert (
124+ monte_carlo_calisto_pre_loaded .compare_plots (monte_carlo_calisto_pre_loaded )
125+ is None
126+ )
127+ assert (
128+ monte_carlo_calisto_pre_loaded .compare_ellipses (
129+ monte_carlo_calisto_pre_loaded
130+ )
131+ is None
132+ )
133+ finally :
134+ _post_test_file_cleanup ()
105135
106136
107137def test_monte_carlo_export_ellipses_to_kml (monte_carlo_calisto_pre_loaded ):
@@ -112,17 +142,18 @@ def test_monte_carlo_export_ellipses_to_kml(monte_carlo_calisto_pre_loaded):
112142 monte_carlo_calisto_pre_loaded : MonteCarlo
113143 The MonteCarlo object, this is a pytest fixture.
114144 """
115- assert (
116- monte_carlo_calisto_pre_loaded .export_ellipses_to_kml (
117- filename = "monte_carlo_class_example.kml" ,
118- origin_lat = 32.990254 ,
119- origin_lon = - 106.974998 ,
120- type = "all" ,
145+ try :
146+ assert (
147+ monte_carlo_calisto_pre_loaded .export_ellipses_to_kml (
148+ filename = "monte_carlo_class_example.kml" ,
149+ origin_lat = 32.990254 ,
150+ origin_lon = - 106.974998 ,
151+ type = "all" ,
152+ )
153+ is None
121154 )
122- is None
123- )
124-
125- os .remove ("monte_carlo_class_example.kml" )
155+ finally :
156+ _post_test_file_cleanup ()
126157
127158
128159@pytest .mark .slow
@@ -134,47 +165,45 @@ def test_monte_carlo_callback(monte_carlo_calisto):
134165 monte_carlo_calisto : MonteCarlo
135166 The MonteCarlo object, this is a pytest fixture.
136167 """
137-
138- # define valid data collector
139- valid_data_collector = {
140- "name" : lambda flight : flight .name ,
141- "density_t0" : lambda flight : flight .env .density (0 ),
142- }
143-
144- monte_carlo_calisto .data_collector = valid_data_collector
145- # NOTE: this is really slow, it runs 10 flight simulations
146- monte_carlo_calisto .simulate (number_of_simulations = 10 , append = False )
147-
148- # tests if print works when we have None in summary
149- monte_carlo_calisto .info ()
150-
151- ## tests if an error is raised for invalid data_collector definitions
152- # invalid type
153- def invalid_data_collector (flight ):
154- return flight .name
155-
156- with pytest .raises (ValueError ):
157- monte_carlo_calisto ._check_data_collector (invalid_data_collector )
158-
159- # invalid key overwrite
160- invalid_data_collector = {"apogee" : lambda flight : flight .apogee }
161- with pytest .raises (ValueError ):
162- monte_carlo_calisto ._check_data_collector (invalid_data_collector )
163-
164- # invalid callback definition
165- invalid_data_collector = {"name" : "Calisto" } # callbacks must be callables!
166- with pytest .raises (ValueError ):
167- monte_carlo_calisto ._check_data_collector (invalid_data_collector )
168-
169- # invalid logic (division by zero)
170- invalid_data_collector = {
171- "density_t0" : lambda flight : flight .env .density (0 ) / "0" ,
172- }
173- monte_carlo_calisto .data_collector = invalid_data_collector
174- # NOTE: this is really slow, it runs 10 flight simulations
175- with pytest .raises (ValueError ):
168+ try :
169+ # define valid data collector
170+ valid_data_collector = {
171+ "name" : lambda flight : flight .name ,
172+ "density_t0" : lambda flight : flight .env .density (0 ),
173+ }
174+
175+ monte_carlo_calisto .data_collector = valid_data_collector
176+ # NOTE: this is really slow, it runs 10 flight simulations
176177 monte_carlo_calisto .simulate (number_of_simulations = 10 , append = False )
177178
178- os .remove ("monte_carlo_test.errors.txt" )
179- os .remove ("monte_carlo_test.outputs.txt" )
180- os .remove ("monte_carlo_test.inputs.txt" )
179+ # tests if print works when we have None in summary
180+ monte_carlo_calisto .info ()
181+
182+ ## tests if an error is raised for invalid data_collector definitions
183+ # invalid type
184+ def invalid_data_collector (flight ):
185+ return flight .name
186+
187+ with pytest .raises (ValueError ):
188+ monte_carlo_calisto ._check_data_collector (invalid_data_collector )
189+
190+ # invalid key overwrite
191+ invalid_data_collector = {"apogee" : lambda flight : flight .apogee }
192+ with pytest .raises (ValueError ):
193+ monte_carlo_calisto ._check_data_collector (invalid_data_collector )
194+
195+ # invalid callback definition
196+ invalid_data_collector = {"name" : "Calisto" } # callbacks must be callables!
197+ with pytest .raises (ValueError ):
198+ monte_carlo_calisto ._check_data_collector (invalid_data_collector )
199+
200+ # invalid logic (division by zero)
201+ invalid_data_collector = {
202+ "density_t0" : lambda flight : flight .env .density (0 ) / "0" ,
203+ }
204+ monte_carlo_calisto .data_collector = invalid_data_collector
205+ # NOTE: this is really slow, it runs 10 flight simulations
206+ with pytest .raises (ValueError ):
207+ monte_carlo_calisto .simulate (number_of_simulations = 10 , append = False )
208+ finally :
209+ _post_test_file_cleanup ()
0 commit comments