26
26
from bson .json_util import object_hook
27
27
from pymongo import monitoring
28
28
from 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 )
30
31
from pymongo .write_concern import WriteConcern
31
32
from test import unittest , client_context
32
33
from test .utils import single_client , wait_until , EventListener
@@ -49,7 +50,6 @@ class TestAllScenarios(unittest.TestCase):
49
50
@client_context .require_connection
50
51
def setUpClass (cls ):
51
52
cls .listener = EventListener ()
52
- cls .listener .add_command_filter ('killCursors' )
53
53
cls .saved_listeners = monitoring ._LISTENERS
54
54
monitoring ._LISTENERS = monitoring ._Listeners ([])
55
55
cls .client = single_client (event_listeners = [cls .listener ])
@@ -68,27 +68,42 @@ def run_scenario(self):
68
68
dbname = scenario_def ['database_name' ]
69
69
collname = scenario_def ['collection_name' ]
70
70
71
- # Clear the kill cursors queue.
72
- self .client ._kill_cursors_executor .wake ()
73
-
74
71
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
+
75
86
coll = self .client [dbname ][collname ]
76
87
coll .drop ()
77
88
coll .insert_many (scenario_def ['data' ])
78
89
self .listener .results .clear ()
79
90
name = camel_to_snake (test ['operation' ]['name' ])
80
- args = test ['operation' ]['arguments' ]
81
91
# Don't send $readPreference to mongos before 2.4.
82
92
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' )
88
102
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 ]
92
107
93
108
if name == 'bulk_write' :
94
109
bulk_args = []
@@ -102,25 +117,20 @@ def run_scenario(self):
102
117
except OperationFailure :
103
118
pass
104
119
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' )
110
120
if 'sort' in args :
111
121
args ['sort' ] = list (args ['sort' ].items ())
112
122
try :
113
123
# Iterate the cursor.
114
124
tuple (coll .find (** args ))
115
125
except OperationFailure :
116
126
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 ()
119
130
started = self .listener .results ['started' ]
120
131
wait_until (
121
132
lambda : started [- 1 ].command_name == 'killCursors' ,
122
133
"publish a start event for killCursors." )
123
- self .listener .add_command_filter ('killCursors' )
124
134
else :
125
135
try :
126
136
getattr (coll , name )(** args )
@@ -132,7 +142,8 @@ def run_scenario(self):
132
142
if event_type == "command_started_event" :
133
143
event = self .listener .results ['started' ].pop (0 )
134
144
# 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' ]):
136
147
event .command ['getMore' ] = 42
137
148
elif event .command_name == 'killCursors' :
138
149
event .command ['cursors' ] = [42 ]
0 commit comments