4242 PickleSignalsTest ,
4343 PickleTest ,
4444)
45- from tests .utils import MongoDBTestCase , get_as_pymongo
45+ from tests .utils import (
46+ MongoDBTestCase ,
47+ db_ops_tracker ,
48+ get_as_pymongo ,
49+ requires_mongodb_gte_44 ,
50+ )
4651
4752TEST_IMAGE_PATH = os .path .join (os .path .dirname (__file__ ), "../fields/mongoengine.png" )
4853
@@ -1871,6 +1876,53 @@ class User(self.Person):
18711876 person = self .Person .objects .get ()
18721877 assert not person .comments_dict ["first_post" ].published
18731878
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+
18741926 def test_delete (self ):
18751927 """Ensure that document may be deleted using the delete method."""
18761928 person = self .Person (name = "Test User" , age = 30 )
@@ -1879,6 +1931,53 @@ def test_delete(self):
18791931 person .delete ()
18801932 assert self .Person .objects .count () == 0
18811933
1934+ @requires_mongodb_gte_44
1935+ def test_delete_propagates_hint_collation_and_comment (self ):
1936+ """Make sure adding a hint/comment/collation to the query gets added to the query"""
1937+ mongo_ver = get_mongodb_version ()
1938+
1939+ base = {"locale" : "en" , "strength" : 2 }
1940+ index_name = "name_1"
1941+
1942+ class AggPerson (Document ):
1943+ name = StringField ()
1944+ meta = {
1945+ "indexes" : [{"fields" : ["name" ], "name" : index_name , "collation" : base }]
1946+ }
1947+
1948+ AggPerson .drop_collection ()
1949+ _ = AggPerson .objects .first ()
1950+
1951+ comment = "test_comment"
1952+
1953+ if PYMONGO_VERSION >= (4 , 1 ):
1954+ with db_ops_tracker () as q :
1955+ _ = AggPerson .objects ().comment (comment ).delete ()
1956+ query_op = q .db .system .profile .find (
1957+ {"ns" : "mongoenginetest.agg_person" }
1958+ )[0 ]
1959+ CMD_QUERY_KEY = "command" if mongo_ver >= MONGODB_36 else "query"
1960+ assert "hint" not in query_op [CMD_QUERY_KEY ]
1961+ assert query_op [CMD_QUERY_KEY ]["comment" ] == comment
1962+ assert "collation" not in query_op [CMD_QUERY_KEY ]
1963+
1964+ with db_ops_tracker () as q :
1965+ _ = AggPerson .objects .hint (index_name ).delete ()
1966+ query_op = q .db .system .profile .find ({"ns" : "mongoenginetest.agg_person" })[0 ]
1967+ CMD_QUERY_KEY = "command" if mongo_ver >= MONGODB_36 else "query"
1968+
1969+ assert query_op [CMD_QUERY_KEY ]["hint" ] == {"$hint" : index_name }
1970+ assert "comment" not in query_op [CMD_QUERY_KEY ]
1971+ assert "collation" not in query_op [CMD_QUERY_KEY ]
1972+
1973+ with db_ops_tracker () as q :
1974+ _ = AggPerson .objects .collation (base ).delete ()
1975+ query_op = q .db .system .profile .find ({"ns" : "mongoenginetest.agg_person" })[0 ]
1976+ CMD_QUERY_KEY = "command" if mongo_ver >= MONGODB_36 else "query"
1977+ assert "hint" not in query_op [CMD_QUERY_KEY ]
1978+ assert "comment" not in query_op [CMD_QUERY_KEY ]
1979+ assert query_op [CMD_QUERY_KEY ]["collation" ] == base
1980+
18821981 def test_save_custom_id (self ):
18831982 """Ensure that a document may be saved with a custom _id."""
18841983
0 commit comments