Skip to content

Commit

Permalink
Added aggregation test for Table::Pivot; adjusted pivot method to agg…
Browse files Browse the repository at this point in the history
…regate values instead of taking just the first one
  • Loading branch information
Bob Nadler authored and Bob Nadler committed Feb 13, 2010
1 parent 7a8b162 commit 9fd92cd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/ruport/data/table.rb
Expand Up @@ -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|
Expand Down
17 changes: 17 additions & 0 deletions test/table_pivot_test.rb
Expand Up @@ -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

@benkoshy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

has this been applied to the offical repo?

Please sign in to comment.