From 33a1eba9d9b9832b922a07087aa53d61fe8901eb Mon Sep 17 00:00:00 2001 From: Dave Brondsema Date: Wed, 15 Mar 2023 15:59:23 -0400 Subject: [PATCH] MIM: raise an error just like pymongo if find/find_one recieves unexpected kwargs --- ming/mim.py | 7 +++++++ ming/tests/test_mim.py | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/ming/mim.py b/ming/mim.py index b5a4bd1..1a4ec16 100644 --- a/ming/mim.py +++ b/ming/mim.py @@ -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 @@ -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') diff --git a/ming/tests/test_mim.py b/ming/tests/test_mim.py index 6eb21d1..418d4ab 100644 --- a/ming/tests/test_mim.py +++ b/ming/tests/test_mim.py @@ -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)