Skip to content

Commit

Permalink
refactor: collapse io and serializer into single class (#30)
Browse files Browse the repository at this point in the history
* refactor: collapse io and serializer into single class

* wip: update io
  • Loading branch information
Noah authored Dec 7, 2019
1 parent 1680d10 commit 40a3001
Show file tree
Hide file tree
Showing 11 changed files with 238 additions and 270 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import pytest
@pytest.fixture
def snapshot_custom(snapshot):
return snapshot.with_class(
io_class=CustomIOClass,
serializer_class=CustomSerializerClass,
)

Expand All @@ -67,7 +66,7 @@ def test_image(snapshot_custom):
assert actual == snapshot_custom
```

Both `CustomIOClass` and `CustomSerializerClass` should extend `syrupy.io.SnapshotIO` and `syrupy.io.SnapshotSerializer` respectively.
`CustomSerializerClass` should extend `syrupy.serializer.SnapshotSerializer`.

## Uninstalling

Expand Down
2 changes: 0 additions & 2 deletions src/syrupy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pytest

from .assertion import SnapshotAssertion
from .io import SnapshotIO
from .serializer import SnapshotSerializer
from .location import TestLocation
from .session import SnapshotSession
Expand Down Expand Up @@ -60,7 +59,6 @@ def snapshot(request):
)
return SnapshotAssertion(
update_snapshots=request.config.option.update_snapshots,
io_class=SnapshotIO,
serializer_class=SnapshotSerializer,
test_location=test_location,
session=request.session._syrupy,
Expand Down
34 changes: 10 additions & 24 deletions src/syrupy/assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from .exceptions import SnapshotDoesNotExist

from .io import SnapshotIO
from .serializer import SnapshotSerializer
from .location import TestLocation

Expand All @@ -15,13 +14,11 @@ def __init__(
self,
*,
update_snapshots: bool,
io_class: Type[SnapshotIO],
serializer_class: Type[SnapshotSerializer],
test_location: TestLocation,
session,
):
self._update_snapshots = update_snapshots
self._io_class = io_class
self._serializer_class = serializer_class
self._test_location = test_location
self._executions = 0
Expand All @@ -31,33 +28,24 @@ def __init__(
self._session: SnapshotSession = session
self._session.register_request(self)

@property
def io(self) -> SnapshotIO:
if not getattr(self, "_io", None):
self._io = self._io_class(
test_location=self._test_location, file_hook=self._file_hook
)
return self._io

@property
def serializer(self) -> SnapshotSerializer:
if not getattr(self, "_serializer", None):
self._serializer = self._serializer_class()
self._serializer = self._serializer_class(
test_location=self._test_location, file_hook=self._file_hook
)
return self._serializer

@property
def num_executions(self) -> int:
return int(self._executions)

def with_class(
self,
io_class: Type[SnapshotIO] = None,
serializer_class: Type[SnapshotSerializer] = None,
self, serializer_class: Type[SnapshotSerializer] = None,
):
return self.__class__(
update_snapshots=self._update_snapshots,
test_location=self._test_location,
io_class=io_class or self._io_class,
serializer_class=serializer_class or self._serializer_class,
session=self._session,
)
Expand All @@ -66,13 +54,12 @@ def assert_match(self, data):
assert self == data

def get_assert_diff(self, data) -> List[str]:
serialized_data = self.serializer.encode(data)
snapshot_data = self._recall_data(index=self.num_executions - 1)
if snapshot_data is None:
return ["Snapshot does not exist!"]

if serialized_data != snapshot_data:
return [f"- {serialized_data}", f"+ {snapshot_data}"]
if data != snapshot_data:
return [f"- {data}", f"+ {snapshot_data}"]

return []

Expand All @@ -91,12 +78,11 @@ def __eq__(self, other) -> bool:
def _assert(self, data) -> bool:
self._session.register_assertion(self)
try:
serialized_data = self.serializer.encode(data)
snapshot_data = self._recall_data(index=self.num_executions)
matches = snapshot_data is not None and serialized_data == snapshot_data
matches = snapshot_data is not None and data == snapshot_data
if not matches and self._update_snapshots:
self.io.create_or_update_snapshot(
serialized_data, index=self.num_executions
self.serializer.create_or_update_snapshot(
serialized_data=data, index=self.num_executions
)
return True
return matches
Expand All @@ -105,6 +91,6 @@ def _assert(self, data) -> bool:

def _recall_data(self, index: int) -> Optional[Any]:
try:
return self.io.read_snapshot(index=index)
return self.serializer.read_snapshot(index=index)
except SnapshotDoesNotExist:
return None
169 changes: 0 additions & 169 deletions src/syrupy/io.py

This file was deleted.

7 changes: 3 additions & 4 deletions src/syrupy/plugins/image/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from .io import AbstractImageSnapshotIO
from .serializer import ImageSnapshotSerializer
from .serializer import AbstractImageSnapshotSerializer


class PNGImageSnapshotIO(AbstractImageSnapshotIO):
class PNGImageSnapshotSerializer(AbstractImageSnapshotSerializer):
@property
def extension(self) -> str:
return "png"


class SVGImageSnapshotIO(AbstractImageSnapshotIO):
class SVGImageSnapshotSerializer(AbstractImageSnapshotSerializer):
@property
def extension(self) -> str:
return "svg"
52 changes: 0 additions & 52 deletions src/syrupy/plugins/image/io.py

This file was deleted.

Loading

0 comments on commit 40a3001

Please sign in to comment.