Skip to content

Commit 1e06d52

Browse files
committed
Skip tests on hint for update/delete for MongoDB < 4.4
1 parent b809090 commit 1e06d52

File tree

3 files changed

+51
-53
lines changed

3 files changed

+51
-53
lines changed

docs/changelog.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Development
1515
- Further to the deprecation warning, remove ability to use an unpacked list to `Queryset.aggregate(*pipeline)`, a plain list must be provided instead `Queryset.aggregate(pipeline)`, as it's closer to pymongo interface
1616
- Further to the deprecation warning, remove `full_response` from `QuerySet.modify` as it wasn't supported with Pymongo 3+
1717
- Fixed stacklevel of many warnings (to point places emitting the warning more accurately)
18-
- Add support for collation/hint/comment to delete/update/aggregate #2842
18+
- Add support for collation/hint/comment to delete/update and aggregate #2842
1919

2020
Changes in 0.29.0
2121
=================

tests/document/test_instance.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
MongoDBTestCase,
4747
db_ops_tracker,
4848
get_as_pymongo,
49+
requires_mongodb_gte_44,
4950
)
5051

5152
TEST_IMAGE_PATH = os.path.join(os.path.dirname(__file__), "../fields/mongoengine.png")
@@ -1875,6 +1876,53 @@ class User(self.Person):
18751876
person = self.Person.objects.get()
18761877
assert not person.comments_dict["first_post"].published
18771878

1879+
@requires_mongodb_gte_44
1880+
def test_update_propagates_hint_collation_and_comment(self):
1881+
"""Make sure adding a hint/comment/collation to the query gets added to the query"""
1882+
mongo_ver = get_mongodb_version()
1883+
1884+
base = {"locale": "en", "strength": 2}
1885+
index_name = "name_1"
1886+
1887+
class AggPerson(Document):
1888+
name = StringField()
1889+
meta = {
1890+
"indexes": [{"fields": ["name"], "name": index_name, "collation": base}]
1891+
}
1892+
1893+
AggPerson.drop_collection()
1894+
_ = AggPerson.objects.first()
1895+
1896+
comment = "test_comment"
1897+
1898+
if PYMONGO_VERSION >= (4, 1):
1899+
with db_ops_tracker() as q:
1900+
_ = AggPerson.objects.comment(comment).update_one(name="something")
1901+
query_op = q.db.system.profile.find(
1902+
{"ns": "mongoenginetest.agg_person"}
1903+
)[0]
1904+
CMD_QUERY_KEY = "command" if mongo_ver >= MONGODB_36 else "query"
1905+
assert "hint" not in query_op[CMD_QUERY_KEY]
1906+
assert query_op[CMD_QUERY_KEY]["comment"] == comment
1907+
assert "collation" not in query_op[CMD_QUERY_KEY]
1908+
1909+
with db_ops_tracker() as q:
1910+
_ = AggPerson.objects.hint(index_name).update_one(name="something")
1911+
query_op = q.db.system.profile.find({"ns": "mongoenginetest.agg_person"})[0]
1912+
CMD_QUERY_KEY = "command" if mongo_ver >= MONGODB_36 else "query"
1913+
1914+
assert query_op[CMD_QUERY_KEY]["hint"] == {"$hint": index_name}
1915+
assert "comment" not in query_op[CMD_QUERY_KEY]
1916+
assert "collation" not in query_op[CMD_QUERY_KEY]
1917+
1918+
with db_ops_tracker() as q:
1919+
_ = AggPerson.objects.collation(base).update_one(name="something")
1920+
query_op = q.db.system.profile.find({"ns": "mongoenginetest.agg_person"})[0]
1921+
CMD_QUERY_KEY = "command" if mongo_ver >= MONGODB_36 else "query"
1922+
assert "hint" not in query_op[CMD_QUERY_KEY]
1923+
assert "comment" not in query_op[CMD_QUERY_KEY]
1924+
assert query_op[CMD_QUERY_KEY]["collation"] == base
1925+
18781926
def test_delete(self):
18791927
"""Ensure that document may be deleted using the delete method."""
18801928
person = self.Person(name="Test User", age=30)
@@ -1883,6 +1931,7 @@ def test_delete(self):
18831931
person.delete()
18841932
assert self.Person.objects.count() == 0
18851933

