ruport 1.6.3 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby -w
2
2
  require File.join(File.expand_path(File.dirname(__FILE__)), "helpers")
3
3
 
4
- class TestRecord < Test::Unit::TestCase
4
+ class TestRecord < Minitest::Test
5
5
 
6
6
  include Ruport::Data
7
7
 
@@ -10,13 +10,13 @@ class TestRecord < Test::Unit::TestCase
10
10
  @record = Ruport::Data::Record.new [1,2,3,4], :attributes => @attributes
11
11
  end
12
12
 
13
- context "when initializing with an array with attributes" do
13
+ describe "when initializing with an array with attributes" do
14
14
  def specify_key_access_should_work
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
- assert_raise(NoMethodError) { @record.f }
19
+ assert_raises(NoMethodError) { @record.f }
20
20
  end
21
21
 
22
22
  def specify_ordinal_access_should_work
@@ -27,7 +27,7 @@ class TestRecord < Test::Unit::TestCase
27
27
  end
28
28
  end
29
29
 
30
- context "when initializing with an array without attributes" do
30
+ describe "when initializing with an array without attributes" do
31
31
  def specify_ordinal_access_should_work
32
32
  record = Ruport::Data::Record.new [1,2,3,4]
33
33
  assert_equal 1, record[0]
@@ -37,7 +37,7 @@ class TestRecord < Test::Unit::TestCase
37
37
  end
38
38
  end
39
39
 
40
- context "when initializing with a hash without attributes" do
40
+ describe "when initializing with a hash without attributes" do
41
41
  def setup
42
42
  @record = Ruport::Data::Record.new({:a => 1, :b => 2, :c => 3},{})
43
43
  end
@@ -50,7 +50,7 @@ class TestRecord < Test::Unit::TestCase
50
50
  end
51
51
  end
52
52
 
53
- context "when initializing with a hash with attributes" do
53
+ describe "when initializing with a hash with attributes" do
54
54
  def setup
55
55
  @record = Record.new({:a => 1, :b => 2, :c => 3 },
56
56
  :attributes => [:c,:b,:a])
@@ -94,7 +94,7 @@ class TestRecord < Test::Unit::TestCase
94
94
  end
95
95
 
96
96
  def test_nonexistent_accessor
97
- assert_raise NoMethodError do
97
+ assert_raises NoMethodError do
98
98
  @record.e
99
99
  end
100
100
  end
@@ -112,7 +112,7 @@ class TestRecord < Test::Unit::TestCase
112
112
  end
113
113
 
114
114
  def test_to_hash
115
- assert_nothing_raised { @record.to_hash }
115
+ @record.to_hash
116
116
  assert_equal({ "a" => 1, "b" => 2, "c" => 3, "d" => 4 }, @record.to_hash)
117
117
  end
118
118
 
@@ -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
- assert_raise(ArgumentError) { r.dup.reorder "foo" }
179
- assert_raise(ArgumentError) { r.dup.reorder 0,5 }
180
- assert_nothing_raised { r.dup.reorder 0 }
181
- assert_nothing_raised { r.dup.reorder "a","b" }
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
@@ -290,7 +290,7 @@ class TestRecord < Test::Unit::TestCase
290
290
  assert_equal "1,2,3\n", a.to_csv
291
291
  end
292
292
 
293
- context "when rendering records" do
293
+ describe "when rendering records" do
294
294
 
295
295
  def specify_record_as_should_work
296
296
  rendered_row = @record.as(:text)
@@ -307,7 +307,7 @@ class TestRecord < Test::Unit::TestCase
307
307
  assert_equal("1\t2\t3\t4\n",rendered_row)
308
308
  end
309
309
 
310
- context "when given bad format names" do
310
+ describe "when given bad format names" do
311
311
  def setup
312
312
  @a = Record.new({ "a" => 1, "b" => 2 })
313
313
  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
@@ -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::Unit::TestCase
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,16 @@ 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
24
  end
25
25
 
26
- class PivotConvertRowOrderToGroupOrderTest < Test::Unit::TestCase
26
+ class PivotConvertRowOrderToGroupOrderTest < Minitest::Test
27
27
 
28
- def convert(src)
29
- Ruport::Data::Table::Pivot.new(
30
- nil, nil, nil, nil
31
- ).convert_row_order_to_group_order(src)
28
+ def convert(row_order)
29
+ Ruport::Data::Table::Pivot.row_order_to_group_order(row_order)
32
30
  end
33
31
 
34
32
  def setup
@@ -62,10 +60,10 @@ class PivotConvertRowOrderToGroupOrderTest < Test::Unit::TestCase
62
60
 
63
61
  end
64
62
 
65
- class PivotPreservesOrdering < Test::Unit::TestCase
63
+ class PivotPreservesOrdering < Minitest::Test
66
64
 
