1 require 'abstract_unit'
2 require 'fixtures/company'
3 require 'fixtures/topic'
5 Company.has_many :accounts
7 class NumericData < ActiveRecord::Base
8 self.table_name = 'numeric_data'
11 class CalculationsTest < Test::Unit::TestCase
12 fixtures :companies, :accounts, :topics
14 def test_should_sum_field
15 assert_equal 318, Account.sum(:credit_limit)
18 def test_should_average_field
19 value = Account.average(:credit_limit)
20 assert_kind_of Float, value
21 assert_in_delta 53.0, value, 0.001
24 def test_should_return_nil_as_average
25 assert_nil NumericData.average(:bank_balance)
28 def test_should_get_maximum_of_field
29 assert_equal 60, Account.maximum(:credit_limit)
32 def test_should_get_maximum_of_field_with_include
33 assert_equal 50, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'")
36 def test_should_get_maximum_of_field_with_scoped_include
37 Account.with_scope :find => { :include => :firm, :conditions => "companies.name != 'Summit'" } do
38 assert_equal 50, Account.maximum(:credit_limit)
42 def test_should_get_minimum_of_field
43 assert_equal 50, Account.minimum(:credit_limit)
46 def test_should_group_by_field
47 c = Account.sum(:credit_limit, :group => :firm_id)
48 [1,6,2].each { |firm_id| assert c.keys.include?(firm_id) }
51 def test_should_group_by_summed_field
52 c = Account.sum(:credit_limit, :group => :firm_id)
54 assert_equal 105, c[6]
58 def test_should_order_by_grouped_field
59 c = Account.sum(:credit_limit, :group => :firm_id, :order => "firm_id")
60 assert_equal [1, 2, 6, 9], c.keys.compact
63 def test_should_order_by_calculation
64 c = Account.sum(:credit_limit, :group => :firm_id, :order => "sum_credit_limit desc, firm_id")
65 assert_equal [105, 60, 53, 50, 50], c.keys.collect { |k| c[k] }
66 assert_equal [6, 2, 9, 1], c.keys.compact
69 def test_should_limit_calculation
70 c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL",
71 :group => :firm_id, :order => "firm_id", :limit => 2)
72 assert_equal [1, 2], c.keys.compact
75 def test_should_limit_calculation_with_offset
76 c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL",
77 :group => :firm_id, :order => "firm_id", :limit => 2, :offset => 1)
78 assert_equal [2, 6], c.keys.compact
81 def test_should_group_by_summed_field_having_condition
82 c = Account.sum(:credit_limit, :group => :firm_id,
83 :having => 'sum(credit_limit) > 50')
85 assert_equal 105, c[6]
89 def test_should_group_by_summed_association
90 c = Account.sum(:credit_limit, :group => :firm)
91 assert_equal 50, c[companies(:first_firm)]
92 assert_equal 105, c[companies(:rails_core)]
93 assert_equal 60, c[companies(:first_client)]
96 def test_should_sum_field_with_conditions
97 assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
100 def test_should_group_by_summed_field_with_conditions
101 c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
104 assert_equal 105, c[6]
105 assert_equal 60, c[2]
108 def test_should_group_by_summed_field_with_conditions_and_having
109 c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
111 :having => 'sum(credit_limit) > 60')
113 assert_equal 105, c[6]
117 def test_should_group_by_fields_with_table_alias
118 c = Account.sum(:credit_limit, :group => 'accounts.firm_id')
119 assert_equal 50, c[1]
120 assert_equal 105, c[6]
121 assert_equal 60, c[2]
124 def test_should_calculate_with_invalid_field
125 assert_equal 6, Account.calculate(:count, '*')
126 assert_equal 6, Account.calculate(:count, :all)
129 def test_should_calculate_grouped_with_invalid_field
130 c = Account.count(:all, :group => 'accounts.firm_id')
136 def test_should_calculate_grouped_association_with_invalid_field
137 c = Account.count(:all, :group => :firm)
138 assert_equal 1, c[companies(:first_firm)]
139 assert_equal 2, c[companies(:rails_core)]
140 assert_equal 1, c[companies(:first_client)]
143 uses_mocha 'group_by_non_numeric_foreign_key_association' do
144 def test_should_group_by_association_with_non_numeric_foreign_key
145 ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}])
148 firm.expects(:id).returns("ABC")
149 firm.expects(:class).returns(Firm)
150 Company.expects(:find).with(["ABC"]).returns([firm])
153 column.expects(:name).at_least_once.returns(:firm_id)
154 column.expects(:type_cast).with("ABC").returns("ABC")
155 Account.expects(:columns).at_least_once.returns([column])
157 c = Account.count(:all, :group => :firm)
158 assert_equal Firm, c.first.first.class
159 assert_equal 1, c.first.last
163 def test_should_not_modify_options_when_using_includes
164 options = {:conditions => 'companies.id > 1', :include => :firm}
165 options_copy = options.dup
167 Account.count(:all, options)
168 assert_equal options_copy, options
171 def test_should_calculate_grouped_by_function
172 c = Company.count(:all, :group => "UPPER(#{QUOTED_TYPE})")
173 assert_equal 2, c[nil]
174 assert_equal 1, c['DEPENDENTFIRM']
175 assert_equal 3, c['CLIENT']
176 assert_equal 2, c['FIRM']
179 def test_should_calculate_grouped_by_function_with_table_alias
180 c = Company.count(:all, :group => "UPPER(companies.#{QUOTED_TYPE})")
181 assert_equal 2, c[nil]
182 assert_equal 1, c['DEPENDENTFIRM']
183 assert_equal 3, c['CLIENT']
184 assert_equal 2, c['FIRM']
187 def test_should_not_overshadow_enumerable_sum
188 assert_equal 6, [1, 2, 3].sum(&:abs)
191 def test_should_sum_scoped_field
192 assert_equal 15, companies(:rails_core).companies.sum(:id)
195 def test_should_sum_scoped_field_with_conditions
196 assert_equal 8, companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
199 def test_should_group_by_scoped_field
200 c = companies(:rails_core).companies.sum(:id, :group => :name)
201 assert_equal 7, c['Leetsoft']
202 assert_equal 8, c['Jadedpixel']
205 def test_should_group_by_summed_field_with_conditions_and_having
206 c = companies(:rails_core).companies.sum(:id, :group => :name,
207 :having => 'sum(id) > 7')
208 assert_nil c['Leetsoft']
209 assert_equal 8, c['Jadedpixel']
212 def test_should_reject_invalid_options
213 assert_nothing_raised do
214 [:count, :sum].each do |func|
215 # empty options are valid
216 Company.send(:validate_calculation_options, func)
217 # these options are valid for all calculations
218 [:select, :conditions, :joins, :order, :group, :having, :distinct].each do |opt|
219 Company.send(:validate_calculation_options, func, opt => true)
223 # :include is only valid on :count
224 Company.send(:validate_calculation_options, :count, :include => true)
227 assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
228 assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
231 def test_should_count_selected_field_with_include
232 assert_equal 6, Account.count(:distinct => true, :include => :firm)
233 assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
236 def test_count_with_column_parameter
237 assert_equal 5, Account.count(:firm_id)
240 def test_count_with_column_and_options_parameter
241 assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50")
244 def test_count_with_no_parameters_isnt_deprecated
245 assert_not_deprecated { Account.count }
248 def test_count_with_too_many_parameters_raises
249 assert_raise(ArgumentError) { Account.count(1, 2, 3) }