-
Notifications
You must be signed in to change notification settings - Fork 44
/
utils_test.rb
210 lines (162 loc) · 6.39 KB
/
utils_test.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
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
require 'test_helper'
class UtilsTest < ActiveSupport::TestCase
klass = SpreadsheetArchitect::Utils
def setup
@options = {
header_style: {background_color: 'AAAAAA', color: 'FFFFFF', align: :center, font_name: 'Arial', font_size: 10, bold: false, italic: false, underline: false},
row_style: {background_color: nil, color: '000000', align: :left, font_name: 'Arial', font_size: 10, bold: false, italic: false, underline: false},
sheet_name: 'My Project Export',
column_styles: [],
range_styles: [],
merges: [],
borders: [],
column_types: [],
column_widths: [],
headers: ['test1', 'test2','test3'],
data: [
['row1'],
['row2 c1', 'row2 c2'],
['the','data']
]
}
end
def teardown
end
test "get_cell_data" do
assert klass.get_cell_data(@options, SpreadsheetArchitect)
assert klass.get_cell_data(@options, Post)
assert klass.get_options({}, SpreadsheetArchitect)
assert_raise SpreadsheetArchitect::Exceptions::MultipleDataSourcesError do
klass.get_cell_data(@options.merge(instances: []), SpreadsheetArchitect)
end
### using Data option
output = klass.get_cell_data(@options.merge(headers: true), SpreadsheetArchitect)
assert_equal [[]], output[:headers]
output = klass.get_cell_data(@options.merge(column_types: nil), SpreadsheetArchitect)
assert_nil output[:column_types]
output = klass.get_cell_data(@options.merge(column_types: []), SpreadsheetArchitect)
assert_nil output[:column_types]
output = klass.get_cell_data(@options.merge(column_types: [:string]), SpreadsheetArchitect)
assert_equal output[:column_types], [:string]
headers = [[1,2,3], [3,4,5]]
output = klass.get_cell_data(@options.merge(headers: headers), SpreadsheetArchitect)
assert_equal headers, output[:headers]
### Using instances option
output = klass.get_cell_data(@options.merge(data: nil), Post.all)
assert output[:instances].is_a?(Array)
output = klass.get_cell_data(@options.merge(data: nil), Post.limit(0))
assert output[:instances].is_a?(Array)
assert_raise SpreadsheetArchitect::Exceptions::NoDataError do
klass.get_cell_data(@options.merge(data: nil, instances: nil), SpreadsheetArchitect)
end
output = klass.get_cell_data(@options.merge(data: nil, instances: [PlainRubyObject.new]), SpreadsheetArchitect)
assert output[:instances].count == 1
end
test "get_options" do
### Empty
assert_not_empty klass.get_options({}, SpreadsheetArchitect)
### using SpreadsheetArchitect
assert_not_empty klass.get_options(@options, SpreadsheetArchitect)
### with model defaults via SPREADSHEET_OPTIONS
assert defined?(CustomPost::SPREADSHEET_OPTIONS)
assert klass.get_options(@options, CustomPost)
### without model defaults via SPREADSHEET_OPTIONS
assert_not_empty klass.get_options(@options, Post)
### without :headers removes :header_style
assert_equal klass.get_options({header_style: false, headers: false}, SpreadsheetArchitect)[:header_style], false
### sets :sheet_name if needed
assert_equal klass.get_options({sheet_name: false}, SpreadsheetArchitect)[:sheet_name], 'Sheet1'
### sets :sheet_name if needed, using pluralized only when using Rails
assert_equal klass.get_options({sheet_name: false}, Post)[:sheet_name], 'Posts'
end
test "convert_styles_to_ods" do
ods_styles = klass.convert_styles_to_ods({
background_color: '333333',
color: '000000',
align: true,
bold: true,
font_size: 14,
italic: true,
underline: true,
test: true
})
assert_equal(ods_styles, {
'cell' => {
'background-color' => '#333333'
},
'text' => {
'color' => '#000000',
'align' => true,
'font-weight' => 'bold',
'font-size' => 14,
'font-style' => 'italic',
'text-underline-type' => 'single',
'text-underline-style' => 'solid'
}
})
end
test "is_ar_model" do
assert klass.is_ar_model?(Post)
assert_not klass.is_ar_model?(SpreadsheetArchitect)
end
test "str_titleize" do
assert_equal(klass.str_titleize('my_project_export'), 'My Project Export')
assert_equal(klass.str_titleize('My Project Export'), 'My Project Export')
assert_equal(klass.str_titleize('TBS report'), 'TBS Report')
end
test "check_option_type" do
klass.check_option_type(@options, :data, Array)
klass.check_option_type(@options, :foo, Array)
assert_raise SpreadsheetArchitect::Exceptions::OptionTypeError do
klass.check_option_type({foo: :bar}, :foo, Array)
end
end
test "verify_option_types" do
klass.verify_option_types(@options)
assert_raise SpreadsheetArchitect::Exceptions::OptionTypeError do
klass.verify_option_types(@options.merge({column_widths: :foobar}))
end
end
test "stringify_keys" do
hash = klass.stringify_keys({})
assert_empty hash
hash = klass.stringify_keys({foo: :bar})
assert_nil hash[:foo]
assert_equal hash['foo'], :bar
hash = klass.stringify_keys({foo: :bar, 'bar' => :foo})
assert_nil hash[:foo]
assert_equal hash['foo'], :bar
hash = klass.stringify_keys({foo: {foo: :bar}})
assert_nil hash[:foo]
assert_equal hash['foo']['foo'], :bar
hash = klass.stringify_keys({foo: {foo: {foo: :bar}}})
assert_nil hash[:foo]
assert_equal hash['foo']['foo']['foo'], :bar
hash = klass.stringify_keys({foo: {foo: {foo: {foo: :bar}}}})
assert_nil hash[:foo]
assert_equal hash['foo']['foo']['foo']['foo'], :bar
end
test "symbolize_keys" do
hash = klass.symbolize_keys({})
assert_empty hash
hash = klass.symbolize_keys({'foo' => :bar})
assert_nil hash['foo']
assert_equal hash[:foo], :bar
hash = klass.symbolize_keys({'foo' => :bar, bar: :foo})
assert_nil hash['foo']
assert_equal hash[:foo], :bar
hash = klass.symbolize_keys({'foo' => {'foo' => :bar}})
assert_nil hash['foo']
assert_equal hash[:foo][:foo], :bar
hash = klass.symbolize_keys({'foo' => {'foo' => {'foo' => :bar}}})
assert_nil hash['foo']
assert_equal hash[:foo][:foo][:foo], :bar
end
test "hash_array_symbolize_keys" do
array = klass.hash_array_symbolize_keys([])
assert_empty array
array = klass.hash_array_symbolize_keys([{'foo' => :bar}])
assert_nil array[0]['foo']
assert_equal array[0][:foo], :bar
end
end