Skip to content
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: support call syntax for snapshot fixture overriding #160

Merged
merged 6 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/syrupy/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class SnapshotAssertion:
_extension_class: Type["AbstractSyrupyExtension"] = attr.ib(kw_only=True)
_test_location: "TestLocation" = attr.ib(kw_only=True)
_update_snapshots: bool = attr.ib(kw_only=True)
_extension: Optional["AbstractSyrupyExtension"] = attr.ib(init=False, default=None)
_executions: int = attr.ib(init=False, default=0, kw_only=True)
_execution_results: Dict[int, "AssertionResult"] = attr.ib(
init=False, factory=dict, kw_only=True
Expand All @@ -53,7 +54,7 @@ def __attrs_post_init__(self) -> None:

@property
def extension(self) -> "AbstractSyrupyExtension":
if not getattr(self, "_extension", None):
if not self._extension:
self._extension: "AbstractSyrupyExtension" = self._extension_class(
test_location=self._test_location
)
Expand All @@ -70,6 +71,10 @@ def executions(self) -> Dict[int, AssertionResult]:
def use_extension(
self, extension_class: Optional[Type["AbstractSyrupyExtension"]] = None,
) -> "SnapshotAssertion":
"""
Creates a new snapshot assertion fixture with the same options but using
specified extension class. This does not perserve assertion index or state.
noahnu marked this conversation as resolved.
Show resolved Hide resolved
"""
return self.__class__(
update_snapshots=self._update_snapshots,
test_location=self._test_location,
Expand All @@ -92,6 +97,14 @@ def get_assert_diff(self, data: "SerializableData") -> List[str]:
diff.extend(self.extension.diff_lines(serialized_data, snapshot_data))
return diff

def __call__(
self, *, extension_class: Optional[Type["AbstractSyrupyExtension"]]
) -> "SnapshotAssertion":
if extension_class:
self._extension_class = extension_class
self._extension = None
return self

def __repr__(self) -> str:
attrs_to_repr = ["name", "num_executions"]
attrs_repr = ", ".join(f"{a}={repr(getattr(self, a))}" for a in attrs_to_repr)
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 33 additions & 25 deletions tests/test_extension_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,48 @@
)


actual_png = base64.b64decode(
b"iVBORw0KGgoAAAANSUhEUgAAADIAAAAyBAMAAADsEZWCAAAAG1BMVEXMzMy"
b"Wlpaqqqq3t7exsbGcnJy+vr6jo6PFxcUFpPI/AAAACXBIWXMAAA7EAAAOxA"
b"GVKw4bAAAAQUlEQVQ4jWNgGAWjgP6ASdncAEaiAhaGiACmFhCJLsMaIiDAE"
b"QEi0WXYEiMCOCJAJIY9KuYGTC0gknpuHwXDGwAA5fsIZw0iYWYAAAAASUVO"
b"RK5CYII="
)
actual_svg = (
'<?xml version="1.0" encoding="UTF-8"?>'
'<svg viewBox="0 0 50 50" xmlns="http://www.w3.org/2000/svg">'
'<g><rect width="50" height="50" fill="#fff"/>'
'<g><g fill="#fff" stroke="#707070">'
'<rect width="50" height="50" stroke="none"/>'
'<rect x="0" y="0" width="50" height="50" fill="none"/></g>'
'<text transform="translate(10 27)" fill="#707070" '
'font-family="ConsolasForPowerline, Consolas for Powerline" font-size="8">'
'<tspan x="0" y="0">50 x 50</tspan></text></g></g></svg>'
)


@pytest.fixture
def snapshot_png(snapshot):
return snapshot.use_extension(PNGImageSnapshotExtension)


def test_image(snapshot_png, snapshot_svg):
actual_png = base64.b64decode(
b"iVBORw0KGgoAAAANSUhEUgAAADIAAAAyBAMAAADsEZWCAAAAG1BMVEXMzMy"
b"Wlpaqqqq3t7exsbGcnJy+vr6jo6PFxcUFpPI/AAAACXBIWXMAAA7EAAAOxA"
b"GVKw4bAAAAQUlEQVQ4jWNgGAWjgP6ASdncAEaiAhaGiACmFhCJLsMaIiDAE"
b"QEi0WXYEiMCOCJAJIY9KuYGTC0gknpuHwXDGwAA5fsIZw0iYWYAAAAASUVO"
b"RK5CYII="
)
def test_image(snapshot_png):
assert actual_png == snapshot_png


@pytest.fixture
def snapshot_svg(snapshot):
return snapshot.use_extension(SVGImageSnapshotExtension)
def test_image_vector(snapshot):
"""
Example of creating a previewable svg snapshot
"""
assert snapshot(extension_class=SVGImageSnapshotExtension) == actual_svg


def test_image_vector(snapshot_svg):
def test_multiple_snapshot_extensions(snapshot):
"""
Example of creating a previewable svg snapshot
Example of switching extension classes on the fly.
These should be indexed in order of assertion.
"""
actual_svg = (
'<?xml version="1.0" encoding="UTF-8"?>'
'<svg viewBox="0 0 50 50" xmlns="http://www.w3.org/2000/svg">'
'<g><rect width="50" height="50" fill="#fff"/>'
'<g><g fill="#fff" stroke="#707070">'
'<rect width="50" height="50" stroke="none"/>'
'<rect x="0" y="0" width="50" height="50" fill="none"/></g>'
'<text transform="translate(10 27)" fill="#707070" '
'font-family="ConsolasForPowerline, Consolas for Powerline" font-size="8">'
'<tspan x="0" y="0">50 x 50</tspan></text></g></g></svg>'
)
assert snapshot_svg == actual_svg
assert actual_svg == snapshot(extension_class=SVGImageSnapshotExtension)
assert actual_svg == snapshot # last extension class specified is used
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like that behaviour can be confusing. Perhaps it should function similar to snapshot.use_extension, where it effectively clones the snapshot instance

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it clones it then it'll lose the context of assertion index, leading to duplicates

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can clone assertion index. I find it strange that calling snapshot at the beginning of a test case can affect snapshot assertions towards the end of the test case. Strange side-effects

Copy link
Collaborator Author

@iamogbz iamogbz Mar 21, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noahnu so your preferred behaviour would be having this line use the default snapshot extension class? It does make it more "pure" but then requires a lot of cloning of state values. Since the snapshot fixture is bound to the test case function the limit of the side effects would only be in specific test, I don't think that's too confusing and it would allow for doing stuff like #161 without a lot of copying.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@noahnu also I don't think we could track with cloning assertion index if each individual assertion in the test case is a new instance

assert actual_png == snapshot(extension_class=PNGImageSnapshotExtension)
assert actual_svg == snapshot(extension_class=SVGImageSnapshotExtension)