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
|
require 'spec_helper'
describe Mongo::Grid::FSBucket::Stream::Read do
let(:fs_options) do
{ }
end
let(:fs) do
authorized_client.database.fs(fs_options)
end
let(:options) do
{ file_id: file_id }
end
let(:filename) do
'specs.rb'
end
let!(:file_id) do
fs.upload_from_stream(filename, File.open(__FILE__))
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
let(:stream) do
described_class.new(fs, options)
end
describe '#initialize' do
it 'sets the file id' do
expect(stream.file_id).to eq(file_id)
end
it 'sets the fs object' do
expect(stream.fs).to eq(fs)
end
context 'when there is a read preference set on the FSBucket' do
let(:fs_options) do
{ read: { mode: :secondary } }
end
it 'uses the read preference of the fs as a default' do
expect(stream.read_preference).to eq(fs.read_preference)
end
end
it 'opens a stream' do
expect(stream.close).to eq(file_id)
end
context 'when provided options' do
context 'when provided read preference' do
let(:options) do
{
file_id: file_id,
read: { mode: :primary_preferred }
}
end
it 'sets the read preference' do
expect(stream.read_preference).to eq(options[:read])
end
it 'sets the read preference on the view' do
expect(stream.send(:view).read).to eq(BSON::Document.new(options[:read]))
end
end
context 'when provided a file_id' do
it 'sets the file id' do
expect(stream.file_id).to eq(options[:file_id])
end
end
end
end
describe '#each' do
let(:filename) do
'specs.rb'
end
let!(:file_id) do
fs.upload_from_stream(filename, File.open(__FILE__))
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
let(:fs_options) do
{ chunk_size: 5 }
end
it 'iterates over all the chunks of the file' do
stream.each do |chunk|
expect(chunk).not_to be(nil)
end
end
context 'when the stream is closed' do
before do
stream.close
end
it 'does not allow further iteration' do
expect {
stream.to_a
}.to raise_error(Mongo::Error::ClosedStream)
end
end
context 'when a chunk is found out of order' do
before do
view = stream.fs.chunks_collection.find({ :files_id => file_id }, options).sort(:n => -1)
stream.instance_variable_set(:@view, view)
expect(stream).to receive(:close)
end
it 'raises an exception' do
expect {
stream.to_a
}.to raise_error(Mongo::Error::MissingFileChunk)
end
it 'closes the query' do
begin
stream.to_a
rescue Mongo::Error::MissingFileChunk
end
end
end
context 'when a chunk does not have the expected length' do
before do
stream.send(:file_info)
stream.instance_variable_get(:@file_info).document['chunkSize'] = 4
expect(stream).to receive(:close)
end
it 'raises an exception' do
expect {
stream.to_a
}.to raise_error(Mongo::Error::UnexpectedChunkLength)
end
it 'closes the query' do
begin
stream.to_a
rescue Mongo::Error::UnexpectedChunkLength
end
end
end
context 'when there is no files document found' do
before do
fs.files_collection.delete_many
end
it 'raises an Exception' do
expect{
stream.to_a
}.to raise_exception(Mongo::Error::FileNotFound)
end
end
end
describe '#read' do
let(:filename) do
'specs.rb'
end
let(:file) do
File.open(__FILE__)
end
let(:file_id) do
fs.upload_from_stream(filename, file)
end
after do
fs.files_collection.delete_many
fs.chunks_collection.delete_many
end
it 'returns a string of all data' do
expect(stream.read.size).to eq(file.size)
end
end
describe '#file_info' do
it 'returns a files information document' do
expect(stream.file_info).to be_a(Mongo::Grid::File::Info)
end
end
describe '#close' do
let(:view) do
stream.instance_variable_get(:@view)
end
before do
stream.to_a
end
it 'returns the file id' do
expect(stream.close).to eq(file_id)
end
context 'when the stream is closed' do
before do
stream.to_a
expect(view).to receive(:close_query).and_call_original
end
it 'calls close_query on the view' do
expect(stream.close).to be_a(BSON::ObjectId)
end
end
context 'when the stream is already closed' do
before do
stream.close
end
it 'raises an exception' do
expect {
stream.close
}.to raise_error(Mongo::Error::ClosedStream)
end
end
end
describe '#closed?' do
context 'when the stream is closed' do
before do
stream.close
end
it 'returns true' do
expect(stream.closed?).to be(true)
end
end
context 'when the stream is still open' do
it 'returns false' do
expect(stream.closed?).to be(false)
end
end
end
end
|