Compare commits

...

2 Commits

Author SHA1 Message Date
Stefan Wojcik
c78e5079d4 extra test for escaping quotes 2016-12-04 15:33:48 -05:00
Stefan Wojcik
ab69e50361 fix __repr__ method of the StrictDict 2016-12-04 15:29:03 -05:00
2 changed files with 17 additions and 8 deletions

View File

@ -438,7 +438,7 @@ class StrictDict(object):
__slots__ = allowed_keys_tuple
def __repr__(self):
return "{%s}" % ', '.join('"{0!s}": {0!r}'.format(k) for k in self.iterkeys())
return "{%s}" % ', '.join('"{0!s}": {1!r}'.format(k, v) for k, v in self.items())
cls._classes[allowed_keys] = SpecificStrictDict
return cls._classes[allowed_keys]

View File

@ -1,4 +1,5 @@
import unittest
from mongoengine.base.datastructures import StrictDict, SemiStrictDict
@ -13,6 +14,14 @@ class TestStrictDict(unittest.TestCase):
d = self.dtype(a=1, b=1, c=1)
self.assertEqual((d.a, d.b, d.c), (1, 1, 1))
def test_repr(self):
d = self.dtype(a=1, b=2, c=3)
self.assertEqual(repr(d), '{"a": 1, "b": 2, "c": 3}')
# make sure quotes are escaped properly
d = self.dtype(a='"', b="'", c="")
self.assertEqual(repr(d), '{"a": \'"\', "b": "\'", "c": \'\'}')
def test_init_fails_on_nonexisting_attrs(self):
self.assertRaises(AttributeError, lambda: self.dtype(a=1, b=2, d=3))