|
1 | 1 | import matplotlib as plt |
2 | 2 | import numpy as np |
3 | 3 | import pytest |
| 4 | +import json |
| 5 | +import os |
4 | 6 |
|
5 | 7 | from rocketpy.simulation import MonteCarlo |
6 | 8 |
|
@@ -185,3 +187,31 @@ def test_estimate_confidence_interval_raises_type_error_for_invalid_statistic(): |
185 | 187 |
|
186 | 188 | with pytest.raises(TypeError): |
187 | 189 | 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