python-benedict/tests/core/test_dump.py

70 lines
1.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from benedict.core import dump as _dump
from decimal import Decimal
2020-02-20 17:08:38 +00:00
from six import PY3
import datetime as dt
import unittest
class dump_test_case(unittest.TestCase):
def test_dump(self):
d = {
'a': {
'b': {
'c': 1
}
}
}
r = """{
"a": {
"b": {
"c": 1
}
}
}"""
o = _dump(d)
self.assertEqual(o, r)
def test_dump_with_datetime(self):
d = {
'datetime': dt.datetime(2019, 6, 11),
}
r = """{
"datetime": "2019-06-11T00:00:00"
}"""
o = _dump(d)
self.assertEqual(o, r)
def test_dump_with_set(self):
d = {
'set': set([0, 1, 2, 3, 4, 5]),
}
r = """{
"set": [
0,
1,
2,
3,
4,
5
]
}"""
o = _dump(d)
2020-02-20 17:08:38 +00:00
if PY3:
self.assertEqual(o, r)
2020-02-20 10:41:26 +00:00
def test_dump_with_unsortable_keys(self):
d = {
None: None,
0: 0,
1: 1,
}
# must not raise TypeError
_dump(d)
d['dt'] = dt
with self.assertRaises(TypeError):
o = _dump(d, sort_keys=False, default=None)