Skip to content

Commit

Permalink
feat: release syrupy v2 (#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahnu authored Apr 10, 2022
2 parents e783187 + d46122f commit bc8b3a9
Show file tree
Hide file tree
Showing 23 changed files with 495 additions and 467 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ def test_bar(snapshot):
}))
```

```ambr
```py
# name: test_bar
<class 'dict'> {
'date_created': <class 'datetime'>,
dict({
'date_created': datetime,
'value': 'Some computed value!!',
}
---
})
# ---
```

#### `exclude`
Expand Down Expand Up @@ -206,15 +206,15 @@ def test_bar(snapshot):
assert actual == snapshot(exclude=props("id", "1"))
```

```ambr
```py
# name: test_bar
<class 'dict'> {
'list': <class 'list'> [
dict({
'list': list([
1,
3,
],
}
---
]),
})
# ---
```

###### `paths(path_string, *path_strings)`
Expand All @@ -234,15 +234,15 @@ def test_bar(snapshot):
assert actual == snapshot(exclude=paths("date", "list.1"))
```

```ambr
```py
# name: test_bar
<class 'dict'> {
'list': <class 'list'> [
dict({
'list': list([
1,
3,
],
}
---
]),
})
# ---
```

#### `extension_class`
Expand Down
26 changes: 19 additions & 7 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ syrupy = 'syrupy'

[tool.poetry.dependencies]
python = '>=3.6,<4'
attrs = '>=18.2.0,<22.0.0'
colored = '>=1.3.92,<2.0.0'
pytest = '>=5.1.0,<8.0.0'
dataclasses = { version = '^0.8.0', python = '>=3.6,<3.7' }

[tool.poetry.group.test.dependencies]
codecov = '^2.1.12'
Expand Down
91 changes: 53 additions & 38 deletions src/syrupy/assertion.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import traceback
from dataclasses import (
dataclass,
field,
)
from gettext import gettext
from typing import (
TYPE_CHECKING,
Expand All @@ -11,8 +15,6 @@
Union,
)

import attr

from .exceptions import SnapshotDoesNotExist

if TYPE_CHECKING:
Expand All @@ -27,16 +29,16 @@
)


@attr.s
@dataclass
class AssertionResult:
snapshot_location: str = attr.ib()
snapshot_name: str = attr.ib()
asserted_data: Optional["SerializedData"] = attr.ib()
recalled_data: Optional["SerializedData"] = attr.ib()
created: bool = attr.ib()
updated: bool = attr.ib()
success: bool = attr.ib()
exception: Optional[Exception] = attr.ib()
snapshot_location: str
snapshot_name: str
asserted_data: Optional["SerializedData"]
recalled_data: Optional["SerializedData"]
created: bool
updated: bool
success: bool
exception: Optional[Exception]

@property
def final_data(self) -> Optional["SerializedData"]:
Expand All @@ -45,43 +47,56 @@ def final_data(self) -> Optional["SerializedData"]:
return self.recalled_data


@attr.s(cmp=False, repr=False)
@dataclass(eq=False, order=False, repr=False)
class SnapshotAssertion:
name: str = attr.ib(default="snapshot")
_session: "SnapshotSession" = attr.ib(kw_only=True)
_extension_class: Type["AbstractSyrupyExtension"] = attr.ib(kw_only=True)
_test_location: "PyTestLocation" = attr.ib(kw_only=True)
_update_snapshots: bool = attr.ib(kw_only=True)
_exclude: Optional["PropertyFilter"] = attr.ib(
init=False, default=None, kw_only=True
session: "SnapshotSession"
extension_class: Type["AbstractSyrupyExtension"]
test_location: "PyTestLocation"
update_snapshots: bool

name: str = "snapshot"

_exclude: Optional["PropertyFilter"] = field(
init=False,
default=None,
)
_custom_index: Optional[str] = field(
init=False,
default=None,
)
_extension: Optional["AbstractSyrupyExtension"] = field(
init=False,
default=None,
)
_custom_index: Optional[str] = attr.ib(init=False, default=None, kw_only=True)
_extension: Optional["AbstractSyrupyExtension"] = attr.ib(
init=False, default=None, kw_only=True
_executions: int = field(
init=False,
default=0,
)
_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
_execution_results: Dict[int, "AssertionResult"] = field(
init=False,
default_factory=dict,
)
_matcher: Optional["PropertyMatcher"] = attr.ib(
init=False, default=None, kw_only=True
_matcher: Optional["PropertyMatcher"] = field(
init=False,
default=None,
)
_post_assert_actions: List[Callable[..., None]] = attr.ib(
init=False, factory=list, kw_only=True
_post_assert_actions: List[Callable[..., None]] = field(
init=False,
default_factory=list,
)

def __attrs_post_init__(self) -> None:
self._session.register_request(self)
def __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)
return extension_class(test_location=self.test_location)

@property
def extension(self) -> "AbstractSyrupyExtension":
if not self._extension:
self._extension = self.__init_extension(self._extension_class)
self._extension = self.__init_extension(self.extension_class)
return self._extension

@property
Expand All @@ -106,10 +121,10 @@ def use_extension(
specified extension class. This does not preserve assertion index or state.
"""
return self.__class__(
update_snapshots=self._update_snapshots,
test_location=self._test_location,
extension_class=extension_class or self._extension_class,
session=self._session,
update_snapshots=self.update_snapshots,
test_location=self.test_location,
extension_class=extension_class or self.extension_class,
session=self.session,
)

def assert_match(self, data: "SerializableData") -> None:
Expand Down Expand Up @@ -193,7 +208,7 @@ def _assert(self, data: "SerializableData") -> bool:
serialized_data=serialized_data, snapshot_data=snapshot_data
)
assertion_success = matches
if not matches and self._update_snapshots:
if not matches and self.update_snapshots:
self.extension.write_snapshot(
data=serialized_data,
index=self.index,
Expand Down
Loading

0 comments on commit bc8b3a9

Please sign in to comment.