Skip to content

Commit

Permalink
restore notice that was unintentionally removed
Browse files Browse the repository at this point in the history
  • Loading branch information
rogthefrog committed Jan 23, 2025
1 parent 5511cc9 commit c2a616c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
7 changes: 6 additions & 1 deletion src/modelbench/hazards.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/modelbench/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
50 changes: 28 additions & 22 deletions tests/modelbench_tests/test_standards.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import pathlib

import pytest
Expand Down Expand Up @@ -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):
Expand All @@ -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

0 comments on commit c2a616c

Please sign in to comment.