67
- def test_group_column_entries_preserves_order_of_occurrence
68
- table = Table('group', 'a', 'b')
65
+ def test_group_column_preserves_order_of_occurrence
66
+ table = Ruport.Table('group', 'a', 'b')
69
67
  [
70
68
  [1, 0, 0],
71
69
  [9, 0, 0],
@@ -75,13 +73,13 @@ class PivotPreservesOrdering < Test::Unit::TestCase
75
73
  [8, 0, 0],
76
74
  [1, 0, 0]
77
75
  ].each {|e| table << e}
78
- assert_equal([1,9,8],
76
+ assert_equal([1,9,8],
79
77
  Ruport::Data::Table::Pivot.
80
- new(table, 'group', 'a', 'b').group_column_entries)
78
+ new(table, 'group', 'a', 'b').column)
81
79
  end
82
80
 
83
- def test_resulting_columns_preserve_ordering_of_rows
84
- table = Table('group', 'a', 'b', 'c')
81
+ def test_pivoted_row_preserves_order_of_input_rows
82
+ table = Ruport.Table('group', 'a', 'b', 'c')
85
83
  [
86
84
  [200, 1, 2, 1],
87
85
  [200, 4, 5, 2],
@@ -93,11 +91,11 @@ class PivotPreservesOrdering < Test::Unit::TestCase
93
91
  [1,4,5],
94
92
  Ruport::Data::Table::Pivot.new(
95
93
  table, 'group', 'a', 'b', :pivot_order => ['c']
96
- ).columns_from_pivot)
94
+ ).row)
97
95
  end
98
96
 
99
97
  def test_preserves_ordering
100
- table = Table('group', 'a', 'b', 'c')
98
+ table = Ruport.Table('group', 'a', 'b', 'c')
101
99
  [
102
100
  [200, 1, 2, 3],
103
101
  [200, 4, 5, 6],
@@ -105,30 +103,76 @@ class PivotPreservesOrdering < Test::Unit::TestCase
105
103
  [100, 4,11,12]
106
104
  ].each {|e| table << e}
107
105
  pivoted = table.pivot('a', :group_by => 'group', :values => 'b')
108
- expected = Table("group",1,4) { |t| t << [200,2,5] << [100,8,11] }
106
+ expected = Ruport.Table("group",1,4) { |t| t << [200,2,5] << [100,8,11] }
109
107
  assert_equal(expected, pivoted)
110
108
  end
111
109
 
112
- def test_preserves_ordering_on_calculated_column
113
- table = Table('group', 'a')
110
+ def test_reorders_a_calculated_column_by_column_name
111
+ table = Ruport.Table('group', 'a')
114
112
  [
115
113
  [1, 1], [2, 2], [3, 3]
116
114
  ].each {|e| table << e}
117
115
  table.add_column('pivotme') {|row| 10 - row.group.to_i}
