Commits

Andy Mikhailenko  committed 9dfedd1

Changed the way indexes are declared; added tests.

  • Participants
  • Parent commits bb31f83
  • Tags 0.2.1

Comments (0)

Files changed (4)

 # Maintainer: Andrey Mikhaylenko <neithere at gmail dot com>
 pkgname=python2-monk
-pkgver=0.2.0
+pkgver=0.2.1
 pkgrel=1
 pkgdesc="A lightweight schema/query framework for MongoDB"
 arch=(any)
 options=(!emptydirs)
 install=
 source=(http://pypi.python.org/packages/source/m/monk/monk-${pkgver}.tar.gz)
-md5sums=('90050e75651999375c88e2ca89a7e528')
+md5sums=('0af9650f8e9d88999a7c29eb430c2ec2')
 
 build() {
    cd "${srcdir}/monk-${pkgver}"

File monk/__init__.py

 .. _pymongo: http://api.mongodb.org/python/current/
 
 """
-__version__ = '0.2.0'
+__version__ = '0.2.1'
 # remember to also update:
 #
 # * PKGBUILD

File monk/modeling.py

 """
 Models
 ======
+
+Declaring indexes
+-----------------
+
+Let's declare a model with indexes::
+
+    class Item(Document):
+        structure = dict(text=unicode, slug=unicode)
+        indexes = dict(text=None, slug=dict(unique=True))
+
+Now create a model instance::
+
+    item = Item(text=u'foo', slug=u'bar')
+
+Save it and make sure the indexes are created::
+
+    item.save(db)
+
+The last line is roughly equivalent to::
+
+    collection = db[item.collection]
+    collection.ensure_index('text')
+    collection.ensure_index('slug', unique=True)
+    collection.save(dict(item))  # also validation, transformation, etc.
+
 """
 from functools import partial
 import types
 
 class MongoBoundDictMixin(object):
     collection = None
-    indexes = []
+    indexes = {}
 
     def __hash__(self):
         """ Collection name and id together make the hash; document class
 
     @classmethod
     def _ensure_indexes(cls, db):
-        for definition in cls.indexes:
-            fields = definition['fields']
-            for field in fields:
-                kwargs = dict(definition)
-                kwargs.pop('fields')
-                db[cls.collection].ensure_index(field, **kwargs)
+        for field, kwargs in cls.indexes.iteritems():
+            kwargs = kwargs or {}
+            db[cls.collection].ensure_index(field, **kwargs)
 
     @classmethod
     def wrap_incoming(cls, data, db):
 
     @classmethod
     def get_one(cls, db, *args, **kwargs):
-        cls._ensure_indexes(db)
         data = db[cls.collection].find_one(*args, **kwargs)
         if data:
             return cls.wrap_incoming(data, db)
     def save(self, db):
         assert self.collection
 
+        self._ensure_indexes(db)
+
         # XXX self.structure belongs to StructuredDictMixin !!
         outgoing = dict(dict_to_db(self, self.structure))
 

File unittests/test_modeling.py

         e = F(title=u'Hello')
         e.save(self.db)
         assert a != e
+
+    def test_index_id(self):
+        "Index for _id is created on first save to a collection"
+        assert self.collection.index_information() == {}
+        self.Entry(title=u'entry').save(self.db)
+        assert '_id_' in self.collection.index_information()
+
+    def test_index_custom(self):
+        "Index for _id is created on first save to a collection"
+        assert self.collection.index_information() == {}
+        class IndexedEntry(self.Entry):
+            indexes = {'title': None}
+        IndexedEntry(title=u'Hello').save(self.db)
+        assert 'title_1' in self.collection.index_information()