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 4 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
33 changes: 29 additions & 4 deletions 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 @@ -51,12 +52,15 @@ class SnapshotAssertion:
def __attrs_post_init__(self) -> None:
self._session.register_request(self)

def __init_extension(
self, extension_class: Type["AbstractSyrupyExtension"]
) -> "AbstractSyrupyExtension":
return extension_class(test_location=self._test_location)

@property
def extension(self) -> "AbstractSyrupyExtension":
if not getattr(self, "_extension", None):
self._extension: "AbstractSyrupyExtension" = self._extension_class(
test_location=self._test_location
)
if not self._extension:
self._extension = self.__init_extension(self._extension_class)
return self._extension

@property
Expand All @@ -70,6 +74,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 +100,16 @@ 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":
"""
Modifies assertion instance options
"""
if extension_class:
self._extension = self.__init_extension(extension_class)
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 Expand Up @@ -131,6 +149,13 @@ def _assert(self, data: "SerializableData") -> bool:
updated=snapshot_updated,
)
self._executions += 1
self._post_assert()

def _post_assert(self) -> None:
"""
Restores assertion instance options
"""
self._extension = None

def _recall_data(self, index: int) -> Optional["SerializableData"]:
try:
Expand Down
3 changes: 3 additions & 0 deletions tests/__snapshots__/test_extension_image.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# name: test_multiple_snapshot_extensions.1
'<?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>'
---
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 # uses initial extension class
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 changed, so that the initial extension class is used when not overridden

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