ruport 0.7.2 → 0.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.
Files changed (53) hide show
  1. data/AUTHORS +7 -3
  2. data/Rakefile +8 -9
  3. data/TODO +16 -0
  4. data/examples/RWEmerson.jpg +0 -0
  5. data/examples/centered_pdf_text_box.rb +66 -0
  6. data/examples/invoice.rb +35 -25
  7. data/examples/invoice_report.rb +1 -1
  8. data/examples/line_plotter.rb +1 -1
  9. data/examples/pdf_table_with_title.rb +42 -0
  10. data/lib/ruport.rb +5 -7
  11. data/lib/ruport.rb.rej +41 -0
  12. data/lib/ruport.rb~ +85 -0
  13. data/lib/ruport/attempt.rb +59 -59
  14. data/lib/ruport/config.rb +15 -4
  15. data/lib/ruport/data.rb +0 -2
  16. data/lib/ruport/data/groupable.rb +25 -16
  17. data/lib/ruport/data/record.rb +128 -102
  18. data/lib/ruport/data/table.rb +352 -199
  19. data/lib/ruport/data/taggable.rb +18 -7
  20. data/lib/ruport/format/html.rb +3 -1
  21. data/lib/ruport/format/latex.rb +1 -1
  22. data/lib/ruport/format/latex.rb.rej +26 -0
  23. data/lib/ruport/format/latex.rb~ +47 -0
  24. data/lib/ruport/format/pdf.rb +111 -28
  25. data/lib/ruport/format/pdf.rb.rej +168 -0
  26. data/lib/ruport/format/pdf.rb~ +189 -0
  27. data/lib/ruport/format/plugin.rb +0 -5
  28. data/lib/ruport/format/svg.rb +4 -4
  29. data/lib/ruport/format/xml.rb +3 -3
  30. data/lib/ruport/generator.rb +66 -27
  31. data/lib/ruport/mailer.rb +4 -1
  32. data/lib/ruport/query.rb +13 -1
  33. data/lib/ruport/renderer.rb +89 -17
  34. data/lib/ruport/renderer/graph.rb +5 -5
  35. data/lib/ruport/renderer/table.rb +8 -9
  36. data/lib/ruport/report.rb +2 -6
  37. data/test/test_config.rb +88 -76
  38. data/test/{test_text_table.rb → test_format_text.rb} +4 -2
  39. data/test/test_groupable.rb +15 -13
  40. data/test/test_query.rb +6 -3
  41. data/test/test_record.rb +57 -33
  42. data/test/test_renderer.rb +77 -0
  43. data/test/test_report.rb +188 -181
  44. data/test/test_ruport.rb +5 -6
  45. data/test/test_table.rb +290 -190
  46. data/test/test_table_renderer.rb +56 -8
  47. data/test/test_taggable.rb +7 -8
  48. data/test/unit.log +259 -7
  49. metadata +22 -19
  50. data/lib/ruport/data/collection.rb +0 -65
  51. data/lib/ruport/data/set.rb +0 -148
  52. data/test/test_collection.rb +0 -30
  53. data/test/test_set.rb +0 -118
data/test/test_ruport.rb CHANGED
@@ -16,7 +16,7 @@ class TestRuport < Test::Unit::TestCase
16
16
 
17
17
  def test_fatal
18
18
  assert_raise(RuntimeError) {
19
- Ruport::complain "Default problem", :status => :fatal,
19
+ Ruport.log "Default problem", :status => :fatal,
20
20
  :output => @output
21
21
  }
22
22
  @output.rewind
@@ -25,9 +25,8 @@ class TestRuport < Test::Unit::TestCase
25
25
 
26
26
  def test_fatal_log_only
27
27
  assert_raise(RuntimeError) {
28
- Ruport::complain "Default problem", :status => :fatal,
29
- :output => @output,
30
- :level => :log_only
28
+ Ruport.log "Default problem",
29
+ :status => :fatal, :output => @output, :level => :log_only
31
30
  }
32
31
  @output.rewind
33
32
  assert_equal("",@output.read)
@@ -36,7 +35,7 @@ class TestRuport < Test::Unit::TestCase
36
35
 
