generated from tophat/new-project-kit
-
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
feat: allow extensions to override snapshot equality check #548
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def testcases_initial(testdir): | ||
testdir.makeconftest( | ||
""" | ||
import pytest | ||
import math | ||
|
||
from syrupy.extensions.amber import AmberSnapshotExtension | ||
|
||
class CustomSnapshotExtension(AmberSnapshotExtension): | ||
def matches(self, *, serialized_data, snapshot_data): | ||
try: | ||
a = float(serialized_data) | ||
b = float(snapshot_data) | ||
return math.isclose(a, b, rel_tol=1e-5) | ||
except: | ||
return False | ||
|
||
@pytest.fixture | ||
def snapshot_custom(snapshot): | ||
return snapshot.use_extension(CustomSnapshotExtension) | ||
""" | ||
) | ||
return { | ||
"passed": ( | ||
""" | ||
def test_passed_custom(snapshot_custom): | ||
assert snapshot_custom == 3.0 | ||
""" | ||
), | ||
"failed": ( | ||
""" | ||
def test_passed_custom(snapshot_custom): | ||
# this comment is required or the test breaks | ||
assert snapshot_custom == 4.0 | ||
""" | ||
), | ||
} | ||
|
||
|
||
@pytest.fixture | ||
def generate_snapshots(testdir, testcases_initial): | ||
testdir.makepyfile(test_file=testcases_initial["passed"]) | ||
result = testdir.runpytest("-v", "--snapshot-update") | ||
return result, testdir, testcases_initial | ||
|
||
|
||
def test_generated_snapshots(generate_snapshots): | ||
result = generate_snapshots[0] | ||
result.stdout.re_match_lines((r"1 snapshot generated\.")) | ||
assert "snapshots unused" not in result.stdout.str() | ||
assert result.ret == 0 | ||
|
||
|
||
def test_approximate_match(generate_snapshots): | ||
testdir = generate_snapshots[1] | ||
testdir.makepyfile( | ||
test_file=""" | ||
def test_passed_custom(snapshot_custom): | ||
assert snapshot_custom == 3.2 | ||
""" | ||
) | ||
result = testdir.runpytest("-v") | ||
result.stdout.re_match_lines((r"test_file.py::test_passed_custom PASSED")) | ||
assert result.ret == 0 | ||
|
||
|
||
def test_failed_snapshots(generate_snapshots): | ||
testdir = generate_snapshots[1] | ||
testdir.makepyfile(test_file=generate_snapshots[2]["failed"]) | ||
result = testdir.runpytest("-v") | ||
result.stdout.re_match_lines((r"1 snapshot failed\.")) | ||
assert result.ret == 1 | ||
|
||
|
||
def test_updated_snapshots(generate_snapshots): | ||
_, testdir, initial = generate_snapshots | ||
testdir.makepyfile(test_file=initial["failed"]) | ||
result = testdir.runpytest("-v", "--snapshot-update") | ||
result.stdout.re_match_lines((r"1 snapshot updated\.")) | ||
assert result.ret == 0 | ||
Comment on lines
+79
to
+84
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is failing locally for me. For some reason, the serialized data is referring to the old value. Some odd caching issue somewhere. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might not be updating the saved file as expected that's why the
test_approximate_match
is passing buttest_failed_snapshots
andtest_updated_snapshots
are failingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed via pdb that's not it. It looks like a python bug, or maybe from pytest's bytecode rewriting