Permalink
Browse files

Added aggregation test for Table::Pivot; adjusted pivot method to agg…

…regate values instead of taking just the first one
  • Loading branch information...
Bob Nadler Bob Nadler
Bob Nadler authored and Bob Nadler committed Feb 13, 2010
1 parent 7a8b162 commit 9fd92cd785e95bb8f04d182a34840467dd7aea96
Showing with 18 additions and 1 deletion.
  1. +1 −1 lib/ruport/data/table.rb
  2. +17 −0 test/table_pivot_test.rb
View
@@ -79,7 +79,7 @@ def to_table
outer_group = outer_grouping[outer_group_name]
pivot_values = pivoted_columns.inject({}) do |hsh, e|
matching_rows = outer_group.rows_with(@pivot_column => e)
hsh[e] = matching_rows.first && matching_rows.first[@summary_column]
hsh[e] = matching_rows && matching_rows.inject(0) { |sum,row| sum + row[@summary_column] }
hsh
end
result << [outer_group_name] + pivoted_columns.map {|e|
View
@@ -132,3 +132,20 @@ def test_preserves_ordering_on_calculated_column_with_proc_pivot_order
end
end
class TablePivotaggregationTest < Test::Unit::TestCase
def setup
table = Table('Region', 'Product', 'Units Sold')
table << ['North','Widget',5]
table << ['North','Widget',10]
table << ['South','Gadget',2]
table << ['South','Gadget',4]
@pivoted = table.pivot('Product', :group_by => 'Region', :values => 'Units Sold')
end
def test_produces_correct_full_table_with_sum
expected = Table("Region","Gadget","Widget") { |t| t << ["North",0,15] << ["South",6,0] }
assert_equal(expected, @pivoted)
end
end

1 comment on commit 9fd92cd

@BKSpurgeon

This comment has been minimized.

Show comment
Hide comment
@BKSpurgeon

BKSpurgeon Sep 11, 2016

has this been applied to the offical repo?

BKSpurgeon commented on 9fd92cd Sep 11, 2016

has this been applied to the offical repo?

Please sign in to comment.