Created
October 17, 2011 14:26
-
-
Save robyoung/1292709 to your computer and use it in GitHub Desktop.
pymongo Cursor and Limit Indexing Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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