File: base_spec.rb

package info (click to toggle)
ruby-roo 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 8,216 kB
  • sloc: ruby: 6,529; xml: 88; makefile: 6
file content (309 lines) | stat: -rw-r--r-- 9,819 bytes parent folder | download
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
require 'spec_helper'

describe Roo::Base do
  let(:klass) do
    Class.new(Roo::Base) do
      def initialize(filename, data = {})
        super(filename)
        @data ||= data
      end

      def read_cells(sheet = default_sheet)
        return if @cells_read[sheet]
        type_map = { String => :string, Date => :date, Numeric => :float }

        @cell[sheet] = @data
        @cell_type[sheet] = Hash[@data.map { |k, v| [k, type_map.find {|type,_| v.is_a?(type) }.last ] }]
        @first_row[sheet] = @data.map { |k, _| k[0] }.min
        @last_row[sheet] = @data.map { |k, _| k[0] }.max
        @first_column[sheet] = @data.map { |k, _| k[1] }.min
        @last_column[sheet] = @data.map { |k, _| k[1] }.max
        @cells_read[sheet] = true
      end

      def cell(row, col, sheet = nil)
        sheet ||= default_sheet
        read_cells(sheet)
        @cell[sheet][[row, col]]
      end

      def celltype(row, col, sheet = nil)
        sheet ||= default_sheet
        read_cells(sheet)
        @cell_type[sheet][[row, col]]
      end

      def sheets
        ['my_sheet', 'blank sheet']
      end
    end
  end

  let(:spreadsheet_data) do
    {
      [3, 1] => 'Header',

      [5, 1] => Date.civil(1961, 11, 21),

      [8, 3] => 'thisisc8',
      [8, 7] => 'thisisg8',

      [12, 1] => 41.0,
      [12, 2] => 42.0,
      [12, 3] => 43.0,
      [12, 4] => 44.0,
      [12, 5] => 45.0,

      [15, 3] => 43.0,
      [15, 4] => 44.0,
      [15, 5] => 45.0,

      [16, 2] => '"Hello world!"',
      [16, 3] => 'forty-three',
      [16, 4] => 'forty-four',
      [16, 5] => 'forty-five'
    }
  end

  let(:spreadsheet) { klass.new('some_file', spreadsheet_data) }

  describe '#uri?' do
    it 'should return true when passed a filename starting with http(s)://' do
      expect(spreadsheet.send(:uri?, 'http://example.com/')).to be_truthy
      expect(spreadsheet.send(:uri?, 'https://example.com/')).to be_truthy
    end

    it 'should return false when passed a filename which does not start with http(s)://' do
      expect(spreadsheet.send(:uri?, 'example.com')).to be_falsy
    end

    it 'should return false when passed non-String object such as Tempfile' do
      expect(spreadsheet.send(:uri?, Tempfile.new('test'))).to be_falsy
    end
  end

  describe '#set' do
    it 'should not update cell when setting an invalid type' do
      spreadsheet.set(1, 1, 1)
      expect { spreadsheet.set(1, 1, :invalid_type) }.to raise_error(ArgumentError)
      expect(spreadsheet.cell(1, 1)).to eq(1)
      expect(spreadsheet.celltype(1, 1)).to eq(:float)
    end
  end

  describe '#first_row' do
    it 'should return the first row' do
      expect(spreadsheet.first_row).to eq(3)
    end
  end

  describe '#last_row' do
    it 'should return the last row' do
      expect(spreadsheet.last_row).to eq(16)
    end
  end

  describe '#first_column' do
    it 'should return the first column' do
      expect(spreadsheet.first_column).to eq(1)
    end
  end

  describe '#first_column_as_letter' do
    it 'should return the first column as a letter' do
      expect(spreadsheet.first_column_as_letter).to eq('A')
    end
  end

  describe '#last_column' do
    it 'should return the last column' do
      expect(spreadsheet.last_column).to eq(7)
    end
  end

  describe '#last_column_as_letter' do
    it 'should return the last column as a letter' do
      expect(spreadsheet.last_column_as_letter).to eq('G')
    end
  end

  describe "#row" do
    it "should return the specified row" do
      expect(spreadsheet.row(12)).to eq([41.0, 42.0, 43.0, 44.0, 45.0, nil, nil])
      expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', "forty-three", "forty-four", "forty-five", nil, nil])
    end

    it "should return the specified row if default_sheet is set by a string" do
      spreadsheet.default_sheet = "my_sheet"
      expect(spreadsheet.row(12)).to eq([41.0, 42.0, 43.0, 44.0, 45.0, nil, nil])
      expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', "forty-three", "forty-four", "forty-five", nil, nil])
    end

    it "should return the specified row if default_sheet is set by an integer" do
      spreadsheet.default_sheet = 0
      expect(spreadsheet.row(12)).to eq([41.0, 42.0, 43.0, 44.0, 45.0, nil, nil])
      expect(spreadsheet.row(16)).to eq([nil, '"Hello world!"', "forty-three", "forty-four", "forty-five", nil, nil])
    end
  end

  describe '#row_with' do
    context 'with a matching header row' do
      it 'returns the row number' do
        expect(spreadsheet.row_with([/Header/])). to eq 3
      end
    end

    context 'without a matching header row' do
      it 'raises an error' do
        expect { spreadsheet.row_with([/Missing Header/]) }.to \
          raise_error(Roo::HeaderRowNotFoundError)
      end

      it 'returns missing headers' do
        expect { spreadsheet.row_with([/Header/, /Missing Header 1/, /Missing Header 2/]) }.to \
          raise_error(Roo::HeaderRowNotFoundError, '[/Missing Header 1/, /Missing Header 2/]')
      end
    end
  end

  describe '#empty?' do
    it 'should return true when empty' do
      expect(spreadsheet.empty?(1, 1)).to be_truthy
      expect(spreadsheet.empty?(8, 3)).to be_falsy
      expect(spreadsheet.empty?('A', 11)).to be_truthy
      expect(spreadsheet.empty?('A', 12)).to be_falsy
    end
  end

  describe '#reload' do
    it 'should return reinitialize the spreadsheet' do
      spreadsheet.reload
      expect(spreadsheet.instance_variable_get(:@cell).empty?).to be_truthy
    end
  end

  describe '#each_with_pagename' do
    context 'when block given' do
      it 'iterate with sheet and sheet_name' do
        sheet_names = []
        spreadsheet.each_with_pagename do |sheet_name, sheet|
          sheet_names << sheet_name
        end
        expect(sheet_names).to eq ['my_sheet', 'blank sheet']
      end
    end

    context 'when called without block' do
      it 'should return an enumerator with all the rows' do
        each_with_pagename = spreadsheet.each_with_pagename
        expect(each_with_pagename).to be_a(Enumerator)
        expect(each_with_pagename.to_a.last).to eq([spreadsheet.default_sheet, spreadsheet])
      end
    end
  end

  describe '#each' do
    it 'should return an enumerator with all the rows' do
      each = spreadsheet.each
      expect(each).to be_a(Enumerator)
      expect(each.to_a.last).to eq([nil, '"Hello world!"', 'forty-three', 'forty-four', 'forty-five', nil, nil])
    end
  end

  describe "#default_sheet=" do
    it "should correctly set the default sheet if passed a string" do
      spreadsheet.default_sheet = "my_sheet"
      expect(spreadsheet.default_sheet).to eq("my_sheet")
    end

    it "should correctly set the default sheet if passed an integer" do
      spreadsheet.default_sheet = 0
      expect(spreadsheet.default_sheet).to eq("my_sheet")
    end

    it "should correctly set the default sheet if passed an integer for the second sheet" do
      spreadsheet.default_sheet = 1
      expect(spreadsheet.default_sheet).to eq("blank sheet")
    end

    it "should raise an error if passed a sheet that does not exist as an integer" do
      expect { spreadsheet.default_sheet = 10 }.to raise_error RangeError
    end

    it "should raise an error if passed a sheet that does not exist as a string" do
      expect { spreadsheet.default_sheet = "does_not_exist" }.to raise_error RangeError
    end
  end

  describe '#to_yaml' do
    it 'should convert the spreadsheet to yaml' do
      expect(spreadsheet.to_yaml({}, 5, 1, 5, 1)).to eq("--- \n" + yaml_entry(5, 1, 'date', '1961-11-21'))
      expect(spreadsheet.to_yaml({}, 8, 3, 8, 3)).to eq("--- \n" + yaml_entry(8, 3, 'string', 'thisisc8'))

      expect(spreadsheet.to_yaml({}, 12, 3, 12, 3)).to eq("--- \n" + yaml_entry(12, 3, 'float', 43.0))

      expect(spreadsheet.to_yaml({}, 12, 3, 12)).to eq(
        "--- \n" + yaml_entry(12, 3, 'float', 43.0) +
        yaml_entry(12, 4, 'float', 44.0) +
        yaml_entry(12, 5, 'float', 45.0))

      expect(spreadsheet.to_yaml({}, 12, 3)).to eq(
      "--- \n" + yaml_entry(12, 3, 'float', 43.0) +
        yaml_entry(12, 4, 'float', 44.0) +
        yaml_entry(12, 5, 'float', 45.0) +
        yaml_entry(15, 3, 'float', 43.0) +
        yaml_entry(15, 4, 'float', 44.0) +
        yaml_entry(15, 5, 'float', 45.0) +
        yaml_entry(16, 3, 'string', 'forty-three') +
        yaml_entry(16, 4, 'string', 'forty-four') +
        yaml_entry(16, 5, 'string', 'forty-five'))
    end
  end

  let(:expected_csv) do
    <<EOS
