ruport 0.11.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +4 -4
- data/Rakefile +1 -1
- data/lib/ruport.rb +1 -1
- data/lib/ruport/acts_as_reportable.rb +34 -2
- data/lib/ruport/data/grouping.rb +6 -4
- data/lib/ruport/formatter/pdf.rb +54 -17
- data/lib/ruport/formatter/text.rb +6 -3
- data/test/acts_as_reportable_test.rb +95 -72
- data/test/csv_formatter_test.rb +40 -36
- data/test/grouping_test.rb +139 -134
- data/test/html_formatter_test.rb +24 -10
- data/test/pdf_formatter_test.rb +102 -8
- data/test/query_test.rb +0 -1
- data/test/record_test.rb +45 -33
- data/test/renderer_test.rb +236 -211
- data/test/sql_split_test.rb +8 -7
- data/test/table_test.rb +437 -425
- data/test/text_formatter_test.rb +62 -45
- metadata +2 -2
data/test/sql_split_test.rb
CHANGED
@@ -2,17 +2,18 @@ require "test/unit"
|
|
2
2
|
require "ruport"
|
3
3
|
class TestSqlSplit < Test::Unit::TestCase
|
4
4
|
include Ruport
|
5
|
+
|
6
|
+
def test_sql_split_trivial
|
7
|
+
sql = "SELECT * FROM ruport_test"
|
8
|
+
split = Query::SqlSplit.new sql
|
9
|
+
assert_equal( 1, split.size )
|
10
|
+
assert_equal( sql, split.first )
|
11
|
+
end
|
5
12
|
|
6
|
-
def
|
13
|
+
def test_sql_split_complex
|
7
14
|
sql = File.read 'test/samples/ruport_test.sql'
|
8
15
|
split = Query::SqlSplit.new sql
|
9
16
|
assert_equal( 'SELECT * FROM ruport_test', split.last )
|
10
17
|
end
|
11
18
|
|
12
|
-
def test_sql_split2
|
13
|
-
sql = "SELECT * FROM ruport_test"
|
14
|
-
split = Query::SqlSplit.new sql
|
15
|
-
assert_equal( 1, split.size )
|
16
|
-
assert_equal( sql, split.first )
|
17
|
-
end
|
18
19
|
end
|
data/test/table_test.rb
CHANGED
@@ -9,7 +9,10 @@ class Person < Ruport::Data::Record
|
|
9
9
|
first_name + " " + last_name
|
10
10
|
end
|
11
11
|
|
12
|
-
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
class DuckRecord < Ruport::Data::Record; end
|
13
16
|
|
14
17
|
class TestTable < Test::Unit::TestCase
|
15
18
|
def test_constructors
|
@@ -31,22 +34,10 @@ class TestTable < Test::Unit::TestCase
|
|
31
34
|
|
32
35
|
a = Ruport::Data::Record.new [1,2,3]
|
33
36
|
b = Ruport::Data::Record.new [1,2,3], :attributes => %w[col1 col2 col3]
|
34
|
-
tables.zip([[],[],[a],[b]]).each
|
35
|
-
assert_equal n, t.data
|
37
|
+
tables.zip([[],[],[a],[b]]).each do |t,n|
|
38
|
+
assert_equal n, t.data
|
39
|
+
end
|
36
40
|
end
|
37
|
-
|
38
|
-
def test_column
|
39
|
-
a = [[1,2,3],[4,5,6]].to_table(%w[a b c])
|
40
|
-
assert_equal [3,6], a.column(2)
|
41
|
-
assert_equal [2,5], a.column("b")
|
42
|
-
|
43
|
-
assert_raise(ArgumentError) { a.column("d") }
|
44
|
-
assert_raise(ArgumentError) { a.column(42) }
|
45
|
-
|
46
|
-
a = [[1],[2],[3],[4]].to_table
|
47
|
-
assert_equal [1,2,3,4], a.column(0)
|
48
|
-
|
49
|
-
end
|
50
41
|
|
51
42
|
def test_to_group
|
52
43
|
a =[[1,2,3],[4,5,6]].to_table(%w[a b c]).to_group("Packrats")
|
@@ -55,24 +46,7 @@ class TestTable < Test::Unit::TestCase
|
|
55
46
|
:name => "Packrats" )
|
56
47
|
assert_equal a,b
|
57
48
|
end
|
58
|
-
|
59
|
-
def test_set_column_names
|
60
|
-
a = [[1,2,3],[4,5,6]].to_table
|
61
|
-
|
62
|
-
assert_equal([],a.column_names)
|
63
|
-
assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a } )
|
64
|
-
|
65
|
-
a.column_names = %w[a b c]
|
66
|
-
assert_equal(%w[a b c],a.column_names)
|
67
|
-
a.each { |r| assert_equal(%w[a b c], r.attributes) }
|
68
|
-
assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a })
|
69
|
-
|
70
|
-
a.column_names = %w[d e f]
|
71
|
-
assert_equal(%w[d e f],a.column_names)
|
72
|
-
a.each { |r| assert_equal(%w[d e f], r.attributes) }
|
73
|
-
assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a })
|
74
|
-
end
|
75
|
-
|
49
|
+
|
76
50
|
def test_rows_with
|
77
51
|
table = [[1,2,3],[1,3,4],[7,8,9]].to_table(%w[a b c])
|
78
52
|
|
@@ -82,24 +56,7 @@ class TestTable < Test::Unit::TestCase
|
|
82
56
|
assert_equal([table[2]], table.rows_with_b(8))
|
83
57
|
assert_equal [table[1]], table.rows_with(%w[a b]) { |a,b| [a,b] == [1,3] }
|
84
58
|
end
|
85
|
-
|
86
|
-
def test_append_record
|
87
|
-
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
88
|
-
table << Ruport::Data::Record.new([1,2,3], :attributes => %w[a b c])
|
89
|
-
assert_equal([1,2,3],table[0].to_a)
|
90
|
-
assert_equal(%w[a b c],table[0].attributes)
|
91
|
-
rec = table[0].dup
|
92
|
-
rec.attributes = %w[a b c d]
|
93
|
-
assert_raise(NoMethodError) { table << Object.new }
|
94
|
-
end
|
95
59
|
|
96
|
-
def test_append_hash
|
97
|
-
table = [[1,2,3],[4,5,6]].to_table(%w[a b c])
|
98
|
-
table << { "a" => 7, "c" => 9, "b" => 8 }
|
99
|
-
|
100
|
-
assert_equal [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c]), table
|
101
|
-
end
|
102
|
-
|
103
60
|
def test_sigma
|
104
61
|
table = [[1,2],[3,4],[5,6]].to_table(%w[col1 col2])
|
105
62
|
assert table.respond_to?(:sigma)
|
@@ -108,19 +65,6 @@ class TestTable < Test::Unit::TestCase
|
|
108
65
|
assert_equal(9,table.sigma("col1"))
|
109
66
|
assert_equal(21,table.sigma { |r| r.col1 + r.col2 })
|
110
67
|
end
|
111
|
-
|
112
|
-
def test_append_table
|
113
|
-
first = Ruport::Data::Table.new :column_names => %w[a b c],
|
114
|
-
:data => [[1,2,3],[4,5,6]]
|
115
|
-
|
116
|
-
second = Ruport::Data::Table.new :column_names => %w[a b c],
|
117
|
-
:data => [[7,8,9],[10,11,12]]
|
118
|
-
|
119
|
-
combo = first + second
|
120
|
-
|
121
|
-
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c],
|
122
|
-
:data => [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]), combo
|
123
|
-
end
|
124
68
|
|
125
69
|
def test_sub_table
|
126
70
|
table = [ [1,2,3,4],[5,6,7,9],
|
@@ -144,7 +88,6 @@ class TestTable < Test::Unit::TestCase
|
|
144
88
|
end
|
145
89
|
|
146
90
|
def test_reduce
|
147
|
-
|
148
91
|
table = [ [1,2,3,4],[5,6,7,9],
|
149
92
|
[10,11,12,13],[14,15,16,17] ].to_table(%w[a b c d])
|
150
93
|
|
@@ -155,80 +98,7 @@ class TestTable < Test::Unit::TestCase
|
|
155
98
|
[10,11,12,13],[14,15,16,17] ].to_table(%w[a b c d])
|
156
99
|
table.reduce(%w[c d a]) { |r| r.a < 10 }
|
157
100
|
|
158
|
-
assert_equal [[3,4,1],[7,9,5]].to_table(%w[c d a]),
|
159
|
-
table
|
160
|
-
|
161
|
-
end
|
162
|
-
|
163
|
-
def test_csv_load
|
164
|
-
table = Ruport::Data::Table.load("test/samples/data.csv")
|
165
|
-
assert_equal %w[col1 col2 col3], table.column_names
|
166
|
-
rows = [%w[a b c],["d",nil,"e"]]
|
167
|
-
table.each { |r| assert_equal rows.shift, r.to_a
|
168
|
-
assert_equal %w[col1 col2 col3], r.attributes }
|
169
|
-
expected = [%w[1 2 3],%w[4 5 6]].to_table(%w[a b c])
|
170
|
-
|
171
|
-
# ticket:94
|
172
|
-
table = Ruport::Data::Table.load( "test/samples/data.tsv",
|
173
|
-
:csv_options => { :col_sep => "\t" } )
|
174
|
-
assert_equal expected, table
|
175
|
-
|
176
|
-
|
177
|
-
expected = ['c','e']
|
178
|
-
|
179
|
-
table = Ruport::Data::Table.load( "test/samples/data.csv", :csv_options =>
|
180
|
-
{ :headers => true, :header_converters => :symbol } ) do |s,r|
|
181
|
-
assert_equal expected.shift, r[:col3]
|
182
|
-
end
|
183
|
-
|
184
|
-
assert_equal [:col1,:col2,:col3], table.column_names
|
185
|
-
|
186
|
-
|
187
|
-
expected = ['c','e']
|
188
|
-
|
189
|
-
Ruport::Data::Table.load( "test/samples/data.csv",
|
190
|
-
:records => true ) do |s,r|
|
191
|
-
assert_equal expected.shift, r.col3
|
192
|
-
assert_kind_of Ruport::Data::Record, r
|
193
|
-
end
|
194
|
-
|
195
|
-
table = Ruport::Data::Table.load("test/samples/data.csv", :has_names => false)
|
196
|
-
assert_equal([],table.column_names)
|
197
|
-
assert_equal([%w[col1 col2 col3],%w[a b c],["d",nil,"e"]].to_table, table)
|
198
|
-
|
199
|
-
end
|
200
|
-
|
201
|
-
# ticket:76
|
202
|
-
def test_parse
|
203
|
-
|
204
|
-
assert_nothing_raised {
|
205
|
-
Ruport::Data::Table.parse("a,b,c\n1,2,3\n")
|
206
|
-
}
|
207
|
-
|
208
|
-
table = Ruport::Data::Table.parse("a,b,c\n1,2,3\n4,5,6\n")
|
209
|
-
expected = [%w[1 2 3],%w[4 5 6]].to_table(%w[a b c])
|
210
|
-
|
211
|
-
table = Ruport::Data::Table.parse( "a\tb\tc\n1\t2\t3\n4\t5\t6\n",
|
212
|
-
:csv_options => { :col_sep => "\t" } )
|
213
|
-
assert_equal expected, table
|
214
|
-
|
215
|
-
table = Ruport::Data::Table.parse( "a,b,c\n1,2,3\n4,5,6\n",
|
216
|
-
:has_names => false)
|
217
|
-
assert_equal([],table.column_names)
|
218
|
-
assert_equal([%w[a b c],%w[1 2 3], %w[4 5 6]].to_table, table)
|
219
|
-
|
220
|
-
|
221
|
-
end
|
222
|
-
|
223
|
-
def test_csv_block_form
|
224
|
-
expected = [%w[a b],%w[1 2],%w[3 4]]
|
225
|
-
t = Ruport::Data::Table.send(:get_table_from_csv,
|
226
|
-
:parse, "a,b\n1,2\n3,4",
|
227
|
-
:has_names => false) do |s,r|
|
228
|
-
assert_equal expected.shift, r
|
229
|
-
s << r
|
230
|
-
end
|
231
|
-
assert_equal [%w[a b],%w[1 2],%w[3 4]].to_table, t
|
101
|
+
assert_equal [[3,4,1],[7,9,5]].to_table(%w[c d a]), table
|
232
102
|
end
|
233
103
|
|
234
104
|
def test_reorder
|
@@ -253,222 +123,57 @@ class TestTable < Test::Unit::TestCase
|
|
253
123
|
r.instance_eval{@attributes}.object_id
|
254
124
|
}
|
255
125
|
end
|
256
|
-
|
257
|
-
def test_add_column
|
258
|
-
|
259
|
-
a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
|
260
|
-
a.add_column("c")
|
261
|
-
assert_equal [[1,2,nil],[3,4,nil],[5,6,nil]].to_table(%w[a b c]), a
|
262
|
-
|
263
|
-
a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
|
264
|
-
a.add_column("c",:default => "x")
|
265
|
-
assert_equal [[1,2,'x'],[3,4,'x'],[5,6,'x']].to_table(%w[a b c]), a
|
266
|
-
|
267
|
-
b = a.dup
|
268
|
-
b.add_column("x",:before => "b")
|
269
|
-
assert_equal [[1,nil,2,'x'],
|
270
|
-
[3,nil,4,'x'],
|
271
|
-
[5,nil,6,'x']].to_table(%w[a x b c]), b
|
272
|
-
|
273
|
-
b = a.dup
|
274
|
-
b.add_column("x",:after => "b")
|
275
|
-
assert_equal [[1,2,nil,'x'],
|
276
|
-
[3,4,nil,'x'],
|
277
|
-
[5,6,nil,'x']].to_table(%w[a b x c]), b
|
278
|
-
|
279
|
-
|
280
|
-
a.add_column("d") { |r| r[0]+r[1] }
|
281
|
-
assert_equal(
|
282
|
-
[ [1,2,'x',3],
|
283
|
-
[3,4,'x',7],
|
284
|
-
[5,6,'x',11] ].to_table(%w[a b c d]), a)
|
285
|
-
|
286
|
-
a.add_column("x",:position => 1)
|
287
|
-
assert_equal(
|
288
|
-
[ [1,nil,2,'x',3],
|
289
|
-
[3,nil,4,'x',7],
|
290
|
-
[5,nil,6,'x',11] ].to_table(%w[a x b c d]), a)
|
291
|
-
|
292
|
-
end
|
293
126
|
|
294
|
-
def
|
295
|
-
|
296
|
-
|
297
|
-
expected = [ [1,2,nil,nil],
|
298
|
-
[3,4,nil,nil],
|
299
|
-
[5,6,nil,nil] ].to_table(%w[a b c d])
|
300
|
-
|
301
|
-
assert_equal expected, a
|
127
|
+
def test_sort_rows_by
|
128
|
+
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
129
|
+
table << [1,2,3] << [6,1,8] << [9,1,4]
|
302
130
|
|
303
|
-
|
131
|
+
table2 = Ruport::Data::Table.new :column_names => [:a, :b, :c]
|
132
|
+
table2 << [1,2,3] << [6,1,8] << [9,1,4]
|
133
|
+
|
134
|
+
sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c]
|
135
|
+
sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4]
|
136
|
+
|
137
|
+
sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
|
138
|
+
sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
|
304
139
|
|
305
|
-
|
140
|
+
sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
|
141
|
+
sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
|
306
142
|
|
307
|
-
|
308
|
-
|
309
|
-
[5,nil,nil,6], ].to_table(%w[a c d b])
|
143
|
+
sorted_table_bs = Ruport::Data::Table.new :column_names => [:a, :b, :c]
|
144
|
+
sorted_table_bs << [6,1,8] << [9,1,4] << [1,2,3]
|
310
145
|
|
311
|
-
assert_equal
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
assert_equal expected, a
|
328
|
-
|
329
|
-
a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
|
330
|
-
a.add_columns(%w[f x],:default => 0)
|
146
|
+
assert_equal sorted_table_a, table.sort_rows_by {|r| r['a']}
|
147
|
+
assert_equal sorted_table_b, table.sort_rows_by(['b'])
|
148
|
+
assert_equal sorted_table_bc, table.sort_rows_by(['b', 'c'])
|
149
|
+
assert_equal sorted_table_bs, table2.sort_rows_by(:b)
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_sort_rows_by!
|
153
|
+
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
154
|
+
table << [1,2,3] << [6,1,8] << [9,1,4]
|
155
|
+
|
156
|
+
sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c]
|
157
|
+
sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4]
|
158
|
+
|
159
|
+
sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
|
160
|
+
sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
|
331
161
|
|
332
|
-
|
333
|
-
|
162
|
+
sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
|
163
|
+
sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
|
334
164
|
|
335
|
-
|
336
|
-
|
337
|
-
end
|
165
|
+
table_a = table.dup
|
166
|
+
table_a.sort_rows_by! { |r| r['a'] }
|
338
167
|
|
339
|
-
|
168
|
+
table_b = table.dup
|
169
|
+
table_b.sort_rows_by!("b")
|
340
170
|
|
341
|
-
|
171
|
+
table_bc = table.dup
|
172
|
+
table_bc.sort_rows_by!(['b', 'c'])
|
342
173
|
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
a.remove_column("b")
|
348
|
-
assert_equal Table(%w[a c]) { |t| t << [1,3] << [4,6] }, a
|
349
|
-
|
350
|
-
b.remove_column(2)
|
351
|
-
assert_equal Table(%w[a b]) { |t| t << [1,2] << [4,5] }, b
|
352
|
-
end
|
353
|
-
|
354
|
-
def test_remove_columns
|
355
|
-
a = Table(%w[a b c d]) { |t| t << [1,2,3,4] << [5,6,7,8] }
|
356
|
-
b = a.dup
|
357
|
-
a.remove_columns("b","d")
|
358
|
-
assert_equal Table(%w[a c]) { |t| t << [1,3] << [5,7] }, a
|
359
|
-
b.remove_columns(%w[a c])
|
360
|
-
assert_equal Table(%w[b d]) { |t| t << [2,4] << [6,8] }, b
|
361
|
-
end
|
362
|
-
|
363
|
-
def test_rename_column
|
364
|
-
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
365
|
-
a.rename_column("b","x")
|
366
|
-
assert_equal Table(%w[a x]) { |t| t << [1,2] << [3,4] }, a
|
367
|
-
end
|
368
|
-
|
369
|
-
|
370
|
-
def test_rename_columns
|
371
|
-
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
372
|
-
a.rename_columns(%w[a b], %w[x y])
|
373
|
-
assert_equal Table(%w[x y]) { |t| t << [1,2] << [3,4] }, a
|
374
|
-
|
375
|
-
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
376
|
-
a.rename_columns("a"=>"x","b"=>"y")
|
377
|
-
assert_equal Table(%w[x y]) { |t| t << [1,2] << [3,4] }, a
|
378
|
-
|
379
|
-
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
380
|
-
assert_raise(ArgumentError) { a.rename_columns(%w[a b], %w[x]) }
|
381
|
-
|
382
|
-
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
383
|
-
a.rename_columns { |r| r.to_sym }
|
384
|
-
assert_equal(a, Table(:a,:b,:c) { |t| t << [1,2,3] << [4,5,6] })
|
385
|
-
|
386
|
-
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
387
|
-
a.rename_columns(%w[a c]) { |r| r.to_sym }
|
388
|
-
assert_equal(a, Table(:a,"b",:c) { |t| t << [1,2,3] << [4,5,6] })
|
389
|
-
end
|
390
|
-
|
391
|
-
def test_swap_column
|
392
|
-
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
393
|
-
a.swap_column("a","b")
|
394
|
-
assert_equal Table(%w[b a]) { |t| t << [2,1] << [4,3] }, a
|
395
|
-
a.swap_column(1,0)
|
396
|
-
assert_equal Table(%w[a b]) { |t| t << [1,2] << [3,4] }, a
|
397
|
-
end
|
398
|
-
|
399
|
-
def test_replace_column
|
400
|
-
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
401
|
-
a.replace_column("b","d") { |r| r.b.to_s }
|
402
|
-
assert_equal Table(%w[a d c]) { |t| t << [1,"2",3] << [4,"5",6] }, a
|
403
|
-
a.replace_column("d") { |r| r.d.to_i }
|
404
|
-
assert_equal Table(%w[a d c]) { |t| t << [1,2,3] << [4,5,6] }, a
|
405
|
-
end
|
406
|
-
|
407
|
-
def test_append_chain
|
408
|
-
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
409
|
-
table << [1,2,3] << [4,5,6] << [7,8,9]
|
410
|
-
assert_equal 3, table.length
|
411
|
-
assert_equal 5, table[1].b
|
412
|
-
end
|
413
|
-
|
414
|
-
def test_to_hack
|
415
|
-
table = Ruport::Data::Table.new :column_names => %w[a b],
|
416
|
-
:data => [[1,2],[3,4],[5,6]]
|
417
|
-
assert_equal("a,b\n1,2\n3,4\n5,6\n",table.to_csv)
|
418
|
-
assert_raises(Ruport::Renderer::UnknownFormatError) { table.to_nothing }
|
419
|
-
end
|
420
|
-
|
421
|
-
|
422
|
-
def test_sort_rows_by
|
423
|
-
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
424
|
-
table << [1,2,3] << [6,1,8] << [9,1,4]
|
425
|
-
|
426
|
-
table2 = Ruport::Data::Table.new :column_names => [:a, :b, :c]
|
427
|
-
table2 << [1,2,3] << [6,1,8] << [9,1,4]
|
428
|
-
|
429
|
-
sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c]
|
430
|
-
sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4]
|
431
|
-
|
432
|
-
sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
|
433
|
-
sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
|
434
|
-
|
435
|
-
sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
|
436
|
-
sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
|
437
|
-
|
438
|
-
sorted_table_bs = Ruport::Data::Table.new :column_names => [:a, :b, :c]
|
439
|
-
sorted_table_bs << [6,1,8] << [9,1,4] << [1,2,3]
|
440
|
-
|
441
|
-
assert_equal sorted_table_a, table.sort_rows_by {|r| r['a']}
|
442
|
-
assert_equal sorted_table_b, table.sort_rows_by(['b'])
|
443
|
-
assert_equal sorted_table_bc, table.sort_rows_by(['b', 'c'])
|
444
|
-
assert_equal sorted_table_bs, table2.sort_rows_by(:b)
|
445
|
-
end
|
446
|
-
|
447
|
-
def test_sort_rows_by!
|
448
|
-
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
449
|
-
table << [1,2,3] << [6,1,8] << [9,1,4]
|
450
|
-
|
451
|
-
sorted_table_a = Ruport::Data::Table.new :column_names => %w[a b c]
|
452
|
-
sorted_table_a << [1,2,3] << [6,1,8] << [9,1,4]
|
453
|
-
|
454
|
-
sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
|
455
|
-
sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
|
456
|
-
|
457
|
-
sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
|
458
|
-
sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
|
459
|
-
|
460
|
-
table_a = table.dup
|
461
|
-
table_a.sort_rows_by! { |r| r['a'] }
|
462
|
-
|
463
|
-
table_b = table.dup
|
464
|
-
table_b.sort_rows_by!("b")
|
465
|
-
|
466
|
-
table_bc = table.dup
|
467
|
-
table_bc.sort_rows_by!(['b', 'c'])
|
468
|
-
|
469
|
-
assert_equal sorted_table_a, table_a
|
470
|
-
assert_equal sorted_table_b, table_b
|
471
|
-
assert_equal sorted_table_bc, table_bc
|
174
|
+
assert_equal sorted_table_a, table_a
|
175
|
+
assert_equal sorted_table_b, table_b
|
176
|
+
assert_equal sorted_table_bc, table_bc
|
472
177
|
end
|
473
178
|
|
474
179
|
def test_array_hack
|
@@ -506,49 +211,9 @@ class TestTable < Test::Unit::TestCase
|
|
506
211
|
b.map { |r| r.name }
|
507
212
|
|
508
213
|
end
|
509
|
-
|
510
|
-
def test_to_hack_takes_args
|
511
|
-
a = Table(%w[hello mr crowley]) << %w[would you like] << %w[one red cat]
|
512
|
-
|
513
|
-
assert_equal "would,you,like\none,red,cat\n",
|
514
|
-
a.to_csv(:show_table_headers => false)
|
515
|
-
|
516
|
-
assert_equal "would,you,like\none,red,cat\n",
|
517
|
-
a.to_csv { |r| r.show_table_headers = false }
|
518
|
-
|
519
|
-
assert_equal "would\tyou\tlike\none\tred\tcat\n",
|
520
|
-
a.to_csv(:show_table_headers => false) { |r|
|
521
|
-
r.format_options = { :col_sep => "\t" }
|
522
|
-
}
|
523
|
-
|
524
|
-
end
|
525
|
-
|
526
|
-
def test_as_throws_proper_errors
|
527
|
-
a = [[1,2,3],[4,5,6]].to_table(%w[a b c])
|
528
|
-
assert_nothing_raised { a.as(:csv) }
|
529
|
-
assert_nothing_raised { a.to_csv }
|
530
|
-
assert_raises(Ruport::Renderer::UnknownFormatError) { a.as(:nothing) }
|
531
|
-
assert_raises(Ruport::Renderer::UnknownFormatError) { a.to_nothing }
|
532
|
-
end
|
533
|
-
|
534
214
|
|
535
215
|
## BUG Traps -------------------------------------------------
|
536
216
|
|
537
|
-
def test_ensure_setting_column_names_changes_record_attributes
|
538
|
-
table = Ruport::Data::Table.new :column_names => %w[a b c],
|
539
|
-
:data => [[1,2,3],[4,5,6]]
|
540
|
-
|
541
|
-
assert_equal %w[a b c], table.column_names
|
542
|
-
assert_equal %w[a b c], table.data[0].attributes
|
543
|
-
assert_equal %w[a b c], table.data[1].attributes
|
544
|
-
|
545
|
-
table.column_names = %w[d e f]
|
546
|
-
|
547
|
-
assert_equal %w[d e f], table.column_names
|
548
|
-
assert_equal %w[d e f], table.data[0].attributes
|
549
|
-
assert_equal %w[d e f], table.data[1].attributes
|
550
|
-
end
|
551
|
-
|
552
217
|
def test_ensure_table_creation_allows_record_coercion
|
553
218
|
table = [[1,2,3],[4,5,6],[7,8,9]].to_table
|
554
219
|
table_with_names = [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c])
|
@@ -577,24 +242,6 @@ class TestTable < Test::Unit::TestCase
|
|
577
242
|
}
|
578
243
|
end
|
579
244
|
|
580
|
-
def test_ensure_using_csv_block_mode_works
|
581
|
-
expected = [%w[a b],%w[1 2],%w[3 4]]
|
582
|
-
t = Ruport::Data::Table.parse("a,b\n1,2\n3,4",:has_names => false) { |s,r|
|
583
|
-
assert_equal expected.shift, r
|
584
|
-
s << r
|
585
|
-
s << r
|
586
|
-
}
|
587
|
-
assert_equal [%w[a b],%w[a b],%w[1 2], %w[1 2],
|
588
|
-
%w[3 4],%w[3 4]].to_table, t
|
589
|
-
x = Ruport::Data::Table.load("test/samples/data.csv") { |s,r|
|
590
|
-
assert_kind_of Ruport::Data::Table, s
|
591
|
-
assert_kind_of Array, r
|
592
|
-
s << r
|
593
|
-
s << r
|
594
|
-
}
|
595
|
-
assert_equal 4, x.length
|
596
|
-
end
|
597
|
-
|
598
245
|
def test_ensure_coerce_sum
|
599
246
|
s = [["1"],["3"],["5"] ].to_table
|
600
247
|
t = [["1.23"],["1.5"]].to_table
|
@@ -625,6 +272,300 @@ class TestTable < Test::Unit::TestCase
|
|
625
272
|
assert_equal [[1,2,3],[4,5,6],[7,8,9]].to_table,a
|
626
273
|
end
|
627
274
|
|
275
|
+
def test_ensure_propagate_record_class
|
276
|
+
a = Table(:record_class => DuckRecord)
|
277
|
+
assert_equal DuckRecord, a.record_class
|
278
|
+
|
279
|
+
b = a.dup
|
280
|
+
assert_equal DuckRecord, b.record_class
|
281
|
+
end
|
282
|
+
|
283
|
+
def test_ensure_reorder_raises_on_bad_reorder_use
|
284
|
+
a = Table() << [1,2,3] << [4,5,6]
|
285
|
+
assert_raise(ArgumentError) { a.reorder("a","b","c") }
|
286
|
+
assert_raise(ArgumentError) { a.reorder(%w[a b c]) }
|
287
|
+
assert_raise(ArgumentError) { a.reorder(2,1,0) }
|
288
|
+
end
|
289
|
+
|
290
|
+
class MySubClass < Ruport::Data::Table; end
|
291
|
+
|
292
|
+
def test_ensure_table_subclasses_render_properly
|
293
|
+
a = MySubClass.new
|
294
|
+
a << [1,2,3] << [4,5,6]
|
295
|
+
assert_equal("1,2,3\n4,5,6\n",a.as(:csv))
|
296
|
+
end
|
297
|
+
|
298
|
+
end
|
299
|
+
|
300
|
+
class TestTableAppendOperations < Test::Unit::TestCase
|
301
|
+
def test_append_record
|
302
|
+
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
303
|
+
table << Ruport::Data::Record.new([1,2,3], :attributes => %w[a b c])
|
304
|
+
assert_equal([1,2,3],table[0].to_a)
|
305
|
+
assert_equal(%w[a b c],table[0].attributes)
|
306
|
+
rec = table[0].dup
|
307
|
+
rec.attributes = %w[a b c d]
|
308
|
+
assert_raise(NoMethodError) { table << Object.new }
|
309
|
+
end
|
310
|
+
|
311
|
+
def test_append_hash
|
312
|
+
table = [[1,2,3],[4,5,6]].to_table(%w[a b c])
|
313
|
+
table << { "a" => 7, "c" => 9, "b" => 8 }
|
314
|
+
|
315
|
+
assert_equal [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c]), table
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_append_table
|
319
|
+
first = Ruport::Data::Table.new :column_names => %w[a b c],
|
320
|
+
:data => [[1,2,3],[4,5,6]]
|
321
|
+
|
322
|
+
second = Ruport::Data::Table.new :column_names => %w[a b c],
|
323
|
+
:data => [[7,8,9],[10,11,12]]
|
324
|
+
|
325
|
+
combo = first + second
|
326
|
+
|
327
|
+
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c],
|
328
|
+
:data => [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]), combo
|
329
|
+
end
|
330
|
+
|
331
|
+
def test_append_chain
|
332
|
+
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
333
|
+
table << [1,2,3] << [4,5,6] << [7,8,9]
|
334
|
+
assert_equal 3, table.length
|
335
|
+
assert_equal 5, table[1].b
|
336
|
+
end
|
337
|
+
end
|
338
|
+
|
339
|
+
class TestTableFormattingHooks < Test::Unit::TestCase
|
340
|
+
|
341
|
+
def test_to_hack_takes_args
|
342
|
+
a = Table(%w[hello mr crowley]) << %w[would you like] << %w[one red cat]
|
343
|
+
|
344
|
+
assert_equal "would,you,like\none,red,cat\n",
|
345
|
+
a.to_csv(:show_table_headers => false)
|
346
|
+
|
347
|
+
assert_equal "would,you,like\none,red,cat\n",
|
348
|
+
a.to_csv { |r| r.show_table_headers = false }
|
349
|
+
|
350
|
+
assert_equal "would\tyou\tlike\none\tred\tcat\n",
|
351
|
+
a.to_csv(:show_table_headers => false) { |r|
|
352
|
+
r.format_options = { :col_sep => "\t" }
|
353
|
+
}
|
354
|
+
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_to_hack
|
358
|
+
table = Ruport::Data::Table.new :column_names => %w[a b],
|
359
|
+
:data => [[1,2],[3,4],[5,6]]
|
360
|
+
assert_equal("a,b\n1,2\n3,4\n5,6\n",table.to_csv)
|
361
|
+
assert_raises(Ruport::Renderer::UnknownFormatError) { table.to_nothing }
|
362
|
+
end
|
363
|
+
|
364
|
+
def test_as_throws_proper_errors
|
365
|
+
a = [[1,2,3],[4,5,6]].to_table(%w[a b c])
|
366
|
+
assert_nothing_raised { a.as(:csv) }
|
367
|
+
assert_nothing_raised { a.to_csv }
|
368
|
+
assert_raises(Ruport::Renderer::UnknownFormatError) { a.as(:nothing) }
|
369
|
+
assert_raises(Ruport::Renderer::UnknownFormatError) { a.to_nothing }
|
370
|
+
end
|
371
|
+
|
372
|
+
end
|
373
|
+
|
374
|
+
class TestTableColumnOperations < Test::Unit::TestCase
|
375
|
+
|
376
|
+
def test_column
|
377
|
+
a = [[1,2,3],[4,5,6]].to_table(%w[a b c])
|
378
|
+
assert_equal [3,6], a.column(2)
|
379
|
+
assert_equal [2,5], a.column("b")
|
380
|
+
|
381
|
+
assert_raise(ArgumentError) { a.column("d") }
|
382
|
+
assert_raise(ArgumentError) { a.column(42) }
|
383
|
+
|
384
|
+
a = [[1],[2],[3],[4]].to_table
|
385
|
+
assert_equal [1,2,3,4], a.column(0)
|
386
|
+
end
|
387
|
+
|
388
|
+
def test_set_column_names
|
389
|
+
a = [[1,2,3],[4,5,6]].to_table
|
390
|
+
|
391
|
+
assert_equal([],a.column_names)
|
392
|
+
assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a } )
|
393
|
+
|
394
|
+
a.column_names = %w[a b c]
|
395
|
+
assert_equal(%w[a b c],a.column_names)
|
396
|
+
a.each { |r| assert_equal(%w[a b c], r.attributes) }
|
397
|
+
assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a })
|
398
|
+
|
399
|
+
a.column_names = %w[d e f]
|
400
|
+
assert_equal(%w[d e f],a.column_names)
|
401
|
+
a.each { |r| assert_equal(%w[d e f], r.attributes) }
|
402
|
+
assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a })
|
403
|
+
end
|
404
|
+
|
405
|
+
def test_add_column
|
406
|
+
|
407
|
+
a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
|
408
|
+
a.add_column("c")
|
409
|
+
assert_equal [[1,2,nil],[3,4,nil],[5,6,nil]].to_table(%w[a b c]), a
|
410
|
+
|
411
|
+
a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
|
412
|
+
a.add_column("c",:default => "x")
|
413
|
+
assert_equal [[1,2,'x'],[3,4,'x'],[5,6,'x']].to_table(%w[a b c]), a
|
414
|
+
|
415
|
+
b = a.dup
|
416
|
+
b.add_column("x",:before => "b")
|
417
|
+
assert_equal [[1,nil,2,'x'],
|
418
|
+
[3,nil,4,'x'],
|
419
|
+
[5,nil,6,'x']].to_table(%w[a x b c]), b
|
420
|
+
|
421
|
+
b = a.dup
|
422
|
+
b.add_column("x",:after => "b")
|
423
|
+
assert_equal [[1,2,nil,'x'],
|
424
|
+
[3,4,nil,'x'],
|
425
|
+
[5,6,nil,'x']].to_table(%w[a b x c]), b
|
426
|
+
|
427
|
+
|
428
|
+
a.add_column("d") { |r| r[0]+r[1] }
|
429
|
+
assert_equal(
|
430
|
+
[ [1,2,'x',3],
|
431
|
+
[3,4,'x',7],
|
432
|
+
[5,6,'x',11] ].to_table(%w[a b c d]), a)
|
433
|
+
|
434
|
+
a.add_column("x",:position => 1)
|
435
|
+
assert_equal(
|
436
|
+
[ [1,nil,2,'x',3],
|
437
|
+
[3,nil,4,'x',7],
|
438
|
+
[5,nil,6,'x',11] ].to_table(%w[a x b c d]), a)
|
439
|
+
|
440
|
+
end
|
441
|
+
|
442
|
+
def test_add_columns
|
443
|
+
a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
|
444
|
+
a.add_columns(%w[c d])
|
445
|
+
expected = [ [1,2,nil,nil],
|
446
|
+
[3,4,nil,nil],
|
447
|
+
[5,6,nil,nil] ].to_table(%w[a b c d])
|
448
|
+
|
449
|
+
assert_equal expected, a
|
450
|
+
|
451
|
+
a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
|
452
|
+
|
453
|
+
a.add_columns(%w[c d],:after => "a")
|
454
|
+
|
455
|
+
expected = [ [1,nil,nil,2],
|
456
|
+
[3,nil,nil,4],
|
457
|
+
[5,nil,nil,6], ].to_table(%w[a c d b])
|
458
|
+
|
459
|
+
assert_equal expected, a
|
460
|
+
|
461
|
+
a.add_columns(%w[x f],:before => "a")
|
462
|
+
|
463
|
+
expected = [ [nil,nil,1,nil,nil,2],
|
464
|
+
[nil,nil,3,nil,nil,4],
|
465
|
+
[nil,nil,5,nil,nil,6] ].to_table(%w[x f a c d b])
|
466
|
+
|
467
|
+
assert_equal expected, a
|
468
|
+
|
469
|
+
a = [[1,2,0],[3,4,0],[5,6,0]].to_table(%w[a b c])
|
470
|
+
|
471
|
+
a.add_columns(%w[x y],:default => 9, :position => 1)
|
472
|
+
|
473
|
+
expected = [[1,9,9,2,0],[3,9,9,4,0],[5,9,9,6,0]].to_table(%w[a x y b c])
|
474
|
+
|
475
|
+
assert_equal expected, a
|
476
|
+
|
477
|
+
a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
|
478
|
+
a.add_columns(%w[f x],:default => 0)
|
479
|
+
|
480
|
+
expected = [[1,2,0,0],[3,4,0,0],[5,6,0,0]].to_table(%w[a b f x])
|
481
|
+
assert_equal expected, a
|
482
|
+
|
483
|
+
assert_raises(RuntimeError) do
|
484
|
+
a.add_columns(%w[a b]) { }
|
485
|
+
end
|
486
|
+
|
487
|
+
end
|
488
|
+
|
489
|
+
def test_remove_column
|
490
|
+
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
491
|
+
b = a.dup
|
492
|
+
|
493
|
+
a.remove_column("b")
|
494
|
+
assert_equal Table(%w[a c]) { |t| t << [1,3] << [4,6] }, a
|
495
|
+
|
496
|
+
b.remove_column(2)
|
497
|
+
assert_equal Table(%w[a b]) { |t| t << [1,2] << [4,5] }, b
|
498
|
+
end
|
499
|
+
|
500
|
+
def test_remove_columns
|
501
|
+
a = Table(%w[a b c d]) { |t| t << [1,2,3,4] << [5,6,7,8] }
|
502
|
+
b = a.dup
|
503
|
+
a.remove_columns("b","d")
|
504
|
+
assert_equal Table(%w[a c]) { |t| t << [1,3] << [5,7] }, a
|
505
|
+
b.remove_columns(%w[a c])
|
506
|
+
assert_equal Table(%w[b d]) { |t| t << [2,4] << [6,8] }, b
|
507
|
+
end
|
508
|
+
|
509
|
+
def test_rename_column
|
510
|
+
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
511
|
+
a.rename_column("b","x")
|
512
|
+
assert_equal Table(%w[a x]) { |t| t << [1,2] << [3,4] }, a
|
513
|
+
end
|
514
|
+
|
515
|
+
def test_rename_columns
|
516
|
+
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
517
|
+
a.rename_columns(%w[a b], %w[x y])
|
518
|
+
assert_equal Table(%w[x y]) { |t| t << [1,2] << [3,4] }, a
|
519
|
+
|
520
|
+
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
521
|
+
a.rename_columns("a"=>"x","b"=>"y")
|
522
|
+
assert_equal Table(%w[x y]) { |t| t << [1,2] << [3,4] }, a
|
523
|
+
|
524
|
+
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
525
|
+
assert_raise(ArgumentError) { a.rename_columns(%w[a b], %w[x]) }
|
526
|
+
|
527
|
+
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
528
|
+
a.rename_columns { |r| r.to_sym }
|
529
|
+
assert_equal(a, Table(:a,:b,:c) { |t| t << [1,2,3] << [4,5,6] })
|
530
|
+
|
531
|
+
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
532
|
+
a.rename_columns(%w[a c]) { |r| r.to_sym }
|
533
|
+
assert_equal(a, Table(:a,"b",:c) { |t| t << [1,2,3] << [4,5,6] })
|
534
|
+
end
|
535
|
+
|
536
|
+
def test_swap_column
|
537
|
+
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
538
|
+
a.swap_column("a","b")
|
539
|
+
assert_equal Table(%w[b a]) { |t| t << [2,1] << [4,3] }, a
|
540
|
+
a.swap_column(1,0)
|
541
|
+
assert_equal Table(%w[a b]) { |t| t << [1,2] << [3,4] }, a
|
542
|
+
end
|
543
|
+
|
544
|
+
def test_replace_column
|
545
|
+
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
546
|
+
a.replace_column("b","d") { |r| r.b.to_s }
|
547
|
+
assert_equal Table(%w[a d c]) { |t| t << [1,"2",3] << [4,"5",6] }, a
|
548
|
+
a.replace_column("d") { |r| r.d.to_i }
|
549
|
+
assert_equal Table(%w[a d c]) { |t| t << [1,2,3] << [4,5,6] }, a
|
550
|
+
end
|
551
|
+
|
552
|
+
# --- BUG TRAPS ------------------------------------
|
553
|
+
|
554
|
+
def test_ensure_setting_column_names_changes_record_attributes
|
555
|
+
table = Ruport::Data::Table.new :column_names => %w[a b c],
|
556
|
+
:data => [[1,2,3],[4,5,6]]
|
557
|
+
|
558
|
+
assert_equal %w[a b c], table.column_names
|
559
|
+
assert_equal %w[a b c], table.data[0].attributes
|
560
|
+
assert_equal %w[a b c], table.data[1].attributes
|
561
|
+
|
562
|
+
table.column_names = %w[d e f]
|
563
|
+
|
564
|
+
assert_equal %w[d e f], table.column_names
|
565
|
+
assert_equal %w[d e f], table.data[0].attributes
|
566
|
+
assert_equal %w[d e f], table.data[1].attributes
|
567
|
+
end
|
568
|
+
|
628
569
|
def test_ensure_setting_column_names_later_does_not_break_replace_column
|
629
570
|
a = [[1,2,3],[4,5,6]].to_table(%w[a b c])
|
630
571
|
a.replace_column("b","q") { |r| r.a + r.c }
|
@@ -648,22 +589,103 @@ class TestTable < Test::Unit::TestCase
|
|
648
589
|
a.replace_column("b","foo") { |r| r.b + 1 }
|
649
590
|
|
650
591
|
assert_equal [[1,6,6],[4,9,9]].to_table(%w[a foo c]), a
|
651
|
-
end
|
592
|
+
end
|
652
593
|
|
653
|
-
|
654
|
-
a = Table(:record_class => DuckRecord)
|
655
|
-
assert_equal DuckRecord, a.record_class
|
594
|
+
end
|
656
595
|
|
657
|
-
|
658
|
-
|
596
|
+
class TestTableFromCSV < Test::Unit::TestCase
|
597
|
+
|
598
|
+
def test_csv_load
|
599
|
+
table = Ruport::Data::Table.load("test/samples/data.csv")
|
600
|
+
assert_equal %w[col1 col2 col3], table.column_names
|
601
|
+
rows = [%w[a b c],["d",nil,"e"]]
|
602
|
+
table.each { |r| assert_equal rows.shift, r.to_a
|
603
|
+
assert_equal %w[col1 col2 col3], r.attributes }
|
604
|
+
expected = [%w[1 2 3],%w[4 5 6]].to_table(%w[a b c])
|
605
|
+
|
606
|
+
# ticket:94
|
607
|
+
table = Ruport::Data::Table.load( "test/samples/data.tsv",
|
608
|
+
:csv_options => { :col_sep => "\t" } )
|
609
|
+
assert_equal expected, table
|
610
|
+
|
611
|
+
|
612
|
+
expected = ['c','e']
|
613
|
+
|
614
|
+
table = Ruport::Data::Table.load( "test/samples/data.csv", :csv_options =>
|
615
|
+
{ :headers => true, :header_converters => :symbol } ) do |s,r|
|
616
|
+
assert_equal expected.shift, r[:col3]
|
617
|
+
end
|
618
|
+
|
619
|
+
assert_equal [:col1,:col2,:col3], table.column_names
|
620
|
+
|
621
|
+
|
622
|
+
expected = ['c','e']
|
623
|
+
|
624
|
+
Ruport::Data::Table.load( "test/samples/data.csv",
|
625
|
+
:records => true ) do |s,r|
|
626
|
+
assert_equal expected.shift, r.col3
|
627
|
+
assert_kind_of Ruport::Data::Record, r
|
628
|
+
end
|
629
|
+
|
630
|
+
table = Ruport::Data::Table.load( "test/samples/data.csv",
|
631
|
+
:has_names => false )
|
632
|
+
assert_equal([],table.column_names)
|
633
|
+
assert_equal([%w[col1 col2 col3],%w[a b c],["d",nil,"e"]].to_table, table)
|
634
|
+
|
659
635
|
end
|
660
636
|
|
661
|
-
|
662
|
-
|
663
|
-
|
664
|
-
|
665
|
-
|
666
|
-
|
637
|
+
# ticket:76
|
638
|
+
def test_parse
|
639
|
+
|
640
|
+
assert_nothing_raised {
|
641
|
+
Ruport::Data::Table.parse("a,b,c\n1,2,3\n")
|
642
|
+
}
|
643
|
+
|
644
|
+
table = Ruport::Data::Table.parse("a,b,c\n1,2,3\n4,5,6\n")
|
645
|
+
expected = [%w[1 2 3],%w[4 5 6]].to_table(%w[a b c])
|
646
|
+
|
647
|
+
table = Ruport::Data::Table.parse( "a\tb\tc\n1\t2\t3\n4\t5\t6\n",
|
648
|
+
:csv_options => { :col_sep => "\t" } )
|
649
|
+
assert_equal expected, table
|
650
|
+
|
651
|
+
table = Ruport::Data::Table.parse( "a,b,c\n1,2,3\n4,5,6\n",
|
652
|
+
:has_names => false)
|
653
|
+
assert_equal([],table.column_names)
|
654
|
+
assert_equal([%w[a b c],%w[1 2 3], %w[4 5 6]].to_table, table)
|
655
|
+
|
656
|
+
|
657
|
+
end
|
658
|
+
|
659
|
+
def test_csv_block_form
|
660
|
+
expected = [%w[a b],%w[1 2],%w[3 4]]
|
661
|
+
t = Ruport::Data::Table.send(:get_table_from_csv,
|
662
|
+
:parse, "a,b\n1,2\n3,4",
|
663
|
+
:has_names => false) do |s,r|
|
664
|
+
assert_equal expected.shift, r
|
665
|
+
s << r
|
666
|
+
end
|
667
|
+
assert_equal [%w[a b],%w[1 2],%w[3 4]].to_table, t
|
668
|
+
end
|
669
|
+
|
670
|
+
# - BUG TRAPS --------------------
|
671
|
+
|
672
|
+
def test_ensure_using_csv_block_mode_works
|
673
|
+
expected = [%w[a b],%w[1 2],%w[3 4]]
|
674
|
+
t = Ruport::Data::Table.parse("a,b\n1,2\n3,4",:has_names => false) { |s,r|
|
675
|
+
assert_equal expected.shift, r
|
676
|
+
s << r
|
677
|
+
s << r
|
678
|
+
}
|
679
|
+
assert_equal [%w[a b],%w[a b],%w[1 2], %w[1 2],
|
680
|
+
%w[3 4],%w[3 4]].to_table, t
|
681
|
+
x = Ruport::Data::Table.load("test/samples/data.csv") { |s,r|
|
682
|
+
assert_kind_of Ruport::Data::Table, s
|
683
|
+
assert_kind_of Array, r
|
684
|
+
s << r
|
685
|
+
s << r
|
686
|
+
}
|
687
|
+
assert_equal 4, x.length
|
688
|
+
end
|
667
689
|
|
668
690
|
def test_ensure_csv_loading_accepts_table_options
|
669
691
|
a = Table("test/samples/addressbook.csv",:record_class => DuckRecord)
|
@@ -676,19 +698,9 @@ class TestTable < Test::Unit::TestCase
|
|
676
698
|
assert_kind_of(DuckRecord,r)
|
677
699
|
end
|
678
700
|
end
|
679
|
-
|
680
|
-
class MySubClass < Ruport::Data::Table; end
|
681
701
|
|
682
|
-
def test_ensure_table_subclasses_render_properly
|
683
|
-
a = MySubClass.new
|
684
|
-
a << [1,2,3] << [4,5,6]
|
685
|
-
assert_equal("1,2,3\n4,5,6\n",a.as(:csv))
|
686
|
-
end
|
687
|
-
|
688
702
|
end
|
689
703
|
|
690
|
-
class DuckRecord < Ruport::Data::Record; end
|
691
|
-
|
692
704
|
class TestTableKernelHack < Test::Unit::TestCase
|
693
705
|
|
694
706
|
def test_simple
|