File: file_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 (191 lines) | stat: -rw-r--r-- 3,917 bytes parent folder | download | duplicates (4)
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
require 'spec_helper'

describe Mongo::Grid::File do

  describe '#==' do

    let(:file) do
      described_class.new('test', :filename => 'test.txt')
    end

    context 'when the object is not a file' do

      it 'returns false' do
        expect(file).to_not eq('testing')
      end
    end

    context 'when the object is a file' do

      context 'when the objects are equal' do

        it 'returns true' do
          expect(file).to eq(file)
        end
      end

      context 'when the objects are not equal' do

        let(:other) do
          described_class.new('tester', :filename => 'test.txt')
        end

        it 'returns false' do
          expect(file).to_not eq(other)
        end
      end
    end
  end

  describe '#initialize' do

    let(:data_size) do
      Mongo::Grid::File::Chunk::DEFAULT_SIZE * 3
    end

    let(:data) do
      'testing'
    end

    before do
      (1..data_size).each{ |i| data << '1' }
    end

    context 'when provided data and file information' do

      let(:file) do
        described_class.new(data, :filename => 'test.txt')
      end

      it 'creates the chunks' do
        expect(file.chunks.size).to eq(4)
      end

      it 'returns data' do
        expect(file.data).to eq(data)
      end
    end

    context 'when data is a ruby file' do

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

      let(:data) do
        ruby_file.read
      end

      let(:file) do
        described_class.new(data, :filename => File.basename(ruby_file.path))
      end

      it 'creates the chunks' do
        expect(file.chunks.size).to eq(4)
      end

      it 'returns data' do
        expect(file.data).to eq(data)
      end
    end

    context 'when data is an IO object' do

      let(:io) do
        StringIO.new('testing')
      end

      let(:file) do
        described_class.new(io, filename: "test.txt")
      end

      it 'creates the chunks' do
        expect(file.chunks).not_to be_empty
      end

      it 'returns data' do
        expect(file.data).to eq 'testing'
      end
    end

    context 'when using idiomatic ruby field names' do

      let(:time) do
        Time.now.utc
      end

      let(:file) do
        described_class.new(
          data,
          :filename => 'test.txt',
          :chunk_size => 100,
          :upload_date => time,
          :content_type => 'text/plain'
        )
      end

      it 'normalizes the chunk size name' do
        expect(file.chunk_size).to eq(100)
      end

      it 'normalizes the upload date name' do
        expect(file.upload_date).to eq(time)
      end

      it 'normalizes the content type name' do
        expect(file.content_type).to eq('text/plain')
      end
    end

    context 'when provided chunks and file information' do

      let(:file_id) do
        BSON::ObjectId.new
      end

      let(:info) do
        BSON::Document.new(
          :_id => file_id,
          :uploadDate => Time.now.utc,
          :filename => 'test.txt',
          :chunkSize => Mongo::Grid::File::Chunk::DEFAULT_SIZE,
          :length => data.length,
          :contentType => Mongo::Grid::File::Info::DEFAULT_CONTENT_TYPE
        )
      end

      let(:chunks) do
        Mongo::Grid::File::Chunk.split(
          data, Mongo::Grid::File::Info.new(info)
        ).map{ |chunk| chunk.document }
      end

      let(:file) do
        described_class.new(chunks, info)
      end

      it 'sets the chunks' do
        expect(file.chunks.size).to eq(4)
      end

      it 'assembles to data' do
        expect(file.data).to eq(data)
      end

      it 'sets the file information' do
        expect(file.info.id).to eq(info[:_id])
      end
    end
  end

  describe '#inspect' do

    let(:file) do
      described_class.new('Hi', :filename => 'test.txt')
    end

    it 'includes the filename' do
      expect(file.inspect).to include('test.txt')
    end
  end
end