ruport 1.6.3 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/AUTHORS +11 -0
- data/CHANGELOG.md +38 -0
- data/HACKING +1 -17
- data/README.md +97 -0
- data/Rakefile +9 -50
- data/examples/add_row_table.rb +46 -0
- data/examples/data/wine.csv +255 -0
- data/examples/pdf_grouping.rb +39 -0
- data/examples/pdf_table.rb +28 -0
- data/examples/pdf_table_from_csv.rb +26 -0
- data/examples/pdf_table_prawn.rb +30 -0
- data/examples/pdf_table_simple.rb +13 -0
- data/examples/row_renderer.rb +1 -1
- data/examples/simple_pdf_lines.rb +1 -1
- data/examples/trac_ticket_status.rb +1 -1
- data/lib/ruport/controller.rb +17 -21
- data/lib/ruport/data/feeder.rb +2 -2
- data/lib/ruport/data/grouping.rb +8 -8
- data/lib/ruport/data/record.rb +4 -4
- data/lib/ruport/data/table.rb +318 -206
- data/lib/ruport/formatter/csv.rb +6 -7
- data/lib/ruport/formatter/html.rb +13 -11
- data/lib/ruport/formatter/markdown.rb +105 -0
- data/lib/ruport/formatter/prawn_pdf.rb +159 -0
- data/lib/ruport/formatter/template.rb +1 -1
- data/lib/ruport/formatter/text.rb +1 -1
- data/lib/ruport/formatter.rb +54 -54
- data/lib/ruport/version.rb +1 -1
- data/lib/ruport.rb +7 -23
- data/test/controller_test.rb +201 -225
- data/test/csv_formatter_test.rb +36 -36
- data/test/data_feeder_test.rb +64 -64
- data/test/expected_outputs/prawn_pdf_formatter/pdf_basic.pdf.test +265 -0
- data/test/grouping_test.rb +103 -102
- data/test/helpers.rb +29 -10
- data/test/html_formatter_test.rb +46 -46
- data/test/markdown_formatter_test.rb +142 -0
- data/test/prawn_pdf_formatter_test.rb +108 -0
- data/test/record_test.rb +91 -91
- data/test/samples/sales.csv +21 -0
- data/test/table_pivot_test.rb +77 -26
- data/test/table_test.rb +376 -354
- data/test/template_test.rb +13 -13
- data/test/text_formatter_test.rb +52 -52
- data/util/bench/data/table/bench_column_manip.rb +0 -1
- data/util/bench/data/table/bench_dup.rb +0 -1
- data/util/bench/data/table/bench_init.rb +1 -2
- data/util/bench/data/table/bench_manip.rb +0 -1
- data/util/bench/formatter/bench_csv.rb +0 -1
- data/util/bench/formatter/bench_html.rb +0 -1
- data/util/bench/formatter/bench_pdf.rb +0 -1
- data/util/bench/formatter/bench_text.rb +0 -1
- metadata +131 -82
- data/README +0 -114
- data/lib/ruport/formatter/pdf.rb +0 -591
- data/test/pdf_formatter_test.rb +0 -354
data/test/table_test.rb
CHANGED
@@ -1,40 +1,39 @@
|
|
1
|
-
#!/usr/bin/env ruby -w
|
1
|
+
#!/usr/bin/env ruby -w
|
2
2
|
require File.join(File.expand_path(File.dirname(__FILE__)), "helpers")
|
3
|
-
TEST_SAMPLES = File.join(File.expand_path(File.dirname(__FILE__)), "samples")
|
4
3
|
|
5
4
|
class Person < Ruport::Data::Record
|
6
|
-
|
5
|
+
|
7
6
|
def name
|
8
7
|
first_name + " " + last_name
|
9
8
|
end
|
10
9
|
|
11
|
-
end
|
10
|
+
end
|
12
11
|
|
13
12
|
class DuckRecord < Ruport::Data::Record; end
|
14
13
|
|
15
|
-
class TestTable < Test
|
14
|
+
class TestTable < Minitest::Test
|
16
15
|
def test_constructors
|
17
16
|
table = Ruport::Data::Table.new
|
18
17
|
|
19
18
|
table2 = Ruport::Data::Table.new :column_names => %w[a b c]
|
20
19
|
table3 = Ruport::Data::Table.new :data => [[1,2,3]]
|
21
|
-
table4 = Ruport::Data::Table.new :column_names => %w[col1 col2 col3],
|
20
|
+
table4 = Ruport::Data::Table.new :column_names => %w[col1 col2 col3],
|
22
21
|
:data => [[1,2,3]]
|
23
22
|
tables = [table,table2,table3,table4]
|
24
23
|
|
25
24
|
tables.zip([[],%w[a b c], [], %w[col1 col2 col3]]).each do |t,n|
|
26
25
|
assert_equal n, t.column_names
|
27
26
|
|
28
|
-
t = Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
|
29
|
-
|
27
|
+
t = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
|
28
|
+
Ruport.Table(t.column_names, :data => t.data)
|
30
29
|
end
|
31
|
-
|
30
|
+
|
32
31
|
a = Ruport::Data::Record.new [1,2,3]
|
33
32
|
b = Ruport::Data::Record.new [1,2,3], :attributes => %w[col1 col2 col3]
|
34
|
-
tables.zip([[],[],[a],[b]]).each do |t,n|
|
35
|
-
assert_equal n, t.data
|
33
|
+
tables.zip([[],[],[a],[b]]).each do |t,n|
|
34
|
+
assert_equal n, t.data
|
36
35
|
end
|
37
|
-
end
|
36
|
+
end
|
38
37
|
|
39
38
|
context "when filtering data" do
|
40
39
|
|
@@ -42,106 +41,106 @@ class TestTable < Test::Unit::TestCase
|
|
42
41
|
@data = [[1,2,3],[4,5,6],[7,8,9]]
|
43
42
|
end
|
44
43
|
|
45
|
-
|
44
|
+
should "specify_filters_should_discard_unmatched_rows" do
|
46
45
|
table = Ruport::Data::Table.new(:column_names => %w[a b c],
|
47
46
|
:data => [[1,2,3],[4,5,6],[7,8,9]],
|
48
47
|
:filters => [ lambda { |r| r.a % 2 == 1 } ] )
|
49
|
-
assert_equal Table(%w[a b c]) << [1,2,3] << [7,8,9], table
|
50
|
-
end
|
51
|
-
|
52
|
-
|
48
|
+
assert_equal Ruport.Table(%w[a b c]) << [1,2,3] << [7,8,9], table
|
49
|
+
end
|
50
|
+
|
51
|
+
should "specify_filters_should_work_on_csvs" do
|
53
52
|
only_ids_less_than_3 = lambda { |r| r["id"].to_i < 3 }
|
54
|
-
table = Table(File.join(TEST_SAMPLES,"addressbook.csv"),
|
53
|
+
table = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"),
|
55
54
|
:filters => [only_ids_less_than_3])
|
56
55
|
assert_equal ["1","2"], table.map { |r| r["id"] }
|
57
56
|
end
|
58
|
-
end
|
59
|
-
|
57
|
+
end
|
58
|
+
|
60
59
|
context "when transforming data" do
|
61
|
-
|
62
|
-
|
63
|
-
@data = [[1,2,3],[4,5,6],[7,8,9]]
|
60
|
+
|
61
|
+
setup do
|
62
|
+
@data = [[1,2,3],[4,5,6],[7,8,9]]
|
64
63
|
end
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
stringify_c = lambda { |r| r.c = r.c.to_s }
|
64
|
+
|
65
|
+
should "specify_transforms_should_modify_table_data" do
|
66
|
+
|
67
|
+
stringify_c = lambda { |r| r.c = r.c.to_s }
|
69
68
|
add_two_to_all_int_cols = lambda { |r|
|
70
69
|
r.each_with_index do |c,i|
|
71
|
-
if
|
70
|
+
if Integer === c
|
72
71
|
r[i] += 2
|
73
72
|
end
|
74
73
|
end
|
75
|
-
|
76
|
-
}
|
77
|
-
|
74
|
+
|
75
|
+
}
|
76
|
+
|
78
77
|
table = Ruport::Data::Table.new(:column_names => %w[a b c],
|
79
78
|
:data => @data,
|
80
79
|
:transforms => [stringify_c,
|
81
80
|
add_two_to_all_int_cols])
|
82
|
-
assert_equal Table(%w[a b c]) << [3,4,"3"] << [6,7,"6"] << [9,10,"9"],
|
81
|
+
assert_equal Ruport.Table(%w[a b c]) << [3,4,"3"] << [6,7,"6"] << [9,10,"9"],
|
83
82
|
table
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
|
-
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
should "specify_transforms_should_work_on_csvs" do
|
88
87
|
ids_to_i = lambda { |r| r["id"] = r["id"].to_i }
|
89
|
-
table = Table(File.join(TEST_SAMPLES,"addressbook.csv"),
|
90
|
-
:filters => [ids_to_i])
|
91
|
-
assert_equal [1,2,3,4,5], table.map { |r| r["id"] }
|
88
|
+
table = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"),
|
89
|
+
:filters => [ids_to_i])
|
90
|
+
assert_equal [1,2,3,4,5], table.map { |r| r["id"] }
|
92
91
|
end
|
93
92
|
end
|
94
93
|
|
95
94
|
def test_to_group
|
96
|
-
a = Table(%w[a b c], :data => [[1,2,3],[4,5,6]]).to_group("Packrats")
|
95
|
+
a = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]]).to_group("Packrats")
|
97
96
|
b = Ruport::Data::Group.new( :data => [[1,2,3],[4,5,6]],
|
98
97
|
:column_names => %w[a b c],
|
99
98
|
:name => "Packrats" )
|
100
99
|
assert_equal a,b
|
101
100
|
end
|
102
|
-
|
101
|
+
|
103
102
|
def test_rows_with
|
104
|
-
table = Table(%w[a b c], :data => [[1,2,3],[1,3,4],[7,8,9]])
|
105
|
-
|
103
|
+
table = Ruport.Table(%w[a b c], :data => [[1,2,3],[1,3,4],[7,8,9]])
|
104
|
+
|
106
105
|
assert_equal([table[0],table[1]],table.rows_with("a" => 1))
|
107
106
|
assert_equal([table[1]],table.rows_with("a" => 1, "b" => 3))
|
108
107
|
assert_equal([table[0]],table.rows_with(:a => 1, :b => 2))
|
109
108
|
assert_equal([table[2]], table.rows_with_b(8))
|
110
109
|
assert_equal [table[1]], table.rows_with(%w[a b]) { |a,b| [a,b] == [1,3] }
|
111
110
|
end
|
112
|
-
|
111
|
+
|
113
112
|
def test_sigma
|
114
|
-
table = Table(%w[col1 col2], :data => [[1,2],[3,4],[5,6]])
|
113
|
+
table = Ruport.Table(%w[col1 col2], :data => [[1,2],[3,4],[5,6]])
|
115
114
|
assert table.respond_to?(:sigma)
|
116
115
|
assert table.respond_to?(:sum)
|
117
116
|
assert_equal(9,table.sigma(0))
|
118
117
|
assert_equal(9,table.sigma("col1"))
|
119
118
|
assert_equal(21,table.sigma { |r| r.col1 + r.col2 })
|
120
119
|
end
|
121
|
-
|
120
|
+
|
122
121
|
def test_sub_table
|
123
|
-
table = Table(%w[a b c d],
|
122
|
+
table = Ruport.Table(%w[a b c d],
|
124
123
|
:data => [ [1,2,3,4],[5,6,7,9],[10,11,12,13],[14,15,16,17] ])
|
125
|
-
|
126
|
-
assert_equal Table(%w[b c], :data => [[6,7],[11,12]]),
|
127
|
-
table.sub_table(%w[b c],1..-2)
|
128
|
-
|
129
|
-
assert_equal Table(%w[c d a], :data => [[3,4,1],[7,9,5]]),
|
130
|
-
table.sub_table(%w[c d a]) { |r| r.a < 10 }
|
131
|
-
|
132
|
-
assert_equal Table(%w[a c], :data => [[1,3],[5,7],[10,12],[14,16]]),
|
124
|
+
|
125
|
+
assert_equal Ruport.Table(%w[b c], :data => [[6,7],[11,12]]),
|
126
|
+
table.sub_table(%w[b c],1..-2)
|
127
|
+
|
128
|
+
assert_equal Ruport.Table(%w[c d a], :data => [[3,4,1],[7,9,5]]),
|
129
|
+
table.sub_table(%w[c d a]) { |r| r.a < 10 }
|
130
|
+
|
131
|
+
assert_equal Ruport.Table(%w[a c], :data => [[1,3],[5,7],[10,12],[14,16]]),
|
133
132
|
table.sub_table(%w[a c])
|
134
|
-
|
135
|
-
assert_equal Table(%w[a b c d], :data => [[10,11,12,13],[14,15,16,17]]),
|
136
|
-
table.sub_table { |r| r.c > 10 }
|
137
|
-
|
138
|
-
assert_equal Table(%w[a b c d], :data => [[10,11,12,13],[14,15,16,17]]),
|
139
|
-
table.sub_table(2..-1)
|
140
|
-
|
141
|
-
end
|
142
|
-
|
133
|
+
|
134
|
+
assert_equal Ruport.Table(%w[a b c d], :data => [[10,11,12,13],[14,15,16,17]]),
|
135
|
+
table.sub_table { |r| r.c > 10 }
|
136
|
+
|
137
|
+
assert_equal Ruport.Table(%w[a b c d], :data => [[10,11,12,13],[14,15,16,17]]),
|
138
|
+
table.sub_table(2..-1)
|
139
|
+
|
140
|
+
end
|
141
|
+
|
143
142
|
def test_subtable_records_have_correct_data
|
144
|
-
table = Table(%w[a b c d],
|
143
|
+
table = Ruport.Table(%w[a b c d],
|
145
144
|
:data => [ [1,2,3,4],[5,6,7,9],[10,11,12,13],[14,15,16,17] ])
|
146
145
|
sub = table.sub_table(%w[b c d]) {|r| r.a == 1 }
|
147
146
|
assert_equal({"b"=>2, "c"=>3, "d"=>4}, sub[0].data)
|
@@ -149,17 +148,17 @@ class TestTable < Test::Unit::TestCase
|
|
149
148
|
end
|
150
149
|
|
151
150
|
def test_reduce
|
152
|
-
table = Table(%w[a b c d],
|
151
|
+
table = Ruport.Table(%w[a b c d],
|
153
152
|
:data => [ [1,2,3,4],[5,6,7,9],[10,11,12,13],[14,15,16,17] ])
|
154
153
|
|
155
154
|
table.reduce(%w[b c],1..-2)
|
156
|
-
assert_equal Table(%w[b c], :data => [[6,7],[11,12]]), table
|
155
|
+
assert_equal Ruport.Table(%w[b c], :data => [[6,7],[11,12]]), table
|
157
156
|
|
158
|
-
table = Table(%w[a b c d],
|
157
|
+
table = Ruport.Table(%w[a b c d],
|
159
158
|
:data => [ [1,2,3,4],[5,6,7,9],[10,11,12,13],[14,15,16,17] ])
|
160
159
|
table.reduce(%w[c d a]) { |r| r.a < 10 }
|
161
160
|
|
162
|
-
assert_equal Table(%w[c d a], :data => [[3,4,1],[7,9,5]]), table
|
161
|
+
assert_equal Ruport.Table(%w[c d a], :data => [[3,4,1],[7,9,5]]), table
|
163
162
|
end
|
164
163
|
|
165
164
|
def test_reorder
|
@@ -169,58 +168,58 @@ class TestTable < Test::Unit::TestCase
|
|
169
168
|
rows = [%w[a c], %w[d e]]
|
170
169
|
table.each { |r| assert_equal rows.shift, r.to_a
|
171
170
|
assert_equal %w[col1 col3], r.attributes }
|
172
|
-
a = Table(%w[a b c], :data => [[1,2,3],[4,5,6]]).reorder 2,0
|
171
|
+
a = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]]).reorder 2,0
|
173
172
|
rows = [[3,1],[6,4]]
|
174
|
-
a.each { |r| assert_equal rows.shift, r.to_a
|
173
|
+
a.each { |r| assert_equal rows.shift, r.to_a
|
175
174
|
assert_equal %w[c a], r.attributes }
|
176
175
|
assert_equal %w[c a], a.column_names
|
177
176
|
|
178
|
-
b = Table(%w[a b c], :data => [[1,2,3],[4,5,6]]).reorder(%w[a c])
|
177
|
+
b = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]]).reorder(%w[a c])
|
179
178
|
rows = [[1,3],[4,6]]
|
180
|
-
b.each { |r|
|
179
|
+
b.each { |r|
|
181
180
|
assert_equal rows.shift, r.to_a
|
182
|
-
assert_equal %w[a c], r.attributes
|
181
|
+
assert_equal %w[a c], r.attributes
|
183
182
|
assert_equal b.column_names.object_id,
|
184
183
|
r.instance_eval{@attributes}.object_id
|
185
184
|
}
|
186
|
-
end
|
187
|
-
|
188
|
-
context "when sorting rows" do
|
189
|
-
|
190
|
-
|
191
|
-
@table = Table(%w[a b c]) << [1,2,3] << [6,1,8] << [9,1,4]
|
192
|
-
@table_with_nils = Table(%w[a b c]) << [1,nil,3] << [9,3,4] << [6,1,8]
|
185
|
+
end
|
186
|
+
|
187
|
+
context "when sorting rows" do
|
188
|
+
|
189
|
+
setup do
|
190
|
+
@table = Ruport.Table(%w[a b c]) << [1,2,3] << [6,1,8] << [9,1,4]
|
191
|
+
@table_with_nils = Ruport.Table(%w[a b c]) << [1,nil,3] << [9,3,4] << [6,1,8]
|
193
192
|
end
|
194
|
-
|
195
|
-
|
196
|
-
t = @table.sort_rows_by("a", :order => :descending )
|
197
|
-
assert_equal Table(%w[a b c]) << [9,1,4] << [6,1,8] << [1,2,3], t
|
198
|
-
|
199
|
-
t = @table.sort_rows_by("c", :order => :descending )
|
200
|
-
assert_equal Table(%w[a b c]) << [6,1,8] << [9,1,4] << [1,2,3], t
|
201
|
-
end
|
202
|
-
|
203
|
-
|
193
|
+
|
194
|
+
should "specify_should_sort_in_reverse_order_on_descending" do
|
195
|
+
t = @table.sort_rows_by("a", :order => :descending )
|
196
|
+
assert_equal Ruport.Table(%w[a b c]) << [9,1,4] << [6,1,8] << [1,2,3], t
|
197
|
+
|
198
|
+
t = @table.sort_rows_by("c", :order => :descending )
|
199
|
+
assert_equal Ruport.Table(%w[a b c]) << [6,1,8] << [9,1,4] << [1,2,3], t
|
200
|
+
end
|
201
|
+
|
202
|
+
should "specify_show_put_rows_with_nil_columns_after_sorted_rows" do
|
204
203
|
# should not effect when using columns that are all populated
|
205
|
-
t = @table_with_nils.sort_rows_by("a")
|
206
|
-
assert_equal Table(%w[a b c]) << [1,nil,3] << [6,1,8] << [9,3,4], t
|
207
|
-
|
204
|
+
t = @table_with_nils.sort_rows_by("a")
|
205
|
+
assert_equal Ruport.Table(%w[a b c]) << [1,nil,3] << [6,1,8] << [9,3,4], t
|
206
|
+
|
208
207
|
t = @table_with_nils.sort_rows_by("b")
|
209
|
-
assert_equal Table(%w[a b c]) << [6,1,8] << [9,3,4] << [1,nil,3], t
|
210
|
-
|
208
|
+
assert_equal Ruport.Table(%w[a b c]) << [6,1,8] << [9,3,4] << [1,nil,3], t
|
209
|
+
|
211
210
|
t = @table_with_nils.sort_rows_by("b", :order => :descending)
|
212
|
-
assert_equal Table(%w[a b c]) << [1,nil,3] << [9,3,4] << [6,1,8], t
|
211
|
+
assert_equal Ruport.Table(%w[a b c]) << [1,nil,3] << [9,3,4] << [6,1,8], t
|
213
212
|
end
|
214
|
-
|
215
|
-
|
213
|
+
|
214
|
+
should "specify_in_place_sort_should_allow_order_by" do
|
216
215
|
@table.sort_rows_by!("a", :order => :descending )
|
217
|
-
assert_equal Table(%w[a b c]) << [9,1,4] << [6,1,8] << [1,2,3], @table
|
216
|
+
assert_equal Ruport.Table(%w[a b c]) << [9,1,4] << [6,1,8] << [1,2,3], @table
|
218
217
|
end
|
219
|
-
|
220
|
-
|
218
|
+
|
219
|
+
should "specify_sort_rows_by" do
|
221
220
|
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
222
|
-
table << [1,2,3] << [6,1,8] << [9,1,4]
|
223
|
-
|
221
|
+
table << [1,2,3] << [6,1,8] << [9,1,4]
|
222
|
+
|
224
223
|
table2 = Ruport::Data::Table.new :column_names => [:a, :b, :c]
|
225
224
|
table2 << [1,2,3] << [6,1,8] << [9,1,4]
|
226
225
|
|
@@ -229,20 +228,20 @@ class TestTable < Test::Unit::TestCase
|
|
229
228
|
|
230
229
|
sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
|
231
230
|
sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
|
232
|
-
|
231
|
+
|
233
232
|
sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
|
234
|
-
sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
|
235
|
-
|
233
|
+
sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
|
234
|
+
|
236
235
|
sorted_table_bs = Ruport::Data::Table.new :column_names => [:a, :b, :c]
|
237
236
|
sorted_table_bs << [6,1,8] << [9,1,4] << [1,2,3]
|
238
|
-
|
237
|
+
|
239
238
|
assert_equal sorted_table_a, table.sort_rows_by {|r| r['a']}
|
240
239
|
assert_equal sorted_table_b, table.sort_rows_by(['b'])
|
241
240
|
assert_equal sorted_table_bc, table.sort_rows_by(['b', 'c'])
|
242
241
|
assert_equal sorted_table_bs, table2.sort_rows_by(:b)
|
243
|
-
end
|
244
|
-
|
245
|
-
|
242
|
+
end
|
243
|
+
|
244
|
+
should "specify_sort_rows_by!" do
|
246
245
|
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
247
246
|
table << [1,2,3] << [6,1,8] << [9,1,4]
|
248
247
|
|
@@ -251,37 +250,68 @@ class TestTable < Test::Unit::TestCase
|
|
251
250
|
|
252
251
|
sorted_table_b = Ruport::Data::Table.new :column_names => %w[a b c]
|
253
252
|
sorted_table_b << [6,1,8] << [9,1,4] << [1,2,3]
|
254
|
-
|
253
|
+
|
255
254
|
sorted_table_bc = Ruport::Data::Table.new :column_names => %w[a b c]
|
256
255
|
sorted_table_bc << [9,1,4] << [6,1,8] << [1,2,3]
|
257
|
-
|
256
|
+
|
258
257
|
table_a = table.dup
|
259
|
-
table_a.sort_rows_by! { |r| r['a'] }
|
260
|
-
|
258
|
+
table_a.sort_rows_by! { |r| r['a'] }
|
259
|
+
|
261
260
|
table_b = table.dup
|
262
261
|
table_b.sort_rows_by!("b")
|
263
|
-
|
262
|
+
|
264
263
|
table_bc = table.dup
|
265
|
-
table_bc.sort_rows_by!(['b', 'c'])
|
266
|
-
|
264
|
+
table_bc.sort_rows_by!(['b', 'c'])
|
265
|
+
|
267
266
|
assert_equal sorted_table_a, table_a
|
268
267
|
assert_equal sorted_table_b, table_b
|
269
268
|
assert_equal sorted_table_bc, table_bc
|
270
269
|
end
|
271
|
-
|
272
|
-
end
|
270
|
+
|
271
|
+
end
|
272
|
+
|
273
|
+
context "when adding rows" do
|
274
|
+
setup do
|
275
|
+
@columns = %w[a b c]
|
276
|
+
@data = [[1,2,3],[4,5,6],[7,8,9]]
|
277
|
+
@table = Ruport::Data::Table.new(:column_names => @columns, :data => @data)
|
278
|
+
@new_row = [-1,-2,-3]
|
279
|
+
end
|
280
|
+
|
281
|
+
should "specify_insert_at_the_beginning" do
|
282
|
+
@table.add_row(@new_row, :position => 0)
|
283
|
+
assert_equal Ruport::Data::Table.new(:column_names => @columns,
|
284
|
+
:data => [@new_row] + @data), @table
|
285
|
+
end
|
286
|
+
|
287
|
+
should "specify_insert_in_the_middle" do
|
288
|
+
@table.add_row(@new_row, :position => 2)
|
289
|
+
assert_equal Ruport::Data::Table.new(:column_names => @columns,
|
290
|
+
:data => [[1,2,3],[4,5,6],[-1,-2,-3],[7,8,9]]), @table
|
291
|
+
end
|
292
|
+
|
293
|
+
should "specify_insert_at_the_end" do
|
294
|
+
@table.add_row(@new_row)
|
295
|
+
assert_equal Ruport::Data::Table.new(:column_names => @columns,
|
296
|
+
:data => @data + [@new_row]), @table
|
297
|
+
end
|
298
|
+
|
299
|
+
should "return_itself" do
|
300
|
+
assert_equal @table.object_id, @table.add_row(@new_row).object_id
|
301
|
+
end
|
302
|
+
end
|
273
303
|
|
274
304
|
def test_record_class
|
275
|
-
a = Ruport::Data::Table.new( :column_names => %w[first_name last_name c],
|
305
|
+
a = Ruport::Data::Table.new( :column_names => %w[first_name last_name c],
|
276
306
|
:data =>[['joe','loop',3],['jim','blue',6]],
|
277
307
|
:record_class => Person )
|
278
|
-
assert_equal a, Table(%w[first_name last_name c],
|
308
|
+
assert_equal a, Ruport.Table(%w[first_name last_name c],
|
279
309
|
:data => [ ['joe','loop',3],['jim','blue',6] ])
|
280
310
|
assert_kind_of Person, a[0]
|
281
311
|
assert_equal 'joe loop', a[0].name
|
282
312
|
assert_equal 'jim blue', a[1].name
|
283
313
|
|
284
|
-
b = Table(%w[first_name last_name], :record_class => Person) do |t|
|
314
|
+
b = Ruport.Table(%w[first_name last_name], :record_class => Person) do |t|
|
285
315
|
t << { 'first_name' => 'joe', 'last_name' => 'frasier' }
|
286
316
|
t << { 'first_name' => 'brian', 'last_name' => 'black' }
|
287
317
|
end
|
@@ -289,72 +319,71 @@ class TestTable < Test::Unit::TestCase
|
|
289
319
|
b.each { |r| assert_kind_of Person, r }
|
290
320
|
|
291
321
|
assert_equal ['joe frasier', 'brian black'], b.map { |r| r.name }
|
292
|
-
end
|
322
|
+
end
|
293
323
|
|
294
324
|
## BUG Traps -------------------------------------------------
|
295
|
-
|
325
|
+
|
296
326
|
def test_ensure_table_creation_allows_record_coercion
|
297
|
-
table = Table([], :data => [[1,2,3],[4,5,6],[7,8,9]])
|
298
|
-
table_with_names = Table(%w[a b c], :data => [[1,2,3],[4,5,6],[7,8,9]])
|
299
|
-
|
327
|
+
table = Ruport.Table([], :data => [[1,2,3],[4,5,6],[7,8,9]])
|
328
|
+
table_with_names = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6],[7,8,9]])
|
329
|
+
|
300
330
|
a,b,c = nil
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
:data => table_with_names.to_a) }
|
331
|
+
a = Ruport.Table(%w[a b c], :data => table.to_a)
|
332
|
+
b = Ruport.Table(%w[d e f], :data => table.to_a)
|
333
|
+
c = Ruport.Table(table_with_names.column_names, :data => table_with_names.to_a)
|
305
334
|
|
306
335
|
[a,b,c].each { |t| assert_equal(3,t.length) }
|
307
336
|
assert_equal %w[a b c], a.column_names
|
308
337
|
a.each { |r|
|
309
338
|
assert_equal %w[a b c], r.attributes
|
310
|
-
|
339
|
+
r.a; r.b; r.c
|
311
340
|
[r.a,r.b,r.c].each { |i| assert(i.kind_of?(Numeric)) }
|
312
341
|
}
|
313
342
|
assert_equal %w[d e f], b.column_names
|
314
343
|
b.each { |r|
|
315
344
|
assert_equal %w[d e f], r.attributes
|
316
|
-
|
345
|
+
r.d; r.e; r.f
|
317
346
|
[r.d,r.e,r.f].each { |i| assert(i.kind_of?(Numeric)) }
|
318
347
|
}
|
319
348
|
c.each { |r|
|
320
|
-
|
349
|
+
r[0]; r[1]; r[2]
|
321
350
|
[r[0],r[1],r[2]].each { |i| assert(i.kind_of?(Numeric)) }
|
322
351
|
}
|
323
|
-
end
|
324
|
-
|
352
|
+
end
|
353
|
+
|
325
354
|
def test_ensure_coerce_sum
|
326
|
-
s = Table([], :data => [["1"],["3"],["5"]])
|
327
|
-
t = Table([], :data => [["1.23"],["1.5"]])
|
328
|
-
|
355
|
+
s = Ruport.Table([], :data => [["1"],["3"],["5"]])
|
356
|
+
t = Ruport.Table([], :data => [["1.23"],["1.5"]])
|
357
|
+
|
329
358
|
assert_equal(9,s.sum(0))
|
330
359
|
assert_equal(2.73,t.sum(0))
|
331
360
|
end
|
332
|
-
|
361
|
+
|
333
362
|
def test_to_yaml
|
334
363
|
require "yaml"
|
335
|
-
a = Table([])
|
336
|
-
|
337
|
-
a = Table(%w[first_name last_name],:record_class => Person) { |t|
|
338
|
-
t << %w[joe loop]
|
364
|
+
a = Ruport.Table([])
|
365
|
+
a.to_yaml
|
366
|
+
a = Ruport.Table(%w[first_name last_name],:record_class => Person) { |t|
|
367
|
+
t << %w[joe loop]
|
339
368
|
}
|
340
369
|
assert_equal "joe loop", a[0].name
|
341
|
-
|
342
|
-
end
|
343
|
-
|
370
|
+
a.to_yaml
|
371
|
+
end
|
372
|
+
|
344
373
|
def test_ensure_subtable_works_with_unnamed_tables
|
345
|
-
a = Table([], :data => [[1,2,3],[4,5,6]])
|
346
|
-
b = a.sub_table { |r| (r[0] % 2).zero? }
|
347
|
-
assert_equal Table([], :data => [[4,5,6]]), b
|
348
|
-
end
|
349
|
-
|
374
|
+
a = Ruport.Table([], :data => [[1,2,3],[4,5,6]])
|
375
|
+
b = a.sub_table { |r| (r[0] % 2).zero? }
|
376
|
+
assert_equal Ruport.Table([], :data => [[4,5,6]]), b
|
377
|
+
end
|
378
|
+
|
350
379
|
def test_ensure_appending_records_works_with_unnamed_tables
|
351
|
-
a = Table([], :data => [[1,2,3],[4,5,6]])
|
380
|
+
a = Ruport.Table([], :data => [[1,2,3],[4,5,6]])
|
352
381
|
a << Ruport::Data::Record.new([7,8,9])
|
353
|
-
assert_equal Table([], :data => [[1,2,3],[4,5,6],[7,8,9]]),a
|
382
|
+
assert_equal Ruport.Table([], :data => [[1,2,3],[4,5,6],[7,8,9]]),a
|
354
383
|
end
|
355
384
|
|
356
385
|
def test_ensure_propagate_record_class
|
357
|
-
a = Table(:record_class => DuckRecord)
|
386
|
+
a = Ruport.Table(:record_class => DuckRecord)
|
358
387
|
assert_equal DuckRecord, a.record_class
|
359
388
|
|
360
389
|
b = a.dup
|
@@ -362,270 +391,270 @@ class TestTable < Test::Unit::TestCase
|
|
362
391
|
end
|
363
392
|
|
364
393
|
def test_ensure_reorder_raises_on_bad_reorder_use
|
365
|
-
a = Table() << [1,2,3] << [4,5,6]
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
end
|
394
|
+
a = Ruport.Table() << [1,2,3] << [4,5,6]
|
395
|
+
assert_raises(ArgumentError) { a.reorder("a","b","c") }
|
396
|
+
assert_raises(ArgumentError) { a.reorder(%w[a b c]) }
|
397
|
+
assert_raises(ArgumentError) { a.reorder(2,1,0) }
|
398
|
+
end
|
370
399
|
|
371
400
|
class MySubClass < Ruport::Data::Table; end
|
372
|
-
|
401
|
+
|
373
402
|
def test_ensure_table_subclasses_render_properly
|
374
403
|
a = MySubClass.new
|
375
404
|
a << [1,2,3] << [4,5,6]
|
376
405
|
assert_equal("1,2,3\n4,5,6\n",a.as(:csv))
|
377
406
|
end
|
378
407
|
|
379
|
-
end
|
408
|
+
end
|
380
409
|
|
381
|
-
class TestTableAppendOperations < Test
|
382
|
-
def test_append_record
|
410
|
+
class TestTableAppendOperations < Minitest::Test
|
411
|
+
def test_append_record
|
383
412
|
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
384
413
|
table << Ruport::Data::Record.new([1,2,3], :attributes => %w[a b c])
|
385
414
|
assert_equal([1,2,3],table[0].to_a)
|
386
415
|
assert_equal(%w[a b c],table[0].attributes)
|
387
416
|
rec = table[0].dup
|
388
417
|
rec.attributes = %w[a b c d]
|
389
|
-
|
418
|
+
assert_raises(NoMethodError) { table << Object.new }
|
390
419
|
end
|
391
|
-
|
420
|
+
|
392
421
|
def test_append_hash
|
393
|
-
table = Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
|
422
|
+
table = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
|
394
423
|
table << { "a" => 7, "c" => 9, "b" => 8 }
|
395
|
-
|
396
|
-
assert_equal Table(%w[a b c], :data => [[1,2,3],[4,5,6],[7,8,9]]), table
|
424
|
+
|
425
|
+
assert_equal Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6],[7,8,9]]), table
|
397
426
|
end
|
398
427
|
|
399
428
|
def test_append_table
|
400
429
|
first = Ruport::Data::Table.new :column_names => %w[a b c],
|
401
430
|
:data => [[1,2,3],[4,5,6]]
|
402
|
-
|
431
|
+
|
403
432
|
second = Ruport::Data::Table.new :column_names => %w[a b c],
|
404
433
|
:data => [[7,8,9],[10,11,12]]
|
405
|
-
|
434
|
+
|
406
435
|
combo = first + second
|
407
|
-
|
408
|
-
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c],
|
436
|
+
|
437
|
+
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c],
|
409
438
|
:data => [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]), combo
|
410
|
-
end
|
411
|
-
|
439
|
+
end
|
440
|
+
|
412
441
|
def test_append_chain
|
413
442
|
table = Ruport::Data::Table.new :column_names => %w[a b c]
|
414
|
-
table << [1,2,3] << [4,5,6] << [7,8,9]
|
443
|
+
table << [1,2,3] << [4,5,6] << [7,8,9]
|
415
444
|
assert_equal 3, table.length
|
416
445
|
assert_equal 5, table[1].b
|
417
|
-
end
|
446
|
+
end
|
418
447
|
end
|
419
448
|
|
420
|
-
class TestTableFormattingHooks < Test
|
421
|
-
|
449
|
+
class TestTableFormattingHooks < Minitest::Test
|
450
|
+
|
422
451
|
def test_to_hack_takes_args
|
423
|
-
a = Table(%w[hello mr crowley]) << %w[would you like] << %w[one red cat]
|
424
|
-
|
452
|
+
a = Ruport.Table(%w[hello mr crowley]) << %w[would you like] << %w[one red cat]
|
453
|
+
|
425
454
|
assert_equal "would,you,like\none,red,cat\n",
|
426
455
|
a.to_csv(:show_table_headers => false)
|
427
|
-
|
456
|
+
|
428
457
|
assert_equal "would,you,like\none,red,cat\n",
|
429
|
-
a.to_csv { |r| r.options.show_table_headers = false }
|
430
|
-
|
458
|
+
a.to_csv { |r| r.options.show_table_headers = false }
|
459
|
+
|
431
460
|
assert_equal "would\tyou\tlike\none\tred\tcat\n",
|
432
461
|
a.to_csv(:show_table_headers => false) { |r|
|
433
462
|
r.options.format_options = { :col_sep => "\t" }
|
434
463
|
}
|
435
|
-
end
|
436
|
-
|
464
|
+
end
|
465
|
+
|
437
466
|
def test_to_hack
|
438
|
-
table = Ruport::Data::Table.new :column_names => %w[a b],
|
467
|
+
table = Ruport::Data::Table.new :column_names => %w[a b],
|
439
468
|
:data => [[1,2],[3,4],[5,6]]
|
440
469
|
assert_equal("a,b\n1,2\n3,4\n5,6\n",table.to_csv)
|
441
470
|
assert_raises(Ruport::Controller::UnknownFormatError) { table.to_nothing }
|
442
471
|
end
|
443
472
|
|
444
473
|
def test_as_throws_proper_errors
|
445
|
-
a = Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
|
446
|
-
|
447
|
-
|
474
|
+
a = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
|
475
|
+
a.as(:csv)
|
476
|
+
a.to_csv
|
448
477
|
assert_raises(Ruport::Controller::UnknownFormatError) { a.as(:nothing) }
|
449
478
|
assert_raises(Ruport::Controller::UnknownFormatError) { a.to_nothing }
|
450
479
|
end
|
451
|
-
|
480
|
+
|
452
481
|
end
|
453
482
|
|
454
|
-
class TestTableColumnOperations < Test
|
455
|
-
|
483
|
+
class TestTableColumnOperations < Minitest::Test
|
484
|
+
|
456
485
|
def test_column
|
457
|
-
a = Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
|
486
|
+
a = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
|
458
487
|
assert_equal [3,6], a.column(2)
|
459
|
-
assert_equal [2,5], a.column("b")
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
a = Table([], :data => [[1],[2],[3],[4]])
|
465
|
-
assert_equal [1,2,3,4], a.column(0)
|
466
|
-
end
|
467
|
-
|
488
|
+
assert_equal [2,5], a.column("b")
|
489
|
+
|
490
|
+
assert_raises(ArgumentError) { a.column("d") }
|
491
|
+
assert_raises(ArgumentError) { a.column(42) }
|
492
|
+
|
493
|
+
a = Ruport.Table([], :data => [[1],[2],[3],[4]])
|
494
|
+
assert_equal [1,2,3,4], a.column(0)
|
495
|
+
end
|
496
|
+
|
468
497
|
def test_set_column_names
|
469
|
-
a = Table([], :data => [[1,2,3],[4,5,6]])
|
470
|
-
|
498
|
+
a = Ruport.Table([], :data => [[1,2,3],[4,5,6]])
|
499
|
+
|
471
500
|
assert_equal([],a.column_names)
|
472
501
|
assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a } )
|
473
|
-
|
502
|
+
|
474
503
|
a.column_names = %w[a b c]
|
475
504
|
assert_equal(%w[a b c],a.column_names)
|
476
|
-
a.each { |r| assert_equal(%w[a b c], r.attributes) }
|
505
|
+
a.each { |r| assert_equal(%w[a b c], r.attributes) }
|
477
506
|
assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a })
|
478
|
-
|
507
|
+
|
479
508
|
a.column_names = %w[d e f]
|
480
509
|
assert_equal(%w[d e f],a.column_names)
|
481
510
|
a.each { |r| assert_equal(%w[d e f], r.attributes) }
|
482
|
-
assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a })
|
483
|
-
end
|
484
|
-
|
511
|
+
assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a })
|
512
|
+
end
|
513
|
+
|
485
514
|
def test_add_column
|
486
|
-
a = Table(%w[a b], :data => [[1,2],[3,4],[5,6]])
|
515
|
+
a = Ruport.Table(%w[a b], :data => [[1,2],[3,4],[5,6]])
|
487
516
|
a.add_column("c")
|
488
|
-
assert_equal Table(%w[a b c], :data => [[1,2,nil],[3,4,nil],[5,6,nil]]), a
|
517
|
+
assert_equal Ruport.Table(%w[a b c], :data => [[1,2,nil],[3,4,nil],[5,6,nil]]), a
|
489
518
|
|
490
|
-
a = Table(%w[a b], :data => [[1,2],[3,4],[5,6]])
|
519
|
+
a = Ruport.Table(%w[a b], :data => [[1,2],[3,4],[5,6]])
|
491
520
|
a.add_column("c",:default => "x")
|
492
|
-
assert_equal Table(%w[a b c], :data => [[1,2,'x'],[3,4,'x'],[5,6,'x']]), a
|
521
|
+
assert_equal Ruport.Table(%w[a b c], :data => [[1,2,'x'],[3,4,'x'],[5,6,'x']]), a
|
493
522
|
|
494
523
|
b = a.dup
|
495
524
|
b.add_column("x",:before => "b")
|
496
|
-
assert_equal Table(%w[a x b c],
|
497
|
-
:data => [[1,nil,2,'x'],[3,nil,4,'x'],[5,nil,6,'x']]), b
|
525
|
+
assert_equal Ruport.Table(%w[a x b c],
|
526
|
+
:data => [[1,nil,2,'x'],[3,nil,4,'x'],[5,nil,6,'x']]), b
|
498
527
|
|
499
528
|
b = a.dup
|
500
529
|
b.add_column("x",:after => "b")
|
501
|
-
assert_equal Table(%w[a b x c],
|
502
|
-
:data => [[1,2,nil,'x'],[3,4,nil,'x'],[5,6,nil,'x']]), b
|
530
|
+
assert_equal Ruport.Table(%w[a b x c],
|
531
|
+
:data => [[1,2,nil,'x'],[3,4,nil,'x'],[5,6,nil,'x']]), b
|
503
532
|
|
504
533
|
|
505
534
|
a.add_column("d") { |r| r[0]+r[1] }
|
506
|
-
assert_equal Table(%w[a b c d],
|
535
|
+
assert_equal Ruport.Table(%w[a b c d],
|
507
536
|
:data => [ [1,2,'x',3],[3,4,'x',7],[5,6,'x',11] ]), a
|
508
537
|
|
509
538
|
a.add_column("x",:position => 1)
|
510
|
-
assert_equal Table(%w[a x b c d],
|
539
|
+
assert_equal Ruport.Table(%w[a x b c d],
|
511
540
|
:data => [ [1,nil,2,'x',3],[3,nil,4,'x',7],[5,nil,6,'x',11] ]), a
|
512
541
|
end
|
513
542
|
|
514
543
|
def test_add_columns
|
515
|
-
a = Table(%w[a b], :data => [[1,2],[3,4],[5,6]])
|
544
|
+
a = Ruport.Table(%w[a b], :data => [[1,2],[3,4],[5,6]])
|
516
545
|
a.add_columns(%w[c d])
|
517
|
-
expected = Table(%w[a b c d],
|
546
|
+
expected = Ruport.Table(%w[a b c d],
|
518
547
|
:data => [ [1,2,nil,nil],[3,4,nil,nil],[5,6,nil,nil] ])
|
519
548
|
|
520
|
-
assert_equal expected, a
|
549
|
+
assert_equal expected, a
|
521
550
|
|
522
|
-
a = Table(%w[a b], :data => [[1,2],[3,4],[5,6]])
|
551
|
+
a = Ruport.Table(%w[a b], :data => [[1,2],[3,4],[5,6]])
|
523
552
|
|
524
553
|
a.add_columns(%w[c d],:after => "a")
|
525
554
|
|
526
|
-
expected = Table(%w[a c d b],
|
527
|
-
:data => [ [1,nil,nil,2],[3,nil,nil,4],[5,nil,nil,6], ])
|
555
|
+
expected = Ruport.Table(%w[a c d b],
|
556
|
+
:data => [ [1,nil,nil,2],[3,nil,nil,4],[5,nil,nil,6], ])
|
528
557
|
|
529
|
-
assert_equal expected, a
|
558
|
+
assert_equal expected, a
|
530
559
|
|
531
560
|
a.add_columns(%w[x f],:before => "a")
|
532
561
|
|
533
|
-
expected = Table(%w[x f a c d b],
|
562
|
+
expected = Ruport.Table(%w[x f a c d b],
|
534
563
|
:data => [ [nil,nil,1,nil,nil,2],
|
535
564
|
[nil,nil,3,nil,nil,4],
|
536
565
|
[nil,nil,5,nil,nil,6] ])
|
537
566
|
|
538
|
-
assert_equal expected, a
|
567
|
+
assert_equal expected, a
|
539
568
|
|
540
|
-
a = Table(%w[a b c], :data => [[1,2,0],[3,4,0],[5,6,0]])
|
569
|
+
a = Ruport.Table(%w[a b c], :data => [[1,2,0],[3,4,0],[5,6,0]])
|
541
570
|
|
542
571
|
a.add_columns(%w[x y],:default => 9, :position => 1)
|
543
572
|
|
544
|
-
expected = Table(%w[a x y b c],
|
545
|
-
:data => [[1,9,9,2,0],[3,9,9,4,0],[5,9,9,6,0]])
|
573
|
+
expected = Ruport.Table(%w[a x y b c],
|
574
|
+
:data => [[1,9,9,2,0],[3,9,9,4,0],[5,9,9,6,0]])
|
546
575
|
|
547
576
|
assert_equal expected, a
|
548
577
|
|
549
|
-
a = Table(%w[a b], :data => [[1,2],[3,4],[5,6]])
|
578
|
+
a = Ruport.Table(%w[a b], :data => [[1,2],[3,4],[5,6]])
|
550
579
|
a.add_columns(%w[f x],:default => 0)
|
551
580
|
|
552
|
-
expected = Table(%w[a b f x], :data => [[1,2,0,0],[3,4,0,0],[5,6,0,0]])
|
581
|
+
expected = Ruport.Table(%w[a b f x], :data => [[1,2,0,0],[3,4,0,0],[5,6,0,0]])
|
553
582
|
assert_equal expected, a
|
554
583
|
|
555
|
-
assert_raises(RuntimeError) do
|
556
|
-
a.add_columns(%w[a b]) { }
|
584
|
+
assert_raises(RuntimeError) do
|
585
|
+
a.add_columns(%w[a b]) { }
|
557
586
|
end
|
558
587
|
end
|
559
588
|
|
560
589
|
def test_remove_column
|
561
|
-
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
590
|
+
a = Ruport.Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
562
591
|
b = a.dup
|
563
592
|
|
564
593
|
a.remove_column("b")
|
565
|
-
assert_equal Table(%w[a c]) { |t| t << [1,3] << [4,6] }, a
|
594
|
+
assert_equal Ruport.Table(%w[a c]) { |t| t << [1,3] << [4,6] }, a
|
566
595
|
|
567
596
|
b.remove_column(2)
|
568
|
-
assert_equal Table(%w[a b]) { |t| t << [1,2] << [4,5] }, b
|
569
|
-
end
|
597
|
+
assert_equal Ruport.Table(%w[a b]) { |t| t << [1,2] << [4,5] }, b
|
598
|
+
end
|
570
599
|
|
571
600
|
def test_remove_columns
|
572
|
-
a = Table(%w[a b c d]) { |t| t << [1,2,3,4] << [5,6,7,8] }
|
601
|
+
a = Ruport.Table(%w[a b c d]) { |t| t << [1,2,3,4] << [5,6,7,8] }
|
573
602
|
b = a.dup
|
574
603
|
a.remove_columns("b","d")
|
575
|
-
assert_equal Table(%w[a c]) { |t| t << [1,3] << [5,7] }, a
|
604
|
+
assert_equal Ruport.Table(%w[a c]) { |t| t << [1,3] << [5,7] }, a
|
576
605
|
b.remove_columns(%w[a c])
|
577
|
-
assert_equal Table(%w[b d]) { |t| t << [2,4] << [6,8] }, b
|
578
|
-
end
|
606
|
+
assert_equal Ruport.Table(%w[b d]) { |t| t << [2,4] << [6,8] }, b
|
607
|
+
end
|
579
608
|
|
580
609
|
def test_rename_column
|
581
|
-
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
610
|
+
a = Ruport.Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
582
611
|
a.rename_column("b","x")
|
583
|
-
assert_equal Table(%w[a x]) { |t| t << [1,2] << [3,4] }, a
|
584
|
-
end
|
612
|
+
assert_equal Ruport.Table(%w[a x]) { |t| t << [1,2] << [3,4] }, a
|
613
|
+
end
|
585
614
|
|
586
615
|
def test_rename_columns
|
587
|
-
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
616
|
+
a = Ruport.Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
588
617
|
a.rename_columns(%w[a b], %w[x y])
|
589
|
-
assert_equal Table(%w[x y]) { |t| t << [1,2] << [3,4] }, a
|
618
|
+
assert_equal Ruport.Table(%w[x y]) { |t| t << [1,2] << [3,4] }, a
|
590
619
|
|
591
|
-
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
620
|
+
a = Ruport.Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
592
621
|
a.rename_columns("a"=>"x","b"=>"y")
|
593
|
-
assert_equal Table(%w[x y]) { |t| t << [1,2] << [3,4] }, a
|
622
|
+
assert_equal Ruport.Table(%w[x y]) { |t| t << [1,2] << [3,4] }, a
|
594
623
|
|
595
|
-
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
596
|
-
|
624
|
+
a = Ruport.Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
625
|
+
assert_raises(ArgumentError) { a.rename_columns(%w[a b], %w[x]) }
|
597
626
|
|
598
|
-
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
627
|
+
a = Ruport.Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
599
628
|
a.rename_columns { |r| r.to_sym }
|
600
|
-
assert_equal(a, Table(:a,:b,:c) { |t| t << [1,2,3] << [4,5,6] })
|
629
|
+
assert_equal(a, Ruport.Table(:a,:b,:c) { |t| t << [1,2,3] << [4,5,6] })
|
601
630
|
|
602
|
-
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
631
|
+
a = Ruport.Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
603
632
|
a.rename_columns(%w[a c]) { |r| r.to_sym }
|
604
|
-
assert_equal(a, Table(:a,"b",:c) { |t| t << [1,2,3] << [4,5,6] })
|
605
|
-
end
|
633
|
+
assert_equal(a, Ruport.Table(:a,"b",:c) { |t| t << [1,2,3] << [4,5,6] })
|
634
|
+
end
|
606
635
|
|
607
636
|
def test_swap_column
|
608
|
-
a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
637
|
+
a = Ruport.Table(%w[a b]) { |t| t << [1,2] << [3,4] }
|
609
638
|
a.swap_column("a","b")
|
610
|
-
assert_equal Table(%w[b a]) { |t| t << [2,1] << [4,3] }, a
|
639
|
+
assert_equal Ruport.Table(%w[b a]) { |t| t << [2,1] << [4,3] }, a
|
611
640
|
a.swap_column(1,0)
|
612
|
-
assert_equal Table(%w[a b]) { |t| t << [1,2] << [3,4] }, a
|
613
|
-
end
|
641
|
+
assert_equal Ruport.Table(%w[a b]) { |t| t << [1,2] << [3,4] }, a
|
642
|
+
end
|
614
643
|
|
615
644
|
def test_replace_column
|
616
|
-
a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
645
|
+
a = Ruport.Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
|
617
646
|
a.replace_column("b","d") { |r| r.b.to_s }
|
618
|
-
assert_equal Table(%w[a d c]) { |t| t << [1,"2",3] << [4,"5",6] }, a
|
647
|
+
assert_equal Ruport.Table(%w[a d c]) { |t| t << [1,"2",3] << [4,"5",6] }, a
|
619
648
|
a.replace_column("d") { |r| r.d.to_i }
|
620
|
-
assert_equal Table(%w[a d c]) { |t| t << [1,2,3] << [4,5,6] }, a
|
621
|
-
end
|
622
|
-
|
623
|
-
# --- BUG TRAPS ------------------------------------
|
624
|
-
|
649
|
+
assert_equal Ruport.Table(%w[a d c]) { |t| t << [1,2,3] << [4,5,6] }, a
|
650
|
+
end
|
651
|
+
|
652
|
+
# --- BUG TRAPS ------------------------------------
|
653
|
+
|
625
654
|
def test_ensure_setting_column_names_changes_record_attributes
|
626
|
-
table = Ruport::Data::Table.new :column_names => %w[a b c],
|
655
|
+
table = Ruport::Data::Table.new :column_names => %w[a b c],
|
627
656
|
:data => [[1,2,3],[4,5,6]]
|
628
|
-
|
657
|
+
|
629
658
|
assert_equal %w[a b c], table.column_names
|
630
659
|
assert_equal %w[a b c], table.data[0].attributes
|
631
660
|
assert_equal %w[a b c], table.data[1].attributes
|
@@ -635,22 +664,22 @@ class TestTableColumnOperations < Test::Unit::TestCase
|
|
635
664
|
assert_equal %w[d e f], table.column_names
|
636
665
|
assert_equal %w[d e f], table.data[0].attributes
|
637
666
|
assert_equal %w[d e f], table.data[1].attributes
|
638
|
-
end
|
639
|
-
|
667
|
+
end
|
668
|
+
|
640
669
|
def test_ensure_setting_column_names_later_does_not_break_replace_column
|
641
|
-
a = Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
|
670
|
+
a = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
|
642
671
|
a.replace_column("b","q") { |r| r.a + r.c }
|
643
672
|
a.column_names = %w[d e f]
|
644
|
-
assert_equal Table(%w[d e f], :data => [[1,4,3],[4,10,6]]), a
|
673
|
+
assert_equal Ruport.Table(%w[d e f], :data => [[1,4,3],[4,10,6]]), a
|
645
674
|
|
646
|
-
a = Table([], :data => [[1,2,3],[4,5,6]])
|
675
|
+
a = Ruport.Table([], :data => [[1,2,3],[4,5,6]])
|
647
676
|
|
648
677
|
a.replace_column(1) { |r| r[0] + r[2] }
|
649
678
|
|
650
679
|
a.column_names = %w[d e f]
|
651
|
-
assert_equal Table(%w[d e f], :data => [[1,4,3],[4,10,6]]), a
|
680
|
+
assert_equal Ruport.Table(%w[d e f], :data => [[1,4,3],[4,10,6]]), a
|
652
681
|
|
653
|
-
a = Table([], :data => [[1,2,3],[4,5,6]])
|
682
|
+
a = Ruport.Table([], :data => [[1,2,3],[4,5,6]])
|
654
683
|
|
655
684
|
a.replace_column(2) { |r| r[0] + 5 }
|
656
685
|
|
@@ -659,98 +688,98 @@ class TestTableColumnOperations < Test::Unit::TestCase
|
|
659
688
|
a.replace_column("b") { |r| r.a + 4 }
|
660
689
|
a.replace_column("b","foo") { |r| r.b + 1 }
|
661
690
|
|
662
|
-
assert_equal Table(%w[a foo c], :data => [[1,6,6],[4,9,9]]), a
|
663
|
-
end
|
691
|
+
assert_equal Ruport.Table(%w[a foo c], :data => [[1,6,6],[4,9,9]]), a
|
692
|
+
end
|
664
693
|
|
665
694
|
def test_ensure_renaming_a_missing_column_fails_silently
|
666
|
-
a = Table(%w[a b c])
|
667
|
-
|
668
|
-
|
669
|
-
|
695
|
+
a = Ruport.Table(%w[a b c])
|
696
|
+
|
697
|
+
a.rename_column("d", "z")
|
698
|
+
|
670
699
|
end
|
671
700
|
|
672
|
-
end
|
701
|
+
end
|
702
|
+
|
703
|
+
class TestTableFromCSV < Minitest::Test
|
673
704
|
|
674
|
-
class TestTableFromCSV < Test::Unit::TestCase
|
675
|
-
|
676
705
|
def test_csv_load
|
677
706
|
table = Ruport::Data::Table.load(File.join(TEST_SAMPLES,"data.csv"))
|
678
707
|
assert_equal %w[col1 col2 col3], table.column_names
|
679
708
|
rows = [%w[a b c],["d",nil,"e"]]
|
680
709
|
table.each { |r| assert_equal rows.shift, r.to_a
|
681
710
|
assert_equal %w[col1 col2 col3], r.attributes }
|
682
|
-
expected = Table(%w[a b c], :data => [%w[1 2 3],%w[4 5 6]])
|
683
|
-
|
711
|
+
expected = Ruport.Table(%w[a b c], :data => [%w[1 2 3],%w[4 5 6]])
|
712
|
+
|
684
713
|
# ticket:94
|
685
|
-
table = Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.tsv"),
|
714
|
+
table = Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.tsv"),
|
686
715
|
:csv_options => { :col_sep => "\t" } )
|
687
|
-
assert_equal expected, table
|
716
|
+
assert_equal expected, table
|
688
717
|
|
689
718
|
expected = ['c','e']
|
690
|
-
|
719
|
+
|
691
720
|
table = Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.csv"),
|
692
721
|
:csv_options => { :headers => true, :header_converters => :symbol }
|
693
|
-
) do |
|
722
|
+
) do |_s,r|
|
694
723
|
assert_equal expected.shift, r[:col3]
|
695
724
|
end
|
696
725
|
|
697
726
|
assert_equal [:col1,:col2,:col3], table.column_names
|
698
727
|
|
699
728
|
expected = ['c','e']
|
700
|
-
|
701
|
-
Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.csv"),
|
702
|
-
:records => true ) do |
|
729
|
+
|
730
|
+
Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.csv"),
|
731
|
+
:records => true ) do |_s,r|
|
703
732
|
assert_equal expected.shift, r.col3
|
704
733
|
assert_kind_of Ruport::Data::Record, r
|
705
734
|
end
|
706
|
-
|
707
|
-
table = Ruport::Data::Table.load( File.join(TEST_SAMPLES, "data.csv"),
|
735
|
+
|
736
|
+
table = Ruport::Data::Table.load( File.join(TEST_SAMPLES, "data.csv"),
|
708
737
|
:has_names => false )
|
709
738
|
assert_equal([],table.column_names)
|
710
|
-
assert_equal(Table([],
|
739
|
+
assert_equal(Ruport.Table([],
|
711
740
|
:data => [%w[col1 col2 col3],%w[a b c],["d",nil,"e"]]), table)
|
712
741
|
end
|
713
742
|
|
714
743
|
# ticket:76
|
715
744
|
def test_parse
|
716
|
-
|
717
|
-
|
718
|
-
|
719
|
-
|
745
|
+
|
746
|
+
Ruport::Data::Table.parse("a,b,c\n1,2,3\n")
|
747
|
+
|
748
|
+
|
720
749
|
table = Ruport::Data::Table.parse("a,b,c\n1,2,3\n4,5,6\n")
|
721
|
-
expected = Table(%w[a b c], :data => [%w[1 2 3],%w[4 5 6]])
|
722
|
-
|
723
|
-
table = Ruport::Data::Table.parse( "a\tb\tc\n1\t2\t3\n4\t5\t6\n",
|
750
|
+
expected = Ruport.Table(%w[a b c], :data => [%w[1 2 3],%w[4 5 6]])
|
751
|
+
|
752
|
+
table = Ruport::Data::Table.parse( "a\tb\tc\n1\t2\t3\n4\t5\t6\n",
|
724
753
|
:csv_options => { :col_sep => "\t" } )
|
725
|
-
assert_equal expected, table
|
754
|
+
assert_equal expected, table
|
726
755
|
|
727
|
-
table = Ruport::Data::Table.parse( "a,b,c\n1,2,3\n4,5,6\n",
|
756
|
+
table = Ruport::Data::Table.parse( "a,b,c\n1,2,3\n4,5,6\n",
|
728
757
|
:has_names => false)
|
729
758
|
assert_equal([],table.column_names)
|
730
|
-
assert_equal(Table([], :data => [%w[a b c],%w[1 2 3],%w[4 5 6]]), table)
|
759
|
+
assert_equal(Ruport.Table([], :data => [%w[a b c],%w[1 2 3],%w[4 5 6]]), table)
|
731
760
|
end
|
732
|
-
|
761
|
+
|
733
762
|
def test_csv_block_form
|
734
763
|
expected = [%w[a b],%w[1 2],%w[3 4]]
|
735
|
-
t = Ruport::Data::Table.send(:get_table_from_csv,
|
736
|
-
:parse, "a,b\n1,2\n3,4",
|
764
|
+
t = Ruport::Data::Table.send(:get_table_from_csv,
|
765
|
+
:parse, "a,b\n1,2\n3,4",
|
737
766
|
:has_names => false) do |s,r|
|
738
767
|
assert_equal expected.shift, r
|
739
|
-
s << r
|
768
|
+
s << r
|
740
769
|
end
|
741
|
-
assert_equal Table([], :data => [%w[a b],%w[1 2],%w[3 4]]), t
|
742
|
-
end
|
743
|
-
|
770
|
+
assert_equal Ruport.Table([], :data => [%w[a b],%w[1 2],%w[3 4]]), t
|
771
|
+
end
|
772
|
+
|
744
773
|
# - BUG TRAPS --------------------
|
745
|
-
|
774
|
+
|
746
775
|
def test_ensure_using_csv_block_mode_works
|
747
776
|
expected = [%w[a b],%w[1 2],%w[3 4]]
|
748
777
|
t = Ruport::Data::Table.parse("a,b\n1,2\n3,4",:has_names => false) { |s,r|
|
749
778
|
assert_equal expected.shift, r
|
750
|
-
s << r
|
779
|
+
s << r
|
751
780
|
s << r
|
752
781
|
}
|
753
|
-
assert_equal Table([],
|
782
|
+
assert_equal Ruport.Table([],
|
754
783
|
:data => [%w[a b],%w[a b],%w[1 2], %w[1 2],%w[3 4],%w[3 4]]), t
|
755
784
|
x = Ruport::Data::Table.load(File.join(TEST_SAMPLES,"data.csv")) { |s,r|
|
756
785
|
assert_kind_of Ruport::Data::Feeder, s
|
@@ -759,37 +788,31 @@ class TestTableFromCSV < Test::Unit::TestCase
|
|
759
788
|
s << r
|
760
789
|
}
|
761
790
|
assert_equal 4, x.length
|
762
|
-
end
|
763
|
-
|
791
|
+
end
|
792
|
+
|
764
793
|
def test_ensure_csv_loading_accepts_table_options
|
765
|
-
|
766
|
-
|
767
|
-
|
768
|
-
|
769
|
-
|
794
|
+
a = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), :record_class => DuckRecord)
|
795
|
+
a.each { |r| assert_kind_of(DuckRecord,r) }
|
796
|
+
end
|
797
|
+
|
770
798
|
def test_ensure_table_from_csv_accepts_record_class_in_block_usage
|
771
|
-
|
772
|
-
|
773
|
-
assert_kind_of(DuckRecord,r)
|
799
|
+
Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), :record_class => DuckRecord, :records => true) do |_s,r|
|
800
|
+
assert_kind_of(DuckRecord,r)
|
774
801
|
end
|
775
802
|
end
|
776
|
-
|
777
803
|
end
|
778
804
|
|
779
|
-
class TestTableKernelHack < Test
|
780
|
-
|
805
|
+
class TestTableKernelHack < Minitest::Test
|
781
806
|
def test_simple
|
782
|
-
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]),
|
783
|
-
|
784
|
-
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]),
|
785
|
-
Table("a","b","c")
|
807
|
+
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]), Ruport.Table(%w[a b c])
|
808
|
+
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]), Ruport.Table("a","b","c")
|
786
809
|
assert_equal Ruport::Data::Table.load(
|
787
810
|
File.join(TEST_SAMPLES,"addressbook.csv")),
|
788
|
-
Table(File.join(TEST_SAMPLES,"addressbook.csv"))
|
811
|
+
Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"))
|
789
812
|
assert_equal Ruport::Data::Table.load(
|
790
813
|
File.join(TEST_SAMPLES,"addressbook.csv"), :has_names => false),
|
791
|
-
Table(File.join(TEST_SAMPLES,"addressbook.csv"), :has_names => false)
|
792
|
-
Table("a","b","c") do |t|
|
814
|
+
Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), :has_names => false)
|
815
|
+
Ruport.Table("a","b","c") do |t|
|
793
816
|
t << [1,2,3]
|
794
817
|
assert_equal(
|
795
818
|
Ruport::Data::Table.new(:column_names => %w[a b c], :data => [[1,2,3]]),
|
@@ -797,19 +820,19 @@ class TestTableKernelHack < Test::Unit::TestCase
|
|
797
820
|
)
|
798
821
|
end
|
799
822
|
|
800
|
-
assert_equal Table("a"), Table(%w[a])
|
801
|
-
assert_equal Table(:a), Table([:a])
|
823
|
+
assert_equal Ruport.Table("a"), Ruport.Table(%w[a])
|
824
|
+
assert_equal Ruport.Table(:a), Ruport.Table([:a])
|
802
825
|
end
|
803
826
|
|
804
827
|
def test_iterators
|
805
|
-
Table(File.join(TEST_SAMPLES,"addressbook.csv")) do |s,r|
|
828
|
+
Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv")) do |s,r|
|
806
829
|
assert_kind_of(Array,r)
|
807
830
|
assert_kind_of(Ruport::Data::Feeder,s)
|
808
831
|
end
|
809
832
|
|
810
833
|
n = 0
|
811
834
|
|
812
|
-
Table(:string => "a,b,c\n1,2,3\n4,5,6\n") do |s,r|
|
835
|
+
Ruport.Table(:string => "a,b,c\n1,2,3\n4,5,6\n") do |s,r|
|
813
836
|
assert_kind_of(Array,r)
|
814
837
|
assert_kind_of(Ruport::Data::Feeder,s)
|
815
838
|
n += 1
|
@@ -817,22 +840,21 @@ class TestTableKernelHack < Test::Unit::TestCase
|
|
817
840
|
|
818
841
|
assert_equal 2, n
|
819
842
|
end
|
820
|
-
|
843
|
+
|
821
844
|
def test_with_file_arg
|
822
|
-
assert_equal Table(File.join(TEST_SAMPLES,"addressbook.csv")),
|
823
|
-
Table(:file => File.join(TEST_SAMPLES,"addressbook.csv"))
|
845
|
+
assert_equal Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv")),
|
846
|
+
Ruport.Table(:file => File.join(TEST_SAMPLES,"addressbook.csv"))
|
824
847
|
end
|
825
|
-
|
848
|
+
|
826
849
|
def test_with_string_arg
|
827
850
|
csv_string = "id,name\n1,Inky\n2,Blinky\n3,Clyde"
|
828
|
-
|
851
|
+
|
829
852
|
assert_equal Ruport::Data::Table.parse(csv_string),
|
830
|
-
Table(:string => csv_string)
|
853
|
+
Ruport.Table(:string => csv_string)
|
831
854
|
end
|
832
855
|
|
833
856
|
def test_ensure_table_hack_accepts_normal_constructor_args
|
834
857
|
assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]),
|
835
|
-
Table(:column_names => %w[a b c])
|
836
|
-
end
|
837
|
-
|
858
|
+
Ruport.Table(:column_names => %w[a b c])
|
859
|
+
end
|
838
860
|
end
|