118
- pivoted = table.pivot('pivotme', :group_by => 'group', :values => 'a',
116
+ pivoted = table.pivot('pivotme', :group_by => 'group', :values => 'a',
119
117
  :pivot_order => :name)
120
118
  assert_equal(['group', 7, 8, 9], pivoted.column_names)
121
119
  end
122
120
 
123
121
  def test_preserves_ordering_on_calculated_column_with_proc_pivot_order
124
- table = Table('group', 'a')
122
+ table = Ruport.Table('group', 'a')
125
123
  [
126
124
  [1, 1], [2, 2], [3, 3]
127
125
  ].each {|e| table << e}
128
126
  table.add_column('pivotme') {|row| 10 - row.group.to_i}
129
- pivoted = table.pivot('pivotme', :group_by => 'group', :values => 'a',
127
+ pivoted = table.pivot('pivotme', :group_by => 'group', :values => 'a',
130
128
  :pivot_order => proc {|row, pivot| pivot })
131
129
  assert_equal(['group', 7, 8, 9], pivoted.column_names)
132
130
  end
133
131
 
134
132
  end
133
+
134
+ class TablePivotOperationTest < Minitest::Test
135
+ def setup
136
+ @rows = [
137
+ Ruport::Data::Record.new('Values' => 3),
138
+ Ruport::Data::Record.new('Values' => 9),
139
+ Ruport::Data::Record.new('Values' => 4)
140
+ ]
141
+ end
142
+
143
+ def test_performs_operation_sum
144
+ sum = Ruport::Data::Table::Pivot::Operations.sum(@rows, 'Values')
145
+ assert_equal 16, sum
146
+ end
147
+
148
+ def test_performs_operation_first
149
+ first = Ruport::Data::Table::Pivot::Operations.first(@rows, 'Values')
150
+ assert_equal 3, first
151
+ end
152
+
153
+ def test_performs_operation_count
154
+ count = Ruport::Data::Table::Pivot::Operations.count(@rows, 'Values')
155
+ assert_equal 3, count
156
+ end
157
+
158
+ def test_performs_operation_mean
159
+ mean = Ruport::Data::Table::Pivot::Operations.mean(@rows, 'Values')
160
+ assert_equal 5, mean
161
+ end
162
+
163
+ def test_performs_operation_min
164
+ min = Ruport::Data::Table::Pivot::Operations.min(@rows, 'Values')
165
+ assert_equal 3, min
166
+ end
167
+
168
+ def test_performs_operation_max
169
+ max = Ruport::Data::Table::Pivot::Operations.max(@rows, 'Values')
170
+ assert_equal 9, max
171
+ end
172
+
173
+ def test_invalid_operation_causes_exception
174
+ assert_raises ArgumentError do
175
+ Ruport::Data::Table::Pivot.new(nil, nil, nil, nil, :operation => :foo)
176
+ end
177
+ end
178
+ end
@@ -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::Unit::TestCase
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
- table_from_records = Table(t.column_names, :data => t.data)
27
+ t = Ruport.Table(%w[a b c], :data => [[1,2,3],[4,5,6]])
28
+ table_from_records = 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
 
@@ -46,102 +45,102 @@ class TestTable < Test::Unit::TestCase
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
- def specify_filters_should_work_on_csvs
48
+ assert_equal Ruport.Table(%w[a b c]) << [1,2,3] << [7,8,9], table
49
+ end
50
+
51
+ def specify_filters_should_work_on_csvs
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
-
60
+
62
61
  def setup
63
- @data = [[1,2,3],[4,5,6],[7,8,9]]
62
+ @data = [[1,2,3],[4,5,6],[7,8,9]]
64
63
  end
65
-
64
+
66
65
  def specify_transforms_should_modify_table_data
67
-
68
- stringify_c = lambda { |r| r.c = r.c.to_s }
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
70
  if Fixnum === 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
- def specify_transforms_should_work_on_csvs
83
+
84
+ end
85
+
86
+ def specify_transforms_should_work_on_csvs
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
-
185
+ end
186
+
187
+ context "when sorting rows" do
188
+
190
189
  def setup
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]
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
-
193
+
195
194
  def specify_should_sort_in_reverse_order_on_descending
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
- def specify_show_put_rows_with_nil_columns_after_sorted_rows
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
+ def specify_show_put_rows_with_nil_columns_after_sorted_rows
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
-
213
+
215
214
  def specify_in_place_sort_should_allow_order_by
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
-
218
+
220
219
  def specify_sort_rows_by
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,19 +228,19 @@ 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
-
242
+ end
243
+
245
244
  def specify_sort_rows_by!
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]
@@ -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
+ def setup
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
+ def specify_insert_at_the_beginning
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
+ def specify_insert_in_the_middle
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
+ def specify_insert_at_the_end
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
+ def should_return_itself
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
- assert_nothing_raised { a = Table(%w[a b c], :data => table.to_a) }
302
- assert_nothing_raised { b = Table(%w[d e f], :data => table.to_a) }
303
- assert_nothing_raised { c = Table(table_with_names.column_names,
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
- assert_nothing_raised { r.a; r.b; r.c }
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
- assert_nothing_raised { r.d; r.e; r.f }
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
- assert_nothing_raised { r[0]; r[1]; r[2] }
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
- assert_nothing_raised { a.to_yaml }
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
- assert_nothing_raised { a.to_yaml }
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
- assert_raise(ArgumentError) { a.reorder("a","b","c") }
367
- assert_raise(ArgumentError) { a.reorder(%w[a b c]) }
368
- assert_raise(ArgumentError) { a.reorder(2,1,0) }
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::Unit::TestCase
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
- assert_raise(NoMethodError) { table << Object.new }
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::Unit::TestCase
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
- assert_nothing_raised { a.as(:csv) }
447
- assert_nothing_raised { a.to_csv }
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::Unit::TestCase
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
- assert_raise(ArgumentError) { a.column("d") }
462
- assert_raise(ArgumentError) { a.column(42) }
463
-
464
- a = Table([], :data => [[1],[2],[3],[4]])
465
- assert_equal [1,2,3,4], a.column(0)
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)
466
495
  end
467
-
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
- assert_raise(ArgumentError) { a.rename_columns(%w[a b], %w[x]) }
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,35 +688,35 @@ 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
- assert_nothing_raised do
668
- a.rename_column("d", "z")
669
- end
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
722
  ) do |s,r|
