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 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
|
require 'spec_helper'
describe Mongo::Grid::FSBucket do
let(:fs) do
described_class.new(client.database, options)
end
let(:client) do
authorized_client
end
let(:options) do
{ }
end
let(:filename) do
'specs.rb'
end
let(:file) do
File.open(__FILE__)
end
describe '#initialize' do
it 'sets the files collection' do
expect(fs.files_collection.name).to eq('fs.files')
end
it 'sets the chunks collection' do
expect(fs.chunks_collection.name).to eq('fs.chunks')
end
context 'when options are provided' do
let(:fs) do
described_class.new(authorized_client.database, options)
end
context 'when a write concern is set' do
context 'when the option :write is provided' do
let(:options) do
{ write: { w: 2 } }
end
it 'sets the write concern' do
expect(fs.send(:write_concern).options).to eq(Mongo::WriteConcern.get(w: 2).options)
end
end
end
context 'when a read preference is set' do
let(:options) do
{ read: { mode: :secondary } }
end
it 'sets the read preference' do
expect(fs.send(:read_preference)).to eq(options[:read])
end
end
context 'when a read preference is not set' do
let(:database) do
authorized_client.with(read: { mode: :secondary }).database
end
let(:fs) do
described_class.new(database, options)
end
it 'uses the read preference of the database' do
expect(fs.read_preference).to be(database.read_preference)
end
end
context 'when a write stream is opened' do
let(:stream) do
fs.open_upload_stream('test.txt')
end
let(:fs) do
described_class.new(authorized_client.database, options)
end
context 'when a write option is specified' do
let(:options) do
{ write: { w: 2 } }
end
it 'passes the write concern to the write stream' do
expect(stream.write_concern.options).to eq(Mongo::WriteConcern.get(options[:write]).options)
end
end
end
end
end
describe '#find' do
let(:fs) do
described_class.new(authorized_client.database)
end
context 'when there is no selector provided' do
let(:files) do
[
Mongo::Grid::File.new('hello world!', :filename => 'test.txt'),
Mongo::Grid::File.new('goodbye world!', :filename => 'test1.txt')
]
end
before do
files.each do |file|
fs.insert_one(file)
end
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
it 'returns a collection view' do
expect(fs.find).to be_a(Mongo::Collection::View)
end
it 'iterates over the documents in the result' do
fs.find.each do |document|
expect(document).to_not be_nil
end
end
end
context 'when provided a filter' do
let(:view) do
fs.find(filename: 'test.txt')
end
it 'returns a collection view for the filter' do
expect(view.filter).to eq('filename' => 'test.txt')
end
end
context 'when options are provided' do
let(:view) do
fs.find({filename: 'test.txt'}, options)
end
context 'when provided batch_size' do
let(:options) do
{ batch_size: 5 }
end
it 'sets the batch_size on the view' do
expect(view.batch_size).to eq(options[:batch_size])
end
end
context 'when provided limit' do
let(:options) do
{ limit: 5 }
end
it 'sets the limit on the view' do
expect(view.limit).to eq(options[:limit])
end
end
context 'when provided no_cursor_timeout' do
let(:options) do
{ no_cursor_timeout: true }
end
it 'sets the no_cursor_timeout on the view' do
expect(view.options[:no_cursor_timeout]).to eq(options[:no_cursor_timeout])
end
end
context 'when provided skip' do
let(:options) do
{ skip: 5 }
end
it 'sets the skip on the view' do
expect(view.skip).to eq(options[:skip])
end
end
context 'when provided sort' do
let(:options) do
{ sort: { 'x' => Mongo::Index::ASCENDING } }
end
it 'sets the sort on the view' do
expect(view.sort).to eq(options[:sort])
end
end
end
end
describe '#find_one' do
let(:fs) do
described_class.new(authorized_client.database)
end
let(:file) do
Mongo::Grid::File.new('hello world!', :filename => 'test.txt')
end
before do
fs.insert_one(file)
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
let(:from_db) do
fs.find_one(:filename => 'test.txt')
end
let(:from_db_upload_date) do
from_db.info.upload_date.strftime("%Y-%m-%d %H:%M:%S")
end
let(:file_info_upload_date) do
file.info.upload_date.strftime("%Y-%m-%d %H:%M:%S")
end
it 'returns the assembled file from the db' do
expect(from_db.filename).to eq(file.info.filename)
end
it 'maps the file info correctly' do
expect(from_db.info.length).to eq(file.info.length)
expect(from_db_upload_date).to eq(file_info_upload_date)
end
end
describe '#insert_one' do
let(:fs) do
described_class.new(authorized_client.database)
end
let(:file) do
Mongo::Grid::File.new('Hello!', :filename => 'test.txt')
end
context 'when inserting the file once' do
let!(:result) do
fs.insert_one(file)
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
let(:from_db) do
fs.find_one(:filename => 'test.txt')
end
it 'inserts the file into the database' do
expect(from_db.filename).to eq(file.info.filename)
end
it 'includes the chunks and data with the file' do
expect(from_db.data).to eq('Hello!')
end
it 'returns the file id' do
expect(result).to eq(file.id)
end
end
context 'when the files collection is empty' do
before do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
expect(fs.files_collection).to receive(:indexes).and_call_original
expect(fs.chunks_collection).to receive(:indexes).and_call_original
fs.insert_one(file)
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
let(:chunks_index) do
fs.database[fs.chunks_collection.name].indexes.get(:files_id => 1, :n => 1)
end
let(:files_index) do
fs.database[fs.files_collection.name].indexes.get(:filename => 1, :uploadDate => 1)
end
it 'creates an index on the files collection' do
expect(files_index[:name]).to eq('filename_1_uploadDate_1')
end
it 'creates an index on the chunks collection' do
expect(chunks_index[:name]).to eq('files_id_1_n_1')
end
context 'when a write operation is called more than once' do
before do
expect(fs).not_to receive(:ensure_indexes!)
end
let(:file2) do
Mongo::Grid::File.new('Goodbye!', :filename => 'test2.txt')
end
it 'only creates the indexes the first time' do
expect(fs.insert_one(file2)).to be_a(BSON::ObjectId)
end
end
end
context 'when the index creation encounters an error' do
before do
fs.chunks_collection.drop
fs.chunks_collection.indexes.create_one(Mongo::Grid::FSBucket::CHUNKS_INDEX, :unique => false)
expect(fs.chunks_collection).to receive(:indexes).and_call_original
expect(fs.files_collection).not_to receive(:indexes)
end
after do
fs.database[fs.chunks_collection.name].indexes.drop_one('files_id_1_n_1')
end
it 'raises the error to the user' do
expect {
fs.insert_one(file)
}.to raise_error(Mongo::Error::OperationFailure)
end
end
context 'when the files collection is not empty' do
before do
fs.files_collection.insert_one(a: 1)
expect(fs.files_collection).not_to receive(:indexes)
expect(fs.chunks_collection).not_to receive(:indexes)
fs.insert_one(file)
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
let(:files_index) do
fs.database[fs.files_collection.name].indexes.get(:filename => 1, :uploadDate => 1)
end
it 'assumes indexes already exist' do
expect(files_index[:name]).to eq('filename_1_uploadDate_1')
end
end
context 'when inserting the file more than once' do
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
it 'raises an error' do
expect {
fs.insert_one(file)
fs.insert_one(file)
}.to raise_error(Mongo::Error::BulkWriteError)
end
end
context 'when the file exceeds the max bson size' do
let(:fs) do
described_class.new(authorized_client.database)
end
let(:file) do
str = 'y' * 16777216
Mongo::Grid::File.new(str, :filename => 'large-file.txt')
end
before do
fs.insert_one(file)
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
it 'successfully inserts the file' do
expect(
fs.find_one(:filename => 'large-file.txt').chunks
).to eq(file.chunks)
end
end
end
describe '#delete_one' do
let(:file) do
Mongo::Grid::File.new('Hello!', :filename => 'test.txt')
end
before do
fs.insert_one(file)
fs.delete_one(file)
end
let(:from_db) do
fs.find_one(:filename => 'test.txt')
end
it 'removes the file from the db' do
expect(from_db).to be_nil
end
end
describe '#delete' do
let(:file_id) do
fs.upload_from_stream(filename, file)
end
before do
fs.delete(file_id)
end
let(:from_db) do
fs.find_one(:filename => filename)
end
it 'removes the file from the db' do
expect(from_db).to be_nil
end
context 'when a custom file id is used' do
let(:custom_file_id) do
fs.upload_from_stream(filename, file, file_id: 'Custom ID')
end
before do
fs.delete(custom_file_id)
end
let(:from_db) do
fs.find_one(:filename => filename)
end
it 'removes the file from the db' do
expect(from_db).to be_nil
end
end
end
context 'when a read stream is opened' do
let(:fs) do
described_class.new(authorized_client.database, options)
end
let(:io) do
StringIO.new
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
describe '#open_download_stream' do
let!(:file_id) do
fs.open_upload_stream(filename) do |stream|
stream.write(file)
end.file_id
end
context 'when a block is provided' do
let!(:stream) do
fs.open_download_stream(file_id) do |stream|
io.write(stream.read)
end
end
it 'returns a Stream::Read object' do
expect(stream).to be_a(Mongo::Grid::FSBucket::Stream::Read)
end
it 'closes the stream after the block completes' do
expect(stream.closed?).to be(true)
end
it 'yields the stream to the block' do
expect(io.size).to eq(file.size)
end
end
context 'when a block is not provided' do
let!(:stream) do
fs.open_download_stream(file_id)
end
it 'returns a Stream::Read object' do
expect(stream).to be_a(Mongo::Grid::FSBucket::Stream::Read)
end
it 'does not close the stream' do
expect(stream.closed?).to be(false)
end
it 'does not yield the stream to the block' do
expect(io.size).to eq(0)
end
end
context 'when a custom file id is provided' do
let(:file) do
File.open(__FILE__)
end
let!(:file_id) do
fs.open_upload_stream(filename, file_id: 'Custom ID') do |stream|
stream.write(file)
end.file_id
end
context 'when a block is provided' do
let!(:stream) do
fs.open_download_stream(file_id) do |stream|
io.write(stream.read)
end
end
it 'yields the stream to the block' do
expect(io.size).to eq(file.size)
end
end
context 'when a block is not provided' do
let!(:stream) do
fs.open_download_stream(file_id)
end
it 'returns a Stream::Read object' do
expect(stream).to be_a(Mongo::Grid::FSBucket::Stream::Read)
end
it 'does not close the stream' do
expect(stream.closed?).to be(false)
end
it 'does not yield the stream to the block' do
expect(io.size).to eq(0)
end
end
end
end
describe '#download_to_stream' do
context 'sessions' do
let(:options) do
{ session: session }
end
let(:file_id) do
fs.open_upload_stream(filename) do |stream|
stream.write(file)
end.file_id
end
let(:operation) do
fs.download_to_stream(file_id, io)
end
let(:client) do
authorized_client
end
it_behaves_like 'an operation using a session'
end
context 'when the file is found' do
let!(:file_id) do
fs.open_upload_stream(filename) do |stream|
stream.write(file)
end.file_id
end
before do
fs.download_to_stream(file_id, io)
end
it 'writes to the provided stream' do
expect(io.size).to eq(file.size)
end
it 'does not close the stream' do
expect(io.closed?).to be(false)
end
context 'when the file has length 0' do
let(:file) do
StringIO.new('')
end
let(:from_db) do
fs.open_upload_stream(filename) { |s| s.write(file) }
fs.find_one(:filename => filename)
end
it 'can read the file back' do
expect(from_db.data.size).to eq(file.size)
end
end
end
context 'when there is no files collection document found' do
it 'raises an exception' do
expect{
fs.download_to_stream(BSON::ObjectId.new, io)
}.to raise_exception(Mongo::Error::FileNotFound)
end
end
context 'when a file has an id that is not an ObjectId' do
before do
fs.insert_one(file)
fs.download_to_stream(file_id, io)
end
let(:file_id) do
'non-object-id'
end
let(:file) do
Mongo::Grid::File.new(File.open(__FILE__).read,
:filename => filename,
:_id => file_id)
end
it 'reads the file successfully' do
expect(io.size).to eq(file.data.size)
end
end
end
context 'when a read preference is specified' do
let(:fs) do
described_class.new(authorized_client.database, options)
end
let(:options) do
{ read: { mode: :secondary } }
end
let(:stream) do
fs.open_download_stream(BSON::ObjectId)
end
it 'sets the read preference on the Stream::Read object' do
expect(stream.read_preference).to eq(options[:read])
end
end
describe '#download_to_stream_by_name' do
let(:files) do
[
StringIO.new('hello 1'),
StringIO.new('hello 2'),
StringIO.new('hello 3'),
StringIO.new('hello 4')
]
end
context ' when using a session' do
let(:options) do
{ session: session }
end
let(:operation) do
fs.download_to_stream_by_name('test.txt', io)
end
let(:client) do
authorized_client
end
before do
files.each do |file|
authorized_client.database.fs.upload_from_stream('test.txt', file)
end
end
let(:io) do
StringIO.new
end
it_behaves_like 'an operation using a session'
end
context 'when not using a session' do
before do
files.each do |file|
fs.upload_from_stream('test.txt', file)
end
end
let(:io) do
StringIO.new
end
context 'when revision is not specified' do
let!(:result) do
fs.download_to_stream_by_name('test.txt', io)
end
it 'returns the most recent version' do
expect(io.string).to eq('hello 4')
end
end
context 'when revision is 0' do
let!(:result) do
fs.download_to_stream_by_name('test.txt', io, revision: 0)
end
it 'returns the original stored file' do
expect(io.string).to eq('hello 1')
end
end
context 'when revision is negative' do
let!(:result) do
fs.download_to_stream_by_name('test.txt', io, revision: -2)
end
it 'returns that number of versions from the most recent' do
expect(io.string).to eq('hello 3')
end
end
context 'when revision is positive' do
let!(:result) do
fs.download_to_stream_by_name('test.txt', io, revision: 1)
end
it 'returns that number revision' do
expect(io.string).to eq('hello 2')
end
end
context 'when the file revision is not found' do
it 'raises a FileNotFound error' do
expect {
fs.download_to_stream_by_name('test.txt', io, revision: 100)
}.to raise_exception(Mongo::Error::InvalidFileRevision)
end
end
context 'when the file is not found' do
it 'raises a FileNotFound error' do
expect {
fs.download_to_stream_by_name('non-existent.txt', io)
}.to raise_exception(Mongo::Error::FileNotFound)
end
end
end
end
describe '#open_download_stream_by_name' do
let(:files) do
[
StringIO.new('hello 1'),
StringIO.new('hello 2'),
StringIO.new('hello 3'),
StringIO.new('hello 4')
]
end
let(:io) do
StringIO.new
end
context ' when using a session' do
let(:options) do
{ session: session }
end
let(:operation) do
fs.download_to_stream_by_name('test.txt', io)
end
let(:client) do
authorized_client
end
before do
files.each do |file|
authorized_client.database.fs.upload_from_stream('test.txt', file)
end
end
let(:io) do
StringIO.new
end
it_behaves_like 'an operation using a session'
end
context 'when not using a session' do
before do
files.each do |file|
fs.upload_from_stream('test.txt', file)
end
end
context 'when a block is provided' do
let(:stream) do
fs.open_download_stream_by_name('test.txt') do |stream|
io.write(stream.read)
end
end
it 'returns a Stream::Read object' do
expect(stream).to be_a(Mongo::Grid::FSBucket::Stream::Read)
end
it 'closes the stream after the block completes' do
expect(stream.closed?).to be(true)
end
it 'yields the stream to the block' do
stream
expect(io.size).to eq(files[0].size)
end
context 'when revision is not specified' do
let!(:result) do
fs.open_download_stream_by_name('test.txt') do |stream|
io.write(stream.read)
end
end
it 'returns the most recent version' do
expect(io.string).to eq('hello 4')
end
end
context 'when revision is 0' do
let!(:result) do
fs.open_download_stream_by_name('test.txt', revision: 0) do |stream|
io.write(stream.read)
end
end
it 'returns the original stored file' do
expect(io.string).to eq('hello 1')
end
end
context 'when revision is negative' do
let!(:result) do
fs.open_download_stream_by_name('test.txt', revision: -2) do |stream|
io.write(stream.read)
end
end
it 'returns that number of versions from the most recent' do
expect(io.string).to eq('hello 3')
end
end
context 'when revision is positive' do
let!(:result) do
fs.open_download_stream_by_name('test.txt', revision: 1) do |stream|
io.write(stream.read)
end
end
it 'returns that number revision' do
expect(io.string).to eq('hello 2')
end
end
context 'when the file revision is not found' do
it 'raises a FileNotFound error' do
expect {
fs.open_download_stream_by_name('test.txt', revision: 100)
}.to raise_exception(Mongo::Error::InvalidFileRevision)
end
end
context 'when the file is not found' do
it 'raises a FileNotFound error' do
expect {
fs.open_download_stream_by_name('non-existent.txt')
}.to raise_exception(Mongo::Error::FileNotFound)
end
end
end
context 'when a block is not provided' do
let!(:stream) do
fs.open_download_stream_by_name('test.txt')
end
it 'returns a Stream::Read object' do
expect(stream).to be_a(Mongo::Grid::FSBucket::Stream::Read)
end
it 'does not close the stream' do
expect(stream.closed?).to be(false)
end
it 'does not yield the stream to the block' do
expect(io.size).to eq(0)
end
end
end
end
end
context 'when a write stream is opened' do
let(:stream) do
fs.open_upload_stream(filename)
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
describe '#open_upload_stream' do
context 'when a block is not provided' do
it 'returns a Stream::Write object' do
expect(stream).to be_a(Mongo::Grid::FSBucket::Stream::Write)
end
it 'creates an ObjectId for the file' do
expect(stream.file_id).to be_a(BSON::ObjectId)
end
context 'when a custom file ID is provided' do
let(:stream) do
fs.open_upload_stream(filename, file_id: 'Custom ID')
end
it 'returns a Stream::Write object' do
expect(stream).to be_a(Mongo::Grid::FSBucket::Stream::Write)
end
it 'creates an ObjectId for the file' do
expect(stream.file_id).to eq('Custom ID')
end
end
end
context 'when a block is provided' do
context 'when a session is not used' do
let!(:stream) do
fs.open_upload_stream(filename) do |stream|
stream.write(file)
end
end
let(:result) do
fs.find_one(filename: filename)
end
it 'returns the stream' do
expect(stream).to be_a(Mongo::Grid::FSBucket::Stream::Write)
end
it 'creates an ObjectId for the file' do
expect(stream.file_id).to be_a(BSON::ObjectId)
end
it 'yields the stream to the block' do
expect(result.data.size).to eq(file.size)
end
it 'closes the stream when the block completes' do
expect(stream.closed?).to be(true)
end
end
end
end
describe '#upload_from_stream' do
let!(:result) do
fs.upload_from_stream(filename, file)
end
let(:file_from_db) do
fs.find_one(:filename => filename)
end
it 'writes to the provided stream' do
expect(file_from_db.data.length).to eq(file.size)
end
it 'does not close the stream' do
expect(file.closed?).to be(false)
end
it 'returns the id of the file' do
expect(result).to be_a(BSON::ObjectId)
end
context 'when the io stream raises an error' do
let(:stream) do
fs.open_upload_stream(filename)
end
before do
allow(fs).to receive(:open_upload_stream).and_yield(stream)
end
context 'when stream#abort does not raise an OperationFailure' do
before do
expect(stream).to receive(:abort).and_call_original
file.close
end
it 'raises the original IOError' do
expect {
fs.upload_from_stream(filename, file)
}.to raise_exception(IOError)
end
end
context 'when stream#abort raises an OperationFailure' do
before do
allow(stream).to receive(:abort).and_raise(Mongo::Error::OperationFailure)
file.close
end
it 'raises the original IOError' do
expect {
fs.upload_from_stream(filename, file)
}.to raise_exception(IOError)
end
end
end
end
context 'when options are provided when opening the write stream' do
let(:stream) do
fs.open_upload_stream(filename, stream_options)
end
context 'when a custom file id is provided' do
let(:stream_options) do
{ file_id: 'Custom ID' }
end
it 'sets the file id on the stream' do
expect(stream.file_id).to eq('Custom ID')
end
end
context 'when a write option is specified' do
let(:stream_options) do
{ write: { w: 2 } }
end
it 'sets the write concern on the write stream' do
expect(stream.write_concern.options).to eq(Mongo::WriteConcern.get(stream_options[:write]).options)
end
end
context 'when there is a chunk size set on the FSBucket' do
let(:stream_options) do
{ }
end
let(:options) do
{ chunk_size: 100 }
end
it 'sets the chunk size as the default on the write stream' do
expect(stream.options[:chunk_size]).to eq(options[:chunk_size])
end
end
context 'when a chunk size option is specified' do
let(:stream_options) do
{ chunk_size: 50 }
end
it 'sets the chunk size on the write stream' do
expect(stream.options[:chunk_size]).to eq(stream_options[:chunk_size])
end
context 'when there is a chunk size set on the FSBucket' do
let(:options) do
{ chunk_size: 100 }
end
let(:fs) do
described_class.new(authorized_client.database, options)
end
it 'uses the chunk size set on the write stream' do
expect(stream.options[:chunk_size]).to eq(stream_options[:chunk_size])
end
end
end
context 'when a file metadata option is specified' do
let(:stream_options) do
{ metadata: { some_field: 1 } }
end
it 'sets the file metadata option on the write stream' do
expect(stream.options[:metadata]).to eq(stream_options[:metadata])
end
end
context 'when a content type option is specified' do
let(:stream_options) do
{ content_type: 'text/plain' }
end
it 'sets the content type on the write stream' do
expect(stream.options[:content_type]).to eq(stream_options[:content_type])
end
end
context 'when a aliases option is specified' do
let(:stream_options) do
{ aliases: [ 'another-name.txt' ] }
end
it 'sets the alias option on the write stream' do
expect(stream.options[:aliases]).to eq(stream_options[:aliases])
end
end
end
end
end
|