37
36
  def test_warn
38
37
  assert_nothing_raised {
39
- Ruport::complain "Default problem", :output => @output
38
+ Ruport.log "Default problem", :output => @output
40
39
  }
41
40
  @output.rewind
42
41
  assert_equal("[!!] Default problem\n",@output.read)
@@ -44,7 +43,7 @@ class TestRuport < Test::Unit::TestCase
44
43
 
45
44
  def test_warn_log_only
46
45
  assert_nothing_raised {
47
- Ruport::complain "Default problem", :output => @output,
46
+ Ruport.log "Default problem", :output => @output,
48
47
  :level => :log_only
49
48
  }
50
49
  @output.rewind
data/test/test_table.rb CHANGED
@@ -2,14 +2,24 @@ require "test/unit"
2
2
  require "ruport"
3
3
  begin; require "rubygems"; rescue LoadError; nil; end
4
4
 
5
+
6
+ class Person < Ruport::Data::Record
7
+
8
+ def name
9
+ first_name + " " + last_name
10
+ end
11
+
12
+ end
13
+
5
14
  class TestTable < Test::Unit::TestCase
6
15
  def test_constructors
7
- table = Ruport::Data::Table.new
16
+ table = Ruport::Data::Table.new
8
17
  table2 = Ruport::Data::Table.new :column_names => %w[a b c]
9
18
  table3 = Ruport::Data::Table.new :data => [[1,2,3]]
10
19
  table4 = Ruport::Data::Table.new :column_names => %w[col1 col2 col3],
11
20
  :data => [[1,2,3]]
12
21
  tables = [table,table2,table3,table4]
22
+
13
23
  tables.zip([[],%w[a b c], [], %w[col1 col2 col3]]).each do |t,n|
14
24
  assert_equal n, t.column_names
15
25
 
@@ -17,65 +27,82 @@ class TestTable < Test::Unit::TestCase
17
27
  t[0].tag :foo
18
28
  t[1].tag :bar
19
29
  table_from_records = t.data.to_table(t.column_names)
20
- assert_equal [:foo], table_from_records[0].tags
21
- assert_equal [:bar], table_from_records[1].tags
30
+ assert_equal Set.new([:foo]), table_from_records[0].tags
31
+ assert_equal Set.new([:bar]), table_from_records[1].tags
22
32
  end
23
33
 
24
34
  a = Ruport::Data::Record.new [1,2,3]
25
- assert a.respond_to?(:[])
26
- b = a.dup
27
- b.attributes = %w[col1 col2 col3]
28
- tables.zip([[],[],[a],[b]]).each { |t,n| assert_equal n, t.data }
29
- end
30
-
31
- def test_ensure_table_creation_allows_record_coercion
32
- table = [[1,2,3],[4,5,6],[7,8,9]].to_table
33
- table_with_names = [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c])
34
-
35
- a,b,c = nil
36
- assert_nothing_raised { a = table.to_a.to_table(%w[a b c]) }
37
- assert_nothing_raised { b = table.to_a.to_table(%w[d e f]) }
38
- assert_nothing_raised { c = table_with_names.to_a.to_table }
39
-
40
- [a,b,c].each { |t| assert_equal(3,t.length) }
41
- assert_equal %w[a b c], a.column_names
42
- a.each { |r|
43
- assert_equal %w[a b c], r.attributes
44
- assert_nothing_raised { r.a; r.b; r.c }
45
- [r.a,r.b,r.c].each { |i| assert(i.kind_of?(Numeric)) }
46
- }
47
- assert_equal %w[d e f], b.column_names
48
- b.each { |r|
49
- assert_equal %w[d e f], r.attributes
50
- assert_nothing_raised { r.d; r.e; r.f }
51
- [r.d,r.e,r.f].each { |i| assert(i.kind_of?(Numeric)) }
52
- }
53
- c.each { |r|
54
- assert_nothing_raised { r[0]; r[1]; r[2] }
55
- [r[0],r[1],r[2]].each { |i| assert(i.kind_of?(Numeric)) }
56
- }
35
+ b = Ruport::Data::Record.new [1,2,3], :attributes => %w[col1 col2 col3]
36
+ tables.zip([[],[],[a],[b]]).each { |t,n|
37
+ assert_equal n, t.data }
38
+ end
39
+
40
+ def test_column
41
+ a = [[1,2,3],[4,5,6]].to_table(%w[a b c])
42
+ assert_equal [3,6], a.column(2)
43
+ assert_equal [2,5], a.column("b")
44
+
45
+ assert_raise(ArgumentError) { a.column("d") }
46
+ assert_raise(ArgumentError) { a.column(42) }
47
+
48
+ a = [[1],[2],[3],[4]].to_table
49
+ assert_equal [1,2,3,4], a.column(0)
50
+
57
51
  end
58
-
59
- def test_sigma
60
- table = [[1,2],[3,4],[5,6]].to_table(%w[col1 col2])
61
- assert table.respond_to?(:sigma)
62
- assert table.respond_to?(:sum)
63
- assert_equal(9,table.sigma(0))
64
- assert_equal(9,table.sigma("col1"))
65
- assert_equal(21,table.sigma { |r| r.col1 + r.col2 })
52
+
53
+ def test_set_column_names
54
+ a = [[1,2,3],[4,5,6]].to_table
55
+
56
+ assert_equal([],a.column_names)
57
+ assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a } )
58
+
59
+ a.column_names = %w[a b c]
60
+ assert_equal(%w[a b c],a.column_names)
61
+ a.each { |r| assert_equal(%w[a b c], r.attributes) }
62
+ assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a })
63
+
64
+ a.column_names = %w[d e f]
65
+ assert_equal(%w[d e f],a.column_names)
66
+ a.each { |r| assert_equal(%w[d e f], r.attributes) }
67
+ assert_equal([[1,2,3],[4,5,6]],a.map { |r| r.to_a })
68
+ end
69
+
70
+ def test_rows_with
71
+ table = [[1,2,3],[1,3,4],[7,8,9]].to_table(%w[a b c])
72
+
73
+ assert_equal([table[0],table[1]],table.rows_with("a" => 1))
74
+ assert_equal([table[1]],table.rows_with("a" => 1, "b" => 3))
75
+ assert_equal([table[0]],table.rows_with(:a => 1, :b => 2))
76
+ assert_equal([table[2]], table.rows_with_b(8))
77
+ assert_equal [table[1]], table.rows_with(%w[a b]) { |a,b| [a,b] == [1,3] }
66
78
  end
67
-
79
+
68
80
  def test_append_record
69
81
  table = Ruport::Data::Table.new :column_names => %w[a b c]
70
82
  table << Ruport::Data::Record.new([1,2,3], :attributes => %w[a b c])
71
- assert_equal([1,2,3],table[0].data)
83
+ assert_equal([1,2,3],table[0].to_a)
72
84
  assert_equal(%w[a b c],table[0].attributes)
73
85
  rec = table[0].dup
74
86
  rec.attributes = %w[a b c d]
75
- assert_raise(ArgumentError) { table << rec }
76
87
  assert_raise(ArgumentError) { table << Object.new }
77
88
  assert_raise(ArgumentError) { table << [].to_table }
78
89
  end
90
+
91
+ def test_append_hash
92
+ table = [[1,2,3],[4,5,6]].to_table(%w[a b c])
93
+ table << { "a" => 7, "c" => 9, "b" => 8 }
94
+
95
+ assert_equal [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c]), table
96
+ end
97
+
98
+ def test_sigma
99
+ table = [[1,2],[3,4],[5,6]].to_table(%w[col1 col2])
100
+ assert table.respond_to?(:sigma)
101
+ assert table.respond_to?(:sum)
102
+ assert_equal(9,table.sigma(0))
103
+ assert_equal(9,table.sigma("col1"))
104
+ assert_equal(21,table.sigma { |r| r.col1 + r.col2 })
105
+ end
79
106
 
80
107
  def test_append_table
81
108
  first = Ruport::Data::Table.new :column_names => %w[a b c],
@@ -89,12 +116,30 @@ class TestTable < Test::Unit::TestCase
89
116
  assert_equal Ruport::Data::Table.new(:column_names => %w[a b c],
90
117
  :data => [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]), combo
91
118
  end
119
+
120
+ def test_sub_table
121
+ table = [ [1,2,3,4],[5,6,7,9],
122
+ [10,11,12,13],[14,15,16,17] ].to_table(%w[a b c d])
123
+
124
+ assert_equal [[6,7],[11,12]].to_table(%w[b c]),
125
+ table.sub_table(%w[b c],1..-2)
126
+
127
+ assert_equal [[3,4,1],[7,9,5]].to_table(%w[c d a]),
128
+ table.sub_table(%w[c d a]) { |r| r.a < 10 }
129
+
130
+ assert_equal [[1,3],[5,7],[10,12],[14,16]].to_table(%w[a c]),
131
+ table.sub_table(%w[a c])
132
+
133
+ assert_equal [[10,11,12,13],[14,15,16,17]].to_table(%w[a b c d]),
134
+ table.sub_table { |r| r.c > 10 }
135
+
136
+ end
92
137
 
93
138
  def test_csv_load
94
139
  table = Ruport::Data::Table.load("test/samples/data.csv")
95
140
  assert_equal %w[col1 col2 col3], table.column_names
96
141
  rows = [%w[a b c],["d",nil,"e"]]
97
- table.each { |r| assert_equal rows.shift, r.data
142
+ table.each { |r| assert_equal rows.shift, r.to_a
98
143
  assert_equal %w[col1 col2 col3], r.attributes }
99
144
  expected = [%w[1 2 3],%w[4 5 6]].to_table(%w[a b c])
100
145
 
@@ -123,7 +168,8 @@ class TestTable < Test::Unit::TestCase
123
168
  :csv_options => { :col_sep => "\t" } )
124
169
  assert_equal expected, table
125
170
 
126
- table = Ruport::Data::Table.parse("a,b,c\n1,2,3\n4,5,6\n", :has_names => false)
171
+ table = Ruport::Data::Table.parse( "a,b,c\n1,2,3\n4,5,6\n",
172
+ :has_names => false)
127
173
  assert_equal([],table.column_names)
128
174
  assert_equal([%w[a b c],%w[1 2 3], %w[4 5 6]].to_table, table)
129
175
 
@@ -132,134 +178,112 @@ class TestTable < Test::Unit::TestCase
132
178
  def test_csv_block_form
133
179
  expected = [%w[a b],%w[1 2],%w[3 4]]
134
180
  t = Ruport::Data::Table.send(:get_table_from_csv,
135
- :parse, "a,b\n1,2\n3,4", :has_names => false) do |s,r|
181
+ :parse, "a,b\n1,2\n3,4",
182
+ :has_names => false) do |s,r|
136
183
  assert_equal expected.shift, r
137
184
  s << r
138
185
  end
139
186
  assert_equal [%w[a b],%w[1 2],%w[3 4]].to_table, t
140
187
  end
141
188
 
142
- def test_ensure_using_csv_block_mode_works
143
- expected = [%w[a b],%w[1 2],%w[3 4]]
144
- t = Ruport::Data::Table.parse("a,b\n1,2\n3,4",:has_names => false) { |s,r|
145
- assert_equal expected.shift, r
146
- s << r
147
- s << r
148
- }
149
- assert_equal [%w[a b],%w[a b],%w[1 2], %w[1 2],
150
- %w[3 4],%w[3 4]].to_table, t
151
- x = Ruport::Data::Table.load("test/samples/data.csv") { |s,r|
152
- assert_kind_of Ruport::Data::Table, s
153
- assert_kind_of Array, r
154
- s << r
155
- s << r
156
- }
157
- assert_equal 4, x.length
158
- end
159
-
160
-
161
189
  def test_reorder
162
190
  table = Ruport::Data::Table.load("test/samples/data.csv")
163
- table.reorder! *%w[col1 col3]
191
+ table.reorder *%w[col1 col3]
164
192
  assert_equal %w[col1 col3], table.column_names
165
193
  rows = [%w[a c], %w[d e]]
166
- table.each { |r| assert_equal rows.shift, r.data
194
+ table.each { |r| assert_equal rows.shift, r.to_a
167
195
  assert_equal %w[col1 col3], r.attributes }
168
196
  a = [[1,2,3],[4,5,6]].to_table(%w[a b c]).reorder 2,0
169
197
  rows = [[3,1],[6,4]]
170
- a.each { |r| assert_equal rows.shift, r.data
198
+ a.each { |r| assert_equal rows.shift, r.to_a
171
199
  assert_equal %w[c a], r.attributes }
172
200
  assert_equal %w[c a], a.column_names
173
201
 
174
202
  b = [[1,2,3],[4,5,6]].to_table(%w[a b c]).reorder(%w[a c])
175
203
  rows = [[1,3],[4,6]]
176
204
  b.each { |r|
177
- assert_equal rows.shift, r.data
205
+ assert_equal rows.shift, r.to_a
178
206
  assert_equal %w[a c], r.attributes
179
207
  assert_equal b.column_names.object_id,
180
208
  r.instance_eval{@attributes}.object_id
181
209
  }
182
210
  end
183
211
 
184
- def test_append_column
212
+ def test_add_column
213
+
185
214
  a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
186
- a.append_column(:name => "c")
215
+ a.add_column("c")
187
216
  assert_equal [[1,2,nil],[3,4,nil],[5,6,nil]].to_table(%w[a b c]), a
188
- a = [[1,2],[3,4],[5,6]].to_table
189
- a.append_column
190
- assert_equal [[1,2,nil],[3,4,nil],[5,6,nil]].to_table, a
217
+
191
218
  a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
192
- a.append_column(:name => "c",:fill => "x")
193
- assert_equal [[1,2,'x'],[3,4,'x'],[5,6,'x']].to_table(%w[a b c]), a
194
- a.append_column(:name => "d") { |r| r.to_a.join("|") }
219
+ a.add_column("c",:default => "x")
220
+ assert_equal [[1,2,'x'],[3,4,'x'],[5,6,'x']].to_table(%w[a b c]), a
221
+
222
+ b = a.dup
223
+ b.add_column("x",:before => "b")
224
+ assert_equal [[1,nil,2,'x'],
225
+ [3,nil,4,'x'],
226
+ [5,nil,6,'x']].to_table(%w[a x b c]), b
227
+
228
+ b = a.dup
229
+ b.add_column("x",:after => "b")
230
+ assert_equal [[1,2,nil,'x'],
231
+ [3,4,nil,'x'],
232
+ [5,6,nil,'x']].to_table(%w[a b x c]), b
233
+
234
+
235
+ a.add_column("d") { |r| r[0]+r[1] }
195
236
  assert_equal(
196
- [ [1,2,'x','1|2|x'],
197
- [3,4,'x',"3|4|x"],
198
- [5,6,'x','5|6|x']].to_table(%w[a b c d]), a)
237
+ [ [1,2,'x',3],
238
+ [3,4,'x',7],
239
+ [5,6,'x',11] ].to_table(%w[a b c d]), a)
240
+
241
+ a.add_column("x",:position => 1)
242
+ assert_equal(
243
+ [ [1,nil,2,'x',3],
244
+ [3,nil,4,'x',7],
245
+ [5,nil,6,'x',11] ].to_table(%w[a x b c d]), a)
199
246
 
200
247
  end
201
-
248
+
202
249
  def test_remove_column
203
- a = [[1,2],[3,4],[5,6]].to_table(%w[a b])
250
+ a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
204
251
  b = a.dup
205
-
206
- b.remove_column("b")
207
- assert_equal [[1],[3],[5]].to_table(%w[a]), b
208
- a.append_column(:name => "c")
209
- assert_not_equal [[1,2],[3,4],[5,6]].to_table(%w[a b]), a
210
- a.remove_column(:name => "c")
211
- assert_equal [[1,2],[3,4],[5,6]].to_table(%w[a b]), a
212
- assert_raises(ArgumentError){a.remove_column(:name => "frank")}
213
- a.remove_column(0)
214
- assert_equal [[2],[4],[6]].to_table(%w[b]), a
215
- assert_equal %w[b], a.column_names
216
- a = [[1,2],[3,4],[5,6]].to_table
217
- a.remove_column(0)
218
- assert_equal [[2],[4],[6]].to_table, a
219
- end
220
-
221
- def test_split
222
- table = Ruport::Data::Table.new :column_names => %w[c1 c2 c3]
223
- table << ['a',2,3]
224
- table << ['d',5,6]
225
- table << ['a',4,5]
226
- table << ['b',3,4]
227
- table << ['d',9,10]
228
-
229
- group = table.split :group => "c1"
230
- assert group.respond_to?(:each_group)
231
- expected = %w[a d b]
232
-
233
- group.each_group { |g| assert_equal(expected.shift, g) }
234
-
235
- t = table.reorder("c2","c3")
236
-
237
- data = [[t[0],t[2]],[t[1],t[4]],[t[3]]]
238
- c1 = Ruport::Data::Record.new data, :attributes => %w[a d b]
239
- assert_equal c1.a, group.a.to_a
240
- assert_equal c1.d, group.d.to_a
241
- assert_equal c1.b, group.b.to_a
242
-
243
- table << ['a',2,7]
244
- table << ['d',9,11]
245
-
246
- group = table.split :group => %w[c1 c2]
247
- assert group.respond_to?(:each_group)
248
- expected = %w[a_2 d_5 a_4 b_3 d_9]
249
-
250
- group.each_group { |g| assert_equal(expected.shift, g) }
251
-
252
- t = table.reorder("c3")
253
- data = [[t[0],t[5]],[t[1]],[t[2]],[t[3]],[t[4],t[6]]]
254
-
255
- c1 = Ruport::Data::Record.new data, :attributes => %w[a_2 d_5 a_4 b_3 d_9]
256
252
 
257
- assert_equal c1.a_2, group.a_2.to_a
258
- assert_equal c1.d_5, group.d_5.to_a
259
- assert_equal c1.a_4, group.a_4.to_a
260
- assert_equal c1.b_3, group.b_3.to_a
261
- assert_equal c1.d_9, group.d_9.to_a
253
+ a.remove_column("b")
254
+ assert_equal Table(%w[a c]) { |t| t << [1,3] << [4,6] }, a
262
255
 
256
+ b.remove_column(2)
257
+ assert_equal Table(%w[a b]) { |t| t << [1,2] << [4,5] }, b
258
+ end
259
+
260
+ def test_remove_columns
261
+ a = Table(%w[a b c d]) { |t| t << [1,2,3,4] << [5,6,7,8] }
262
+ b = a.dup
263
+ a.remove_columns("b","d")
264
+ assert_equal Table(%w[a c]) { |t| t << [1,3] << [5,7] }, a
265
+ b.remove_columns(%w[a c])
266
+ assert_equal Table(%w[b d]) { |t| t << [2,4] << [6,8] }, b
267
+ end
268
+
269
+ def test_rename_column
270
+ a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
271
+ a.rename_column("b","x")
272
+ assert_equal Table(%w[a x]) { |t| t << [1,2] << [3,4] }, a
273
+ end
274
+
275
+ def test_swap_column
276
+ a = Table(%w[a b]) { |t| t << [1,2] << [3,4] }
277
+ a.swap_column("a","b")
278
+ assert_equal Table(%w[b a]) { |t| t << [2,1] << [4,3] }, a
279
+ a.swap_column(1,0)
280
+ assert_equal Table(%w[a b]) { |t| t << [1,2] << [3,4] }, a
281
+ end
282
+
283
+ def test_replace_column
284
+ a = Table(%w[a b c]) { |t| t << [1,2,3] << [4,5,6] }
285
+ a.replace_column("b","d") { |r| r.b.to_s }
286
+ assert_equal Table(%w[a d c]) { |t| t << [1,"2",3] << [4,"5",6] }, a
263
287
  end
264
288
 
265
289
  def test_append_chain
@@ -275,55 +299,7 @@ class TestTable < Test::Unit::TestCase
275
299
  assert_equal("a,b\n1,2\n3,4\n5,6\n",table.to_csv)
276
300
  end
277
301
 
278
- def test_to_set
279
- table = Ruport::Data::Table.new :column_names => %w[a b],
280
- :data => [[1,2],[3,4],[5,6]]
281
- a = table.to_set
282
- b = Ruport::Data::Set.new :data => [table[1],table[0],table[2]]
283
-
284
- assert_equal a,b
285
- end
286
302
 
287
- def test_ensure_coerce_sum
288
-
289
- s = [["1"],["3"],["5"] ].to_table
290
- t = [["1.23"],["1.5"]].to_table
291
-
292
- assert_equal(9,s.sum(0))
293
- assert_equal(2.73,t.sum(0))
294
-
295
- end
296
-
297
-
298
- #Ticket #142
299
- def test_ensure_constructor_dups_record_tags
300
- a = [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c])
301
- b = a.dup
302
-
303
- a[0].tag :foo
304
- assert_equal [], b[0].tags
305
- assert_equal [:foo],a[0].tags
306
-
307
- b[1].tag :bar
308
- assert_equal [], a[1].tags
309
- assert_equal [:bar], b[1].tags
310
- end
311
-
312
- def test_setting_column_names_changes_record_attributes
313
- table = Ruport::Data::Table.new :column_names => %w[a b c],
314
- :data => [[1,2,3],[4,5,6]]
315
-
316
- assert_equal %w[a b c], table.column_names
317
- assert_equal %w[a b c], table.data[0].attributes
318
- assert_equal %w[a b c], table.data[1].attributes
319
-
320
- table.column_names = %w[d e f]
321
-
322
- assert_equal %w[d e f], table.column_names
323
- assert_equal %w[d e f], table.data[0].attributes
324
- assert_equal %w[d e f], table.data[1].attributes
325
- end
326
-
327
303
  def test_sort_rows_by