1934+
@requires_mongodb_gte_44
18861935
def test_delete_propagates_hint_collation_and_comment(self):
18871936
"""Make sure adding a hint/comment/collation to the query gets added to the query"""
18881937
mongo_ver = get_mongodb_version()

tests/fields/test_binary_field.py

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44
from bson import Binary
55

66
from mongoengine import *
7-
from mongoengine.mongodb_support import (
8-
MONGODB_36,
9-
get_mongodb_version,
10-
)
11-
from mongoengine.pymongo_support import PYMONGO_VERSION
12-
from tests.utils import MongoDBTestCase, db_ops_tracker
7+
from tests.utils import MongoDBTestCase
138

149
BIN_VALUE = "\xa9\xf3\x8d(\xd7\x03\x84\xb4k[\x0f\xe3\xa2\x19\x85p[J\xa3\xd2>\xde\xe6\x87\xb1\x7f\xc6\xe6\xd9r\x18\xf5".encode(
1510
"latin-1"
@@ -151,49 +146,3 @@ class MyDocument(Document):
151146
assert n_updated == 1
152147
fetched = MyDocument.objects.with_id(doc.id)
153148
assert fetched.bin_field == BIN_VALUE
154-
155-
def test_update_propagates_hint_collation_and_comment(self):
156-
"""Make sure adding a hint/comment/collation to the query gets added to the query"""
157-
mongo_ver = get_mongodb_version()
158-
159-
base = {"locale": "en", "strength": 2}
160-
index_name = "name_1"
161-
162-
class AggPerson(Document):
163-
name = StringField()
164-
meta = {
165-
"indexes": [{"fields": ["name"], "name": index_name, "collation": base}]
166-
}
167-
168-
AggPerson.drop_collection()
169-
_ = AggPerson.objects.first()
170-
171-
comment = "test_comment"
172-
173-
if PYMONGO_VERSION >= (4, 1):
174-
with db_ops_tracker() as q:
175-
_ = AggPerson.objects.comment(comment).update_one(name="something")
176-
query_op = q.db.system.profile.find(
177-
{"ns": "mongoenginetest.agg_person"}
178-
)[0]
179-
CMD_QUERY_KEY = "command" if mongo_ver >= MONGODB_36 else "query"
180-
assert "hint" not in query_op[CMD_QUERY_KEY]
181-
assert query_op[CMD_QUERY_KEY]["comment"] == comment
182-
assert "collation" not in query_op[CMD_QUERY_KEY]
183-
184-
with db_ops_tracker() as q:
185-
_ = AggPerson.objects.hint(index_name).update_one(name="something")
186-
query_op = q.db.system.profile.find({"ns": "mongoenginetest.agg_person"})[0]
187-
CMD_QUERY_KEY = "command" if mongo_ver >= MONGODB_36 else "query"
188-
189-
assert query_op[CMD_QUERY_KEY]["hint"] == {"$hint": index_name}
190-
assert "comment" not in query_op[CMD_QUERY_KEY]
191-
assert "collation" not in query_op[CMD_QUERY_KEY]
192-
193-
with db_ops_tracker() as q:
194-
_ = AggPerson.objects.collation(base).update_one(name="something")
195-
query_op = q.db.system.profile.find({"ns": "mongoenginetest.agg_person"})[0]
196-
CMD_QUERY_KEY = "command" if mongo_ver >= MONGODB_36 else "query"
197-
assert "hint" not in query_op[CMD_QUERY_KEY]
198-
assert "comment" not in query_op[CMD_QUERY_KEY]
199-
assert query_op[CMD_QUERY_KEY]["collation"] == base

0 commit comments

Comments
 (0)