42
42
PickleSignalsTest ,
43
43
PickleTest ,
44
44
)
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
+ )
46
51
47
52
TEST_IMAGE_PATH = os .path .join (os .path .dirname (__file__ ), "../fields/mongoengine.png" )
48
53
@@ -1871,6 +1876,53 @@ class User(self.Person):
1871
1876
person = self .Person .objects .get ()
1872
1877
assert not person .comments_dict ["first_post" ].published
1873
1878
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
+
1874
1926
def test_delete (self ):
1875
1927
"""Ensure that document may be deleted using the delete method."""
1876
1928
person = self .Person (name = "Test User" , age = 30 )
@@ -1879,6 +1931,53 @@ def test_delete(self):
1879
1931
person .delete ()
1880
1932
assert self .Person .objects .count () == 0
1881
1933
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
+
1882
1981
def test_save_custom_id (self ):
1883
1982
"""Ensure that a document may be saved with a custom _id."""
1884
1983
0 commit comments