Skip to content

Commit

Permalink
fix: support call syntax for snapshot fixture overriding (#160)
Browse files Browse the repository at this point in the history
* wip: call syntax with differentiation

* test: add example

* refactor: callout state mutation

* cr: restore fixture instance options after assertion

* cr: fix typo

Co-authored-by: Noah <noah.negin-ulster@tophatmonocle.com>
  • Loading branch information
iamogbz and Noah authored Mar 24, 2020
1 parent 6d038f6 commit 4cf051c
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 29 deletions.
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 preserve assertion index or state.
"""
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
assert actual_png == snapshot(extension_class=PNGImageSnapshotExtension)
assert actual_svg == snapshot(extension_class=SVGImageSnapshotExtension)

0 comments on commit 4cf051c

Please sign in to comment.