Skip to content

Commit e3473df

Browse files
GMinoruyGui-FernandesBR
authored andcommitted
ENH: adds unit test of the function export_results
1 parent 1dadbae commit e3473df

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

tests/unit/simulation/test_monte_carlo.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import matplotlib as plt
22
import numpy as np
33
import pytest
4+
import json
5+
import os
46

57
from rocketpy.simulation import MonteCarlo
68

@@ -185,3 +187,31 @@ def test_estimate_confidence_interval_raises_type_error_for_invalid_statistic():
185187

186188
with pytest.raises(TypeError):
187189
mc.estimate_confidence_interval("apogee", statistic="not_a_function")
190+
191+
192+
def test_monte_carlo_export_results_to_csv_and_json_files(monte_carlo_calisto, tmp_path):
193+
"""Checks that the export_results create .csv and .json files
194+
195+
Parameters
196+
----------
197+
monte_carlo_calisto : MonteCarlo
198+
Fixture that has the .txt files necessary for the export_results
199+
"""
200+
try:
201+
mc = monte_carlo_calisto
202+
mc.filename = tmp_path / "mock_output"
203+
mock_data = {"apogee": 100, "max_velocity": 255}
204+
with open(tmp_path / "mock_output.outputs.txt", "w") as f:
205+
f.write(json.dumps(mock_data) + "\n")
206+
207+
mc.export_results(tmp_path / "mock_outputs_in_csv", "csv")
208+
expected_file_in_csv = tmp_path / f"{"mock_outputs_in_csv"}.csv"
209+
assert expected_file_in_csv.exists()
210+
211+
mc.export_results(tmp_path / "mock_output_in_json", "json")
212+
expected_file_in_json = tmp_path / f"{"mock_output_in_json"}.json"
213+
assert expected_file_in_json.exists()
214+
finally:
215+
os.remove("monte_carlo_test.errors.txt")
216+
os.remove("monte_carlo_test.inputs.txt")
217+
os.remove("monte_carlo_test.outputs.txt")

0 commit comments

Comments
 (0)