Skip to content

Commit ab2eb9f

Browse files
committed
coverage fix
1 parent 292deb2 commit ab2eb9f

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

tests/unit/test_serialisation/test_serialisation.py

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,55 @@ def test_binary_operator_serialisation(self):
817817
values = [c.value for c in expr2.children]
818818
assert values == [2, 3]
819819

820+
def test_symbol_deserialization_with_domains(self):
821+
json_data = {
822+
"type": "Symbol",
823+
"name": "test symbol",
824+
"domains": {
825+
"primary": ["negative electrode", "separator", "positive electrode"],
826+
"secondary": ["current collector"],
827+
},
828+
}
829+
830+
symbol = Serialise.convert_symbol_from_json(json_data)
831+
832+
assert isinstance(symbol, pybamm.Symbol)
833+
assert symbol.name == "test symbol"
834+
assert symbol.domains == {
835+
"primary": ["negative electrode", "separator", "positive electrode"],
836+
"secondary": ["current collector"],
837+
"tertiary": [],
838+
"quaternary": [],
839+
}
840+
841+
def test_import_base_class_non_builtin_object(self, tmp_path):
842+
# Minimal model JSON with a non-existent base class
843+
model_json = {
844+
"schema_version": "1.0",
845+
"pybamm_version": pybamm.__version__,
846+
"model": {
847+
"base_class": "nonexistent_module.DummyModel",
848+
"name": "DummyModel",
849+
"rhs": [],
850+
"algebraic": [],
851+
"initial_conditions": [],
852+
"boundary_conditions": [],
853+
"events": [],
854+
"variables": {},
855+
},
856+
}
857+
858+
file_path = tmp_path / "model.json"
859+
860+
with open(file_path, "w") as f:
861+
json.dump(model_json, f)
862+
863+
with pytest.raises(
864+
ImportError,
865+
match=r"(?i)Could not import base class 'nonexistent_module\.DummyModel'",
866+
):
867+
Serialise.load_custom_model(str(file_path))
868+
820869
def test_function_parameter_with_diff_variable_serialisation(self):
821870
x = pybamm.Variable("x")
822871
diff_var = pybamm.Variable("r")
@@ -854,13 +903,22 @@ def test_indefinite_integral_serialisation(self):
854903
expr2 = Serialise.convert_symbol_from_json(json_dict)
855904
assert isinstance(expr2, pybamm.IndefiniteIntegral)
856905
assert isinstance(expr2.child, pybamm.SpatialVariable)
857-
858906
assert expr2.child.name == "x"
859907
assert isinstance(expr2.integration_variable, list)
860908
assert len(expr2.integration_variable) == 1
861909
assert isinstance(expr2.integration_variable[0], pybamm.SpatialVariable)
862910
assert expr2.integration_variable[0].name == "x"
863911

912+
bad_json_dict = json_dict.copy()
913+
bad_json_dict["integration_variable"] = {
914+
"type": "Symbol", # Something not a SpatialVariable
915+
"name": "not spatial",
916+
"domains": {},
917+
}
918+
919+
with pytest.raises(TypeError, match=r"Expected SpatialVariable"):
920+
Serialise.convert_symbol_from_json(bad_json_dict)
921+
864922
def test_invalid_filename(self):
865923
model = pybamm.lithium_ion.DFN()
866924
with pytest.raises(

0 commit comments

Comments
 (0)