Fixed to_json returns empty dict from generator. #38

This commit is contained in:
Fabio Caccamo 2020-09-29 10:31:55 +02:00
parent 06c13218cd
commit 497d3782c0
2 changed files with 32 additions and 1 deletions

View File

@ -10,7 +10,7 @@ class BaseDict(dict):
self._dict = args[0]
else:
self._dict = dict(*args, **kwargs)
super(BaseDict, self).__init__(*args, **kwargs)
super(BaseDict, self).__init__(self._dict)
def __bool__(self):
return bool(self._dict)

View File

@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
from benedict import benedict
import unittest
class github_issue_0038_test_case(unittest.TestCase):
"""
https://github.com/fabiocaccamo/python-benedict/issues/38
To run this specific test:
- Run python -m unittest tests.github.test_issue_0038
"""
@staticmethod
def get_dict_generator():
for k, v in enumerate('abcd'):
yield k, v
def test_init_with_generator(self):
b = benedict(self.get_dict_generator())
self.assertEqual(b, {0: 'a', 1: 'b', 2: 'c', 3: 'd'})
self.assertEqual(b.to_json(), '{"0": "a", "1": "b", "2": "c", "3": "d"}')
# recast benedict to dict and back to benedict
b = benedict(self.get_dict_generator())
d = dict(b)
b = benedict(d)
self.assertEqual(b, {0: 'a', 1: 'b', 2: 'c', 3: 'd'})
self.assertEqual(b.to_json(), '{"0": "a", "1": "b", "2": "c", "3": "d"}')