328
304
  table = Ruport::Data::Table.new :column_names => %w[a b c]
329
305
  table << [1,2,3] << [6,1,8] << [9,1,4]
@@ -353,9 +329,133 @@ class TestTable < Test::Unit::TestCase
353
329
 
354
330
  assert_equal table, table2
355
331
 
332
+ end
333
+
334
+ # for those in a meta-mood (mostly just a collection coverage )
335
+ def test_table_to_table
336
+ a = [[1,2,3]].to_table
337
+ assert_kind_of Ruport::Data::Table, a
338
+ assert_equal [[1,2,3]].to_table(%w[a b c]),
339
+ a.to_table(:column_names => %w[a b c])
356
340
  end
357
341
 
342
+ def test_record_class
343
+ a = Ruport::Data::Table.new( :column_names => %w[first_name last_name c],
344
+ :data =>[['joe','loop',3],['jim','blue',6]],
345
+ :record_class => Person )
346
+ assert_equal a, [
347
+ ['joe','loop',3],['jim','blue',6]
348
+ ].to_table(%w[first_name last_name c])
349
+ assert_kind_of Person, a[0]
350
+ assert_equal 'joe loop', a[0].name
351
+ assert_equal 'jim blue', a[1].name
352
+
353
+ b = Table(%w[first_name last_name], :record_class => Person ) do |t|
354
+ t << { 'first_name' => 'joe', 'last_name' => 'frasier' }
355
+ t << { 'first_name' => 'brian', 'last_name' => 'black' }
356
+ end
357
+
358
+ b.each { |r| assert_kind_of Person, r }
359
+
360
+ assert_equal ['joe frasier', 'brian black'],
361
+ b.map { |r| r.name }
362
+
363
+ end
364
+
365
+
366
+ ## BUG Traps -------------------------------------------------
367
+
368
+ def test_ensure_setting_column_names_changes_record_attributes
369
+ table = Ruport::Data::Table.new :column_names => %w[a b c],
370
+ :data => [[1,2,3],[4,5,6]]
371
+
372
+ assert_equal %w[a b c], table.column_names
373
+ assert_equal %w[a b c], table.data[0].attributes
374
+ assert_equal %w[a b c], table.data[1].attributes
375
+
376
+ table.column_names = %w[d e f]
377
+
378
+ assert_equal %w[d e f], table.column_names
379
+ assert_equal %w[d e f], table.data[0].attributes
380
+ assert_equal %w[d e f], table.data[1].attributes
381
+ end
382
+
383
+ def test_ensure_table_creation_allows_record_coercion
384
+ table = [[1,2,3],[4,5,6],[7,8,9]].to_table
385
+ table_with_names = [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c])
386
+
387
+ a,b,c = nil
388
+ assert_nothing_raised { a = table.to_a.to_table(%w[a b c]) }
389
+ assert_nothing_raised { b = table.to_a.to_table(%w[d e f]) }
390
+ assert_nothing_raised { c = table_with_names.to_a.to_table }
358
391
 
