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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
|
# frozen_string_literal: true
# rubocop:todo all
# Copyright (C) 2014-2020 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.
module Mongo
module CRUD
class Operation
# Instantiate the operation.
#
# @param [ Hash ] spec The operation specification.
# @param [ Hash ] outcome_spec The outcome specification.
# If not provided, outcome is taken out of operation specification.
#
# @since 2.0.0
def initialize(crud_test, spec, outcome_spec = nil)
@crud_test = crud_test
@spec = IceNine.deep_freeze(spec)
@name = spec['name']
if spec['arguments']
@arguments = BSON::ExtJSON.parse_obj(spec['arguments'], mode: :bson)
else
@arguments = {}
end
@outcome = Outcome.new(outcome_spec || spec)
end
attr_reader :spec
# The operation name.
#
# @return [ String ] name The operation name.
#
# @since 2.0.0
attr_reader :name
attr_reader :arguments
attr_reader :outcome
def object
@spec['object'] || 'collection'
end
# Which collection to verify results from.
# Returns the collection name specified on the operation, or
# the collection name for the entire spec file.
def verify_collection_name
if outcome && outcome.collection_name
outcome.collection_name
else
@spec['collection_name'] || 'crud_spec_test'
end
end
# Whether the operation is expected to have results.
#
# @example Whether the operation is expected to have results.
# operation.has_results?
#
# @return [ true, false ] If the operation is expected to have results.
#
# @since 2.0.0
def has_results?
!(name == 'aggregate' &&
pipeline.find {|op| op.keys.include?('$out') })
end
# Execute the operation.
#
# @example Execute the operation.
# operation.execute
#
# @param [ Collection ] collection The collection to execute the operation on.
#
# @return [ Result, Array<Hash> ] The result of executing the operation.
#
# @since 2.0.0
def execute(target)
op_name = ::Utils.underscore(name)
if target.is_a?(Mongo::Database)
op_name = "db_#{op_name}"
elsif target.is_a?(Mongo::Client)
op_name = "client_#{op_name}"
end
send(op_name, target, Context.new)
end
def database_options
if opts = @spec['databaseOptions']
::Utils.convert_operation_options(opts)
else
nil
end
end
def collection_options
::Utils.convert_operation_options(@spec['collectionOptions'])
end
private
# read operations
def aggregate(collection, context)
collection.aggregate(arguments['pipeline'], transformed_options(context)).to_a
end
def db_aggregate(database, context)
database.aggregate(arguments['pipeline'], transformed_options(context)).to_a
end
def count(collection, context)
collection.count(arguments['filter'], transformed_options(context))
end
def count_documents(collection, context)
collection.count_documents(arguments['filter'], transformed_options(context))
end
def distinct(collection, context)
collection.distinct(arguments['fieldName'], arguments['filter'], transformed_options(context))
end
def estimated_document_count(collection, context)
collection.estimated_document_count(transformed_options(context))
end
def find(collection, context)
opts = transformed_options(context)
if arguments['modifiers']
opts = opts.merge(modifiers: BSON::Document.new(arguments['modifiers']))
end
if read_preference
collection = collection.with(read: read_preference)
end
collection.find(arguments['filter'], opts).to_a
end
def find_one(collection, context)
find(collection, context).first
end
def watch(collection, context)
collection.watch
end
def db_watch(database, context)
database.watch
end
def client_watch(client, context)
client.watch
end
def download(fs_bucket, context)
stream = fs_bucket.open_download_stream(arguments['id'])
stream.read
end
def download_by_name(fs_bucket, context)
stream = fs_bucket.open_download_stream_by_name(arguments['filename'])
stream.read
end
def map_reduce(collection, context)
view = Mongo::Collection::View.new(collection)
mr = Mongo::Collection::View::MapReduce.new(view, arguments['map'].javascript, arguments['reduce'].javascript)
mr.to_a
end
# write operations
def bulk_write(collection, context)
result = collection.bulk_write(requests, transformed_options(context))
return_doc = {}
return_doc['deletedCount'] = result.deleted_count || 0
return_doc['insertedIds'] = result.inserted_ids if result.inserted_ids
return_doc['insertedCount'] = result.inserted_count || 0
return_doc['upsertedId'] = result.upserted_id if arguments['upsert']
return_doc['upsertedIds'] = result.upserted_ids if result.upserted_ids
return_doc['upsertedCount'] = result.upserted_count || 0
return_doc['matchedCount'] = result.matched_count || 0
return_doc['modifiedCount'] = result.modified_count || 0
return_doc
end
def delete_many(collection, context)
result = collection.delete_many(arguments['filter'], transformed_options(context))
{ 'deletedCount' => result.deleted_count }
end
def delete_one(collection, context)
result = collection.delete_one(arguments['filter'], transformed_options(context))
{ 'deletedCount' => result.deleted_count }
end
def insert_many(collection, context)
result = collection.insert_many(arguments['documents'], transformed_options(context))
{ 'insertedIds' => result.inserted_ids }
end
def insert_one(collection, context)
result = collection.insert_one(arguments['document'], transformed_options(context))
{ 'insertedId' => result.inserted_id }
end
def replace_one(collection, context)
result = collection.replace_one(arguments['filter'], arguments['replacement'], transformed_options(context))
update_return_doc(result)
end
def update_many(collection, context)
result = collection.update_many(arguments['filter'], arguments['update'], transformed_options(context))
update_return_doc(result)
end
def update_one(collection, context)
result = collection.update_one(arguments['filter'], arguments['update'], transformed_options(context))
update_return_doc(result)
end
def find_one_and_delete(collection, context)
collection.find_one_and_delete(arguments['filter'], transformed_options(context))
end
def find_one_and_replace(collection, context)
collection.find_one_and_replace(arguments['filter'], arguments['replacement'], transformed_options(context))
end
def find_one_and_update(collection, context)
collection.find_one_and_update(arguments['filter'], arguments['update'], transformed_options(context))
end
# ddl
def client_list_databases(client, context)
client.list_databases
end
def client_list_database_names(client, context)
client.list_databases({}, true)
end
def client_list_database_objects(client, context)
client.list_mongo_databases
end
def db_list_collections(database, context)
database.list_collections
end
def db_list_collection_names(database, context)
database.collection_names
end
def db_list_collection_objects(database, context)
database.collections
end
def create_collection(database, context)
opts = transformed_options(context)
database[arguments.fetch('collection')]
.create(
{
session: opts[:session],
encrypted_fields: opts[:encrypted_fields],
validator: opts[:validator],
}.compact
)
end
def rename(collection, context)
collection.client.use(:admin).command({
renameCollection: "#{collection.database.name}.#{collection.name}",
to: "#{collection.database.name}.#{arguments['to']}"
})
end
def drop(collection, context)
opts = transformed_options(context)
collection.drop(encrypted_fields: opts[:encrypted_fields])
end
def drop_collection(database, context)
opts = transformed_options(context)
database[arguments.fetch('collection')].drop(encrypted_fields: opts[:encrypted_fields])
end
def create_index(collection, context)
# The Ruby driver method uses `key` while the createIndexes server
# command and the test specifiecation use 'keys`.
opts = BSON::Document.new(options)
if opts.key?(:keys)
opts[:key] = opts.delete(:keys)
end
session = opts.delete(:session)
collection.indexes(session: session && context.send(session)).create_many([opts])
end
def drop_index(collection, context)
unless options.keys == %i(name)
raise "Only name is allowed when dropping the index"
end
name = options[:name]
collection.indexes.drop_one(name)
end
def list_indexes(collection, context)
collection.indexes.to_a
end
# special
def assert_collection_exists(client, context)
c = client.use(dn = arguments.fetch('database'))
unless c.database.collection_names.include?(cn = arguments.fetch('collection'))
raise "Collection #{cn} does not exist in database #{dn}, but must"
end
end
def assert_collection_not_exists(client, context)
c = client.use(dn = arguments.fetch('database'))
if c.database.collection_names.include?(cn = arguments.fetch('collection'))
raise "Collection #{cn} exists in database #{dn}, but must not"
end
end
def assert_index_exists(client, context)
c = client.use(dn = arguments.fetch('database'))
coll = c[cn = arguments.fetch('collection')]
unless coll.indexes.map { |doc| doc['name'] }.include?(ixn = arguments.fetch('index'))
raise "Index #{ixn} does not exist in collection #{cn} in database #{dn}, but must"
end
end
def assert_index_not_exists(client, context)
c = client.use(dn = arguments.fetch('database'))
coll = c[cn = arguments.fetch('collection')]
begin
if coll.indexes.map { |doc| doc['name'] }.include?(ixn = arguments.fetch('index'))
raise "Index #{ixn} exists in collection #{cn} in database #{dn}, but must not"
end
rescue Mongo::Error::OperationFailure::Family => e
if e.to_s =~ /ns does not exist/
# Success.
else
raise
end
end
end
def configure_fail_point(client, context)
fp = arguments.fetch('failPoint')
$disable_fail_points ||= []
$disable_fail_points << [
fp,
ClusterConfig.instance.primary_address,
]
client.use('admin').database.command(fp)
end
# options & arguments
def options
out = {}
# Most tests have an "arguments" key which is a hash of options to
# be provided to the operation. The command monitoring unacknowledged
# bulk write test is an exception in that it has an "options" key
# with the options.
arguments.merge(arguments['options'] || {}).each do |spec_k, v|
ruby_k = ::Utils.underscore(spec_k).to_sym
ruby_k = {
min: :min_value,
max: :max_value,
show_record_id: :show_disk_loc
}[ruby_k] || ruby_k
if respond_to?("transform_#{ruby_k}", true)
v = send("transform_#{ruby_k}", v)
end
out[ruby_k] = v
end
out
end
def requests
arguments['requests'].map do |request|
case request.keys.first
when 'insertOne' then
{ insert_one: request['insertOne']['document'] }
when 'updateOne' then
update = request['updateOne']
{ update_one: { filter: update['filter'], update: update['update'] } }
when 'name' then
bulk_request(request)
end
end
end
def bulk_request(request)
op_name = ::Utils.underscore(request['name'])
args = ::Utils.shallow_snakeize_hash(request['arguments'])
if args[:document]
unless args.keys == [:document]
raise "If :document is given, it must be the only key"
end
args = args[:document]
end
{ op_name => args }
end
def upsert
arguments['upsert']
end
def transform_return_document(v)
::Utils.underscore(v).to_sym
end
def update
arguments['update']
end
def transform_read_preference(v)
::Utils.snakeize_hash(v)
end
def read_preference
transform_read_preference(@spec['read_preference'])
end
def update_return_doc(result)
return_doc = {}
return_doc['upsertedId'] = result.upserted_id if arguments['upsert']
return_doc['upsertedCount'] = result.upserted_count
return_doc['matchedCount'] = result.matched_count
return_doc['modifiedCount'] = result.modified_count if result.modified_count
return_doc
end
def transformed_options(context)
opts = options.dup
if opts[:session]
opts[:session] = case opts[:session]
when 'session0'
unless context.session0
raise "Trying to use session0 but it is not in context"
end
context.session0
when 'session1'
unless context.session1
raise "Trying to use session1 but it is not in context"
end
context.session1
else
raise "Invalid session name '#{opts[:session]}'"
end
end
opts
end
end
end
end
|