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

feat: provide __repr__ for SnapshotAssertion #600

Merged
merged 3 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
54 changes: 48 additions & 6 deletions src/syrupy/assertion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import traceback
from collections import namedtuple
from dataclasses import (
dataclass,
field,
Expand All @@ -15,6 +16,7 @@
)

from .exceptions import SnapshotDoesNotExist
from .extensions.amber.serializer import Repr

if TYPE_CHECKING:
from .extensions.base import AbstractSyrupyExtension
Expand Down Expand Up @@ -54,8 +56,6 @@ class SnapshotAssertion:
test_location: "PyTestLocation"
update_snapshots: bool

name: str = "snapshot"

_exclude: Optional["PropertyFilter"] = field(
init=False,
default=None,
Expand All @@ -76,6 +76,9 @@ class SnapshotAssertion:
init=False,
default_factory=dict,
)
_execution_name_index: Dict["SnapshotIndex", int] = field(
init=False, default_factory=dict
)
_matcher: Optional["PropertyMatcher"] = field(
init=False,
default=None,
Expand Down Expand Up @@ -104,7 +107,7 @@ def num_executions(self) -> int:
return int(self._executions)

@property
def executions(self) -> Dict[int, AssertionResult]:
def executions(self) -> Dict[int, "AssertionResult"]:
return self._execution_results

@property
Expand All @@ -113,6 +116,44 @@ def index(self) -> "SnapshotIndex":
return self._custom_index
return self.num_executions

@property
def name(self) -> str:
return self._custom_index or "snapshot"

@property
def __repr(self) -> "SerializableData":
SnapshotAssertionRepr = namedtuple( # type: ignore
"SnapshotAssertion", ["name", "num_executions"]
)
assertion_result = self.executions.get(
(self._custom_index and self._execution_name_index.get(self._custom_index))
or self.num_executions - 1
)
return (
Repr(str(assertion_result.final_data))
if assertion_result
else SnapshotAssertionRepr(
name=self.name,
num_executions=self.num_executions,
)
)

@property
def __matcher(self) -> "PropertyMatcher":
"""
Get matcher that replaces `SnapshotAssertion` with one that can be serialized
"""

def _matcher(**kwargs: Any) -> Optional["SerializableData"]:
maybe_assertion = kwargs.get("data")
if isinstance(maybe_assertion, SnapshotAssertion):
return maybe_assertion.__repr
if self._matcher:
return self._matcher(**kwargs)
return maybe_assertion

return _matcher

def use_extension(
self, extension_class: Optional[Type["AbstractSyrupyExtension"]] = None
) -> "SnapshotAssertion":
Expand All @@ -132,7 +173,7 @@ def assert_match(self, data: "SerializableData") -> None:

def _serialize(self, data: "SerializableData") -> "SerializedData":
return self.extension.serialize(
data, exclude=self._exclude, matcher=self._matcher
data, exclude=self._exclude, matcher=self.__matcher
)

def get_assert_diff(self) -> List[str]:
Expand Down Expand Up @@ -190,8 +231,8 @@ def __call__(
self.__with_prop("_snapshot_diff", diff)
return self

def __dir__(self) -> List[str]:
return ["name", "num_executions"]
def __repr__(self) -> str:
return str(self._serialize(self.__repr))

def __eq__(self, other: "SerializableData") -> bool:
return self._assert(other)
Expand Down Expand Up @@ -233,6 +274,7 @@ def _assert(self, data: "SerializableData") -> bool:
finally:
snapshot_created = snapshot_data is None and assertion_success
snapshot_updated = matches is False and assertion_success
self._execution_name_index[self.index] = self._executions
self._execution_results[self._executions] = AssertionResult(
snapshot_location=snapshot_location,
snapshot_name=snapshot_name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,15 @@
# name: test_parameter_with_dot[value.with.dot]
'value.with.dot'
# ---
# name: test_reflection
# name: test_reflection.1
SnapshotAssertion(
name='snapshot',
name='reflectionA',
num_executions=0,
)
# ---
# name: test_reflection[reflectionA]
SnapshotAssertion(
name='reflectionA',
num_executions=0,
)
# ---
Expand Down
3 changes: 3 additions & 0 deletions tests/syrupy/extensions/amber/test_amber_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def test_non_snapshots(snapshot):


def test_reflection(snapshot):
"""assert [expected|snapshot] == [actual|received]"""
assert snapshot(name="reflectionA") == snapshot
assert snapshot == snapshot
assert snapshot == snapshot(name="reflectionA")


def test_empty_snapshot(snapshot):
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
"<class 'SnapshotAssertion'>"
{
"name": "snapshot",
"num_executions": 0
}