File: write_spec.rb

package info (click to toggle)
ruby-mongo 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,332 kB
  • sloc: ruby: 45,579; makefile: 5
file content (502 lines) | stat: -rw-r--r-- 11,721 bytes parent folder | download | duplicates (3)
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
require 'spec_helper'

describe Mongo::Grid::FSBucket::Stream::Write do

  let(:file) do
    File.open(__FILE__)
  end

  let(:file2) do
    File.open(__FILE__)
  end

  let(:fs_options) do
    { }
  end

  let(:fs) do
    authorized_client.database.fs(fs_options)
  end

  let(:filename) do
    'specs.rb'
  end

  let(:extra_options) do
    { }
  end

  let(:options) do
    { filename: filename }.merge(extra_options)
  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 be_a(BSON::ObjectId)
    end

    it 'sets the fs object' do
      expect(stream.fs).to eq(fs)
    end

    it 'opens a stream' do
      expect(stream.close).to be_a(BSON::ObjectId)
    end

    context 'when the fs has a write concern', if: standalone? do

      let(:fs_options) do
        { write: INVALID_WRITE_CONCERN }
      end

      it 'uses the write concern of the fs as a default' do
        expect{
          stream.close
        }.to raise_exception(Mongo::Error::OperationFailure)
      end
    end

    context 'when the fs does not have a write concern' do

      let(:fs) do
        authorized_client.with(write: nil).database.fs
      end

      it 'uses the write concern default at the operation level' do
        expect(stream.write(file).closed?).to eq(false)
      end
    end

    context 'when provided options' do

      context 'when provided a write option' do

        let(:extra_options) do
          {
            write: INVALID_WRITE_CONCERN
          }
        end

        let(:expected) do
          Mongo::WriteConcern.get(options[:write]).options
        end

        it 'sets the write concern' do
          expect(stream.write_concern.options).to eq(expected)
        end

        context 'when chunks are inserted' do

          it 'uses that write concern' do
            expect(stream.send(:chunks_collection).write_concern.options[:w]).to eq(expected[:w])
          end
        end

        context 'when a files document is inserted' do

          it 'uses that write concern' do
            expect(stream.send(:files_collection).write_concern.options[:w]).to eq(expected[:w])
          end
        end
      end

      context 'when provided a metadata document' do

        let(:options) do
          {
              metadata: { 'some_field' => 'test-file' }
          }
        end

        it 'sets the metadata document' do
          expect(stream.send(:file_info).metadata).to eq(options[:metadata])
        end
      end

      context 'when provided a chunk size option' do

        let(:options) do
          {
              chunk_size: 50
          }
        end

        it 'sets the chunk size' do
          expect(stream.send(:file_info).chunk_size).to eq(options[:chunk_size])
        end

        context 'when chunk size is also set on the FSBucket object' do

          let(:fs_options) do
            {
                chunk_size: 100
            }
          end

          it 'uses the write stream options' do
            expect(stream.send(:file_info).chunk_size).to eq(options[:chunk_size])
          end
        end
      end

      context 'when provided a content type option' do

        let(:options) do
          {
              content_type: 'text/plain'
          }
        end

        it 'sets the content type' do
          expect(stream.send(:file_info).content_type).to eq(options[:content_type])
        end
      end

      context 'when provided an aliases option' do

        let(:options) do
          {
              aliases: [ 'testing-file' ]
          }
        end

        it 'sets the aliases' do
          expect(stream.send(:file_info).document[:aliases]).to eq(options[:aliases])
        end
      end

      context 'when provided a file_id option' do

        let(:options) do
          {
            file_id: 'Custom ID'
          }
        end

        it 'assigns the stream the file id' do
          expect(stream.file_id).to eq(options[:file_id])
        end
      end
    end
  end

  describe '#write' do

    after do
      fs.files_collection.delete_many
      fs.chunks_collection.delete_many
    end

    let(:file_from_db) do
      fs.find_one(filename: filename)
    end

    context 'when the stream is written to' do

      before do
        stream.write(file)
      end

      it 'does not close the stream' do
        expect(stream).not_to receive(:close)
      end
    end

    context 'when indexes need to be ensured' do

      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
          stream.write(file)
        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 write is called more than once' do

          before do
            expect(fs).not_to receive(:ensure_indexes!)
          end

          it 'only creates the indexes the first time' do
            stream.write(file2)
          end
        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)
          stream.write(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 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 {
            stream.write(file)
          }.to raise_error(Mongo::Error::OperationFailure)
        end
      end
    end

    context 'when provided an io stream' do

      context 'when no file id is specified' do

        before do
          stream.write(file)
          stream.close
        end

        it 'writes the contents of the stream' do
          expect(file_from_db.data.size).to eq(file.size)
        end

        it 'updates the length written' do
          expect(stream.send(:file_info).document['length']).to eq(file.size)
        end

        it 'updates the position (n)' do
          expect(stream.instance_variable_get(:@n)).to eq(1)
        end
      end

      context 'when a custom file id is provided' do

        let(:extra_options) do
          {
            file_id: 'Custom ID'
          }
        end

        let!(:id) do
          stream.write(file)
          stream.close
        end

        it 'writes the contents of the stream' do
          expect(file_from_db.data.size).to eq(file.size)
        end

        it 'updates the length written' do
          expect(stream.send(:file_info).document['length']).to eq(file.size)
        end

        it 'updates the position (n)' do
          expect(stream.instance_variable_get(:@n)).to eq(1)
        end

        it 'uses the custom file id' do
          expect(id).to eq(options[:file_id])
        end
      end

      context 'when the user file contains no data' do

        before do
          stream.write(file)
          stream.close
        end

        let(:file) do
          StringIO.new('')
        end

        let(:files_coll_doc) do
          stream.fs.files_collection.find(filename: filename).to_a.first
        end

        let(:chunks_documents) do
          stream.fs.chunks_collection.find(files_id: stream.file_id).to_a
        end

        it 'creates a files document' do
          expect(files_coll_doc).not_to be(nil)
        end

        it 'sets length to 0 in the files document' do
          expect(files_coll_doc['length']).to eq(0)
        end

        it 'does not insert any chunks' do
          expect(file_from_db.data.size).to eq(file.size)
        end
      end
    end

    context 'when the stream is written to multiple times' do

      before do
        stream.write(file)
        stream.write(file2)
        stream.close
      end

      it 'writes the contents of the stream' do
        expect(file_from_db.data.size).to eq(file.size * 2)
      end

      it 'updates the length written' do
        expect(stream.send(:file_info).document['length']).to eq(file.size * 2)
      end

      it 'updates the position (n)' do
        expect(stream.instance_variable_get(:@n)).to eq(2)
      end
    end

    context 'when the stream is closed' do

      before do
        stream.close
      end

      it 'does not allow further writes' do
        expect {
          stream.write(file)
        }.to raise_error(Mongo::Error::ClosedStream)
      end
    end
  end

  describe '#close' do

    let(:file_content) do
      File.open(__FILE__).read
    end

    context 'when close is called on the stream' do

      before do
        stream.write(file)
      end

      let(:file_id) do
        stream.file_id
      end

      it 'returns the file id' do
        expect(stream.close).to eq(file_id)
      end
    end

    context 'when the stream is closed' do

      before do
        stream.write(file)
        stream.close
      end

      let(:md5) do
        Digest::MD5.new.update(file_content).hexdigest
      end

      let(:files_coll_doc) do
        stream.fs.files_collection.find(filename: filename).to_a.first
      end

      it 'inserts a file documents in the files collection' do
        expect(files_coll_doc['_id']).to eq(stream.file_id)
      end

      it 'updates the length in the files collection file document' do
        expect(stream.send(:file_info).document[:length]).to eq(file.size)
      end

      it 'updates the md5 in the files collection file document' do
        expect(stream.send(:file_info).document[:md5]).to eq(md5)
      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