Skip to content

Commit

Permalink
update case for serialize Box and BoxList to standard python dict and…
Browse files Browse the repository at this point in the history
… list, add test cases
  • Loading branch information
0xGosu committed Apr 30, 2020
1 parent 894dfa3 commit 1f1e8c0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion nameko_django/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.1
1.1.2
4 changes: 4 additions & 0 deletions nameko_django/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def encode_nondefault_object(obj):
return
if hasattr(obj, '_asdict') and callable(obj._asdict):
return dict(obj._asdict())
elif hasattr(obj, 'to_dict') and callable(obj.to_dict):
return dict(obj.to_dict())
elif hasattr(obj, 'to_list') and callable(obj.to_list):
return list(obj.to_list())
elif isinstance(obj, tuple): # tuple will be treated as list
return list(obj)
elif isinstance(obj, Enum) and hasattr(obj, 'value'):
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
django==1.11.*
nameko==2.11.*
msgpack==0.5.*
msgpack==1.0.*
aenum==2.1.*
python-box==3.2.*
tox==3.13.*
flake8==3.7.8
nose==1.3.7
pytest==4.6.6
pytest-django==3.6.0
pytest-runner==5.1

4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@
install_requires=[
'nameko>=2.11.0',
'django>=1.10',
'msgpack>=0.5.0',
'msgpack>=1.0.0',
'aenum>=2.1.0'
],
setup_requires=[
] + pytest_runner,
test_suite='nose.collector',
tests_require=['pyparsing==2.4.2', 'nose==1.3.7', 'pytest==4.6.6', 'pytest-django==3.6.0'],
tests_require=['pyparsing==2.4.2', 'nose==1.3.7', 'pytest==4.6.6', 'pytest-django==3.6.0', 'python-box==3.2.0'],
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
Expand Down
15 changes: 15 additions & 0 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from django.utils import timezone
from nose import tools
from django.db.models import ObjectDoesNotExist
from box import Box, BoxList


def test_simple_list():
Expand Down Expand Up @@ -59,6 +60,13 @@ def test_simple_tuple():
assert [list(e) if isinstance(e, tuple) else e for e in test_data] == dec_data


def test_simple_boxlist():
test_data = [{'a': 1}, {'b': 2}, {'c': 3}, 'd', 0]
enc_data = dumps(BoxList(test_data))
dec_data = loads(enc_data)
assert test_data == dec_data


def test_unicode():
test_data = ["", "abcd", ["defgh"], "Русский текст"]
enc_data = dumps(test_data)
Expand Down Expand Up @@ -89,6 +97,13 @@ def test_simple_dictionary():
assert test_data == dec_data


def test_simple_boxdict():
test_data = {'a': 1, 'b': 2, 'c': 3, 'd': '', 'e': {'e0': 0, 'e1': -1, 'ee': ['e', 'e']}}
enc_data = dumps(Box(test_data))
dec_data = loads(enc_data)
assert test_data == dec_data


def test_datetime_simple():
d0 = datetime(2019, 9, 26, 9, 16, 35, 881134, tzinfo=FixedOffset(0, name="UTC"))
d1 = datetime(2019, 10, 16, 9, 32, 20, 555204, tzinfo=FixedOffset(0, name="UTC"))
Expand Down

0 comments on commit 1f1e8c0

Please sign in to comment.