-
Notifications
You must be signed in to change notification settings - Fork 67
/
api_data_spec.rb
172 lines (152 loc) · 4.64 KB
/
api_data_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
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
require 'spec_helper'
describe Squib::Deck do
context '#csv' do
it 'loads basic csv data' do
expect(Squib.csv(file: csv_file('basic.csv')).to_h.to_h).to eq({
'h1' => [1, 3],
'h2' => [2, 4]
})
end
it 'collapses duplicate columns and warns' do
expect(Squib.logger).to receive(:warn)
.with('CSV duplicated the following column keys: h1,h1')
expect(Squib.csv(file: csv_file('dup_cols.csv')).to_h.to_h).to eq({
'h1' => [1, 3],
'h2' => [5, 7],
'H2' => [6, 8],
'h3' => [9, 10],
})
end
it 'strips spaces by default' do
expect(Squib.csv(file: csv_file('with_spaces.csv')).to_h).to eq({
'With Spaces' => ['a b c', 3],
'h2' => [2, 4],
'h3' => [3, nil]
})
end
it 'skips space stripping if told to' do
expect(Squib.csv(strip: false, file: csv_file('with_spaces.csv')).to_h).to eq({
' With Spaces ' => ['a b c ', 3],
'h2' => [2, 4],
'h3' => [3, nil]
})
end
it 'explodes quantities' do
expect(Squib.csv(file: csv_file('qty.csv')).to_h).to eq({
'Name' => %w(Ha Ha Ha Ho),
'Qty' => [3, 3, 3, 1],
})
end
it 'explodes quantities on specified header' do
expect(Squib.csv(explode: 'Quantity', file: csv_file('qty_named.csv')).to_h).to eq({
'Name' => %w(Ha Ha Ha Ho),
'Quantity' => [3, 3, 3, 1],
})
end
it 'loads inline data' do
hash = Squib.csv(data: "h1,h2\n1,2\n3,4")
expect(hash.to_h).to eq({
'h1' => [1, 3],
'h2' => [2, 4]
})
end
it 'loads csv with newlines' do
hash = Squib.csv(file: csv_file('newline.csv'))
expect(hash.to_h).to eq({
'title' => ['Foo'],
'level' => [1],
'notes' => ["a\nb"]
})
end
it 'loads custom CSV options' do
hash = Squib.csv(file: csv_file('custom_opts.csv'),
col_sep: '-', quote_char: '|')
expect(hash.to_h).to eq({
'x' => ['p'],
'y' => ['q-r']
})
end
it 'yields to block when given' do
data = Squib.csv(file: csv_file('basic.csv')) do |header, value|
case header
when 'h1'
value * 2
else
'ha'
end
end
expect(data.to_h).to eq({
'h1' => [2, 6],
'h2' => %w(ha ha),
})
end
it 'replaces newlines whenever its a string' do
data = Squib.csv(file: csv_file('yield.csv')) do |header, value|
if value.respond_to? :gsub
value.gsub '%n', "\n"
else
value
end
end
expect(data.to_h).to eq({
'a' => ["foo\nbar", 1],
'b' => [1, "blah\n"],
})
end
end
context '#xlsx' do
it 'loads basic xlsx data' do
expect(Squib.xlsx(file: xlsx_file('basic.xlsx')).to_h).to eq({
'Name' => %w(Larry Curly Mo),
'General Number' => %w(1 2 3), # general types always get loaded as strings with no conversion
'Actual Number' => [4.0, 5.0, 6.0], # numbers get auto-converted to integers
})
end
it 'loads xlsx with formulas' do
expect(Squib.xlsx(file: xlsx_file('formulas.xlsx')).to_h).to eq({
'A' => %w(1 2),
'B' => %w(3 4),
'Sum' => %w(4 6),
})
end
it 'loads xlsm files with macros' do
expect(Squib.xlsx(file: xlsx_file('with_macros.xlsm')).to_h).to eq({
'foo' => %w(8 10),
'bar' => %w(9 11),
})
end
it 'strips whitespace by default' do
expect(Squib.xlsx(file: xlsx_file('whitespace.xlsx')).to_h).to eq({
'With Whitespace' => ['foo', 'bar', 'baz'],
})
end
it 'does not strip whitespace when specified' do
expect(Squib.xlsx(file: xlsx_file('whitespace.xlsx'), strip: false).to_h).to eq({
' With Whitespace ' => ['foo ', ' bar', ' baz '],
})
end
it 'yields to block when given' do
data = Squib.xlsx(file: xlsx_file('basic.xlsx')) do |header, value|
case header
when 'Name'
'he'
when 'Actual Number'
value * 2
else
'ha'
end
end
expect(data.to_h).to eq({
'Name' => %w(he he he),
'General Number' => %w(ha ha ha),
'Actual Number' => [8.0, 10.0, 12.0],
})
end
it 'explodes quantities' do
expect(Squib.xlsx(explode: 'Qty', file: xlsx_file('explode_quantities.xlsx')).to_h).to eq({
'Name' => ['Zergling', 'Zergling', 'Zergling', 'High Templar'],
'Qty' => %w(3 3 3 1),
})
end
end
end