Skip to content

Commit

Permalink
Merge pull request #76 from saraducks/HSS-12948_Add_decimal_support_t…
Browse files Browse the repository at this point in the history
…o_json_record

Adding the Decimal type check to_json record
  • Loading branch information
hearsaydev authored Mar 6, 2020
2 parents 3369963 + 2eefe9b commit 1bc734a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 1 deletion normalize/record/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from __future__ import absolute_import
import six
import decimal

from builtins import str
from past.builtins import basestring
Expand Down Expand Up @@ -239,7 +240,7 @@ def to_json(record, extraneous=True, prop=None):
elif isinstance(record, (list, tuple, set, frozenset)):
return list(_json_data(x, extraneous) for x in record)

elif isinstance(record, (basestring, int, float, type(None))):
elif isinstance(record, (basestring, int, float, decimal.Decimal, type(None))):
return record

else:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
install_requires=install_requires,
tests_require=tests_require,
test_suite="tests",
version='2.0.2',
version='2.0.3',
url="http://hearsaycorp.github.io/normalize",
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down
22 changes: 20 additions & 2 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


import six
import decimal
from builtins import object
from datetime import date
from datetime import datetime
Expand Down Expand Up @@ -78,12 +79,29 @@ class DemoType(Record):
self.assertIsInstance(demo.num, six.integer_types)
demo.num = "123.0"
self.assertIsInstance(demo.num, float)
demo.num = "nan"
self.assertIsInstance(demo.num, float)

with self.assertRaises(exc.CoerceError):
demo.num = "123.0a"

demo.num = "nan"
self.assertIsInstance(demo.num, float)
demo.num = decimal.Decimal("nan")
self.assertIsInstance(demo.num, decimal.Decimal)

demo.num = decimal.Decimal("123.0")
self.assertIsInstance(demo.num, decimal.Decimal)

demo.num = decimal.Decimal(12)
self.assertIsInstance(demo.num, decimal.Decimal)

with self.assertRaises(decimal.InvalidOperation):
demo.num = decimal.Decimal(" ")

with self.assertRaises(decimal.InvalidOperation):
demo.num = decimal.Decimal("abc")

with self.assertRaises(decimal.InvalidOperation):
demo.num = decimal.Decimal("123.0a")

def test_dates_and_integer_types(self):
class Props(Record):
Expand Down

0 comments on commit 1bc734a

Please sign in to comment.