2626from bson .json_util import object_hook
2727from pymongo import monitoring
2828from pymongo .errors import OperationFailure
29- from pymongo .read_preferences import make_read_preference
29+ from pymongo .read_preferences import (make_read_preference ,
30+ read_pref_mode_from_name )
3031from pymongo .write_concern import WriteConcern
3132from test import unittest , client_context
3233from test .utils import single_client , wait_until , EventListener
@@ -49,7 +50,6 @@ class TestAllScenarios(unittest.TestCase):
4950 @client_context .require_connection
5051 def setUpClass (cls ):
5152 cls .listener = EventListener ()
52- cls .listener .add_command_filter ('killCursors' )
5353 cls .saved_listeners = monitoring ._LISTENERS
5454 monitoring ._LISTENERS = monitoring ._Listeners ([])
5555 cls .client = single_client (event_listeners = [cls .listener ])
@@ -68,27 +68,42 @@ def run_scenario(self):
6868 dbname = scenario_def ['database_name' ]
6969 collname = scenario_def ['collection_name' ]
7070
71- # Clear the kill cursors queue.
72- self .client ._kill_cursors_executor .wake ()
73-
7471 for test in scenario_def ['tests' ]:
72+ ver = client_context .version [:2 ]
73+ if "ignore_if_server_version_greater_than" in test :
74+ version = test ["ignore_if_server_version_greater_than" ]
75+ if ver > tuple (map (int , version .split ("." ))):
76+ continue
77+ if "ignore_if_server_version_less_than" in test :
78+ version = test ["ignore_if_server_version_less_than" ]
79+ if ver < tuple (map (int , version .split ("." ))):
80+ continue
81+ if "ignore_if_topology_type" in test :
82+ types = set (test ["ignore_if_topology_type" ])
83+ if "sharded" in types and client_context .is_mongos :
84+ continue
85+
7586 coll = self .client [dbname ][collname ]
7687 coll .drop ()
7788 coll .insert_many (scenario_def ['data' ])
7889 self .listener .results .clear ()
7990 name = camel_to_snake (test ['operation' ]['name' ])
80- args = test ['operation' ]['arguments' ]
8191 # Don't send $readPreference to mongos before 2.4.
8292 if (client_context .version .at_least (2 , 4 , 0 )
83- and 'readPreference' in args ):
84- pref = make_read_preference (
85- args ['readPreference' ]['mode' ], None )
86- coll = coll .with_options (read_preference = pref )
87- if 'writeConcern' in args :
93+ and 'read_preference' in test ['operation' ]):
94+ mode = read_pref_mode_from_name (
95+ test ['operation' ]['read_preference' ]['mode' ])
96+ coll = coll .with_options (
97+ read_preference = make_read_preference (mode , None ))
98+
99+ test_args = test ['operation' ]['arguments' ]
100+ if 'writeConcern' in test_args :
101+ concern = test_args .pop ('writeConcern' )
88102 coll = coll .with_options (
89- write_concern = WriteConcern (** args ['writeConcern' ]))
90- for arg in args :
91- args [camel_to_snake (arg )] = args .pop (arg )
103+ write_concern = WriteConcern (** concern ))
104+ args = {}
105+ for arg in test_args :
106+ args [camel_to_snake (arg )] = test_args [arg ]
92107
93108 if name == 'bulk_write' :
94109 bulk_args = []
@@ -102,25 +117,20 @@ def run_scenario(self):
102117 except OperationFailure :
103118 pass
104119 elif name == 'find' :
105- if 'limit' in args :
106- # XXX: Skip killCursors test when using the find command.
107- if client_context .version .at_least (3 , 1 , 1 ):
108- continue
109- self .listener .remove_command_filter ('killCursors' )
110120 if 'sort' in args :
111121 args ['sort' ] = list (args ['sort' ].items ())
112122 try :
113123 # Iterate the cursor.
114124 tuple (coll .find (** args ))
115125 except OperationFailure :
116126 pass
117- # Wait for the killCursors thread to run.
118- if 'limit' in args :
127+ # Wait for the killCursors thread to run if necessary.
128+ if 'limit' in args and client_context .version [:2 ] < (3 , 1 ):
129+ self .client ._kill_cursors_executor .wake ()
119130 started = self .listener .results ['started' ]
120131 wait_until (
121132 lambda : started [- 1 ].command_name == 'killCursors' ,
122133 "publish a start event for killCursors." )
123- self .listener .add_command_filter ('killCursors' )
124134 else :
125135 try :
126136 getattr (coll , name )(** args )
@@ -132,7 +142,8 @@ def run_scenario(self):
132142 if event_type == "command_started_event" :
133143 event = self .listener .results ['started' ].pop (0 )
134144 # The tests substitute 42 for any number other than 0.
135- if event .command_name == 'getMore' :
145+ if (event .command_name == 'getMore'
146+ and event .command ['getMore' ]):
136147 event .command ['getMore' ] = 42
137148 elif event .command_name == 'killCursors' :
138149 event .command ['cursors' ] = [42 ]
0 commit comments