export_to_spreadsheet 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/export_to_spreadsheet.rb +2 -2
- data/lib/sales_clic_exporter/base.rb +1 -2
- metadata +3271 -6620
- data/CHANGELOG +0 -3
- data/MIT-LICENSE +0 -20
- data/Manifest +0 -3354
- data/README.rdoc +0 -267
- data/Rakefile +0 -23
- data/export_to_spreadsheet.gemspec +0 -54
- data/init.rb +0 -3
- data/install.rb +0 -2
- data/test/excel_test.rb +0 -308
- data/test/google_spreadsheets_test.rb +0 -337
- data/test/resources/invalid_excel_import_with_duplicates_values.xls +0 -0
- data/test/resources/invalid_file_format.txt +0 -1
- data/test/resources/valid_excel_import.xls +0 -0
- data/test/spreadsheet_to_array_test.rb +0 -30
- data/test/test_helper.rb +0 -47
data/init.rb
DELETED
data/install.rb
DELETED
data/test/excel_test.rb
DELETED
@@ -1,308 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ExcelTest < ActiveSupport::TestCase
|
4
|
-
Sample_string = 'Test chaîne'
|
5
|
-
|
6
|
-
def setup
|
7
|
-
@obj = create_obj
|
8
|
-
#The export initialization process is done in two steps because of rjb which takes a lot of memory :
|
9
|
-
#First we set some variables with the private method prepare_export. We use send to be able to clal this method from outside only in the tests
|
10
|
-
@excel = @obj.send(:prepare_export, :excel)
|
11
|
-
#Then we fork the process when possible and finally load rjb
|
12
|
-
#with the method finish_initialize
|
13
|
-
@excel.finish_initialize
|
14
|
-
end
|
15
|
-
|
16
|
-
def teardown
|
17
|
-
if File.file?(@excel.path)
|
18
|
-
assert File.unlink(@excel.path)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
###################################
|
23
|
-
# Tests sur les arguments
|
24
|
-
###################################
|
25
|
-
|
26
|
-
def test_write_method_without_block_is_ignored
|
27
|
-
assert_equal @excel.write, @excel
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_h_table_with_and_without_options_and_splat_arguments
|
31
|
-
assert_method_returns_self('h_table', [%w{a b c}, %w{d e f}, %w{g h}], ['toto', 'tata', 'tutu'])
|
32
|
-
assert_method_returns_self('h_table', [%w{a b c}, %w{d e f}, %w{g h}], ['toto', 'tata', 'tutu'], {:border_bottom => true})
|
33
|
-
assert_method_returns_self('h_table', [%w{a b c}, %w{d e f}, %w{g h}], 'toto', 'tata', 'tutu')
|
34
|
-
assert_method_returns_self('h_table', [%w{a b c}, %w{d e f}, %w{g h}], 'toto', 'tata', 'tutu', {:border_bottom => true})
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_v_table_with_and_without_splat_arguments
|
38
|
-
assert_method_returns_self('v_table', %w{toto tata tutu}, 'colone 1', 'colone 2', 'colone 3', 'colone 4')
|
39
|
-
assert_method_returns_self('v_table', %w{toto tata tutu}, ['colone 1', 'colone 2', 'colone 3', 'colone 4'])
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_line_with_and_without_splat_arguments
|
43
|
-
assert_method_returns_self('line', 'colone 1', 'colone 2', 'colone 3', 'colone 4')
|
44
|
-
assert_method_returns_self('line', ['colone 1', 'colone 2', 'colone 3', 'colone 4'])
|
45
|
-
assert_method_returns_self('line', 'colone 1')
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_line_without_arguments_throws_exception
|
49
|
-
assert_raise ArgumentError do
|
50
|
-
@excel.line
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def test_h_table_without_values_as_an_array_throws_exception
|
55
|
-
assert_raise ArgumentError do
|
56
|
-
@excel.h_table('test', 'test')
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_newline_can_be_called_without_arguments
|
61
|
-
assert_nothing_raised do
|
62
|
-
@excel.newline
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
######################################################################
|
67
|
-
# On teste les retours pour vérifier que les méthodes sont bien
|
68
|
-
# chainables
|
69
|
-
######################################################################
|
70
|
-
|
71
|
-
def test_title_1_returns_self
|
72
|
-
assert_method_returns_self('title_1', Sample_string)
|
73
|
-
end
|
74
|
-
|
75
|
-
def test_title_2_returns_self
|
76
|
-
assert_method_returns_self('title_2', Sample_string)
|
77
|
-
end
|
78
|
-
|
79
|
-
def test_title_3_returns_self
|
80
|
-
assert_method_returns_self('title_3', Sample_string)
|
81
|
-
end
|
82
|
-
|
83
|
-
def test_h_table_returns_self
|
84
|
-
assert_method_returns_self('h_table', [%w{a b c}, %w{d e f}, %w{g h}], ['toto', 'tata', 'tutu'], {:border_bottom => true})
|
85
|
-
end
|
86
|
-
|
87
|
-
def test_v_table_returns_self
|
88
|
-
assert_method_returns_self('v_table', %w{toto tata tutu}, 'colone 1', 'colone 2', 'colone 3', 'colone 4')
|
89
|
-
end
|
90
|
-
|
91
|
-
def test_line_returns_self
|
92
|
-
assert_method_returns_self('line', 'colone 1', 'colone 2', 'colone 3', 'colone 4')
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_newline_returns_self
|
96
|
-
assert_method_returns_self('newline')
|
97
|
-
end
|
98
|
-
|
99
|
-
def test_freezepane_returns_self
|
100
|
-
assert_method_returns_self('freezepane')
|
101
|
-
end
|
102
|
-
|
103
|
-
def test_write_returns_self
|
104
|
-
assert_method_returns_self('write')
|
105
|
-
end
|
106
|
-
|
107
|
-
######################################################################
|
108
|
-
# On teste sur toutes les méthodes rajoutent bien une ligne
|
109
|
-
######################################################################
|
110
|
-
|
111
|
-
def test_title_1_increments_current_row_index
|
112
|
-
assert_method_increments_current_row_index(1, 'title_1', Sample_string)
|
113
|
-
end
|
114
|
-
|
115
|
-
def test_title_2_increments_current_row_index
|
116
|
-
assert_method_increments_current_row_index(1, 'title_2', Sample_string)
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_title_3_increments_current_row_index
|
120
|
-
assert_method_increments_current_row_index(1, 'title_3', Sample_string)
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_h_table_increments_current_row_index
|
124
|
-
headers = ['toto', 'tata', 'tutu']
|
125
|
-
lines_added = headers.map(&:size).max + 1 # + 1 pour la ligne vide de séparation
|
126
|
-
assert_method_increments_current_row_index(lines_added, 'h_table', [%w{a b c}, %w{d e f}, %w{g h}], ['toto', 'tata', 'tutu'], {:border_bottom => true})
|
127
|
-
end
|
128
|
-
|
129
|
-
def test_v_table_increments_current_row_index
|
130
|
-
headers = %w{toto tata tutu}
|
131
|
-
values = ['colone 1', 'colone 2', 'colone 3', 'colone 4']
|
132
|
-
lines_added = [values.size, headers.size].max
|
133
|
-
assert_method_increments_current_row_index(lines_added, 'v_table', headers, *values)
|
134
|
-
end
|
135
|
-
|
136
|
-
def test_line_increments_current_row_index
|
137
|
-
assert_method_increments_current_row_index(1, 'line', 'colone 1', 'colone 2', 'colone 3', 'colone 4')
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_newline_increments_current_row_index
|
141
|
-
assert_method_increments_current_row_index(1, 'newline')
|
142
|
-
end
|
143
|
-
|
144
|
-
def test_freezepane_does_not_increment_current_row_index
|
145
|
-
row_index = @excel.current_row_index
|
146
|
-
index_expected = row_index # Pas de changement
|
147
|
-
|
148
|
-
@excel.freezepane
|
149
|
-
|
150
|
-
assert_equal index_expected, @excel.current_row_index
|
151
|
-
end
|
152
|
-
|
153
|
-
##########################################
|
154
|
-
# Tests sur la sauvegarde
|
155
|
-
##########################################
|
156
|
-
|
157
|
-
def test_save
|
158
|
-
@excel.line("ligne de test")
|
159
|
-
@excel.save
|
160
|
-
assert File.file?(@excel.path)
|
161
|
-
assert File.unlink(@excel.path) # Suppression du fichier
|
162
|
-
end
|
163
|
-
|
164
|
-
##########################################
|
165
|
-
# Test de lecture des données sur le fichier Excel
|
166
|
-
##########################################
|
167
|
-
|
168
|
-
def test_title_1_writes_correct_values
|
169
|
-
text = 'Titre looooooooooong'
|
170
|
-
# Valeur attendue
|
171
|
-
values = [[text]]
|
172
|
-
assert_write_correct_value_with_function(values, 'title_1', text)
|
173
|
-
end
|
174
|
-
|
175
|
-
def test_title_2_writes_correct_values
|
176
|
-
text = 'Titre looooooooooong'
|
177
|
-
values = [[text]]
|
178
|
-
assert_write_correct_value_with_function(values, 'title_2', text)
|
179
|
-
end
|
180
|
-
|
181
|
-
def test_title_3_writes_correct_values
|
182
|
-
text = 'Titre looooooooooong'
|
183
|
-
# 'nil' vient du fait qu'un titre de niveau 3 commence sur la seconde colone
|
184
|
-
values = [[nil, text]]
|
185
|
-
assert_write_correct_value_with_function(values, 'title_3', text)
|
186
|
-
end
|
187
|
-
|
188
|
-
def test_line_writes_correct_values
|
189
|
-
values = [[nil, 'Valeurs', 'sur', 'une', 'ligne']]
|
190
|
-
assert_write_correct_value_with_function(values, 'line', *['Valeurs', 'sur', 'une', 'ligne'])
|
191
|
-
end
|
192
|
-
|
193
|
-
def test_h_table_writes_correct_values
|
194
|
-
# Valeurs attendues
|
195
|
-
values = [
|
196
|
-
[nil, 'colone 1', 'colone 2', 'colone 3'],
|
197
|
-
[nil],
|
198
|
-
[nil, 'a', 'b', 'c'],
|
199
|
-
[nil, nil, nil, 'd'],
|
200
|
-
[nil, 'e'],
|
201
|
-
[nil, nil, 'f']
|
202
|
-
]
|
203
|
-
# Ce que j'écris dans le fichier
|
204
|
-
headers = ['colone 1', 'colone 2', 'colone 3']
|
205
|
-
write = [ ['a', 'b', 'c'], [nil, nil, 'd'], ['e'], [nil, 'f', nil] ]
|
206
|
-
assert_write_correct_value_with_function(values, 'h_table', write, headers, {:border_bottom => true})
|
207
|
-
end
|
208
|
-
|
209
|
-
# Il n'y a pas de test où h_table est appelé avec :border_bottom => false
|
210
|
-
# car la ligne de démarquation est ajoutée dans tous les cas
|
211
|
-
|
212
|
-
def test_v_table_writes_correct_values
|
213
|
-
values = [
|
214
|
-
[nil, 'colone 1', 'valeur 1'],
|
215
|
-
[nil, 'colone 2', 'valeur 2'],
|
216
|
-
[nil, 'colone 3' ],
|
217
|
-
[nil, nil, 'valeur 3'],
|
218
|
-
[nil, 'colone 4', 'valeur 4']
|
219
|
-
]
|
220
|
-
headers = ['colone 1', 'colone 2', 'colone 3', nil, 'colone 4']
|
221
|
-
write = ['valeur 1', 'valeur 2', nil, 'valeur 3', 'valeur 4']
|
222
|
-
assert_write_correct_value_with_function(values, 'v_table', write, headers)
|
223
|
-
end
|
224
|
-
|
225
|
-
def test_complete_write_document
|
226
|
-
@excel.title_1('Titre 1')
|
227
|
-
@excel.title_2('Titre 2')
|
228
|
-
@excel.title_3('Titre 3')
|
229
|
-
@excel.line("Ligne de test")
|
230
|
-
@excel.h_table([%w{a b c}, %w{d e f}, %w{g h}], ['toto', 'tata', 'tutu'], {:border_bottom => true})
|
231
|
-
@excel.v_table(%w{toto tata tutu}, ['colone 1', 'colone 2', 'colone 3', 'colone 4'])
|
232
|
-
@excel.newline
|
233
|
-
@excel.line("Autre ligne de test")
|
234
|
-
@excel.save
|
235
|
-
|
236
|
-
assert File.file?(@excel.path)
|
237
|
-
|
238
|
-
expected_values = []
|
239
|
-
# Les 'nil' sont dus au fait que la première colone est vide
|
240
|
-
expected_values << ["Titre 1"]
|
241
|
-
expected_values << ["Titre 2"]
|
242
|
-
expected_values << [nil, "Titre 3"]
|
243
|
-
expected_values << [nil, "Ligne de test"]
|
244
|
-
expected_values << [nil, 'toto', 'tata', 'tutu']
|
245
|
-
expected_values << [nil]
|
246
|
-
expected_values << [nil, 'a', 'b', 'c']
|
247
|
-
expected_values << [nil, 'd', 'e', 'f']
|
248
|
-
expected_values << [nil, 'g', 'h']
|
249
|
-
expected_values << [nil, 'colone 1', 'toto']
|
250
|
-
expected_values << [nil, 'colone 2', 'tata']
|
251
|
-
expected_values << [nil, 'colone 3', 'tutu']
|
252
|
-
expected_values << [nil, 'colone 4']
|
253
|
-
expected_values << [nil]
|
254
|
-
expected_values << [nil, "Autre ligne de test"]
|
255
|
-
|
256
|
-
assert_equal expected_values, array_from_excel(@excel.path)
|
257
|
-
|
258
|
-
assert File.unlink(@excel.path)
|
259
|
-
|
260
|
-
assert ! File.file?(@excel.path)
|
261
|
-
end
|
262
|
-
|
263
|
-
|
264
|
-
##########################################
|
265
|
-
# Méthodes privées définies pour les tests
|
266
|
-
##########################################
|
267
|
-
|
268
|
-
private
|
269
|
-
|
270
|
-
def assert_method_returns_self(method, *args)
|
271
|
-
return_class = @excel.send(method, *args).class
|
272
|
-
|
273
|
-
assert_equal @excel.class, return_class
|
274
|
-
end
|
275
|
-
|
276
|
-
def assert_method_increments_current_row_index(number_of_lines_added, method, *args)
|
277
|
-
row_index = @excel.current_row_index
|
278
|
-
index_expected = row_index + number_of_lines_added
|
279
|
-
|
280
|
-
@excel.send(method, *args)
|
281
|
-
|
282
|
-
assert_equal index_expected, @excel.current_row_index
|
283
|
-
end
|
284
|
-
|
285
|
-
def assert_write_correct_value_with_function(expected_values, method, *args)
|
286
|
-
@excel.send(method, *args)
|
287
|
-
@excel.save
|
288
|
-
|
289
|
-
assert File.file?(@excel.path)
|
290
|
-
|
291
|
-
assert_equal expected_values, array_from_excel(@excel.path)
|
292
|
-
|
293
|
-
assert File.unlink(@excel.path)
|
294
|
-
|
295
|
-
assert ! File.file?(@excel.path)
|
296
|
-
end
|
297
|
-
|
298
|
-
def upload_excel_file(filepath)
|
299
|
-
bytes = open(filepath,'rb'){|io| io.read}
|
300
|
-
excel = SpreadsheetToArray::FromExcel.new(bytes)
|
301
|
-
end
|
302
|
-
|
303
|
-
def array_from_excel(filepath)
|
304
|
-
excel = upload_excel_file(filepath)
|
305
|
-
excel.values
|
306
|
-
end
|
307
|
-
|
308
|
-
end
|
@@ -1,337 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class GoogleSpreadsheetsTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
Sample_string = 'Test chaîne'
|
6
|
-
|
7
|
-
if ! File.file?(ENV["HOME"] + '/.ruby_google_spreadsheet.token')
|
8
|
-
puts "Pour la connexion à Google vous devez indiquer votre email de connexion et votre mot de passe."
|
9
|
-
puts "Les tokens générés sont stockés dans le fichier ~/.ruby_google_spreadsheet.token que vous pouvez supprimer."
|
10
|
-
end
|
11
|
-
|
12
|
-
def setup
|
13
|
-
@obj = create_obj
|
14
|
-
@google = SalesClicExporter::Document::GoogleSpreadsheets.new @obj.name
|
15
|
-
end
|
16
|
-
|
17
|
-
def teardown
|
18
|
-
# Sauvegarde du document
|
19
|
-
@google.save
|
20
|
-
# Suppression des feuilles crées
|
21
|
-
@google.permanent_destroy
|
22
|
-
end
|
23
|
-
|
24
|
-
###################################
|
25
|
-
# Tests sur les arguments
|
26
|
-
###################################
|
27
|
-
|
28
|
-
def test_line_with_and_without_splat_arguments
|
29
|
-
assert_method_returns_self('line', 'colone 1', 'colone 2', 'colone 3', 'colone 4')
|
30
|
-
assert_method_returns_self('line', ['colone 1', 'colone 2', 'colone 3', 'colone 4'])
|
31
|
-
assert_method_returns_self('line', 'colone 1')
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_line_without_arguments_throws_exception
|
35
|
-
assert_raise ArgumentError do
|
36
|
-
@google.line
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_h_table_without_values_as_an_array_throws_exception
|
41
|
-
assert_raise ArgumentError do
|
42
|
-
@google.h_table('test', 'test')
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
def test_h_table_with_and_without_options_and_splat_arguments
|
47
|
-
assert_method_returns_self('h_table', [%w{a b c}, %w{d e f}, %w{g h}], ['toto', 'tata', 'tutu'])
|
48
|
-
assert_method_returns_self('h_table', [%w{a b c}, %w{d e f}, %w{g h}], ['toto', 'tata', 'tutu'], {:border_bottom => true})
|
49
|
-
assert_method_returns_self('h_table', [%w{a b c}, %w{d e f}, %w{g h}], 'toto', 'tata', 'tutu')
|
50
|
-
assert_method_returns_self('h_table', [%w{a b c}, %w{d e f}, %w{g h}], 'toto', 'tata', 'tutu', {:border_bottom => true})
|
51
|
-
end
|
52
|
-
|
53
|
-
def test_v_table_with_and_without_splat_arguments
|
54
|
-
assert_method_returns_self('v_table', %w{toto tata tutu}, 'colone 1', 'colone 2', 'colone 3', 'colone 4')
|
55
|
-
assert_method_returns_self('v_table', %w{toto tata tutu}, ['colone 1', 'colone 2', 'colone 3', 'colone 4'])
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_newline_can_be_called_without_arguments
|
59
|
-
assert_nothing_raised do
|
60
|
-
@google.newline
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_write_method_without_block_is_ignored
|
65
|
-
assert_equal @google.write, @google
|
66
|
-
end
|
67
|
-
|
68
|
-
|
69
|
-
######################################################################
|
70
|
-
# On teste les retours pour vérifier que les méthodes sont bien
|
71
|
-
# chainables
|
72
|
-
######################################################################
|
73
|
-
|
74
|
-
def test_line_returns_self
|
75
|
-
assert_method_returns_self('line', 'colone 1', 'colone 2', 'colone 3', 'colone 4')
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_h_table_returns_self
|
79
|
-
assert_method_returns_self('h_table', [%w{a b c}, %w{d e f}, %w{g h}], ['toto', 'tata', 'tutu'], {:border_bottom => true})
|
80
|
-
end
|
81
|
-
|
82
|
-
def test_v_table_returns_self
|
83
|
-
assert_method_returns_self('v_table', %w{toto tata tutu}, 'colone 1', 'colone 2', 'colone 3', 'colone 4')
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_newline_returns_self
|
87
|
-
assert_method_returns_self('newline')
|
88
|
-
end
|
89
|
-
|
90
|
-
def test_freezepane_returns_self
|
91
|
-
assert_method_returns_self('freezepane')
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_title_1_returns_self
|
95
|
-
assert_method_returns_self('title_1', Sample_string)
|
96
|
-
end
|
97
|
-
|
98
|
-
def test_title_2_returns_self
|
99
|
-
assert_method_returns_self('title_2', Sample_string)
|
100
|
-
end
|
101
|
-
|
102
|
-
def test_title_3_returns_self
|
103
|
-
assert_method_returns_self('title_3', Sample_string)
|
104
|
-
end
|
105
|
-
|
106
|
-
######################################################################
|
107
|
-
# On teste sur toutes les méthodes rajoutent bien une ligne
|
108
|
-
######################################################################
|
109
|
-
|
110
|
-
def test_h_table_increments_current_row_index
|
111
|
-
headers = ['toto', 'tata', 'tutu']
|
112
|
-
lines_added = headers.map(&:size).max + 1 # + 1 pour la ligne vide de séparation
|
113
|
-
assert_method_increments_current_row_index(lines_added, 'h_table', [%w{a b c}, %w{d e f}, %w{g h}], ['toto', 'tata', 'tutu'], {:border_bottom => true})
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_v_table_increments_current_row_index
|
117
|
-
headers = %w{toto tata tutu}
|
118
|
-
values = ['colone 1', 'colone 2', 'colone 3', 'colone 4']
|
119
|
-
lines_added = [values.size, headers.size].max
|
120
|
-
assert_method_increments_current_row_index(lines_added, 'v_table', headers, *values)
|
121
|
-
end
|
122
|
-
|
123
|
-
def test_line_increments_current_row_index
|
124
|
-
assert_method_increments_current_row_index(1, 'line', 'colone 1', 'colone 2', 'colone 3', 'colone 4')
|
125
|
-
end
|
126
|
-
|
127
|
-
def test_newline_increments_current_row_index
|
128
|
-
assert_method_increments_current_row_index(1, 'newline')
|
129
|
-
end
|
130
|
-
|
131
|
-
# Freezepane ne DOIT pas incrémenter le nombre de lignes
|
132
|
-
def test_freezepane_does_not_increment_current_row_index
|
133
|
-
row_index = @google.current_row_index
|
134
|
-
index_expected = row_index # Le nombre de lignes ne doit PAS changer
|
135
|
-
|
136
|
-
@google.freezepane
|
137
|
-
|
138
|
-
assert_equal index_expected, @google.current_row_index
|
139
|
-
end
|
140
|
-
|
141
|
-
#########################################################
|
142
|
-
# Test de lecture des données sur le document spreadsheet
|
143
|
-
#########################################################
|
144
|
-
|
145
|
-
def test_title_1_writes_correct_values
|
146
|
-
text = 'Titre looooooooooong'
|
147
|
-
# Valeur attendue
|
148
|
-
values = [ expected_title(text) ]
|
149
|
-
assert_write_correct_value_with_function(values, 'title_1', text)
|
150
|
-
end
|
151
|
-
|
152
|
-
def test_title_2_writes_correct_values
|
153
|
-
text = 'Titre looooooooooong'
|
154
|
-
values = [ expected_title(text) ]
|
155
|
-
assert_write_correct_value_with_function(values, 'title_2', text)
|
156
|
-
end
|
157
|
-
|
158
|
-
def test_title_3_writes_correct_values
|
159
|
-
text = 'Titre looooooooooong'
|
160
|
-
values = [ expected_subtitle(text) ]
|
161
|
-
assert_write_correct_value_with_function(values, 'title_3', text)
|
162
|
-
end
|
163
|
-
|
164
|
-
def test_line_writes_correct_values
|
165
|
-
# Valeurs attendues
|
166
|
-
values = [ expected_text('Valeurs', 'sur', 'une', 'ligne') ]
|
167
|
-
assert_write_correct_value_with_function(values, 'line', *['Valeurs', 'sur', 'une', 'ligne'])
|
168
|
-
end
|
169
|
-
|
170
|
-
def test_h_table_writes_correct_values
|
171
|
-
# Valeurs attendues
|
172
|
-
values = []
|
173
|
-
values += expected_table([
|
174
|
-
['colone 1', 'colone 2', 'colone 3'],
|
175
|
-
[nil],
|
176
|
-
['a', 'b', 'c'],
|
177
|
-
[nil, nil, 'd'],
|
178
|
-
['e'],
|
179
|
-
[nil, 'f']
|
180
|
-
])
|
181
|
-
# Ce que j'écris dans le fichier
|
182
|
-
headers = ['colone 1', 'colone 2', 'colone 3']
|
183
|
-
write = [ ['a', 'b', 'c'], [nil, nil, 'd'], ['e'], [nil, 'f', nil] ]
|
184
|
-
assert_write_correct_value_with_function(values, 'h_table', write, headers, {:border_bottom => true})
|
185
|
-
end
|
186
|
-
|
187
|
-
# Il n'y a pas de test où h_table est appelé avec :border_bottom => false
|
188
|
-
# car la ligne de démarquation est ajoutée dans tous les cas
|
189
|
-
|
190
|
-
def test_v_table_writes_correct_values
|
191
|
-
values = []
|
192
|
-
values += expected_table([
|
193
|
-
['colone 1', 'valeur 1'],
|
194
|
-
['colone 2', 'valeur 2'],
|
195
|
-
['colone 3' ],
|
196
|
-
[ nil, 'valeur 3'],
|
197
|
-
['colone 4', 'valeur 4']])
|
198
|
-
headers = ['colone 1', 'colone 2', 'colone 3', nil, 'colone 4']
|
199
|
-
write = ['valeur 1', 'valeur 2', nil, 'valeur 3', 'valeur 4']
|
200
|
-
assert_write_correct_value_with_function(values, 'v_table', write, headers)
|
201
|
-
end
|
202
|
-
|
203
|
-
def test_complete_write_document
|
204
|
-
@google.title_1('Titre 1')
|
205
|
-
@google.title_2('Titre 2')
|
206
|
-
@google.title_3('Titre 3')
|
207
|
-
@google.line("Ligne de test")
|
208
|
-
@google.h_table([%w{a b c}, %w{d e f}, %w{g h}], ['toto', 'tata', 'tutu'], {:border_bottom => true})
|
209
|
-
@google.v_table(%w{toto tata tutu}, ['colone 1', 'colone 2', 'colone 3', 'colone 4'])
|
210
|
-
@google.newline
|
211
|
-
@google.line("Autre ligne de test")
|
212
|
-
@google.save
|
213
|
-
|
214
|
-
expected_values = []
|
215
|
-
expected_values << expected_title('Titre 1')
|
216
|
-
expected_values << expected_title('Titre 2')
|
217
|
-
expected_values << expected_subtitle("Titre 3")
|
218
|
-
expected_values << expected_text("Ligne de test")
|
219
|
-
expected_values << expected_text('toto', 'tata', 'tutu')
|
220
|
-
expected_values << [nil]
|
221
|
-
expected_values += expected_table([['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h']])
|
222
|
-
expected_values += expected_table([['colone 1', 'toto'], ['colone 2', 'tata'], ['colone 3', 'tutu'], ['colone 4']])
|
223
|
-
expected_values << [nil]
|
224
|
-
expected_values << expected_text("Autre ligne de test")
|
225
|
-
|
226
|
-
assert_equal expected_values, array_from_google_spreadsheet
|
227
|
-
end
|
228
|
-
|
229
|
-
private
|
230
|
-
|
231
|
-
def assert_method_returns_self(method, *args)
|
232
|
-
return_class = @google.send(method, *args).class
|
233
|
-
|
234
|
-
assert_equal @google.class, return_class
|
235
|
-
end
|
236
|
-
|
237
|
-
def assert_method_increments_current_row_index(number_of_lines_added, method, *args)
|
238
|
-
row_index = @google.current_row_index
|
239
|
-
index_expected = row_index + number_of_lines_added
|
240
|
-
|
241
|
-
@google.send(method, *args)
|
242
|
-
|
243
|
-
assert_equal index_expected, @google.current_row_index
|
244
|
-
end
|
245
|
-
|
246
|
-
def assert_write_correct_value_with_function(expected_values, method, *args)
|
247
|
-
@google.send(method, *args)
|
248
|
-
|
249
|
-
@google.save
|
250
|
-
|
251
|
-
assert_equal expected_values, array_from_google_spreadsheet
|
252
|
-
end
|
253
|
-
|
254
|
-
def array_from_google_spreadsheet
|
255
|
-
# On travaille sur la première spreadsheet enregistrée, et sur sa
|
256
|
-
# première worksheet
|
257
|
-
sheet = @google.google_doc.spreadsheets.first.worksheets.first
|
258
|
-
|
259
|
-
values = []
|
260
|
-
for row_index in 1..sheet.max_rows
|
261
|
-
row_values = []
|
262
|
-
for col_index in 1..sheet.max_cols
|
263
|
-
cell = sheet[row_index, col_index]
|
264
|
-
row_values << (cell.blank? ? nil : cell)
|
265
|
-
end
|
266
|
-
# Suppression des éléments nil après le dernier élément non nil
|
267
|
-
values << remove_nil_elements_from_end_of_array(row_values)
|
268
|
-
end
|
269
|
-
|
270
|
-
# On supprime les [nil] en double en fin de tableau
|
271
|
-
remove_nil_arrays_from_array(values)
|
272
|
-
end
|
273
|
-
|
274
|
-
# Soit un tableau qui comprend des valeurs nil et non-nil. La fonction retire
|
275
|
-
# les éléments nil "en trop" depuis la fin du tableau (en s'arrêtant au premier élément
|
276
|
-
# non-nil depuis la fin du tableau)
|
277
|
-
def remove_nil_elements_from_end_of_array(ary)
|
278
|
-
return [nil] if ary.compact.size == 0
|
279
|
-
|
280
|
-
reverse_ary = ary.reverse
|
281
|
-
last_non_nil_element = reverse_ary.compact.first
|
282
|
-
last_non_nil_element_index = reverse_ary.index(last_non_nil_element)
|
283
|
-
non_nil_elements = reverse_ary.slice(last_non_nil_element_index..reverse_ary.size)
|
284
|
-
non_nil_elements.reverse
|
285
|
-
end
|
286
|
-
|
287
|
-
# Soit un tableau contenant des tableaux contenant des valeurs (nil et non-nil) ou seulement nil.
|
288
|
-
# La fonction retire les éléments [nil] "en trop" depuis la fin du tableau (en s'arrêtant au premier
|
289
|
-
# élément diférent de [nil] depuis la fin du tableau)
|
290
|
-
def remove_nil_arrays_from_array(ary)
|
291
|
-
return nil if ary.compact.size == 0
|
292
|
-
|
293
|
-
reverse_ary = ary.reverse
|
294
|
-
# On cherche le dernier élément qui n'est pas égal à [nil]
|
295
|
-
# donc on prend PAS ceux qui ne contiennent qu'un élément et dont cet élément serait nil
|
296
|
-
# et on garde le dernier élément
|
297
|
-
last_non_nil_element = ary.select{ |v| v.size != 1 || ! v.first.nil? }.last
|
298
|
-
last_non_nil_element_index = reverse_ary.index(last_non_nil_element)
|
299
|
-
non_nil_elements = reverse_ary.slice(last_non_nil_element_index..reverse_ary.size)
|
300
|
-
non_nil_elements.reverse
|
301
|
-
end
|
302
|
-
|
303
|
-
# When a lines contains a title, it should be written on the first cell
|
304
|
-
def expected_title(title)
|
305
|
-
title.to_a
|
306
|
-
end
|
307
|
-
|
308
|
-
|
309
|
-
# When a lines contains a subtitle title_3), it should be written on the default cell
|
310
|
-
def expected_subtitle(title)
|
311
|
-
# Returns an Array with nil cells merged with an Array containing the title
|
312
|
-
array_with_nil_cells + title.to_a
|
313
|
-
end
|
314
|
-
|
315
|
-
# When a lines contains a text, it should be written from the second cell
|
316
|
-
def expected_text(*text)
|
317
|
-
array_with_nil_cells + text
|
318
|
-
end
|
319
|
-
|
320
|
-
# When many lines contain a table, it should be written from the second column
|
321
|
-
def expected_table(ary)
|
322
|
-
ary.map do |line|
|
323
|
-
# An empty line should just have nil once
|
324
|
-
line.size == 1 && line.first.nil? ? line : (array_with_nil_cells + line)
|
325
|
-
end
|
326
|
-
end
|
327
|
-
|
328
|
-
def array_with_nil_cells
|
329
|
-
Array.new(cell_index)
|
330
|
-
end
|
331
|
-
|
332
|
-
def cell_index
|
333
|
-
# The cell index in spreadsheets starts from 1, and Ruby starts from 0
|
334
|
-
SalesClicExporter::Document::GoogleSpreadsheets::Default_cell_index - 1
|
335
|
-
end
|
336
|
-
|
337
|
-
end
|
Binary file
|
@@ -1 +0,0 @@
|
|
1
|
-
|
Binary file
|
@@ -1,30 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class SpreadsheetToArrayTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
def test_values_are_read_correctly
|
6
|
-
assert_nothing_raised do
|
7
|
-
|
8
|
-
array = array_from_excel('valid_excel_import.xls')
|
9
|
-
|
10
|
-
first_line_values = ['Objectif', 5936.535, 7730.663, 9524.791 , 11318.919, 13113.047, 14907.175, 16701.303, 18495.431]
|
11
|
-
second_line_values = ['Réalisé', 4635.394, 9695.558, 14755.722, 19815.886, 24876.050, 29936.214, 34996.378, 40056.542]
|
12
|
-
|
13
|
-
|
14
|
-
excel_first_line_values = array[2]
|
15
|
-
|
16
|
-
excel_sec_line_values = array[3]
|
17
|
-
|
18
|
-
# Les comparaisons sont faites en string pour éviter les problèmes de précision des flottants
|
19
|
-
assert_equal first_line_values.map(&:to_s) , excel_first_line_values.map(&:to_s)
|
20
|
-
assert_equal second_line_values.map(&:to_s), excel_sec_line_values.map(&:to_s)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
def test_exception_is_thrown_when_byte_array_entry_is_not_valid
|
25
|
-
assert_raise RuntimeError do
|
26
|
-
array_from_excel('invalid_file_format.txt')
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|