,,,,,,
,,,,,,
"Header",,,,,,
,,,,,,
1961-11-21,,,,,,
,,,,,,
,,,,,,
,,"thisisc8",,,,"thisisg8"
,,,,,,
,,,,,,
,,,,,,
41,42,43,44,45,,
,,,,,,
,,,,,,
,,43,44,45,,
,"""Hello world!""","forty-three","forty-four","forty-five",,
EOS
  end

  let(:expected_csv_with_semicolons) { expected_csv.gsub(/\,/, ';') }

  describe '#to_csv' do
    it 'should convert the spreadsheet to csv' do
      expect(spreadsheet.to_csv).to eq(expected_csv)
    end

    it 'should convert the spreadsheet to csv using the separator when is passed on the parameter' do
      expect(spreadsheet.to_csv(separator: ';')).to eq(expected_csv_with_semicolons)
    end

    context 'should contains the deprecation warning message' do
      it 'convert the spreadsheet to csv using the separator' do
        converting =-> { spreadsheet.to_csv(nil, ';') }
        expect(converting.call).to eq(expected_csv_with_semicolons)
        expect(&converting).to output(/DEPRECATION.*:separator\b/).to_stderr
      end

      it 'be able to arguments: filename, separator, sheet' do
        converting =-> { spreadsheet.to_csv(nil, ';', spreadsheet.default_sheet) }
        expect(converting.call).to eq(expected_csv_with_semicolons)
        expect(&converting).to output(/DEPRECATION.*:sheet\b/).to_stderr
      end
    end
  end
end