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 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
|
# Copyright (C) 2014-2017 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.
# Matcher for determining whether the operation completed successfully.
#
# @since 2.1.0
RSpec::Matchers.define :completes_successfully do |test|
match do |actual|
actual == test.expected_result || test.expected_result.nil?
end
end
# Matcher for determining whether the actual chunks collection matches
# the expected chunks collection.
#
# @since 2.1.0
RSpec::Matchers.define :match_chunks_collection do |expected|
match do |actual|
return true if expected.nil?
if expected.find.to_a.empty?
actual.find.to_a.empty?
else
actual.find.all? do |doc|
if matching_doc = expected.find(files_id: doc['files_id'], n: doc['n']).first
matching_doc.all? do |k, v|
doc[k] == v || k == '_id'
end
else
false
end
end
end
end
end
# Matcher for determining whether the actual files collection matches
# the expected files collection.
#
# @since 2.1.0
RSpec::Matchers.define :match_files_collection do |expected|
match do |actual|
return true if expected.nil?
actual.find.all? do |doc|
if matching_doc = expected.find(_id: doc['_id']).first
matching_doc.all? do |k, v|
doc[k] == v
end
else
false
end
end
end
end
# Matcher for determining whether the operation raised the correct error.
#
# @since 2.1.0
RSpec::Matchers.define :match_error do |error|
match do |actual|
Mongo::GridFS::Test::ERROR_MAPPING[error] == actual.class
end
end
module Mongo
module GridFS
# Represents a GridFS specification test.
#
# @since 2.1.0
class Spec
# @return [ String ] description The spec description.
#
# @since 2.1.0
attr_reader :description
# Instantiate the new spec.
#
# @example Create the spec.
# Spec.new(file)
#
# @param [ String ] file The name of the file.
#
# @since 2.1.0
def initialize(file)
@spec = YAML.load(ERB.new(File.new(file).read).result)
@description = File.basename(file)
@data = @spec['data']
end
# Get a list of Tests for each test definition.
#
# @example Get the list of Tests.
# spec.tests
#
# @return [ Array<Test> ] The list of Tests.
#
# @since 2.1.0
def tests
@tests ||= @spec['tests'].collect do |test|
Test.new(@data, test)
end
end
end
# Contains shared helper functions for converting YAML test values to Ruby objects.
#
# @since 2.1.0
module Convertible
# Convert an integer to the corresponding CRUD method suffix.
#
# @param [ Integer ] int The limit.
#
# @return [ String ] The CRUD method suffix.
#
# @since 2.1.0
def limit(int)
int == 0 ? 'many' : 'one'
end
# Convert an id value to a BSON::ObjectId.
#
# @param [ Object ] v The value to convert.
# @param [ Hash ] opts The options.
#
# @option opts [ BSON::ObjectId ] :id The id override.
#
# @return [ BSON::ObjectId ] The object id.
#
# @since 2.1.0
def convert__id(v, opts = {})
to_oid(v, opts[:id])
end
# Convert a value to a date.
#
# @param [ Object ] v The value to convert.
# @param [ Hash ] opts The options.
#
# @return [ Time ] The upload date time value.
#
# @since 2.1.0
def convert_uploadDate(v, opts = {})
v.is_a?(Time) ? v : v['$date'] ? Time.parse(v['$date']) : upload_date
end
# Convert an file id value to a BSON::ObjectId.
#
# @param [ Object ] v The value to convert.
# @param [ Hash ] opts The options.
#
# @option opts [ BSON::ObjectId ] :id The id override.
#
# @return [ BSON::ObjectId ] The object id.
#
# @since 2.1.0
def convert_files_id(v, opts = {})
to_oid(v, opts[:files_id])
end
# Convert a value to BSON::Binary data.
#
# @param [ Object ] v The value to convert.
# @param [ Hash ] opts The options.
#
# @return [ BSON::Binary ] The converted data.
#
# @since 2.1.0
def convert_data(v, opts = {})
v.is_a?(BSON::Binary) ? v : BSON::Binary.new(to_hex(v['$hex'], opts), :generic)
end
# Transform documents to have the correct object types for serialization.
#
# @param [ Array<Hash> ] docs The documents to transform.
# @param [ Hash ] opts The options.
#
# @return [ Array<Hash> ] The transformed documents.
#
# @since 2.1.0
def transform_docs(docs, opts = {})
docs.collect do |doc|
doc.each do |k, v|
doc[k] = send("convert_#{k}", v, opts) if respond_to?("convert_#{k}")
end
doc
end
end
# Convert a string to a hex value.
#
# @param [ String ] string The value to convert.
# @param [ Hash ] opts The options.
#
# @return [ String ] The hex value.
#
# @since 2.1.0
def to_hex(string, opts = {})
[ string ].pack('H*')
end
# Convert an object id represented in json to a BSON::ObjectId.
# A new BSON::ObjectId is returned if the json document is empty.
#
# @param [ Object ] value The value to convert.
# @param [ Object ] id The id override.
#
# @return [ BSON::ObjectId ] The object id.
#
# @since 2.1.0
def to_oid(value, id = nil)
if id
id
elsif value.is_a?(BSON::ObjectId)
value
elsif value['$oid']
BSON::ObjectId.from_string(value['$oid'])
else
BSON::ObjectId.new
end
end
# Convert options.
#
# @return [ Hash ] The options.
#
# @since 2.1.0
def options
@act['arguments']['options'].reduce({}) do |opts, (k, v)|
opts.merge!(chunk_size: v) if k == "chunkSizeBytes"
opts.merge!(upload_date: upload_date)
opts.merge!(content_type: v) if k == "contentType"
opts.merge!(metadata: v) if k == "metadata"
opts
end
end
end
# Represents a single GridFS test.
#
# @since 2.1.0
class Test
include Convertible
extend Forwardable
def_delegators :@operation, :expected_files_collection,
:expected_chunks_collection,
:result,
:expected_error,
:expected_result,
:error?
# The test description.
#
# @return [ String ] The test description.
#
# @since 2.1.0
attr_reader :description
# The upload date to use in the test.
#
# @return [ Time ] The upload date.
#
# @since 2.1.0
attr_reader :upload_date
# Mapping of test error strings to driver classes.
#
# @since 2.1.0
ERROR_MAPPING = {
'FileNotFound' => Mongo::Error::FileNotFound,
'ChunkIsMissing' => Mongo::Error::MissingFileChunk,
'ChunkIsWrongSize' => Mongo::Error::UnexpectedChunkLength,
'ExtraChunk' => Mongo::Error::ExtraFileChunk,
'RevisionNotFound' => Mongo::Error::InvalidFileRevision
}
# Instantiate the new GridFS::Test.
#
# @example Create the test.
# Test.new(data, test)
#
# @param [ Array<Hash> ] data The documents the files and chunks
# collections must have before the test runs.
# @param [ Hash ] test The test specification.
#
# @since 2.1.0
def initialize(data, test)
@pre_data = data
@description = test['description']
@upload_date = Time.now
if test['assert']['error']
@operation = UnsuccessfulOp.new(self, test)
else
@operation = SuccessfulOp.new(self, test)
end
@result = nil
end
# Whether the expected and actual collections should be compared after the test runs.
#
# @return [ true, false ] Whether the actual and expected collections should be compared.
#
# @since 2.1.0
def assert_data?
@operation.assert['data']
end
# Run the test.
#
# @example Run the test
# test.run(fs)
#
# @param [ Mongo::Grid::FSBucket ] fs The Grid::FSBucket to use in the test.
#
# @since 2.1.0
def run(fs)
setup(fs)
@operation.run(fs)
end
# Clear the files and chunks collection in the FSBucket and other collections used in the test.
#
# @example Clear the test collections
# test.clear_collections(fs)
#
# @param [ Mongo::Grid::FSBucket ] fs The Grid::FSBucket whose collections should be cleared.
#
# @since 2.1.0
def clear_collections(fs)
fs.files_collection.delete_many
fs.chunks_collection.delete_many
@operation.clear_collections(fs)
end
private
def setup(fs)
insert_pre_data(fs)
@operation.arrange(fs)
end
def files_data
@files_data ||= transform_docs(@pre_data['files'])
end
def chunks_data
@chunks_data ||= transform_docs(@pre_data['chunks'])
end
def insert_pre_files_data(fs)
fs.files_collection.insert_many(files_data)
fs.database['expected.files'].insert_many(files_data) if assert_data?
end
def insert_pre_chunks_data(fs)
fs.chunks_collection.insert_many(chunks_data)
fs.database['expected.chunks'].insert_many(chunks_data) if assert_data?
end
def insert_pre_data(fs)
insert_pre_files_data(fs) unless files_data.empty?
insert_pre_chunks_data(fs) unless chunks_data.empty?
end
# Contains logic and helper methods shared between a successful and
# non-successful GridFS test operation.
#
# @since 2.1.0
module Operable
extend Forwardable
def_delegators :@test, :upload_date
# The test operation name.
#
# @return [ String ] The operation name.
#
# @since 2.1.0
attr_reader :op
# The test assertion.
#
# @return [ Hash ] The test assertion definition.
#
# @since 2.1.0
attr_reader :assert
# The operation result.
#
# @return [ Object ] The operation result.
#
# @since 2.1.0
attr_reader :result
# The collection containing the expected files.
#
# @return [ Mongo::Collection ] The expected files collection.
#
# @since 2.1.0
attr_reader :expected_files_collection
# The collection containing the expected chunks.
#
# @return [ Mongo::Collection ] The expected chunks collection.
#
# @since 2.1.0
attr_reader :expected_chunks_collection
# Instantiate the new test operation.
#
# @example Create the test operation.
# Test.new(data, test)
#
# @param [ Test ] test The test.
# @param [ Hash ] spec The test specification.
#
# @since 2.1.0
def initialize(test, spec)
@test = test
@arrange = spec['arrange']
@act = spec['act']
@op = @act['operation']
@arguments = @act['arguments']
@assert = spec['assert']
end
# Arrange the data before running the operation.
# This sets up the correct scenario for the test.
#
# @example Arrange the data.
# operation.arrange(fs)
#
# @param [ Grid::FSBucket ] fs The FSBucket used in the test.
#
# @since 2.1.0
def arrange(fs)
if @arrange
@arrange['data'].each do |data|
send("#{data.keys.first}_exp_data", fs, data)
end
end
end
# Run the test operation.
#
# @example Execute the operation.
# operation.run(fs)
#
# @param [ Grid::FSBucket ] fs The FSBucket used in the test.
#
# @result [ Object ] The operation result.
#
# @since 2.1.0
def run(fs)
@expected_files_collection = fs.database['expected.files']
@expected_chunks_collection = fs.database['expected.chunks']
act(fs)
prepare_expected_collections(fs)
result
end
private
def prepare_expected_collections(fs)
if @test.assert_data?
@assert['data'].each do |data|
op = "#{data.keys.first}_exp_data"
send(op, fs, data)
end
end
end
def insert_exp_data(fs, data)
coll = fs.database[data['insert']]
if coll.name =~ /.files/
opts = { id: @result }
else
opts = { files_id: @result }
end
coll.insert_many(transform_docs(data['documents'], opts))
end
def delete_exp_data(fs, data)
coll = fs.database[data['delete']]
data['deletes'].each do |del|
id = del['q'].keys.first
coll.find(id => to_oid(del['q'][id])).send("delete_#{limit(del['limit'])}")
end
end
def update_exp_data(fs, data)
coll = fs.database[data['update']]
data['updates'].each do |update|
sel = update['q'].merge('files_id' => to_oid(update['q']['files_id']))
data = BSON::Binary.new(to_hex(update['u']['$set']['data']['$hex']), :generic)
u = update['u'].merge('$set' => { 'data' => data })
coll.find(sel).update_one(u)
end
end
def upload(fs)
io = StringIO.new(to_hex(@arguments['source']['$hex']))
fs.upload_from_stream(@arguments['filename'], io, options)
end
def download(fs)
io = StringIO.new.set_encoding(BSON::BINARY)
fs.download_to_stream(to_oid(@arguments['id']), io)
io.string
end
def download_by_name(fs)
io = StringIO.new.set_encoding(BSON::BINARY)
if @arguments['options']
fs.download_to_stream_by_name(@arguments['filename'], io, revision: @arguments['options']['revision'])
else
fs.download_to_stream_by_name(@arguments['filename'], io)
end
io.string
end
def delete(fs)
fs.delete(to_oid(@arguments['id']))
end
end
# A GridFS test operation that is expected to succeed.
#
# @since 2.1.0
class SuccessfulOp
include Convertible
include Test::Operable
# The expected result of executing the operation.
#
# @example Get the expected result.
# operation.expected_result
#
# @result [ Object ] The operation result.
#
# @since 2.1.0
def expected_result
if @assert['result'] == '&result'
@result
elsif @assert['result'] != 'void'
to_hex(@assert['result']['$hex'])
end
end
# Execute the operation.
#
# @example Execute the operation.
# operation.act(fs)
#
# @param [ Grid::FSBucket ] fs The FSBucket used in the test.
#
# @result [ Object ] The operation result.
#
# @since 2.1.0
def act(fs)
@result = send(op, fs)
end
# Whether this operation is expected to raise an error.
#
# @return [ false ] The operation is expected to succeed.
#
# @since 2.1.0
def error?
false
end
end
class UnsuccessfulOp
include Convertible
include Test::Operable
# Whether this operation is expected to raise an error.
#
# @return [ true ] The operation is expected to fail.
#
# @since 2.1.0
def error?
true
end
# The expected error.
#
# @example Execute the operation.
# operation.expected_error
#
# @return [ String ] The expected error name.
#
# @since 2.1.0
def expected_error
@assert['error']
end
# Execute the operation.
#
# @example Execute the operation.
# operation.act(fs)
#
# @param [ Grid::FSBucket ] fs The FSBucket used in the test.
#
# @result [ Mongo::Error ] The error encountered.
#
# @since 2.1.0
def act(fs)
begin
send(op, fs)
rescue => ex
@result = ex
end
end
end
end
end
end
|