Skip to content

Instantly share code, notes, and snippets.

@robyoung
Created October 17, 2011 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robyoung/1292709 to your computer and use it in GitHub Desktop.
Save robyoung/1292709 to your computer and use it in GitHub Desktop.
pymongo Cursor and Limit Indexing Issue
import pymongo
import unittest
class TestIndexFailure(unittest.TestCase):
def setUp(self):
self.coll = pymongo.Connection()['test']['test']
self.coll.drop()
self.coll.save({"key":"Value1"})
self.coll.save({"key":"Value2"})
self.idval = self.coll.find_one({"key":"Value1"})["_id"]
def tearDown(self):
self.coll.drop()
del self.coll
def testQueryFind(self):
q = self.coll.find(spec={"key":"Value1"})
self.assertEqual("Value1", q[0]['key'])
self.assertRaises(IndexError, lambda: q[1])
def testQueryIdFieldFind(self):
"""A cursor based on an _id query will return the matched document whatever the index."""
q = self.coll.find(spec={"_id":self.idval})
self.assertEqual("Value1", q[0]['key'])
self.assertRaises(IndexError, lambda: q[1])
def testQueryLimitFind(self):
"""A cursor with a limit will continue to return values for indexes after the limit."""
q = self.coll.find(limit=1)
self.assertRegexpMatches(q[0]['key'], r'^Value\d')
self.assertRaises(IndexError, lambda: q[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment