ruport 1.6.3 → 1.8.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.
- 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/record_test.rb
CHANGED
@@ -1,75 +1,75 @@
|
|
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
3
|
|
4
|
-
class TestRecord < Test
|
4
|
+
class TestRecord < Minitest::Test
|
5
5
|
|
6
6
|
include Ruport::Data
|
7
|
-
|
7
|
+
|
8
8
|
def setup
|
9
9
|
@attributes = %w[a b c d]
|
10
|
-
@record = Ruport::Data::Record.new [1,2,3,4], :attributes => @attributes
|
11
|
-
end
|
12
|
-
|
13
|
-
context "when initializing with an array with attributes" do
|
14
|
-
|
10
|
+
@record = Ruport::Data::Record.new [1,2,3,4], :attributes => @attributes
|
11
|
+
end
|
12
|
+
|
13
|
+
context "when initializing with an array with attributes" do
|
14
|
+
should "test_specify_key_access_should_work" do
|
15
15
|
assert_equal 1, @record["a"]
|
16
16
|
assert_equal 4, @record["d"]
|
17
17
|
assert_equal 2, @record.b
|
18
18
|
assert_equal 3, @record.c
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
|
19
|
+
assert_raises(NoMethodError) { @record.f }
|
20
|
+
end
|
21
|
+
|
22
|
+
should "test_specify_ordinal_access_should_work" do
|
23
23
|
assert_equal 1, @record[0]
|
24
24
|
assert_equal 2, @record[1]
|
25
25
|
assert_equal 3, @record[2]
|
26
26
|
assert_equal 4, @record[3]
|
27
27
|
end
|
28
|
-
end
|
29
|
-
|
28
|
+
end
|
29
|
+
|
30
30
|
context "when initializing with an array without attributes" do
|
31
|
-
|
31
|
+
should "test_specify_ordinal_access_should_work" do
|
32
32
|
record = Ruport::Data::Record.new [1,2,3,4]
|
33
33
|
assert_equal 1, record[0]
|
34
34
|
assert_equal 2, record[1]
|
35
35
|
assert_equal 3, record[2]
|
36
|
-
assert_equal 4, record[3]
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
|
41
|
-
def setup
|
36
|
+
assert_equal 4, record[3]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "when initializing with a hash without attributes" do
|
41
|
+
def setup
|
42
42
|
@record = Ruport::Data::Record.new({:a => 1, :b => 2, :c => 3},{})
|
43
|
-
end
|
44
|
-
|
45
|
-
def
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_specify_key_access_should_work
|
46
46
|
assert_equal 1, @record[:a]
|
47
47
|
assert_equal 2, @record[:b]
|
48
48
|
assert_equal 3, @record[:c]
|
49
|
-
assert_equal 3, @record.c
|
50
|
-
end
|
49
|
+
assert_equal 3, @record.c
|
50
|
+
end
|
51
51
|
end
|
52
|
-
|
53
|
-
|
52
|
+
|
53
|
+
describe "when initializing with a hash with attributes" do
|
54
54
|
def setup
|
55
|
-
@record = Record.new({:a => 1, :b => 2, :c => 3 },
|
55
|
+
@record = Record.new({:a => 1, :b => 2, :c => 3 },
|
56
56
|
:attributes => [:c,:b,:a])
|
57
|
-
end
|
58
|
-
|
59
|
-
def
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_specify_key_access_should_work
|
60
60
|
assert_equal 1, @record[:a]
|
61
61
|
assert_equal 2, @record[:b]
|
62
62
|
assert_equal 3, @record[:c]
|
63
|
-
assert_equal 3, @record.c
|
63
|
+
assert_equal 3, @record.c
|
64
64
|
end
|
65
|
-
|
66
|
-
def
|
65
|
+
|
66
|
+
def test_specify_ordinal_access_should_work
|
67
67
|
assert_equal 3, @record[0]
|
68
|
-
assert_equal 2, @record[1]
|
69
|
-
assert_equal 1, @record[2]
|
68
|
+
assert_equal 2, @record[1]
|
69
|
+
assert_equal 1, @record[2]
|
70
70
|
end
|
71
71
|
end
|
72
|
-
|
72
|
+
|
73
73
|
def test_bracket_equals
|
74
74
|
@record[1] = "godzilla"
|
75
75
|
@record["d"] = "mothra"
|
@@ -80,25 +80,25 @@ class TestRecord < Test::Unit::TestCase
|
|
80
80
|
assert_equal @record[3], "mothra"
|
81
81
|
assert_equal @record["d"], "mothra"
|
82
82
|
end
|
83
|
-
|
83
|
+
|
84
84
|
def test_accessors
|
85
85
|
assert_equal @record.a, @record["a"]
|
86
86
|
assert_equal @record.b, @record["b"]
|
87
87
|
assert_equal @record.c, @record["c"]
|
88
88
|
assert_equal @record.d, @record["d"]
|
89
|
-
end
|
90
|
-
|
89
|
+
end
|
90
|
+
|
91
91
|
def test_can_has_id
|
92
|
-
record = Ruport::Data::Record.new(:id => 12345)
|
92
|
+
record = Ruport::Data::Record.new(:id => 12345)
|
93
93
|
assert_equal 12345, record.id
|
94
94
|
end
|
95
95
|
|
96
96
|
def test_nonexistent_accessor
|
97
|
-
|
97
|
+
assert_raises NoMethodError do
|
98
98
|
@record.e
|
99
99
|
end
|
100
100
|
end
|
101
|
-
|
101
|
+
|
102
102
|
def test_attribute_setting
|
103
103
|
@record.a = 10
|
104
104
|
@record.b = 20
|
@@ -112,24 +112,24 @@ class TestRecord < Test::Unit::TestCase
|
|
112
112
|
end
|
113
113
|
|
114
114
|
def test_to_hash
|
115
|
-
|
115
|
+
@record.to_hash
|
116
116
|
assert_equal({ "a" => 1, "b" => 2, "c" => 3, "d" => 4 }, @record.to_hash)
|
117
|
-
end
|
118
|
-
|
117
|
+
end
|
118
|
+
|
119
119
|
def test_rename_attribute
|
120
120
|
@record.rename_attribute("b","x")
|
121
121
|
assert_equal %w[a x c d], @record.attributes
|
122
122
|
assert_equal 2, @record["x"]
|
123
123
|
assert_equal 2, @record.x
|
124
124
|
assert_equal 2, @record[1]
|
125
|
-
end
|
125
|
+
end
|
126
126
|
|
127
127
|
def test_equality
|
128
128
|
|
129
129
|
dc = %w[a b c d]
|
130
130
|
dc2 = %w[a b c d]
|
131
131
|
dc3 = %w[a b c]
|
132
|
-
|
132
|
+
|
133
133
|
rec1 = Record.new [1,2,3,4]
|
134
134
|
rec2 = Record.new [1,2,3,4]
|
135
135
|
rec3 = Record.new [1,2]
|
@@ -137,7 +137,7 @@ class TestRecord < Test::Unit::TestCase
|
|
137
137
|
rec5 = Record.new [1,2,3,4], :attributes => dc2
|
138
138
|
rec6 = Record.new [1,2,3,4], :attributes => dc3
|
139
139
|
rec7 = Record.new [1,2], :attributes => dc
|
140
|
-
|
140
|
+
|
141
141
|
[:==, :eql?].each do |op|
|
142
142
|
assert rec1.send(op, rec2)
|
143
143
|
assert rec4.send(op, rec5)
|
@@ -162,7 +162,7 @@ class TestRecord < Test::Unit::TestCase
|
|
162
162
|
|
163
163
|
assert_equal [1,2,3,4], @record.to_a
|
164
164
|
assert_equal %w[a b c d], @record.attributes
|
165
|
-
|
165
|
+
|
166
166
|
@record.reorder "a","d","b","c"
|
167
167
|
assert_equal [1,4,2,3], @record.to_a
|
168
168
|
assert_equal %w[a d b c], @record.attributes
|
@@ -175,10 +175,10 @@ class TestRecord < Test::Unit::TestCase
|
|
175
175
|
assert_equal [1,2,3], r.to_a
|
176
176
|
assert_equal %w[a b c], r.attributes
|
177
177
|
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
178
|
+
assert_raises(ArgumentError) { r.dup.reorder "foo" }
|
179
|
+
assert_raises(ArgumentError) { r.dup.reorder 0,5 }
|
180
|
+
r.dup.reorder 0
|
181
|
+
r.dup.reorder "a","b"
|
182
182
|
end
|
183
183
|
|
184
184
|
def test_dup
|
@@ -199,12 +199,12 @@ class TestRecord < Test::Unit::TestCase
|
|
199
199
|
s = Record.new :attributes => %w[a b], :data => [1,2]
|
200
200
|
assert_equal r.hash, s.hash
|
201
201
|
end
|
202
|
-
|
202
|
+
|
203
203
|
def test_records_with_differing_attrs_and_data_hash_differently
|
204
204
|
r = Record.new [1,2],:attributes => %w[a b]
|
205
205
|
s = Record.new [nil,nil],:attributes => %w[a b]
|
206
206
|
assert r.hash != s.hash
|
207
|
-
|
207
|
+
|
208
208
|
t = Record.new [1,3],:attributes => %w[a b]
|
209
209
|
assert r.hash != t.hash
|
210
210
|
end
|
@@ -225,7 +225,7 @@ class TestRecord < Test::Unit::TestCase
|
|
225
225
|
new_object_id = @record.instance_variable_get(:@attributes).object_id
|
226
226
|
assert_equal a.object_id, new_object_id
|
227
227
|
end
|
228
|
-
|
228
|
+
|
229
229
|
#----------------------------------------------------------------------
|
230
230
|
# BUG Traps
|
231
231
|
#----------------------------------------------------------------------
|
@@ -245,35 +245,35 @@ class TestRecord < Test::Unit::TestCase
|
|
245
245
|
end
|
246
246
|
|
247
247
|
# Ticket #172
|
248
|
-
def test_ensure_get_really_indifferent
|
248
|
+
def test_ensure_get_really_indifferent
|
249
249
|
a = Record.new({"a" => 1, "b" => 2})
|
250
250
|
assert_equal(2,a.get("b"))
|
251
251
|
assert_equal(2,a.get(:b))
|
252
|
-
a = Record.new({:a => 1, :b => 2})
|
252
|
+
a = Record.new({:a => 1, :b => 2})
|
253
253
|
assert_equal(2,a.get("b"))
|
254
254
|
assert_equal(2,a.get(:b))
|
255
255
|
end
|
256
|
-
|
256
|
+
|
257
257
|
def test_ensure_get_throws_argument_error
|
258
258
|
a = Record.new({"a" => 1, "b" => 2})
|
259
259
|
assert_raises(ArgumentError) { a.get([]) }
|
260
260
|
end
|
261
|
-
|
261
|
+
|
262
262
|
def test_ensure_delete_removes_attribute
|
263
263
|
a = Record.new({"a" => 1, "b" => 2})
|
264
264
|
assert_equal({"a" => 1, "b" => 2}, a.data)
|
265
265
|
assert_equal(["a","b"], a.attributes)
|
266
|
-
|
266
|
+
|
267
267
|
a.send(:delete, "a")
|
268
268
|
assert_equal({"b" => 2}, a.data)
|
269
269
|
assert_equal(["b"], a.attributes)
|
270
270
|
end
|
271
|
-
|
271
|
+
|
272
272
|
def test_ensure_bracket_equals_updates_attributes
|
273
273
|
a = Record.new({"a" => 1, "b" => 2})
|
274
274
|
assert_equal({"a" => 1, "b" => 2}, a.data)
|
275
275
|
assert_equal(["a","b"], a.attributes)
|
276
|
-
|
276
|
+
|
277
277
|
a["b"] = 3
|
278
278
|
assert_equal({"a" => 1, "b" => 3}, a.data)
|
279
279
|
assert_equal(["a","b"], a.attributes)
|
@@ -289,44 +289,44 @@ class TestRecord < Test::Unit::TestCase
|
|
289
289
|
a = MyRecordSub.new [1,2,3]
|
290
290
|
assert_equal "1,2,3\n", a.to_csv
|
291
291
|
end
|
292
|
-
|
293
|
-
context "when rendering records" do
|
294
|
-
|
295
|
-
|
292
|
+
|
293
|
+
context "when rendering records" do
|
294
|
+
|
295
|
+
should "test_specify_record_as_should_work" do
|
296
296
|
rendered_row = @record.as(:text)
|
297
297
|
assert_equal("| 1 | 2 | 3 | 4 |\n", rendered_row)
|
298
298
|
end
|
299
299
|
|
300
|
-
|
300
|
+
should "test_specify_record_to_format_should_work_without_options" do
|
301
301
|
rendered_row = @record.to_text
|
302
|
-
assert_equal("| 1 | 2 | 3 | 4 |\n", rendered_row)
|
303
|
-
end
|
304
|
-
|
305
|
-
|
302
|
+
assert_equal("| 1 | 2 | 3 | 4 |\n", rendered_row)
|
303
|
+
end
|
304
|
+
|
305
|
+
should "test_specify_record_to_format_should_work_with_options" do
|
306
306
|
rendered_row = @record.to_csv(:format_options => { :col_sep => "\t"})
|
307
|
-
assert_equal("1\t2\t3\t4\n",rendered_row)
|
308
|
-
end
|
309
|
-
|
310
|
-
|
311
|
-
def setup
|
312
|
-
@a = Record.new({ "a" => 1, "b" => 2 })
|
307
|
+
assert_equal("1\t2\t3\t4\n",rendered_row)
|
308
|
+
end
|
309
|
+
|
310
|
+
describe "when given bad format names" do
|
311
|
+
def setup
|
312
|
+
@a = Record.new({ "a" => 1, "b" => 2 })
|
313
313
|
end
|
314
314
|
|
315
|
-
def
|
316
|
-
assert_raises(Ruport::Controller::UnknownFormatError) { @a.as(:nothing) }
|
317
|
-
end
|
318
|
-
|
319
|
-
def
|
315
|
+
def test_specify_as_should_throw_proper_errors
|
316
|
+
assert_raises(Ruport::Controller::UnknownFormatError) { @a.as(:nothing) }
|
317
|
+
end
|
318
|
+
|
319
|
+
def test_specify_to_format_should_throw_proper_errors
|
320
320
|
assert_raises(Ruport::Controller::UnknownFormatError) { @a.to_nothing }
|
321
|
-
end
|
322
|
-
end
|
323
|
-
|
321
|
+
end
|
322
|
+
end
|
323
|
+
|
324
324
|
## -- BUG TRAPS --------------------
|
325
|
-
|
326
|
-
|
325
|
+
|
326
|
+
should "test_specify_attributes_should_not_be_broken_by_to_hack" do
|
327
327
|
record = Ruport::Data::Record.new [1,2], :attributes => %w[a to_something]
|
328
328
|
assert_equal 2, record.to_something
|
329
|
-
end
|
329
|
+
end
|
330
330
|
end
|
331
331
|
|
332
332
|
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
Region,Product,Units Sold
|
2
|
+
North,Widget,8
|
3
|
+
South,Gadget,2
|
4
|
+
East,Gizmo,8
|
5
|
+
West,Widget,6
|
6
|
+
South,Widget,6
|
7
|
+
East,Gadget,5
|
8
|
+
West,Gizmo,1
|
9
|
+
East,Gadget,1
|
10
|
+
West,Widget,4
|
11
|
+
West,Gadget,8
|
12
|
+
North,Gizmo,2
|
13
|
+
South,Gizmo,5
|
14
|
+
East,Widget,3
|
15
|
+
North,Gadget,1
|
16
|
+
South,Gizmo,7
|
17
|
+
North,Gizmo,8
|
18
|
+
North,Widget,4
|
19
|
+
East,Gadget,4
|
20
|
+
West,Gizmo,6
|
21
|
+
South,Gadget,3
|
data/test/table_pivot_test.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
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
3
|
|
4
|
-
class TablePivotSimpleCaseTest < Test
|
4
|
+
class TablePivotSimpleCaseTest < Minitest::Test
|
5
5
|
|
6
6
|
def setup
|
7
|
-
table = Table('a', 'b', 'c')
|
7
|
+
table = Ruport.Table('a', 'b', 'c')
|
8
8
|
table << [1,3,6]
|
9
9
|
table << [1,4,7]
|
10
10
|
table << [2,3,8]
|
@@ -17,18 +17,23 @@ class TablePivotSimpleCaseTest < Test::Unit::TestCase
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def test_produces_correct_full_table
|
20
|
-
expected = Table("a",3,4) { |t| t << [1,6,7] << [2,8,9] }
|
20
|
+
expected = Ruport.Table("a",3,4) { |t| t << [1,6,7] << [2,8,9] }
|
21
21
|
assert_equal(expected, @pivoted)
|
22
22
|
end
|
23
23
|
|
24
|
+
def test_pivot_empty_table
|
25
|
+
tab = Ruport.Table ["id", "num_sessions", "status", "diagnosis"]
|
26
|
+
tab << ["one", 10, :aborted, :good]
|
27
|
+
tab << ["two", 10, :success, :bad]
|
28
|
+
|
29
|
+
tab.pivot("status", :group_by=>"diagnosis", :values=>"num_sessions", :operation=>:mean)
|
30
|
+
end
|
24
31
|
end
|
25
32
|
|
26
|
-
class PivotConvertRowOrderToGroupOrderTest < Test
|
33
|
+
class PivotConvertRowOrderToGroupOrderTest < Minitest::Test
|
27
34
|
|
28
|
-
def convert(
|
29
|
-
Ruport::Data::Table::Pivot.
|
30
|
-
nil, nil, nil, nil
|
31
|
-
).convert_row_order_to_group_order(src)
|
35
|
+
def convert(row_order)
|
36
|
+
Ruport::Data::Table::Pivot.row_order_to_group_order(row_order)
|
32
37
|
end
|
33
38
|
|
34
39
|
def setup
|
@@ -57,15 +62,15 @@ class PivotConvertRowOrderToGroupOrderTest < Test::Unit::TestCase
|
|
57
62
|
end
|
58
63
|
|
59
64
|
def test_nil
|
60
|
-
|
65
|
+
assert_nil convert(nil)
|
61
66
|
end
|
62
67
|
|
63
68
|
end
|
64
69
|
|
65
|
-
class PivotPreservesOrdering < Test
|
70
|
+
class PivotPreservesOrdering < Minitest::Test
|
66
71
|
|
67
|
-
def
|
68
|
-
table = Table('group', 'a', 'b')
|
72
|
+
def test_group_column_preserves_order_of_occurrence
|
73
|
+
table = Ruport.Table('group', 'a', 'b')
|
69
74
|
[
|
70
75
|
[1, 0, 0],
|
71
76
|
[9, 0, 0],
|
@@ -75,13 +80,13 @@ class PivotPreservesOrdering < Test::Unit::TestCase
|
|
75
80
|
[8, 0, 0],
|
76
81
|
[1, 0, 0]
|
77
82
|
].each {|e| table << e}
|
78
|
-
assert_equal([1,9,8],
|
83
|
+
assert_equal([1,9,8],
|
79
84
|
Ruport::Data::Table::Pivot.
|
80
|
-
new(table, 'group', 'a', 'b').
|
85
|
+
new(table, 'group', 'a', 'b').column)
|
81
86
|
end
|
82
87
|
|
83
|
-
def
|
84
|
-
table = Table('group', 'a', 'b', 'c')
|
88
|
+
def test_pivoted_row_preserves_order_of_input_rows
|
89
|
+
table = Ruport.Table('group', 'a', 'b', 'c')
|
85
90
|
[
|
86
91
|
[200, 1, 2, 1],
|
87
92
|
[200, 4, 5, 2],
|
@@ -93,11 +98,11 @@ class PivotPreservesOrdering < Test::Unit::TestCase
|
|
93
98
|
[1,4,5],
|
94
99
|
Ruport::Data::Table::Pivot.new(
|
95
100
|
table, 'group', 'a', 'b', :pivot_order => ['c']
|
96
|
-
).
|
101
|
+
).row)
|
97
102
|
end
|
98
103
|
|
99
104
|
def test_preserves_ordering
|
100
|
-
table = Table('group', 'a', 'b', 'c')
|
105
|
+
table = Ruport.Table('group', 'a', 'b', 'c')
|
101
106
|
[
|
102
107
|
[200, 1, 2, 3],
|
103
108
|
[200, 4, 5, 6],
|
@@ -105,30 +110,76 @@ class PivotPreservesOrdering < Test::Unit::TestCase
|
|
105
110
|
[100, 4,11,12]
|
106
111
|
].each {|e| table << e}
|
107
112
|
pivoted = table.pivot('a', :group_by => 'group', :values => 'b')
|
108
|
-
expected = Table("group",1,4) { |t| t << [200,2,5] << [100,8,11] }
|
113
|
+
expected = Ruport.Table("group",1,4) { |t| t << [200,2,5] << [100,8,11] }
|
109
114
|
assert_equal(expected, pivoted)
|
110
115
|
end
|
111
116
|
|
112
|
-
def
|
113
|
-
table = Table('group', 'a')
|
117
|
+
def test_reorders_a_calculated_column_by_column_name
|
118
|
+
table = Ruport.Table('group', 'a')
|
114
119
|
[
|
115
120
|
[1, 1], [2, 2], [3, 3]
|
116
121
|
].each {|e| table << e}
|
117
122
|
table.add_column('pivotme') {|row| 10 - row.group.to_i}
|
118
|
-
pivoted = table.pivot('pivotme', :group_by => 'group', :values => 'a',
|
123
|
+
pivoted = table.pivot('pivotme', :group_by => 'group', :values => 'a',
|
119
124
|
:pivot_order => :name)
|
120
125
|
assert_equal(['group', 7, 8, 9], pivoted.column_names)
|
121
126
|
end
|
122
127
|
|
123
128
|
def test_preserves_ordering_on_calculated_column_with_proc_pivot_order
|
124
|
-
table = Table('group', 'a')
|
129
|
+
table = Ruport.Table('group', 'a')
|
125
130
|
[
|
126
131
|
[1, 1], [2, 2], [3, 3]
|
127
132
|
].each {|e| table << e}
|
128
133
|
table.add_column('pivotme') {|row| 10 - row.group.to_i}
|
129
|
-
pivoted = table.pivot('pivotme', :group_by => 'group', :values => 'a',
|
130
|
-
:pivot_order => proc {|
|
134
|
+
pivoted = table.pivot('pivotme', :group_by => 'group', :values => 'a',
|
135
|
+
:pivot_order => proc {|_row, pivot| pivot })
|
131
136
|
assert_equal(['group', 7, 8, 9], pivoted.column_names)
|
132
137
|
end
|
133
138
|
|
134
139
|
end
|
140
|
+
|
141
|
+
class TablePivotOperationTest < Minitest::Test
|
142
|
+
def setup
|
143
|
+
@rows = [
|
144
|
+
Ruport::Data::Record.new('Values' => 3),
|
145
|
+
Ruport::Data::Record.new('Values' => 9),
|
146
|
+
Ruport::Data::Record.new('Values' => 4)
|
147
|
+
]
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_performs_operation_sum
|
151
|
+
sum = Ruport::Data::Table::Pivot::Operations.sum(@rows, 'Values')
|
152
|
+
assert_equal 16, sum
|
153
|
+
end
|
154
|
+
|
155
|
+
def test_performs_operation_first
|
156
|
+
first = Ruport::Data::Table::Pivot::Operations.first(@rows, 'Values')
|
157
|
+
assert_equal 3, first
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_performs_operation_count
|
161
|
+
count = Ruport::Data::Table::Pivot::Operations.count(@rows, 'Values')
|
162
|
+
assert_equal 3, count
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_performs_operation_mean
|
166
|
+
mean = Ruport::Data::Table::Pivot::Operations.mean(@rows, 'Values')
|
167
|
+
assert_equal 5, mean
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_performs_operation_min
|
171
|
+
min = Ruport::Data::Table::Pivot::Operations.min(@rows, 'Values')
|
172
|
+
assert_equal 3, min
|
173
|
+
end
|
174
|
+
|
175
|
+
def test_performs_operation_max
|
176
|
+
max = Ruport::Data::Table::Pivot::Operations.max(@rows, 'Values')
|
177
|
+
assert_equal 9, max
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_invalid_operation_causes_exception
|
181
|
+
assert_raises ArgumentError do
|
182
|
+
Ruport::Data::Table::Pivot.new(nil, nil, nil, nil, :operation => :foo)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
end
|