Permalink
Browse files

added first helper

  • Loading branch information...
1 parent 787091b commit 3467b134a9b2bcb9b0127904a0384e77556c41e5 Waylon Calabrese committed Nov 10, 2011
Showing with 22 additions and 0 deletions.
  1. +6 −0 mogo/model.py
  2. +16 −0 tests/test_usage.py
View
@@ -339,6 +339,12 @@ def search(cls, **kwargs):
return cls.find(query)
@classmethod
+ def first(cls, **kwargs):
+ """ Helper for returning Blah.search(foo=bar).first(). """
+ result = cls.search(**kwargs)
+ return result.first()
+
+ @classmethod
def grab(cls, object_id):
""" A shortcut to retrieve one object by its id. """
if type(object_id) != cls._id_type:
View
@@ -380,6 +380,22 @@ def test_inheritance(self):
self.assertEqual(Person.find()[0].name, "Testing")
self.assertEqual(Person.find()[1]['another_field'], "foobar")
+ def test_first(self):
+ conn = connect(DBNAME)
+ foo = Foo.new()
+ foo.bar = u"search"
+ foo.save(safe=True)
+ for x in xrange(3):
+ foo_x = Foo.new()
+ foo_x.bar = u"search"
+ foo_x.save(safe=True)
+ result = foo.first(bar=u"search")
+ try:
+ self.assertTrue(result == foo)
+ finally:
+ foo.delete()
+ conn.disconnect()
+
def tearDown(self):
conn = pymongo.Connection()
if DELETE:

0 comments on commit 3467b13

Please sign in to comment.