Skip to content

Commit

Permalink
Migrate tests for model serialization to goldens.
Browse files Browse the repository at this point in the history
This is to ensure that whenever we change the model serialization code, it is much easier to update the tests. The changes should be more readable this way.

When code changes to request changes to the goldens, the author of the change will need to run a `pytest` command to update the files by passing a flag, then add the goldens to the PR (ideally via a new commit).

This also ensures that we can test all models in all scenarios, in a similar way. Thus, removed the tests for empty models that were just testing for empty manifest. Added a new model that only contains an empty file.

Signed-off-by: Mihai Maruseac <[email protected]>
  • Loading branch information
mihaimaruseac committed Jul 22, 2024
1 parent 99f4d8a commit 9e7b76a
Show file tree
Hide file tree
Showing 40 changed files with 320 additions and 492 deletions.
27 changes: 27 additions & 0 deletions model_signing/serialization/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,36 @@
from model_signing.serialization import test_support


def pytest_addoption(parser):
"""Adds a flag argument to update the goldens."""
parser.addoption(
"--update_goldens",
action="store_true",
default=False,
help="update golden files",
)


# Note: Don't make fixtures with global scope as we are altering the models!
@pytest.fixture
def sample_model_file(tmp_path_factory):
"""A model with just a single file."""
file = tmp_path_factory.mktemp("model") / "file"
file.write_bytes(test_support.KNOWN_MODEL_TEXT)
return file


@pytest.fixture
def empty_model_file(tmp_path_factory):
"""A model with just an empty file."""
file = tmp_path_factory.mktemp("model") / "file"
file.write_bytes(b"")
return file


@pytest.fixture
def sample_model_folder(tmp_path_factory):
"""A model with multiple files and directories."""
model_root = tmp_path_factory.mktemp("model") / "root"
model_root.mkdir()

Expand All @@ -55,13 +68,27 @@ def sample_model_folder(tmp_path_factory):

@pytest.fixture
def empty_model_folder(tmp_path_factory):
"""A model with just an empty directory."""
model_root = tmp_path_factory.mktemp("model") / "root"
model_root.mkdir()
return model_root


@pytest.fixture
def model_folder_with_empty_file(tmp_path_factory):
"""A model with just an empty file, inside a directory."""
model_root = tmp_path_factory.mktemp("model") / "root"
model_root.mkdir()

empty_file = model_root / "empty_file"
empty_file.write_bytes(b"")

return model_root


@pytest.fixture
def deep_model_folder(tmp_path_factory):
"""A model with a deep directory hierarchy."""
model_root = tmp_path_factory.mktemp("model") / "root"
model_root.mkdir()

Expand Down
Loading

0 comments on commit 9e7b76a

Please sign in to comment.