Skip to content

Commit

Permalink
Remove redundant call of the fork function
Browse files Browse the repository at this point in the history
  • Loading branch information
n0nSmoker committed Jul 13, 2024
1 parent 23ed84f commit 035e250
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 29 deletions.
14 changes: 4 additions & 10 deletions sqlalchemy_serializer/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,16 @@ def is_forkable(self, value):
value, (Iterable, dict, SerializerMixin)
)

def fork(self, value, key: t.Optional[str] = None):
def fork(self, value, key: str):
"""
Process data in a separate serializer
:return: serialized value
"""
serializer = Serializer(**self.opts._asdict())
serializer.set_recursion_depth(self.recursion_depth + 1)
if key is None:
serializer.schema = self.schema
else:
serializer.schema = self.schema.fork(key=key)
serializer.schema = self.schema.fork(key=key)

logger.debug("Fork serializer for type:%s", get_type(value))
logger.debug("Fork serializer for type:%s key:%s", get_type(value), key)
return serializer(value)

def serialize(self, value):
Expand All @@ -209,10 +206,7 @@ def serialize_iter(self, value: Iterable) -> list:
res = []
for v in value:
try:
if self.is_forkable(v):
r = self.fork(value=v)
else:
r = self.serialize(v)
r = self.serialize(v)
except IsNotSerializable:
logger.warning("Can not serialize type:%s", get_type(v))
continue
Expand Down
24 changes: 5 additions & 19 deletions tests/test_serializer_fork_function.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,3 @@
def test_fork_with_none_key(mocker, get_serializer):
value = "test_value"
expected = "serialized"
schema = mocker.MagicMock()
serializer = get_serializer()

mocker.patch(
"sqlalchemy_serializer.serializer.Serializer.__call__", return_value=expected
)

mocker.patch.object(serializer, "schema", schema)
result = serializer.fork(value)

assert result == expected
schema.fork.assert_not_called()


def test_fork_with_key(mocker, get_serializer):
value, key, expected = "test_value", "test_key", "serialized"
schema = mocker.MagicMock()
Expand All @@ -32,11 +15,14 @@ def test_fork_with_key(mocker, get_serializer):


def test_fork_logger(mocker, get_serializer):
value, key = "test_value", "test_key"
mocked_logger = mocker.patch("sqlalchemy_serializer.serializer.logger")
mocker.patch("sqlalchemy_serializer.serializer.Serializer.__call__")
serializer = get_serializer()

mocker.patch.object(serializer, "schema", mocker.MagicMock())
serializer.fork("value")
serializer.fork(value, key=key)

mocked_logger.debug.assert_called_once_with("Fork serializer for type:%s", "str")
mocked_logger.debug.assert_called_once_with(
"Fork serializer for type:%s key:%s", "str", key
)

0 comments on commit 035e250

Please sign in to comment.