Skip to content

Commit

Permalink
MIM: raise an error just like pymongo if find/find_one recieves unexp…
Browse files Browse the repository at this point in the history
…ected kwargs
  • Loading branch information
brondsem committed Mar 16, 2023
1 parent 30841b0 commit 33a1eba
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ming/mim.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,18 @@
import bson
from bson.raw_bson import RawBSONDocument
from pymongo import database, collection, ASCENDING, MongoClient, UpdateOne
from pymongo.cursor import Cursor as PymongoCursor
from pymongo.errors import InvalidOperation, OperationFailure, DuplicateKeyError
from pymongo.results import DeleteResult, UpdateResult, InsertManyResult, InsertOneResult

log = logging.getLogger(__name__)


class PymongoCursorNoCleanup(PymongoCursor):
def __del__(self):
pass


class Connection:
_singleton = None

Expand Down Expand Up @@ -366,6 +372,7 @@ def _gen():
def find(self, filter=None, projection=None, skip=0, limit=0, **kwargs):
if filter is None:
filter = {}
PymongoCursorNoCleanup(collection=self, **kwargs) # use this to raise any errors on invalid kwargs
cur = Cursor(collection=self, projection=projection, limit=limit, skip=skip,
_iterator_gen=lambda: self._find(filter, **kwargs))
sort = kwargs.get('sort')
Expand Down
6 changes: 6 additions & 0 deletions ming/tests/test_mim.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ def test_find_with_projection_of_text_score(self):
assert o['c'] == [1, 2, 3]
assert o['score'] == 1.0 # MIM currently always reports 1 as the score.

def test_find_with_invalid_kwargs(self):
self.assertRaises(TypeError, self.bind.db.coll.find, foo=123)
self.assertRaises(TypeError, self.bind.db.coll.find, {'a': 2}, foo=123)
self.assertRaises(TypeError, self.bind.db.coll.find_one, foo=123)
self.bind.db.coll.find(allow_disk_use=True) # kwargs that pymongo knows are ok

def test_rewind(self):
collection = self.bind.db.coll
collection.insert({'a':'b'}, safe=True)
Expand Down

0 comments on commit 33a1eba

Please sign in to comment.