Skip to content

Commit cab6436

Browse files
committed
added to_json() method to SimulationInput to be able to compare built input with reference result (as a consistency check) + updated test
1 parent 921d95e commit cab6436

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

src/sparging/inputs.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ def __post_init__(self):
8888
f"In {self.__class__.__name__}: Invalid type for '{key}': expected a pint.Quantity, got {value} of type {type(value)}"
8989
)
9090

91+
def to_json(self, path: str):
92+
import json
93+
94+
with open(path, "w") as f:
95+
json.dump(
96+
{key: str(value) for key, value in self.__dict__.items()}, f, indent=2
97+
)
98+
9199
@classmethod
92100
def from_parameters(
93101
cls,

test/test_simulation_input.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
import pytest
1515
import dataclasses
16+
import difflib
17+
import logging
18+
from pathlib import Path
1619

1720
# define standard LIBRA input parameters to be used in multiple tests
1821
geom = ColumnGeometry(
@@ -39,9 +42,10 @@
3942
)
4043

4144

42-
def test_from_parameters_success():
45+
def test_from_parameters_success(tmp_path):
4346
"""
4447
Test that SimulationInput.from_parameters successfully creates a SimulationInput object from minimal input objects
48+
and that the generated SimulationInput is consistent with the standard input it should yield
4549
"""
4650
sim_input = SimulationInput.from_parameters(
4751
geom, flibe, operating_params, sparging_params
@@ -57,16 +61,31 @@ def test_from_parameters_success():
5761
f"Expected field '{field.name}' to be a pint.Quantity, got {type(value)}"
5862
)
5963

64+
reference_path = Path(__file__).with_name("standard_input.json")
65+
generated_path = Path(tmp_path).joinpath("generated_input.json")
66+
67+
sim_input.to_json(generated_path)
68+
69+
generated_text = generated_path.read_text(encoding="utf-8")
70+
reference_text = reference_path.read_text(encoding="utf-8")
71+
72+
diff = "\n".join(
73+
difflib.unified_diff(
74+
reference_text.splitlines(),
75+
generated_text.splitlines(),
76+
fromfile=str(reference_path.name),
77+
tofile=str(generated_path.name),
78+
lineterm="",
79+
)
80+
)
81+
assert generated_text == reference_text, f"Log output mismatch:\n{diff}"
82+
6083

6184
def test_find_in_graph_logging(tmp_path):
6285
"""
6386
Test that the `find_in_graph` function logs the expected output when searching for a parameter in the graph.
6487
"""
6588
# BUILD
66-
import difflib
67-
import logging
68-
from pathlib import Path
69-
7089
reference_log_path = Path(__file__).with_name("test_find_in_graph.reference.log")
7190
generated_log_path = Path(tmp_path).joinpath("test_find_in_graph.generated.log")
7291

0 commit comments

Comments
 (0)