-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer_spec.rb
70 lines (57 loc) · 1.71 KB
/
writer_spec.rb
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
# frozen_string_literal: true
RSpec.describe TableStructure::CSV::Writer do
let(:schema) do
::Micro::UserTableSchema.new
end
let(:inner_writer_options) do
{ header: { context: {}, step: 10 } }
end
let(:csv_writer) do
described_class.new(schema, **inner_writer_options.merge(csv_writer_options))
end
let(:handler) { ->(values) { values } }
let(:items) do
[
{ id: 1, name: 'a' },
{ id: 2, name: 'b' }
]
end
describe '#write' do
before do
require 'csv'
writer = double('TableStructure::Writer')
expect(TableStructure::Writer).to receive(:new)
.with(schema, header: { context: {}, step: 10 })
.and_return(writer)
expect(writer).to receive(:write)
.with(items, to: instance_of(::CSV)) do |&block|
expect(block).to eq handler
end
end
let(:output) { [] }
context 'when `bom: true` is specified' do
let(:csv_writer_options) { { bom: true } }
it 'writes items' do
csv_writer.write(items, to: output, &handler)
expect(output).to eq ["\uFEFF"]
end
end
context 'when `bom: false` is specified' do
let(:csv_writer_options) { { bom: false } }
it 'writes items' do
csv_writer.write(items, to: output, &handler)
expect(output).to be_empty
end
end
context 'when `csv_options` is specified' do
let(:csv_options) { { col_sep: ',' } }
let(:csv_writer_options) { { csv_options: csv_options } }
it 'passes specified csv_options' do
expect(::CSV).to receive(:new)
.with(output, **csv_options)
.and_call_original
csv_writer.write(items, to: output, &handler)
end
end
end
end