-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from n0nSmoker/tests/serializer.fixture
Introduce serializer fixture
- Loading branch information
Showing
3 changed files
with
38 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,38 @@ | ||
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): | ||
def test_fork_with_none_key(mocker, serializer): | ||
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) | ||
with mocker.patch.object(serializer, "schema", schema): | ||
result = serializer.fork(value) | ||
|
||
assert result == expected | ||
schema.fork.assert_not_called() | ||
|
||
|
||
def test_fork_with_key(mocker, test_class): | ||
def test_fork_with_key(mocker, serializer): | ||
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) | ||
with mocker.patch.object(serializer, "schema", schema): | ||
result = serializer.fork(value=value, key=key) | ||
|
||
assert result == expected | ||
schema.fork.assert_called_once_with(key=key) | ||
|
||
|
||
def test_fork_logger(mocker, test_class): | ||
def test_fork_logger(mocker, serializer): | ||
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") | ||
with mocker.patch.object(serializer, "schema", mocker.MagicMock()): | ||
serializer.fork("value") | ||
|
||
mocked_logger.debug.assert_called_once_with("Fork serializer for type:%s", "str") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,5 @@ | ||
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_set_recursion_depth_success(test_class): | ||
def test_set_recursion_depth_success(serializer): | ||
new_recursion_depth = 123 | ||
assert test_class.recursion_depth == 0 | ||
test_class.set_recursion_depth(new_recursion_depth) | ||
assert test_class.recursion_depth == new_recursion_depth | ||
assert serializer.recursion_depth == 0 | ||
serializer.set_recursion_depth(new_recursion_depth) | ||
assert serializer.recursion_depth == new_recursion_depth |