diff --git a/src/modelbench/hazards.py b/src/modelbench/hazards.py index 3690dab6..f086a377 100644 --- a/src/modelbench/hazards.py +++ b/src/modelbench/hazards.py @@ -172,7 +172,12 @@ def reload(self): self.metadata = contents.get("_metadata", {}) self.data = contents.get("standards", {}) - def save(self): + def save(self, generated_by: str = ""): + if not generated_by: + generated_by = self.__class__.__name__ + notice = f"This file is auto-generated by {generated_by}; avoid editing it manually." + self.metadata = {"NOTICE": notice, "run_info": self.metadata.get("run_info", [])} + with open(self.path, "w") as of: contents = {"_metadata": self.metadata, "standards": self.data} json.dump(contents, of, indent=4) diff --git a/src/modelbench/run.py b/src/modelbench/run.py index 89e4dd36..8f5634b6 100644 --- a/src/modelbench/run.py +++ b/src/modelbench/run.py @@ -366,7 +366,7 @@ def update_standards_to(standards_file): } new_standards.data = new_standard_data new_standards.append_run_info(new_run_info) - new_standards.save() + new_standards.save(generated_by=sys.argv[0]) STANDARDS = new_standards diff --git a/tests/modelbench_tests/test_standards.py b/tests/modelbench_tests/test_standards.py index cf82da50..72661af7 100644 --- a/tests/modelbench_tests/test_standards.py +++ b/tests/modelbench_tests/test_standards.py @@ -1,3 +1,4 @@ +import copy import pathlib import pytest @@ -41,6 +42,7 @@ def test_reload(standard): assert isinstance(standard.data, dict) assert isinstance(standard.metadata, dict) assert isinstance(standard.metadata["run_info"], dict) + assert standard.metadata["NOTICE"] == "THIS FILE IS ONLY USED IN UNIT TESTS. THE NUMBERS ARE FAKE." def test_append_run_info(standard, extra1, extra2): @@ -54,28 +56,32 @@ def test_append_run_info(standard, extra1, extra2): assert len(standard.metadata["run_info"]) == 3 -def test_save(standard, extra1, extra2, tmp_path): +def test_save(standard, tmp_path): assert isinstance(standard.metadata["run_info"], dict) - file_path = tmp_path / "new_standard.json" - - # make sure saving doesn't change the contents - new_standard = Standards(file_path, auto_load=False) + new_file_path = tmp_path / "new_standard_1.json" + new_standard = Standards(new_file_path, auto_load=False) new_standard.data = standard.data new_standard.metadata = standard.metadata - new_standard.save() - assert pathlib.Path.exists(file_path) - - newer_standard = Standards(file_path) - assert newer_standard.data == standard.data - assert newer_standard.metadata == standard.metadata - - newer_standard.append_run_info(extra1) - newer_standard.append_run_info(extra2) - newer_standard.save() - newer_standard.reload() - - assert isinstance(newer_standard.metadata["run_info"], list) - assert len(newer_standard.metadata["run_info"]) == 3 - assert newer_standard.metadata["run_info"][0] == standard.metadata["run_info"] - assert newer_standard.metadata["run_info"][1] == extra1 - assert newer_standard.metadata["run_info"][2] == extra2 + new_standard.save("pytest") + assert pathlib.Path.exists(new_file_path) + new_standard.reload() + assert new_standard.metadata["NOTICE"] == "This file is auto-generated by pytest; avoid editing it manually." + + +def test_save_notice(standard, extra1, extra2, tmp_path): + new_standard = copy.deepcopy(standard) + new_standard.path = tmp_path / "new_standard_2.json" + assert new_standard.data == standard.data + assert new_standard.metadata == standard.metadata + + new_standard.append_run_info(extra1) + new_standard.append_run_info(extra2) + new_standard.save("pytest again") + new_standard.reload() + + assert isinstance(new_standard.metadata["run_info"], list) + assert new_standard.metadata["NOTICE"] == "This file is auto-generated by pytest again; avoid editing it manually." + assert len(new_standard.metadata["run_info"]) == 3 + assert new_standard.metadata["run_info"][0] == standard.metadata["run_info"] + assert new_standard.metadata["run_info"][1] == extra1 + assert new_standard.metadata["run_info"][2] == extra2