Skip to content

Commit

Permalink
Merge pull request #3 from tranvietanh1991/develop
Browse files Browse the repository at this point in the history
fix the case TypeError: can't serialize tuple(.., .., ) due to strict…
  • Loading branch information
0xGosu committed Apr 22, 2020
2 parents 030788e + 894dfa3 commit c3d3efe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion nameko_django/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0
1.1.1
5 changes: 4 additions & 1 deletion nameko_django/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ def encode_nondefault_object(obj):
return
if hasattr(obj, '_asdict') and callable(obj._asdict):
return dict(obj._asdict())
elif isinstance(obj, tuple): # tuple will be treated as list
return list(obj)
elif isinstance(obj, Enum) and hasattr(obj, 'value'):
return obj.value
elif isinstance(obj, Constant) and hasattr(obj, '_value_'):
Expand Down Expand Up @@ -171,7 +173,8 @@ def dumps(o):
def loads(s):
if not isinstance(s, string_types):
s = bytes(s)
r = unpackb(s, ext_hook=django_ext_hook, object_hook=decode_dict_object, list_hook=decode_list_object, raw=False)
r = unpackb(s, ext_hook=django_ext_hook, object_hook=decode_dict_object, list_hook=decode_list_object,
raw=False, strict_map_key=False)
if isinstance(r, string_types):
return decode_single_object(r)
else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
setup_requires=[
] + pytest_runner,
test_suite='nose.collector',
tests_require=['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'],
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
Expand Down
44 changes: 37 additions & 7 deletions tests/test_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,43 @@
from aenum import Enum, IntEnum, Constant
from collections import namedtuple
from django.utils.timezone import FixedOffset
from django.utils import timezone
from nose import tools
from django.db.models import ObjectDoesNotExist


def test_simple_list():
test_data = [
0, 1, 127, 128, 255, 256, 65535, 65536, 4294967295, 4294967296,
-1, -32, -33, -128, -129, -32768, -32769, -4294967296, -4294967297,
[0, 1, 127, 128, 255, 256, 65535, 65536, 4294967295, 4294967296],
[-1, -32, -33, -128, -129, -32768, -32769, -4294967296, -4294967297],
1.0,
b"", b"a", b"a" * 31, b"a" * 32,
None, True, False,
[], [[], ], [[], None, ],
{None: 0},
{0: None},
(1 << 23),
]
enc_data = dumps(test_data)
dec_data = loads(enc_data)
assert test_data == dec_data


def test_simple_tuple():
test_data = (
[0, 1, 127, 128, 255, 256, 65535, 65536, 4294967295, 4294967296],
(-1, -32, -33, -128, -129, -32768, -32769, -4294967296, -4294967297),
1.0,
b"", b"a", b"a" * 31, b"a" * 32,
None, True, False,
[], [[], ], [[], None, ],
{0: None},
(1 << 23),
)
enc_data = dumps(test_data)
dec_data = loads(enc_data)
assert [list(e) if isinstance(e, tuple) else e for e in test_data] == dec_data


def test_unicode():
test_data = ["", "abcd", ["defgh"], "Русский текст"]
enc_data = dumps(test_data)
Expand Down Expand Up @@ -190,11 +207,24 @@ def test_decimal():
DATABASES=dict(default={'ENGINE': 'django.db.backends.sqlite3'}),
DEFAULT_INDEX_TABLESPACE='indexes',
LOGGING={},
USE_TZ=True,
TIME_ZONE='UTC'
)

settings.configure(**DJANGO_DEFAULT_SETTING)


def test_django_timezone():
now = timezone.now()
enc_data = dumps(now)
dec_data = loads(enc_data)
assert now == dec_data
today = timezone.localdate()
enc_data = dumps(today)
dec_data = loads(enc_data)
assert today == dec_data


def test_django_orm():
from django.contrib.auth.models import User
test_user = User(username="test_user", email="test_user@gmail.com")
Expand All @@ -206,7 +236,7 @@ def test_django_orm():

def test_django_orm_queryset():
from django.contrib.auth.models import User
test_user_qs = User.objects.all().filter(last_login__isnull=False, id__gt=1000, date_joined__gt=datetime.now())
test_user_qs = User.objects.all().filter(last_login__isnull=False, id__gt=1000, date_joined__gt=timezone.now())
enc_data = dumps(test_user_qs)
dec_data = loads(enc_data)
qs1 = test_user_qs.query
Expand Down Expand Up @@ -246,7 +276,7 @@ def test_django_orm_eval_with_db2():
@pytest.mark.django_db
def test_django_orm_queryset_with_db():
from django.contrib.auth.models import User
test_user_qs = User.objects.all().filter(last_login__isnull=False, id__gt=1000, date_joined__gt=datetime.now())
test_user_qs = User.objects.all().filter(last_login__isnull=False, id__gt=1000, date_joined__gt=timezone.now())
enc_data = dumps(test_user_qs)
dec_data = loads(enc_data)
qs1 = test_user_qs.query
Expand All @@ -259,8 +289,8 @@ def test_django_orm_queryset_with_db():
def test_django_orm_queryset_eval_with_db(admin_user):
from django.contrib.auth.models import User
test_user_qs = User.objects.all().filter(id__gte=1, # last_login__isnull=False,
date_joined__gt='2018-11-22 00:47:14.263837')
qs_string = '(auth.User: {})'.format("id >= 1 and date_joined > '2018-11-22 00:47:14.263837'")
date_joined__gt='2018-11-22 00:47:14.263837+00:00')
qs_string = '(auth.User: {})'.format("id >= 1 and date_joined > '2018-11-22 00:47:14.263837+00:00'")
logger.debug(qs_string)
enc_data = dumps(qs_string)
dec_data = loads(enc_data)
Expand Down

0 comments on commit c3d3efe

Please sign in to comment.