|
2 | 2 | #
|
3 | 3 | # SPDX-License-Identifier: MPL-2.0
|
4 | 4 |
|
| 5 | +import json |
5 | 6 | from pathlib import Path
|
6 | 7 | from unittest.mock import MagicMock, mock_open, patch
|
7 | 8 |
|
8 | 9 | import numpy as np
|
9 | 10 | import pytest
|
10 | 11 |
|
11 |
| -from power_grid_model import initialize_array |
| 12 | +from power_grid_model import DatasetType, initialize_array |
12 | 13 | from power_grid_model._core.power_grid_meta import power_grid_meta_data
|
13 | 14 | from power_grid_model.data_types import Dataset
|
14 | 15 | from power_grid_model.utils import (
|
| 16 | + LICENSE_TEXT, |
| 17 | + _make_test_case, |
15 | 18 | get_component_batch_size,
|
16 | 19 | get_dataset_batch_size,
|
17 | 20 | get_dataset_scenario,
|
@@ -164,3 +167,54 @@ def test_msgpack_serialize(serialize_mock: MagicMock, open_mock: MagicMock):
|
164 | 167 |
|
165 | 168 | def test_self_test():
|
166 | 169 | self_test()
|
| 170 | + |
| 171 | + |
| 172 | +@pytest.mark.parametrize( |
| 173 | + ("output_dataset_type", "output_file_name", "update_data"), |
| 174 | + ( |
| 175 | + (DatasetType.sym_output, "sym_output_batch.json", {"version": "1.0", "data": "update_data"}), |
| 176 | + (DatasetType.sym_output, "sym_output.json", None), |
| 177 | + (DatasetType.asym_output, "asym_output_batch.json", {"version": "1.0", "data": "update_data"}), |
| 178 | + (DatasetType.asym_output, "asym_output.json", None), |
| 179 | + (DatasetType.sc_output, "sc_output_batch.json", {"version": "1.0", "data": "update_data"}), |
| 180 | + (DatasetType.sc_output, "sc_output.json", None), |
| 181 | + ), |
| 182 | +) |
| 183 | +@patch.object(Path, "write_text", autospec=True) |
| 184 | +@patch("power_grid_model.utils.json_serialize_to_file") |
| 185 | +def test__make_test_case( |
| 186 | + serialize_to_file_mock: MagicMock, write_text_mock: MagicMock, output_dataset_type, output_file_name, update_data |
| 187 | +): |
| 188 | + input_data: Dataset = {"version": "1.0", "data": "input_data"} |
| 189 | + output_data: Dataset = {"version": "1.0", "data": "output_data"} |
| 190 | + output_path = Path("test_path") |
| 191 | + params = {"param1": "value1", "param2": "value2"} |
| 192 | + |
| 193 | + _make_test_case( |
| 194 | + output_path=output_path, |
| 195 | + input_data=input_data, |
| 196 | + output_data=output_data, |
| 197 | + params=params, |
| 198 | + output_dataset_type=output_dataset_type, |
| 199 | + update_data=update_data, |
| 200 | + ) |
| 201 | + |
| 202 | + serialize_to_file_mock.assert_any_call( |
| 203 | + file_path=output_path / "input.json", data=input_data, dataset_type=DatasetType.input |
| 204 | + ) |
| 205 | + serialize_to_file_mock.assert_any_call( |
| 206 | + file_path=output_path / output_file_name, data=output_data, dataset_type=output_dataset_type |
| 207 | + ) |
| 208 | + write_text_mock.assert_any_call(output_path / "params.json", data=json.dumps(params, indent=2), encoding="utf-8") |
| 209 | + for file_name in ["input.json.license", f"{output_file_name}.license", "params.json.license"]: |
| 210 | + write_text_mock.assert_any_call(output_path / file_name, data=LICENSE_TEXT, encoding="utf-8") |
| 211 | + if update_data is not None: |
| 212 | + write_text_mock.assert_any_call(output_path / "update_batch.json.license", data=LICENSE_TEXT, encoding="utf-8") |
| 213 | + serialize_to_file_mock.assert_any_call( |
| 214 | + file_path=output_path / "update_batch.json", data=update_data, dataset_type=DatasetType.update |
| 215 | + ) |
| 216 | + assert write_text_mock.call_count == 5 |
| 217 | + assert serialize_to_file_mock.call_count == 3 |
| 218 | + else: |
| 219 | + assert write_text_mock.call_count == 4 |
| 220 | + assert serialize_to_file_mock.call_count == 2 |
0 commit comments