-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support ignoring fields when serializing #149
Comments
Thoughts on We could support strings (literal property matchers) and predicates |
@noahnu I think I prefer
What would be the arguments to the predicate? def predicate(field: str, value: Any, path: Tuple[str] = ()) -> bool:
"""
field: current field name to be excluded
value: current field value to be excluded
path: fields traversed so far
""" We could replace the |
That feels more like a "transformation" function. A |
We use this sort of approach internally. I took a stab at porting what I had over into something more Syrupy-native, but for us there's a twist. For non-deterministic values, we keep track on a per-test-case the number of unique values of that type we've seen. For example, the first time we see a new UUID, we'll replace it with e.g. In general I think this sort of approach lets you get slightly nicer assertions around even non-deterministic values, in their being at least the non-deterministic UUIDs or timestamps in ways that you expect. What stopped me from implementing this in a reasonable way was that everything on |
@taion while this is pending, would mocking the UUID you use with one that just increments each time and then resetting the value after each test case work? e.g. class MockUUID:
value = 0
def __init__(self):
self.increment()
@classmethod
def increment(cls):
cls.value += 1
@classmethod
def reset(cls):
cls.value = 0
def __repr__(self):
return f"<UUID(index={self.value})>"
@pytest.fixture
def mock_uuid(monkeypatch):
monkeypatch.setattr("src.path.to.module.uuid.uuid4", MockUUID)
yield
MockUUID.reset()
def test_non_deterministic_1(snapshot, mock_uuid):
assert NonDeterministic() == snapshot
assert NonDeterministic() == snapshot
def test_non_deterministic_2(snapshot, mock_uuid):
assert NonDeterministic() == snapshot |
That's neat – it could definitely work for some cases (though there are a few places where we generate v5 UUIDs off other external IDs coming in). To clarify, though, we're not blocked on this at all; we just run our custom "mask nondeterministic values" function on our data before asserting on the snapshot. |
🎉 This issue has been resolved in version 0.5.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
Is your feature request related to a problem? Please describe.
When asserting on snapshots support filtering out properties that should not be matched on. This would allow deterministic snapshots when matching on objects that generate realtime values in tests without needing to mock.
Describe the solution you'd like
Describe alternatives you've considered
This is already possible but is cumbersome to implement, and cannot easily be extended to other serialization types.
Additional context
The text was updated successfully, but these errors were encountered: