-
Notifications
You must be signed in to change notification settings - Fork 37
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
fix: check current session's pending-write queue when recalling snapshots (e.g. diffing) #927
Changes from 1 commit
dbf395a
0fb10fd
0f694ca
c577ba6
55c4995
9a3c9f0
817de52
a86f5d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -67,17 +67,25 @@ class SnapshotSession: | |||||
List[Tuple["SerializedData", "PyTestLocation", "SnapshotIndex"]], | ||||||
] = field(default_factory=dict) | ||||||
|
||||||
def queue_snapshot_write( | ||||||
def _snapshot_write_queue_key( | ||||||
self, | ||||||
extension: "AbstractSyrupyExtension", | ||||||
test_location: "PyTestLocation", | ||||||
data: "SerializedData", | ||||||
index: "SnapshotIndex", | ||||||
) -> None: | ||||||
) -> Tuple[Type["AbstractSyrupyExtension"], str]: | ||||||
snapshot_location = extension.get_location( | ||||||
test_location=test_location, index=index | ||||||
) | ||||||
key = (extension.__class__, snapshot_location) | ||||||
return (extension.__class__, snapshot_location) | ||||||
|
||||||
def queue_snapshot_write( | ||||||
self, | ||||||
extension: "AbstractSyrupyExtension", | ||||||
test_location: "PyTestLocation", | ||||||
data: "SerializedData", | ||||||
index: "SnapshotIndex", | ||||||
) -> None: | ||||||
key = self._snapshot_write_queue_key(extension, test_location, index) | ||||||
queue = self._queued_snapshot_writes.get(key, []) | ||||||
queue.append((data, test_location, index)) | ||||||
self._queued_snapshot_writes[key] = queue | ||||||
|
@@ -93,6 +101,27 @@ def flush_snapshot_write_queue(self) -> None: | |||||
) | ||||||
self._queued_snapshot_writes = {} | ||||||
|
||||||
def recall_snapshot( | ||||||
self, | ||||||
extension: "AbstractSyrupyExtension", | ||||||
test_location: "PyTestLocation", | ||||||
index: "SnapshotIndex", | ||||||
) -> Optional["SerializedData"]: | ||||||
"""Find the current value of the snapshot, for this session, either a pending write or the actual snapshot.""" | ||||||
|
||||||
key = self._snapshot_write_queue_key(extension, test_location, index) | ||||||
queue = self._queued_snapshot_writes.get(key) | ||||||
if queue: | ||||||
# find the last (i.e. most recent) write to this index/location in the queue: | ||||||
for queue_data, queue_test_location, queue_index in reversed(queue): | ||||||
if queue_index == index and queue_test_location == test_location: | ||||||
return queue_data | ||||||
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 loops is a potential performance issue, but I suspect it is okay. In particular: it scales like O(length of This function is called twice in
If every assertion in a ambr file needs an update, this leads to quadratic O(number of assertions in that file^2) behaviour:
i.e. N assertions ends up iterating over I think this is probably okay:
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. There appears to be a slowdown: #927 (comment) I ran
and "main":
The 1000x writes test performs 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 comes up when users are leveraging test parameterization over data sets. i.e. the benchmark case of parameterizing over 1000 test cases isn't too unrealistic from how I've seen syrupy used in the past. 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. FYI, the benchmarking is broken in CI for contributors (something to do with permissions in the github workflow I need to fix), however you should be able to run it locally with 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. Updated. See #927 (comment) Thanks for proving my "I think" wasn't accurate 😄 |
||||||
|
||||||
# no queue, or no matching write, so just read the snapshot directly: | ||||||
return extension.read_snapshot( | ||||||
test_location=test_location, index=index, session_id=str(id(self)) | ||||||
) | ||||||
|
||||||
@property | ||||||
def update_snapshots(self) -> bool: | ||||||
return bool(self.pytest_session.config.option.update_snapshots) | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import pytest | ||
|
||
_TEST = """ | ||
def test_foo(snapshot): | ||
assert {**base} == snapshot(name="a") | ||
assert {**base, **extra} == snapshot(name="b", diff="a") | ||
""" | ||
|
||
|
||
def _make_file(testdir, base, extra): | ||
testdir.makepyfile( | ||
test_file="\n\n".join([f"base = {base!r}", f"extra = {extra!r}", _TEST]) | ||
) | ||
|
||
|
||
def _run_test(testdir, base, extra, expected_update_lines): | ||
_make_file(testdir, base=base, extra=extra) | ||
|
||
# Run with --snapshot-update, to generate/update snapshots: | ||
result = testdir.runpytest( | ||
"-v", | ||
"--snapshot-update", | ||
) | ||
result.stdout.re_match_lines((expected_update_lines,)) | ||
assert result.ret == 0 | ||
|
||
# Run without --snapshot-update, to validate the snapshots are actually up-to-date | ||
result = testdir.runpytest("-v") | ||
result.stdout.re_match_lines((r"2 snapshots passed\.",)) | ||
assert result.ret == 0 | ||
|
||
|
||
def test_diff_lifecycle(testdir) -> pytest.Testdir: | ||
# first: create both snapshots completely from scratch | ||
_run_test( | ||
testdir, | ||
base={"A": 1}, | ||
extra={"X": 10}, | ||
expected_update_lines=r"2 snapshots generated\.", | ||
) | ||
|
||
# second: edit the base data, to change the data for both snapshots (only changes the serialized output for the base snapshot `a`). | ||
_run_test( | ||
testdir, | ||
base={"A": 1, "B": 2}, | ||
extra={"X": 10}, | ||
expected_update_lines=r"1 snapshot passed. 1 snapshot updated\.", | ||
) | ||
|
||
# third: edit just the extra data (only changes the serialized output for the diff snapshot `b`) | ||
_run_test( | ||
testdir, | ||
base={"A": 1, "B": 2}, | ||
extra={"X": 10, "Y": 20}, | ||
expected_update_lines=r"1 snapshot passed. 1 snapshot updated\.", | ||
) |
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.
I note that #606 added this
session_id
parameter, which I imagine was meant to help with circumstances like this, but it seems like it ends up ignored/unused in practice?cache_key
: https://github.com/tophat/syrupy/blob/ef8189c68460593fead9c484405b755e272c8cca/src/syrupy/extensions/amber/__init__.py#L57-L66session_id
: https://github.com/tophat/syrupy/blob/ef8189c68460593fead9c484405b755e272c8cca/src/syrupy/extensions/single_file.py#L92-L103And, in practice, I think it may be quite hard to use, since the relevant cached data is held in memory in the session itself (i.e. the extension
_read_snapshot_data_from_location
method calls don't really have access to it at all).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.
The session_id is used as the cache_key argument in Amber's
__cacheable_read_snapshot
method. Although it's not used in the function, it's used by the lru_cache decorator (which caches based on the kwargs I believe). So it essentially invalidates the cache