392
+ [a,b,c].each { |t| assert_equal(3,t.length) }
393
+ assert_equal %w[a b c], a.column_names
394
+ a.each { |r|
395
+ assert_equal %w[a b c], r.attributes
396
+ assert_nothing_raised { r.a; r.b; r.c }
397
+ [r.a,r.b,r.c].each { |i| assert(i.kind_of?(Numeric)) }
398
+ }
399
+ assert_equal %w[d e f], b.column_names
400
+ b.each { |r|
401
+ assert_equal %w[d e f], r.attributes
402
+ assert_nothing_raised { r.d; r.e; r.f }
403
+ [r.d,r.e,r.f].each { |i| assert(i.kind_of?(Numeric)) }
404
+ }
405
+ c.each { |r|
406
+ assert_nothing_raised { r[0]; r[1]; r[2] }
407
+ [r[0],r[1],r[2]].each { |i| assert(i.kind_of?(Numeric)) }
408
+ }
409
+ end
410
+
411
+ def test_ensure_using_csv_block_mode_works
412
+ expected = [%w[a b],%w[1 2],%w[3 4]]
413
+ t = Ruport::Data::Table.parse("a,b\n1,2\n3,4",:has_names => false) { |s,r|
414
+ assert_equal expected.shift, r
415
+ s << r
416
+ s << r
417
+ }
418
+ assert_equal [%w[a b],%w[a b],%w[1 2], %w[1 2],
419
+ %w[3 4],%w[3 4]].to_table, t
420
+ x = Ruport::Data::Table.load("test/samples/data.csv") { |s,r|
421
+ assert_kind_of Ruport::Data::Table, s
422
+ assert_kind_of Array, r
423
+ s << r
424
+ s << r
425
+ }
426
+ assert_equal 4, x.length
427
+ end
428
+
429
+ # bug found with paul novak 2007.01.17
430
+ def test_ensure_tags_preserved_in_subtable
431
+ a = [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c])
432
+ a[1].tag(:foo)
433
+ a.create_group("bar") { |r| r.b < 6 }
434
+ assert_equal Set.new(["grp_bar"]), a.group("bar")[0].tags
435
+ assert_equal Set.new([:foo,"grp_bar"]), a.group("bar")[1].tags
436
+ end
437
+
438
+ def test_ensure_coerce_sum
439
+ s = [["1"],["3"],["5"] ].to_table
440
+ t = [["1.23"],["1.5"]].to_table
441
+
442
+ assert_equal(9,s.sum(0))
443
+ assert_equal(2.73,t.sum(0))
444
+ end
445
+
446
+ #Ticket #142
447
+ def test_ensure_constructor_dups_record_tags
448
+ a = [[1,2,3],[4,5,6],[7,8,9]].to_table(%w[a b c])
449
+ b = a.dup
450
+
451
+ a[0].tag :foo
452
+ assert_equal Set.new([]), b[0].tags
453
+ assert_equal Set.new([:foo]),a[0].tags
454
+
455
+ b[1].tag :bar
456
+ assert_equal Set.new([]), a[1].tags
457
+ assert_equal Set.new([:bar]), b[1].tags
458
+ end
359
459
 
360
460
  end
361
461
 
@@ -377,5 +477,5 @@ class TestTableKernelHack < Test::Unit::TestCase
377
477
  assert_equal Table("a"), Table(%w[a])
378
478
  assert_equal Table(:a), Table([:a])
379
479
  end
380
-
480
+
381
481
  end