Skip to content

Commit

Permalink
Merge pull request #27 from paylogic/fix-list-type
Browse files Browse the repository at this point in the history
Fix List raising the wrong errors (or not error at all)
  • Loading branch information
youtux committed Sep 30, 2020
2 parents 9d48e90 + 2781c26 commit 0671945
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Expand Up @@ -2,11 +2,11 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/psf/black
rev: 19.10b0
rev: 20.8b1
hooks:
- id: black
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand Down
6 changes: 6 additions & 0 deletions CHANGES.rst
@@ -1,6 +1,12 @@
Changelog
=========

1.7.0
-----

* Fix `List` type raising unrelated errors (or no error at all, when passing a dictionary) when deserializing objects that are not lists.


1.6.1
-----

Expand Down
2 changes: 1 addition & 1 deletion halogen/__init__.py
@@ -1,6 +1,6 @@
"""halogen public API."""

__version__ = "1.6.1"
__version__ = "1.7.0"

try:
from halogen.schema import Schema, attr, Attr, Link, Curie, Embedded, Accessor
Expand Down
7 changes: 5 additions & 2 deletions halogen/types.py
Expand Up @@ -64,8 +64,11 @@ def serialize(self, value, **kwargs):

def deserialize(self, value, **kwargs):
"""Deserialize every item of the list."""
if self.allow_scalar and not isinstance(value, (list, tuple)):
value = [value]
if not isinstance(value, (list, tuple)):
if self.allow_scalar:
value = [value]
else:
raise ValidationError('"{}" is not a list'.format(value))
value = super(List, self).deserialize(value)
result = []
errors = []
Expand Down
3 changes: 2 additions & 1 deletion tests/serialize/test_simple.py
Expand Up @@ -81,7 +81,8 @@ class Error(halogen.Schema):
message = halogen.Attr(attr=lambda error, language: error["message"][language])

error = Error.serialize(
{"message": {"dut": "Ongeldig e-mailadres", "eng": "Invalid email address"}}, language="dut",
{"message": {"dut": "Ongeldig e-mailadres", "eng": "Invalid email address"}},
language="dut",
)

assert error == {"message": "Ongeldig e-mailadres"}
16 changes: 12 additions & 4 deletions tests/test_types.py
Expand Up @@ -10,6 +10,7 @@

from pytz import timezone
from halogen import types
from halogen.exceptions import ValidationError


def test_type():
Expand All @@ -28,6 +29,14 @@ def test_list():
assert value == type_.deserialize(value)


@pytest.mark.parametrize("input", [{"foo": "bar"}, 42, 11.5, True, False, None, "", "foo"])
def test_list_bad_input(input):
"""Test that the deserialization fails correctly when the input is not a list, and scalars are not allowed"""
type_ = types.List(allow_scalar=False)
with pytest.raises(ValidationError, match=".*is not a list"):
type_.deserialize(input)


@pytest.mark.parametrize(
["value", "serialized"],
[
Expand Down Expand Up @@ -60,9 +69,7 @@ def test_isoutcdatetime_bc():
assert type_.deserialize("1799-12-31T23:00:00Z") == value.replace(microsecond=0)


@pytest.mark.parametrize(
"value", ["01.01.1981 11:11:11", "123x3"],
)
@pytest.mark.parametrize("value", ["01.01.1981 11:11:11", "123x3"])
def test_isoutcdatetime_wrong(value):
"""Test iso datetime when wrong value is passed."""
type_ = types.ISOUTCDateTime()
Expand Down Expand Up @@ -178,7 +185,8 @@ def test_amount_serialize():
def test_nullable_type():

nested_type = mock.MagicMock(
serialize=mock.MagicMock(return_value="serialize"), deserialize=mock.MagicMock(return_value="deserialize"),
serialize=mock.MagicMock(return_value="serialize"),
deserialize=mock.MagicMock(return_value="deserialize"),
)

nullable = types.Nullable(nested_type)
Expand Down

0 comments on commit 0671945

Please sign in to comment.