From 7247b9b68ec546c4f0f43b01455dc1c6545492f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastien=20G=C3=A9rard?= Date: Sun, 24 Feb 2019 22:34:17 +0100 Subject: [PATCH] additional fixes to support Mongo3.4 --- .travis.yml | 2 +- mongoengine/document.py | 4 ++-- mongoengine/mongodb_support.py | 2 +- tests/document/indexes.py | 6 ++++++ tests/utils.py | 10 +++++----- 5 files changed, 15 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index a73391aa..64086357 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ # combinations: # * MongoDB v2.6 is currently the "main" version tested against Python v2.7, # v3.5, v3.6, PyPy, and PyMongo v3.x. -# * MongoDB v3.0, v3.2 are tested against Python v2.7, v3.5 & v3.6 +# * MongoDB v3.0 & v3.2 are tested against Python v2.7, v3.5 & v3.6 # and Pymongo v3.5 & v3.x # * MongoDB v3.4 is tested against v3.6 and Pymongo v3.x # Reminder: Update README.rst if you change MongoDB versions we test. diff --git a/mongoengine/document.py b/mongoengine/document.py index 84c1d699..b671ac6b 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -808,7 +808,7 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): db.drop_collection(col_name) @classmethod - def create_index(cls, keys, background=False, **kwargs): + def _create_index(cls, keys, background=False, **kwargs): """Creates the given indexes if required. :param keys: a single index key or a list of index keys (to @@ -851,7 +851,7 @@ class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): warnings.warn(msg, DeprecationWarning) elif not IS_PYMONGO_3: kwargs.update({'drop_dups': drop_dups}) - return cls.create_index(key_or_list, background=background, **kwargs) + return cls._create_index(key_or_list, background=background, **kwargs) @classmethod def ensure_indexes(cls): diff --git a/mongoengine/mongodb_support.py b/mongoengine/mongodb_support.py index b5f3bdc8..717a3d81 100644 --- a/mongoengine/mongodb_support.py +++ b/mongoengine/mongodb_support.py @@ -1,5 +1,5 @@ """ -Helper functions, constants, and types to aid with MongoDB v3.x support +Helper functions, constants, and types to aid with MongoDB version support """ from mongoengine.connection import get_connection diff --git a/tests/document/indexes.py b/tests/document/indexes.py index a21b45bc..36fbae46 100644 --- a/tests/document/indexes.py +++ b/tests/document/indexes.py @@ -603,18 +603,24 @@ class IndexesTest(unittest.TestCase): @requires_mongodb_gte_34 def test_primary_key_unique_not_working_under_mongo_34(self): + """Relates to #1445""" class Blog(Document): id = StringField(primary_key=True, unique=True) + Blog.drop_collection() + with self.assertRaises(OperationFailure) as ctx_err: Blog(id='garbage').save() self.assertIn("The field 'unique' is not valid for an _id index specification", str(ctx_err.exception)) @requires_mongodb_lte_32 def test_primary_key_unique_working_under_mongo_32(self): + """Relates to #1445""" class Blog(Document): id = StringField(primary_key=True, unique=True) + Blog.drop_collection() + Blog(id='garbage').save() def test_unique_with(self): diff --git a/tests/utils.py b/tests/utils.py index e0380dbc..e94e4a80 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -33,12 +33,12 @@ def get_as_pymongo(doc): return doc.__class__.objects.as_pymongo().get(id=doc.id) -def _decorated_with_ver_requirement(func, mongo_version_req, oper=operator.ge): +def _decorated_with_ver_requirement(func, mongo_version_req, oper): """Return a given function decorated with the version requirement for a particular MongoDB version tuple. :param mongo_version_req: The mongodb version requirement (tuple(int, int)) - :param oper: The operator to apply + :param oper: The operator to apply (e.g: operator.ge) """ def _inner(*args, **kwargs): mongodb_v = get_mongodb_version() @@ -56,7 +56,7 @@ def requires_mongodb_gte_34(func): """Raise a SkipTest exception if we're working with MongoDB version lower than v3.4 """ - return _decorated_with_ver_requirement(func, MONGODB_34) + return _decorated_with_ver_requirement(func, MONGODB_34, oper=operator.ge) def requires_mongodb_lte_32(func): @@ -70,14 +70,14 @@ def requires_mongodb_gte_26(func): """Raise a SkipTest exception if we're working with MongoDB version lower than v2.6. """ - return _decorated_with_ver_requirement(func, MONGODB_26) + return _decorated_with_ver_requirement(func, MONGODB_26, oper=operator.ge) def requires_mongodb_gte_3(func): """Raise a SkipTest exception if we're working with MongoDB version lower than v3.0. """ - return _decorated_with_ver_requirement(func, MONGODB_3) + return _decorated_with_ver_requirement(func, MONGODB_3, oper=operator.ge) def skip_pymongo3(f):