1. Andy Mikhailenko
  2. monk

Commits

Andy Mikhailenko  committed 5e8bbd2

Fix tests (failing after latest refactoring)

  • Participants
  • Parent commits 7456147
  • Branches default

Comments (0)

Files changed (4)

File monk/modeling.py

View file
     def __setattr__(self, attr, value):
         if not attr.startswith('_') and attr in self:
             self[attr] = value
+        else:
+            # Ambigous intent: cannot tell whether user wants to create
+            # a dictionary key or actually set an object attribute.
+            # Assuming the last option.
+            super(DotExpandedDictMixin, self).__setattr__(attr, value)
 
     def __setitem__(self, key, value):
         if isinstance(value, dict) and \
 
     def validate(self):
         validation.validate_structure(self.structure, self)
-
-
-def Document(*args, **kwargs):
-    import warnings
-    warnings.warn('monk.modeling.Document is deprecated, use '
-                  'monk.mongo.Document instead', DeprecationWarning)
-    import monk.mongo
-    return monk.mongo.Document(*args, **kwargs)

File monk/mongo.py

View file
 
 """
 from bson import DBRef
+from monk import modeling
 
 
 class MongoResultSet(object):
 
 
 class Document(
-        TypedDictReprMixin,
-        DotExpandedDictMixin,
-        StructuredDictMixin,
+        modeling.TypedDictReprMixin,
+        modeling.DotExpandedDictMixin,
+        modeling.StructuredDictMixin,
         MongoBoundDictMixin,
         dict
     ):

File unittests/test_modeling.py

View file
 Modeling tests
 ~~~~~~~~~~~~~~
 """
-from monk import modeling
+import mock
+import pytest
+
+from monk import manipulation, modeling
 
 
 def test_make_dot_expanded():
 
     data = modeling.DotExpandedDict()
-    assert id(data) == id(make_dot_expanded(data))
+    assert id(data) == id(modeling.make_dot_expanded(data))
 
     data = {'foo': {'bar': 123}}
-    result = make_dot_expanded(data)
-    assert isinstance(result, DotExpandedDict)
-    assert isinstance(result['foo'], DotExpandedDict)
+    result = modeling.make_dot_expanded(data)
+    assert isinstance(result, modeling.DotExpandedDict)
+    assert isinstance(result['foo'], modeling.DotExpandedDict)
     assert result.foo.bar == 123
 
-    data = {'foo': [{'bar': 123, 'baz': {'quux': 456}}]
-    result = make_dot_expanded(data)
-    assert isinstance(result, DotExpandedDict)
-    assert isinstance(result['foo'][0], DotExpandedDict)
-    assert isinstance(result['foo'][1], DotExpandedDict)
+    data = {'foo': [{'bar': 123, 'baz': {'quux': 456}}, {'zzz': 'yyy'}]}
+    result = modeling.make_dot_expanded(data)
+    assert isinstance(result, modeling.DotExpandedDict)
+    assert isinstance(result['foo'][0], modeling.DotExpandedDict)
+    assert isinstance(result['foo'][1], modeling.DotExpandedDict)
     assert result.foo[0].bar == 123
     assert result.foo[0].baz.quux == 456
 
-    assert make_dot_expanded(123) == 123
-    assert make_dot_expanded('foo') == 'foo'
+    assert modeling.make_dot_expanded(123) == 123
+    assert modeling.make_dot_expanded('foo') == 'foo'
 
 
 def test_dot_expanded_dict():
     obj = modeling.DotExpandedDict(foo=dict(bar=123))
     assert obj.foo.bar == 123
 
+    obj.foo.bar = u'Whoa'
+    assert obj.foo.bar == u'Whoa'
+    assert obj.foo.bar == obj['foo']['bar']
+
+    obj = modeling.DotExpandedDict(comments=[{'text': 'hi'}])
+    assert obj.comments[0].text == obj['comments'][0]['text']
 
 def test_dot_expanded_dict_mixin():
     class Entry(modeling.DotExpandedDictMixin, dict):
         pass
 
-    obj = Entry(foo=dict(bar=123), baz='quux', comments=[{'text': 'hi'}])
-    assert obj.foo.bar == 123
+    entry = Entry(foo=123)
 
     # getattr -> getitem
-    assert entry['baz'] == 'quux'
-    assert entry['baz'] == entry.title
+    assert entry['foo'] == 123
+    assert entry['foo'] == entry.foo
     with pytest.raises(AttributeError):
         entry.nonexistent_key
-    assert entry['foo']['bar'] == entry.foo.bar
 
     # setattr -> setitem
+    entry.foo = 'bar'
+    assert entry.foo == 'bar'
+    assert entry.foo == entry['foo']
+
+    # setattr -> setitem  won't work if key did not exist
+    #   (reason: ambiguity of intent)
     entry.title = u'zzz'
-    assert entry.title == u'zzz'
-    assert entry.title == entry['title']
-
-    entry.foo.bar = u'Whoa'
-    assert entry.foo.bar == u'Whoa'
-    assert entry.foo.bar == entry['foo']['bar']
-
-    assert entry.comments[0].text == entry['comments'][0]['text']
-
+    assert 'title' not in entry
+    assert hasattr(entry, 'title')
 
 def test_typed_dict_repr_mixin():
     class Entry(modeling.TypedDictReprMixin, dict):
         structure = {'foo': int, 'bar': {'quux': 123}}
 
     obj = Entry()
-    assert obj._insert_defaults() == manipulation.merged(obj.structure, obj)
+    expected = manipulation.merged(obj.structure, obj)
+    obj._insert_defaults()
+    assert obj == expected
 
     data = {'foo': 5, 'bar': {'quux': 9}}
     obj = Entry(data)

File unittests/test_mongo.py

View file
 import pymongo
 import pytest
 
-from bson import DBRef
-from monk import modeling
+from bson import DBRef, ObjectId
+from monk import mongo
 
 
 class TestDocumentModel:
-    class Entry(modeling.Document):
+    class Entry(mongo.Document):
         structure = {
             'title': unicode,
             'author': {
         assert entry.comments[0].is_spam == False
 
     def test_callable_defaults_builtin_func(self):
-        class Event(modeling.Document):
+        class Event(mongo.Document):
             structure = {
                 'time': datetime.datetime.utcnow,
             }
             event.validate()
 
     def test_callable_defaults_custom_func(self):
-        class Event(modeling.Document):
+        class Event(mongo.Document):
             structure = {
                 'text': lambda: u'hello'
             }
 
     def test_callable_defaults_custom_func_nested(self):
         # Issue #1  https://bitbucket.org/neithere/monk/issue/1/callable-defaults-in-nested-structures
-        class Event(modeling.Document):
+        class Event(mongo.Document):
             structure = {
                 'content': {
                     'text': lambda: u'hello'
 
     DATABASE = 'test_monk'
 
-    class Entry(modeling.Document):
+    class Entry(mongo.Document):
         collection = 'entries'
         structure = {
             '_id': ObjectId,
         d = dict(title=u'Hello')
         assert a != d
 
-        class E(modeling.Document):
+        class E(mongo.Document):
             structure = self.Entry.structure
         e = E(title=u'Hello')
         assert a != e
 
-        class F(modeling.Document):
+        class F(mongo.Document):
             collection = 'comments'
             structure = self.Entry.structure
         e = F(title=u'Hello')