Data#
- class safir.testing.data.Data(root, *, update_test_data=False)#
Bases:
objectLocate, read, and write test data.
This class provides utility functions to load test data, compare test output against expected output stored in files, and optionally update stored data.
Stored output data (as specified in
assert_*methods) will be updated (and thus theassert_*methods will never fail) if theupdate_test_dataconstructor argument isTrue. Generally this should be set by the fixture if the environment variableUPDATE_TEST_DATAis set.Applications are actively encouraged to subclass this class to add more specific
read_*,write_*, andassert_*methods that are specific to the data types used by that application. Those methods should call the methods provided by this base class to perform lower-level operations. In particular, convert objects to JSON and useassert_json_matchesorassert_pydantic_matchesto compare them so that stored output data updating is properly supported.- Parameters:
Examples
This class is generally created and returned by a fixture in
tests/conftest.pysuch as the following:import os from pathlib import Path from safir.testing.data import Data @pytest.fixture def data() -> Data: update = bool(os.getenv("UPDATE_TEST_PATH")) return Data(Path(__file__).parent / "data", update_test_data=update)
Methods Summary
assert_json_matches(seen, path)Raise an assertion if the saved expected output doesn't match.
assert_pydantic_matches(seen, path)Raise an assertion if the saved Pydantic model doesn't match.
assert_text_matches(seen, path)Raise an assertion if the saved expected output doesn't match.
path(path[, extension])Construct a path to a test data file.
read_json(path)Read test data as JSON and return its decoded form.
read_pydantic(model_type, path)Read test data as a Pydantic model.
read_text(path)Read test data as text.
write_json(data, path)Write data as JSON to the test data directory.
write_pydantic(data, path)Write a Pydantic model as JSON to the test data directory.
write_text(data, path)Write data as text to the test data directory.
Methods Documentation
- assert_json_matches(seen, path)#
Raise an assertion if the saved expected output doesn’t match.
<ANY>strings in the saved expected output are converted to theunittest.mock.ANYwildcard before comparison.- Parameters:
- Raises:
AssertionError – Raised if the data doesn’t match.
- Return type:
- assert_pydantic_matches(seen, path)#
Raise an assertion if the saved Pydantic model doesn’t match.
The Pydantic model is serialized to JSON and then compared against the stored JSON serialization, since that produces a better rich diff on mismatch.
This method will miss differences that are not part of the JSON serialization of the model, and only models that can be serialized to JSON can be compared.
- Parameters:
seen (
BaseModel) – Any Pydantic model. The saved data will be presumed to be for the same model.path (
str) – Path relative totests/dataof the expected output. A.jsonextension will be added automatically.
- Raises:
AssertionError – Raised if the data doesn’t match.
- Return type:
- assert_text_matches(seen, path)#
Raise an assertion if the saved expected output doesn’t match.
- Parameters:
- Raises:
AssertionError – Raised if the data doesn’t match.
- Return type:
- path(path, extension=None)#
Construct a path to a test data file.
- Parameters:
- Returns:
Full path to file.
- Return type:
- read_json(path)#
Read test data as JSON and return its decoded form.
<ANY>strings in the saved expected output are converted to theunittest.mock.ANYwildcard.
- read_pydantic(model_type, path)#
Read test data as a Pydantic model.
- read_text(path)#
Read test data as text.
- write_json(data, path)#
Write data as JSON to the test data directory.
- write_pydantic(data, path)#
Write a Pydantic model as JSON to the test data directory.