forked from mongodb/mongo-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_crud_v2.py
More file actions
258 lines (225 loc) · 10.9 KB
/
test_crud_v2.py
File metadata and controls
258 lines (225 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Copyright 2019-present MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Test the collection module."""
import json
import os
import sys
sys.path[0:0] = [""]
from bson.py3compat import iteritems
from pymongo import operations, WriteConcern
from pymongo.command_cursor import CommandCursor
from pymongo.cursor import Cursor
from pymongo.errors import PyMongoError
from pymongo.read_concern import ReadConcern
from pymongo.results import _WriteResult, BulkWriteResult
from test import unittest, client_context, IntegrationTest
from test.utils import (camel_to_snake, camel_to_upper_camel,
camel_to_snake_args, drop_collections,
parse_collection_options, rs_client,
OvertCommandListener, TestCreator)
# Location of JSON test specifications.
_TEST_PATH = os.path.join(
os.path.dirname(os.path.realpath(__file__)), 'crud', 'v2')
# Default test database and collection names.
TEST_DB = 'testdb'
TEST_COLLECTION = 'testcollection'
class TestAllScenarios(IntegrationTest):
def run_operation(self, collection, test):
# Iterate over all operations.
for opdef in test['operations']:
# Convert command from CamelCase to pymongo.collection method.
operation = camel_to_snake(opdef['name'])
# Get command handle on target entity (collection/database).
target_object = opdef.get('object', 'collection')
if target_object == 'database':
cmd = getattr(collection.database, operation)
elif target_object == 'collection':
collection = collection.with_options(**dict(
parse_collection_options(opdef.get(
'collectionOptions', {}))))
cmd = getattr(collection, operation)
else:
self.fail("Unknown object name %s" % (target_object,))
# Convert arguments to snake_case and handle special cases.
arguments = opdef['arguments']
options = arguments.pop("options", {})
for option_name in options:
arguments[camel_to_snake(option_name)] = options[option_name]
if operation == "bulk_write":
# Parse each request into a bulk write model.
requests = []
for request in arguments["requests"]:
bulk_model = camel_to_upper_camel(request["name"])
bulk_class = getattr(operations, bulk_model)
bulk_arguments = camel_to_snake_args(request["arguments"])
requests.append(bulk_class(**bulk_arguments))
arguments["requests"] = requests
else:
for arg_name in list(arguments):
c2s = camel_to_snake(arg_name)
# PyMongo accepts sort as list of tuples.
if arg_name == "sort":
sort_dict = arguments[arg_name]
arguments[arg_name] = list(iteritems(sort_dict))
# Named "key" instead not fieldName.
if arg_name == "fieldName":
arguments["key"] = arguments.pop(arg_name)
# Aggregate uses "batchSize", while find uses batch_size.
elif arg_name == "batchSize" and operation == "aggregate":
continue
# Requires boolean returnDocument.
elif arg_name == "returnDocument":
arguments[c2s] = arguments[arg_name] == "After"
else:
arguments[c2s] = arguments.pop(arg_name)
if opdef.get('error') is True:
with self.assertRaises(PyMongoError):
cmd(**arguments)
else:
result = cmd(**arguments)
self.check_result(opdef.get('result'), result)
def check_result(self, expected_result, result):
if expected_result is None:
return True
if isinstance(result, Cursor) or isinstance(result, CommandCursor):
return list(result) == expected_result
elif isinstance(result, _WriteResult):
for res in expected_result:
prop = camel_to_snake(res)
# SPEC-869: Only BulkWriteResult has upserted_count.
if (prop == "upserted_count" and
not isinstance(result, BulkWriteResult)):
if result.upserted_id is not None:
upserted_count = 1
else:
upserted_count = 0
if upserted_count != expected_result[res]:
return False
elif prop == "inserted_ids":
# BulkWriteResult does not have inserted_ids.
if isinstance(result, BulkWriteResult):
if len(expected_result[res]) != result.inserted_count:
return False
else:
# InsertManyResult may be compared to [id1] from the
# crud spec or {"0": id1} from the retryable write spec.
ids = expected_result[res]
if isinstance(ids, dict):
ids = [ids[str(i)] for i in range(len(ids))]
if ids != result.inserted_ids:
return False
elif prop == "upserted_ids":
# Convert indexes from strings to integers.
ids = expected_result[res]
expected_ids = {}
for str_index in ids:
expected_ids[int(str_index)] = ids[str_index]
if expected_ids != result.upserted_ids:
return False
elif getattr(result, prop) != expected_result[res]:
return False
return True
else:
if not expected_result:
return result is None
else:
return result == expected_result
def check_events(self, expected_events, listener):
res = listener.results
if not len(expected_events):
return
# Expectations only have CommandStartedEvents.
self.assertEqual(len(res['started']), len(expected_events))
for i, expectation in enumerate(expected_events):
event_type = next(iter(expectation))
event = res['started'][i]
# The tests substitute 42 for any number other than 0.
if (event.command_name == 'getMore'
and event.command['getMore']):
event.command['getMore'] = 42
elif event.command_name == 'killCursors':
event.command['cursors'] = [42]
# Add upsert and multi fields back into expectations.
elif event.command_name == 'update':
updates = expectation[event_type]['command']['updates']
for update in updates:
update.setdefault('upsert', False)
update.setdefault('multi', False)
# Replace afterClusterTime: 42 with actual afterClusterTime.
expected_cmd = expectation[event_type]['command']
expected_read_concern = expected_cmd.get('readConcern')
if expected_read_concern is not None:
time = expected_read_concern.get('afterClusterTime')
if time == 42:
actual_time = event.command.get(
'readConcern', {}).get('afterClusterTime')
if actual_time is not None:
expected_read_concern['afterClusterTime'] = actual_time
for attr, expected in expectation[event_type].items():
actual = getattr(event, attr)
if isinstance(expected, dict):
for key, val in expected.items():
if val is None:
if key in actual:
self.fail("Unexpected key [%s] in %r" % (
key, actual))
elif key not in actual:
self.fail("Expected key [%s] in %r" % (
key, actual))
else:
self.assertEqual(val, actual[key],
"Key [%s] in %s" % (key, actual))
else:
self.assertEqual(actual, expected)
def create_test(scenario_def, test, name):
def run_scenario(self):
listener = OvertCommandListener()
# New client, to avoid interference from pooled sessions.
# Convert test['clientOptions'] to dict to avoid a Jython bug using "**"
# with ScenarioDict.
client = rs_client(event_listeners=[listener],
**dict(test.get('clientOptions', {})))
# Close the client explicitly to avoid having too many threads open.
self.addCleanup(client.close)
# Get database and collection objects.
database = getattr(
client, scenario_def.get('database_name', TEST_DB))
drop_collections(database)
collection = getattr(
database, scenario_def.get('collection_name', TEST_COLLECTION))
# Populate collection with data and run test.
collection.with_options(
write_concern=WriteConcern(w="majority")).insert_many(
scenario_def.get('data', []))
listener.results.clear()
self.run_operation(collection, test)
# Assert expected events.
self.check_events(test.get('expectations', {}), listener)
# Assert final state is expected.
expected_outcome = test.get('outcome', {}).get('collection')
if expected_outcome is not None:
collname = expected_outcome.get('name')
if collname is not None:
o_collection = getattr(database, collname)
else:
o_collection = collection
o_collection = o_collection.with_options(
read_concern=ReadConcern(level="local"))
self.assertEqual(list(o_collection.find()),
expected_outcome['data'])
return run_scenario
test_creator = TestCreator(create_test, TestAllScenarios, _TEST_PATH)
test_creator.create_tests()
if __name__ == "__main__":
unittest.main()