1212# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313# See the License for the specific language governing permissions and
1414# limitations under the License.
15+ from contextlib import asynccontextmanager , contextmanager
1516
1617from pymongo .errors import OperationFailure
1718from twisted .internet import defer
@@ -39,19 +40,43 @@ def tearDown(self):
3940 yield self .db .system .profile .drop ()
4041 yield self .conn .disconnect ()
4142
42- @defer .inlineCallbacks
43- def test_Hint (self ):
43+ @asynccontextmanager
44+ async def _assert_single_command_with_option (self , optionname , optionvalue ):
45+ # Checking that `optionname` appears in profiler log with specified value
46+
47+ await self .db .command ("profile" , 2 )
48+ yield
49+ await self .db .command ("profile" , 0 )
50+
51+ profile_filter = {"command." + optionname : optionvalue }
52+ cnt = await self .db .system .profile .count (profile_filter )
53+ await self .db .system .profile .drop ()
54+ self .assertEqual (cnt , 1 )
55+
56+ async def test_Hint (self ):
4457 # find() should fail with 'bad hint' if hint specifier works correctly
4558 self .assertFailure (
4659 self .coll .find ({}, sort = qf .hint ([("x" , 1 )])), OperationFailure
4760 )
61+ self .assertFailure (self .coll .find ().hint ({"x" : 1 }), OperationFailure )
4862
4963 # create index and test it is honoured
50- yield self .coll .create_index (qf .sort (qf .ASCENDING ("x" )), name = "test_index" )
51- found_1 = yield self .coll .find ({}, sort = qf .hint ([("x" , 1 )]))
52- found_2 = yield self .coll .find ({}, sort = qf .hint (qf .ASCENDING ("x" )))
53- found_3 = yield self .coll .find ({}, sort = qf .hint ("test_index" ))
54- self .assertTrue (found_1 == found_2 == found_3 )
64+ await self .coll .create_index (qf .sort (qf .ASCENDING ("x" )), name = "test_index" )
65+ forms = [
66+ [("x" , 1 )],
67+ {"x" : 1 },
68+ qf .ASCENDING ("x" ),
69+ ]
70+ for form in forms :
71+ async with self ._assert_single_command_with_option ("hint" , {"x" : 1 }):
72+ await self .coll .find ({}, sort = qf .hint (form ))
73+ async with self ._assert_single_command_with_option ("hint" , {"x" : 1 }):
74+ await self .coll .find ().hint (form )
75+
76+ async with self ._assert_single_command_with_option ("hint" , "test_index" ):
77+ await self .coll .find ({}, sort = qf .hint ("test_index" ))
78+ async with self ._assert_single_command_with_option ("hint" , "test_index" ):
79+ await self .coll .find ().hint ("test_index" )
5580
5681 # find() should fail with 'bad hint' if hint specifier works correctly
5782 self .assertFailure (
@@ -67,13 +92,18 @@ def test_SortAscendingMultipleFields(self):
6792 qf .sort (qf .ASCENDING (["x" , "y" ])),
6893 qf .sort (qf .ASCENDING ("x" ) + qf .ASCENDING ("y" )),
6994 )
95+ self .assertEqual (
96+ qf .sort (qf .ASCENDING (["x" , "y" ])),
97+ qf .sort ({"x" : 1 , "y" : 1 }),
98+ )
7099
71100 def test_SortOneLevelList (self ):
72101 self .assertEqual (qf .sort ([("x" , 1 )]), qf .sort (("x" , 1 )))
73102
74103 def test_SortInvalidKey (self ):
75104 self .assertRaises (TypeError , qf .sort , [(1 , 2 )])
76105 self .assertRaises (TypeError , qf .sort , [("x" , 3 )])
106+ self .assertRaises (TypeError , qf .sort , {"x" : 3 })
77107
78108 def test_SortGeoIndexes (self ):
79109 self .assertEqual (qf .sort (qf .GEO2D ("x" )), qf .sort ([("x" , "2d" )]))
@@ -83,45 +113,33 @@ def test_SortGeoIndexes(self):
83113 def test_TextIndex (self ):
84114 self .assertEqual (qf .sort (qf .TEXT ("title" )), qf .sort ([("title" , "text" )]))
85115
86- def __3_2_or_higher (self ):
87- return self .db .command ("buildInfo" ).addCallback (
88- lambda info : info ["versionArray" ] >= [3 , 2 ]
89- )
90-
91- def __3_6_or_higher (self ):
92- return self .db .command ("buildInfo" ).addCallback (
93- lambda info : info ["versionArray" ] >= [3 , 6 ]
94- )
95-
96- @defer .inlineCallbacks
97- def __test_simple_filter (self , filter , optionname , optionvalue ):
98- # Checking that `optionname` appears in profiler log with specified value
99-
100- yield self .db .command ("profile" , 2 )
101- yield self .coll .find ({}, sort = filter )
102- yield self .db .command ("profile" , 0 )
103-
104- if (yield self .__3_6_or_higher ()):
105- profile_filter = {"command." + optionname : optionvalue }
106- elif (yield self .__3_2_or_higher ()):
107- # query options format in system.profile have changed in MongoDB 3.2
108- profile_filter = {"query." + optionname : optionvalue }
109- else :
110- profile_filter = {"query.$" + optionname : optionvalue }
111-
112- cnt = yield self .db .system .profile .count (profile_filter )
113- self .assertEqual (cnt , 1 )
114-
115- @defer .inlineCallbacks
116- def test_Comment (self ):
116+ async def test_SortProfile (self ):
117+ forms = [
118+ qf .DESCENDING ("x" ),
119+ {"x" : - 1 },
120+ [("x" , - 1 )],
121+ ("x" , - 1 ),
122+ ]
123+ for form in forms :
124+ async with self ._assert_single_command_with_option ("sort.x" , - 1 ):
125+ await self .coll .find ({}, sort = qf .sort (form ))
126+ async with self ._assert_single_command_with_option ("sort.x" , - 1 ):
127+ await self .coll .find ().sort (form )
128+
129+ async def test_Comment (self ):
117130 comment = "hello world"
118131
119- yield self .__test_simple_filter (qf .comment (comment ), "comment" , comment )
132+ async with self ._assert_single_command_with_option ("comment" , comment ):
133+ await self .coll .find ({}, sort = qf .comment (comment ))
134+ async with self ._assert_single_command_with_option ("comment" , comment ):
135+ await self .coll .find ().comment (comment )
120136
121137 @defer .inlineCallbacks
122138 def test_Explain (self ):
123139 result = yield self .coll .find ({}, sort = qf .explain ())
124140 self .assertTrue ("executionStats" in result [0 ] or "nscanned" in result [0 ])
141+ result = yield self .coll .find ().explain ()
142+ self .assertTrue ("executionStats" in result [0 ] or "nscanned" in result [0 ])
125143
126144 @defer .inlineCallbacks
127145 def test_FilterMerge (self ):
@@ -136,12 +154,7 @@ def test_FilterMerge(self):
136154 yield self .coll .find ({}, sort = qf .sort (qf .ASCENDING ("x" )) + qf .comment (comment ))
137155 yield self .db .command ("profile" , 0 )
138156
139- if (yield self .__3_6_or_higher ()):
140- profile_filter = {"command.sort.x" : 1 , "command.comment" : comment }
141- elif (yield self .__3_2_or_higher ()):
142- profile_filter = {"query.sort.x" : 1 , "query.comment" : comment }
143- else :
144- profile_filter = {"query.$orderby.x" : 1 , "query.$comment" : comment }
157+ profile_filter = {"command.sort.x" : 1 , "command.comment" : comment }
145158 cnt = yield self .db .system .profile .count (profile_filter )
146159 self .assertEqual (cnt , 1 )
147160
0 commit comments