Repository: MongoEngine/mongoengine

Issue:
Strange __repr__ method of StrictDict
In __repr__method of the StrictDict class i saw:

```
def __repr__(self):
       return "{%s}" % ', '.join('"{0!s}": {0!r}'.format(k) for k in self.iterkeys())
```
May be it is an error and author means:
```
def __repr__(self):
    return "{%s}" % ', '.join('"{0!s}": {1!r}'.format(k, v) for k, v in self.iteritems())
```

It is here: [mongoengine/base/datastructures.py:440](https://github.com/MongoEngine/mongoengine/blob/master/mongoengine/base/datastructures.py#L440)




Discussion:
Do I have to do a pull request? Let me know your opinion on it.
Just verified that a strict document with a following schema
```
class TestParentDoc(Document):
    text = StringField()
    STRICT = True
```

shows invalid data for `doc._data`. For example, if `doc` looks like so in MongoDB `{'_id': ObjectId('5833326ac6e47b7d0d750f46'), 'whatever': 1}`, then:

```
In [22]: doc._data
Out[22]: {"id": 'id', "text": 'text'}
```

Here's a comparison of the `__repr__` we have right now vs what you suggested:
```
In [29]: print "{%s}" % ', '.join('"{0!s}": {0!r}'.format(k) for k in a._data.iterkeys())
{"id": 'id', "text": 'text'}

In [30]: print "{%s}" % ', '.join('"{0!s}": {1!r}'.format(k, v) for k, v in a._data.iteritems())
{"id": ObjectId('5833326ac6e47b7d0d750f46'), "text": None}
```

Clearly your solution is better @sergyp. Could you submit a PR that also includes unit tests for this? I'd love to see at least one unit test that also shows we properly escape `\"` and `\'` existing within the values of a dict.
@sergyp will you have a chance to look into creating a PR for this issue?

Resolving patch:
diff --git a/mongoengine/base/datastructures.py b/mongoengine/base/datastructures.py
--- a/mongoengine/base/datastructures.py
+++ b/mongoengine/base/datastructures.py
@@ -438,7 +438,7 @@ class SpecificStrictDict(cls):
                 __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]


Test patch:
diff --git a/tests/test_datastructures.py b/tests/test_datastructures.py
--- a/tests/test_datastructures.py
+++ b/tests/test_datastructures.py
@@ -1,5 +1,6 @@
 import unittest
-from mongoengine.base.datastructures import StrictDict, SemiStrictDict 
+
+from mongoengine.base.datastructures import StrictDict, SemiStrictDict
 
 
 class TestStrictDict(unittest.TestCase):
@@ -13,9 +14,17 @@ def test_init(self):
         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))
-        
+
     def test_eq(self):
         d = self.dtype(a=1, b=1, c=1)
         dd = self.dtype(a=1, b=1, c=1)
@@ -24,7 +33,7 @@ def test_eq(self):
         g = self.strict_dict_class(("a", "b", "c", "d"))(a=1, b=1, c=1, d=1)
         h = self.strict_dict_class(("a", "c", "b"))(a=1, b=1, c=1)
         i = self.strict_dict_class(("a", "c", "b"))(a=1, b=1, c=2)
-        
+
         self.assertEqual(d, dd)
         self.assertNotEqual(d, e)
         self.assertNotEqual(d, f)
@@ -38,19 +47,19 @@ def test_setattr_getattr(self):
         d.a = 1
         self.assertEqual(d.a, 1)
         self.assertRaises(AttributeError, lambda: d.b)
-    
+
     def test_setattr_raises_on_nonexisting_attr(self):
         d = self.dtype()
 
         def _f():
             d.x = 1
         self.assertRaises(AttributeError, _f)
-    
+
     def test_setattr_getattr_special(self):
         d = self.strict_dict_class(["items"])
         d.items = 1
         self.assertEqual(d.items, 1)
-    
+
     def test_get(self):
         d = self.dtype(a=1)
         self.assertEqual(d.get('a'), 1)
@@ -88,7 +97,7 @@ def test_setattr_getattr_nonexisting_attr_succeeds(self):
     def test_init_succeeds_with_nonexisting_attrs(self):
         d = self.dtype(a=1, b=1, c=1, x=2)
         self.assertEqual((d.a, d.b, d.c, d.x), (1, 1, 1, 2))
-   
+
     def test_iter_with_nonexisting_attrs(self):
         d = self.dtype(a=1, b=1, c=1, x=2)
         self.assertEqual(list(d), ['a', 'b', 'c', 'x'])