@@ -697,60 +726,60 @@ class TestTableFromCSV < Test::Unit::TestCase
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"),
729
+
730
+ Ruport::Data::Table.load( File.join(TEST_SAMPLES,"data.csv"),
702
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
- assert_nothing_raised {
717
- Ruport::Data::Table.parse("a,b,c\n1,2,3\n")
718
- }
719
745
 
720
- 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]])
746
+ Ruport::Data::Table.parse("a,b,c\n1,2,3\n")
722
747
 
723
- table = Ruport::Data::Table.parse( "a\tb\tc\n1\t2\t3\n4\t5\t6\n",
748
+
749
+ table = Ruport::Data::Table.parse("a,b,c\n1,2,3\n4,5,6\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,37 @@ 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
- a = Table(File.join(TEST_SAMPLES,"addressbook.csv"),
794
+ a = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"),
766
795
  :record_class => DuckRecord)
767
796
  a.each { |r| assert_kind_of(DuckRecord,r) }
768
- end
769
-
797
+ end
798
+
770
799
  def test_ensure_table_from_csv_accepts_record_class_in_block_usage
771
- a = Table(File.join(TEST_SAMPLES,"addressbook.csv"),
800
+ a = Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"),
772
801
  :record_class => DuckRecord, :records => true) do |s,r|
773
- assert_kind_of(DuckRecord,r)
802
+ assert_kind_of(DuckRecord,r)
774
803
  end
775
804
  end
776
-
805
+
777
806
  end
778
807
 
779
- class TestTableKernelHack < Test::Unit::TestCase
780
-
808
+ class TestTableKernelHack < Minitest::Test
809
+
781
810
  def test_simple
782
811
  assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]),
783
- Table(%w[a b c])
812
+ Ruport.Table(%w[a b c])
784
813
  assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]),
785
- Table("a","b","c")
814
+ Ruport.Table("a","b","c")
786
815
  assert_equal Ruport::Data::Table.load(
787
816
  File.join(TEST_SAMPLES,"addressbook.csv")),
788
- Table(File.join(TEST_SAMPLES,"addressbook.csv"))
817
+ Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"))
789
818
  assert_equal Ruport::Data::Table.load(
790
819
  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|
820
+ Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv"), :has_names => false)
821
+ Ruport.Table("a","b","c") do |t|
793
822
  t << [1,2,3]
794
823
  assert_equal(
795
824
  Ruport::Data::Table.new(:column_names => %w[a b c], :data => [[1,2,3]]),
@@ -797,19 +826,19 @@ class TestTableKernelHack < Test::Unit::TestCase
797
826
  )
798
827
  end
799
828
 
800
- assert_equal Table("a"), Table(%w[a])
801
- assert_equal Table(:a), Table([:a])
829
+ assert_equal Ruport.Table("a"), Ruport.Table(%w[a])
830
+ assert_equal Ruport.Table(:a), Ruport.Table([:a])
802
831
  end
803
832
 
804
833
  def test_iterators
805
- Table(File.join(TEST_SAMPLES,"addressbook.csv")) do |s,r|
834
+ Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv")) do |s,r|
806
835
  assert_kind_of(Array,r)
807
836
  assert_kind_of(Ruport::Data::Feeder,s)
808
837
  end
809
838
 
810
839
  n = 0
811
840
 
812
- Table(:string => "a,b,c\n1,2,3\n4,5,6\n") do |s,r|
841
+ Ruport.Table(:string => "a,b,c\n1,2,3\n4,5,6\n") do |s,r|
813
842
  assert_kind_of(Array,r)
814
843
  assert_kind_of(Ruport::Data::Feeder,s)
815
844
  n += 1
@@ -817,22 +846,22 @@ class TestTableKernelHack < Test::Unit::TestCase
817
846
 
818
847
  assert_equal 2, n
819
848
  end
820
-
849
+
821
850
  def test_with_file_arg
822
- assert_equal Table(File.join(TEST_SAMPLES,"addressbook.csv")),
823
- Table(:file => File.join(TEST_SAMPLES,"addressbook.csv"))
851
+ assert_equal Ruport.Table(File.join(TEST_SAMPLES,"addressbook.csv")),
852
+ Ruport.Table(:file => File.join(TEST_SAMPLES,"addressbook.csv"))
824
853
  end
825
-
854
+
826
855
  def test_with_string_arg
827
856
  csv_string = "id,name\n1,Inky\n2,Blinky\n3,Clyde"
828
-
857
+
829
858
  assert_equal Ruport::Data::Table.parse(csv_string),
830
- Table(:string => csv_string)
859
+ Ruport.Table(:string => csv_string)
831
860
  end
832
861
 
833
862
  def test_ensure_table_hack_accepts_normal_constructor_args
834
863
  assert_equal Ruport::Data::Table.new(:column_names => %w[a b c]),
835
- Table(:column_names => %w[a b c])
836
- end
837
-
864
+ Ruport.Table(:column_names => %w[a b c])
865
+ end
866
+
838
867
  end