Skip to content

Commit

Permalink
Merge pull request #48 from n0nSmoker/refactor/fork-function
Browse files Browse the repository at this point in the history
Refactor fork function
  • Loading branch information
n0nSmoker authored Jul 7, 2024
2 parents ccc78ff + f8537a9 commit c410d87
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 7 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ FILE = $(file)
test:
TEST_FILE=$(FILE) docker-compose up --build --abort-on-container-exit

format:
black .
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ services:
"poetry",
"run",
"pytest",
"-vv",
"--pylama",
"--cov=sqlalchemy_serializer",
"--cov-report",
Expand Down
19 changes: 18 additions & 1 deletion poetry.lock

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

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pytest-cov = "5.0.0"
pylama = "8.4.1"
requests = "2.32.3"
black = "^24.4.2"
pytest-mock = "^3.14.0"

[build-system]
requires = ["poetry-core"]
Expand Down
21 changes: 15 additions & 6 deletions sqlalchemy_serializer/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,6 @@ def fork(self, value, key: t.Optional[str] = None):
Process data in a separate serializer
:return: serialized value
"""
if not self.is_forkable(value):
return self.serialize(value)

serializer = Serializer(**self.opts._asdict())
if key is None:
serializer.schema = self.schema
Expand All @@ -206,18 +203,26 @@ def serialize_iter(self, value: Iterable) -> list:
res = []
for v in value:
try:
res.append(self.fork(value=v))
if self.is_forkable(v):
r = self.fork(value=v)
else:
r = self.serialize(v)
except IsNotSerializable:
logger.warning("Can not serialize type:%s", get_type(v))
continue

res.append(r)
return res

def serialize_dict(self, value: dict) -> dict:
res = {}
for k, v in value.items():
if self.schema.is_included(k): # TODO: Skip check if is NOT greedy
logger.debug("Serialize key:%s type:%s of dict", k, get_type(v))
res[k] = self.fork(key=k, value=v)
if self.is_forkable(v):
res[k] = self.fork(key=k, value=v)
else:
res[k] = self.serialize(v)
else:
logger.debug("Skip key:%s of dict", k)
return res
Expand All @@ -236,7 +241,11 @@ def serialize_model(self, value) -> dict:
logger.debug(
"Serialize key:%s type:%s model:%s", k, get_type(v), get_type(value)
)
res[k] = self.fork(key=k, value=v)
if self.is_forkable(v):
res[k] = self.fork(key=k, value=v)
else:
res[k] = self.serialize(v)

else:
logger.debug("Skip key:%s of model:%s", k, get_type(value))
return res
Expand Down
58 changes: 58 additions & 0 deletions tests/test_serializer_fork_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pytest

from sqlalchemy_serializer.serializer import Serializer


@pytest.fixture
def test_class():
serializer = Serializer(
date_format="%Y-%m-%d",
datetime_format="%Y-%m-%d %H:%M:%S",
time_format="%H:%M",
decimal_format="{}",
tzinfo=None,
serialize_types=(),
)
return serializer


def test_fork_with_none_key(mocker, test_class):
value = "test_value"
expected = "serialized"
schema = mocker.MagicMock()
mocker.patch(
"sqlalchemy_serializer.serializer.Serializer.__call__", return_value=expected
)

with mocker.patch.object(test_class, "schema", schema):
result = test_class.fork(value)

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


def test_fork_with_key(mocker, test_class):
value, key, expected = "test_value", "test_key", "serialized"
schema = mocker.MagicMock()

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

with mocker.patch.object(test_class, "schema", schema):
result = test_class.fork(value=value, key=key)

assert result == expected
schema.fork.assert_called_once_with(key=key)


def test_fork_logger(mocker, test_class):
mocked_logger = mocker.patch("sqlalchemy_serializer.serializer.logger")
mocker.patch("sqlalchemy_serializer.serializer.Serializer.__call__")

with mocker.patch.object(test_class, "schema", mocker.MagicMock()):
test_class.fork("value")

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

0 comments on commit c410d87

Please sign in to comment.