Commits

Andy Mikhailenko committed 52f7fa3

Added tests for basic MongoDB interaction via Document.

Comments (0)

Files changed (2)

 
 from pymongo import dbref
 
+from monk import manipulation
 from monk import validation
 
 
     def __iter__(self):
         return (self._wrap(x) for x in self._cursor)
 
+    def __getitem__(self, index):
+        return self._wrap(self._cursor[index])
+
     def __getattr__(self, attr):
         return getattr(self._cursor, attr)
 
 
     def save(self, db):
         assert self.collection
-        #self.populate_defaults()
-        self.validate()
         outgoing = dict(dict_to_db(self, self.structure))
         db[self.collection].save(outgoing)
 
     #validators = {}
     with_skeleton = True
 
-#    def __init__(self, *args, **kwargs):
-#        super(StructuredDict, self).__init__(*args, **kwargs)
+    def __init__(self, *args, **kwargs):
+        super(StructuredDictMixin, self).__init__(*args, **kwargs)
+        self._insert_defaults()
+
+    def _insert_defaults(self):
+        with_defaults = manipulation.merged(self.structure, self)
+        for key, value in with_defaults.iteritems():
+            self[key] = value
 
     def validate(self):
         validation.validate_structure(self.structure, self)
 
 
 class Document(TypedDictReprMixin, MongoBoundDictMixin,
-               StructuredDictMixin, DotExpandedDict):
+               DotExpandedDict, StructuredDictMixin):
     """ A structured dictionary that is bound to MongoDB and supports dot
     notation for access to items.
     """
+    def save(self, db):
+        self.validate()
+        super(Document, self).save(db)
 
 
 def dict_from_db(data, db):

unittests/test_modeling.py

 
         assert entry.comments[0].text == entry['comments'][0]['text']
 
+    @pytest.mark.xfail
     def test_defaults(self):
         entry = self.Entry(self.data)
         assert entry.comments[0].is_spam == False
 
 
-    '''
-    TEST_DATABASE_NAME = 'test_monk'
+class TestMongo:
 
+    DATABASE = 'test_monk'
 
-    class BaseTestCase(unittest2.TestCase):
-        FIXTURES = {}
+    class Entry(modeling.Document):
+        collection = 'entries'
+        structure = {
+            'title': unicode,
+        }
 
-        def setUp(self):
-            self.db = pymongo.Connection()[TEST_DATABASE_NAME]
-            self.load_fixtures()
+    def setup_method(self, method):
+        self.db = pymongo.Connection()[self.DATABASE]
+        self.collection = self.db[self.Entry.collection]
+        self.collection.drop()
 
-        def load_fixtures(self):
-            for cname, documents in self.FIXTURES.iteritems():
-                collection = self.db[cname]
-                collection.drop()
-                for document in documents:
-                    collection.insert(document)
-    '''
+    def test_query(self):
+        self.collection.insert({'title': u'Hello world!'})
+        entries = self.Entry.find(self.db, {'title': u'Hello world!'})
+        assert entries.count() == 1
+        entry = entries[0]
+        assert entry.title == u'Hello world!'
+
+    def test_insert(self):
+        entry = self.Entry(title=u'Hello')
+        entry.save(self.db)
+        assert self.collection.find().count() == 1
+        assert self.collection.find({'title': u'Hello'}).count() == 1