2 require 'models/company'
6 Company
.has_many
:accounts
8 class NumericData
< ActiveRecord
::Base
9 self.table_name
= 'numeric_data'
12 class CalculationsTest
< ActiveRecord
::TestCase
13 fixtures
:companies, :accounts, :topics
15 def test_should_sum_field
16 assert_equal
318, Account
.sum(:credit_limit)
19 def test_should_average_field
20 value
= Account
.average(:credit_limit)
21 assert_kind_of BigDecimal
, value
22 assert_equal BigDecimal
.new('53.0'), value
25 def test_should_return_nil_as_average
26 assert_nil NumericData
.average(:bank_balance)
29 def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal
30 assert_equal
0, NumericData
.send(:type_cast_calculated_value, 0, nil, 'avg')
31 assert_equal
53.0, NumericData
.send(:type_cast_calculated_value, 53, nil, 'avg')
34 def test_should_get_maximum_of_field
35 assert_equal
60, Account
.maximum(:credit_limit)
38 def test_should_get_maximum_of_field_with_include
39 assert_equal
50, Account
.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'")
42 def test_should_get_maximum_of_field_with_scoped_include
43 Account.with_scope :find => { :include => :firm, :conditions => "companies
.name !
= 'Summit'" } do
44 assert_equal 50, Account.maximum(:credit_limit)
48 def test_should_get_minimum_of_field
49 assert_equal 50, Account.minimum(:credit_limit)
52 def test_should_group_by_field
53 c = Account.sum(:credit_limit, :group => :firm_id)
54 [1,6,2].each { |firm_id| assert c.keys.include?(firm_id) }
57 def test_should_group_by_summed_field
58 c = Account.sum(:credit_limit, :group => :firm_id)
60 assert_equal 105, c[6]
64 def test_should_order_by_grouped_field
65 c = Account.sum(:credit_limit, :group => :firm_id, :order => "firm_id
")
66 assert_equal [1, 2, 6, 9], c.keys.compact
69 def test_should_order_by_calculation
70 c = Account.sum(:credit_limit, :group => :firm_id, :order => "sum_credit_limit desc
, firm_id
")
71 assert_equal [105, 60, 53, 50, 50], c.keys.collect { |k| c[k] }
72 assert_equal [6, 2, 9, 1], c.keys.compact
75 def test_should_limit_calculation
76 c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL
",
77 :group => :firm_id, :order => "firm_id
", :limit => 2)
78 assert_equal [1, 2], c.keys.compact
81 def test_should_limit_calculation_with_offset
82 c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL
",
83 :group => :firm_id, :order => "firm_id
", :limit => 2, :offset => 1)
84 assert_equal [2, 6], c.keys.compact
87 def test_should_group_by_summed_field_having_condition
88 c = Account.sum(:credit_limit, :group => :firm_id,
89 :having => 'sum(credit_limit) > 50')
91 assert_equal 105, c[6]
95 def test_should_group_by_summed_field_having_sanitized_condition
96 c = Account.sum(:credit_limit, :group => :firm_id,
97 :having => ['sum(credit_limit) > ?', 50])
99 assert_equal 105, c[6]
100 assert_equal 60, c[2]
103 def test_should_group_by_summed_association
104 c = Account.sum(:credit_limit, :group => :firm)
105 assert_equal 50, c[companies(:first_firm)]
106 assert_equal 105, c[companies(:rails_core)]
107 assert_equal 60, c[companies(:first_client)]
110 def test_should_sum_field_with_conditions
111 assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
114 def test_should_return_zero_if_sum_conditions_return_nothing
115 assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2')
116 assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2')
119 def test_sum_should_return_valid_values_for_decimals
120 NumericData.create(:bank_balance => 19.83)
121 assert_equal 19.83, NumericData.sum(:bank_balance)
124 def test_should_group_by_summed_field_with_conditions
125 c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
128 assert_equal 105, c[6]
129 assert_equal 60, c[2]
132 def test_should_group_by_summed_field_with_conditions_and_having
133 c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
135 :having => 'sum(credit_limit) > 60')
137 assert_equal 105, c[6]
141 def test_should_group_by_fields_with_table_alias
142 c = Account.sum(:credit_limit, :group => 'accounts.firm_id')
143 assert_equal 50, c[1]
144 assert_equal 105, c[6]
145 assert_equal 60, c[2]
148 def test_should_calculate_with_invalid_field
149 assert_equal 6, Account.calculate(:count, '*')
150 assert_equal 6, Account.calculate(:count, :all)
153 def test_should_calculate_grouped_with_invalid_field
154 c = Account.count(:all, :group => 'accounts.firm_id')
160 def test_should_calculate_grouped_association_with_invalid_field
161 c = Account.count(:all, :group => :firm)
162 assert_equal 1, c[companies(:first_firm)]
163 assert_equal 2, c[companies(:rails_core)]
164 assert_equal 1, c[companies(:first_client)]
167 def test_should_group_by_association_with_non_numeric_foreign_key
168 ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all
" => 1, "firm_id
" => "ABC
"}])
171 firm.expects(:id).returns("ABC
")
172 firm.expects(:class).returns(Firm)
173 Company.expects(:find).with(["ABC
"]).returns([firm])
176 column.expects(:name).at_least_once.returns(:firm_id)
177 column.expects(:type_cast).with("ABC
").returns("ABC
")
178 Account.expects(:columns).at_least_once.returns([column])
180 c = Account.count(:all, :group => :firm)
181 first_key = c.keys.first
182 assert_equal Firm, first_key.class
183 assert_equal 1, c[first_key]
186 def test_should_calculate_grouped_association_with_foreign_key_option
187 Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id'
188 c = Account.count(:all, :group => :another_firm)
189 assert_equal 1, c[companies(:first_firm)]
190 assert_equal 2, c[companies(:rails_core)]
191 assert_equal 1, c[companies(:first_client)]
194 def test_should_not_modify_options_when_using_includes
195 options = {:conditions => 'companies.id > 1', :include => :firm}
196 options_copy = options.dup
198 Account.count(:all, options)
199 assert_equal options_copy, options
202 def test_should_calculate_grouped_by_function
203 c = Company.count(:all, :group => "UPPER(#{QUOTED_TYPE})")
204 assert_equal 2, c[nil]
205 assert_equal 1, c['DEPENDENTFIRM']
206 assert_equal 3, c['CLIENT']
207 assert_equal 2, c['FIRM']
210 def test_should_calculate_grouped_by_function_with_table_alias
211 c = Company.count(:all, :group => "UPPER(companies
.#{QUOTED_TYPE})")
212 assert_equal 2, c[nil]
213 assert_equal 1, c['DEPENDENTFIRM']
214 assert_equal 3, c['CLIENT']
215 assert_equal 2, c['FIRM']
218 def test_should_not_overshadow_enumerable_sum
219 assert_equal 6, [1, 2, 3].sum(&:abs)
222 def test_should_sum_scoped_field
223 assert_equal 15, companies(:rails_core).companies.sum(:id)
226 def test_should_sum_scoped_field_with_conditions
227 assert_equal 8, companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
230 def test_should_group_by_scoped_field
231 c = companies(:rails_core).companies.sum(:id, :group => :name)
232 assert_equal 7, c['Leetsoft']
233 assert_equal 8, c['Jadedpixel']
236 def test_should_group_by_summed_field_with_conditions_and_having
237 c = companies(:rails_core).companies.sum(:id, :group => :name,
238 :having => 'sum(id) > 7')
239 assert_nil c['Leetsoft']
240 assert_equal 8, c['Jadedpixel']
243 def test_should_reject_invalid_options
244 assert_nothing_raised do
245 [:count, :sum].each do |func|
246 # empty options are valid
247 Company.send(:validate_calculation_options, func)
248 # these options are valid for all calculations
249 [:select, :conditions, :joins, :order, :group, :having, :distinct].each do |opt|
250 Company.send(:validate_calculation_options, func, opt => true)
254 # :include is only valid on :count
255 Company.send(:validate_calculation_options, :count, :include => true)
258 assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
259 assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
262 def test_should_count_selected_field_with_include
263 assert_equal 6, Account.count(:distinct => true, :include => :firm)
264 assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
267 def test_should_count_scoped_select
268 Account.update_all("credit_limit
= NULL
")
269 assert_equal 0, Account.scoped(:select => "credit_limit
").count
272 def test_should_count_scoped_select_with_options
273 Account.update_all("credit_limit
= NULL
")
274 Account.last.update_attribute('credit_limit', 49)
275 Account.first.update_attribute('credit_limit', 51)
277 assert_equal 1, Account.scoped(:select => "credit_limit
").count(:conditions => ['credit_limit >= 50'])
280 def test_should_count_manual_select_with_include
281 assert_equal 6, Account.count(:select => "DISTINCT accounts
.id
", :include => :firm)
284 def test_count_with_column_parameter
285 assert_equal 5, Account.count(:firm_id)
288 def test_count_with_column_and_options_parameter
289 assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit
= 50")
292 def test_count_with_no_parameters_isnt_deprecated
293 assert_not_deprecated { Account.count }
296 def test_count_with_too_many_parameters_raises
297 assert_raise(ArgumentError) { Account.count(1, 2, 3) }
300 def test_should_sum_expression
301 assert_equal '636', Account.sum("2 * credit_limit
")
304 def test_count_with_from_option
305 assert_equal Company.count(:all), Company.count(:all, :from => 'companies')
306 assert_equal Account.count(:all, :conditions => "credit_limit
= 50"),
307 Account.count(:all, :from => 'accounts', :conditions => "credit_limit
= 50")
308 assert_equal Company.count(:type, :conditions => {:type => "Firm
"}),
309 Company.count(:type, :conditions => {:type => "Firm
"}, :from => 'companies')
312 def test_sum_with_from_option
313 assert_equal Account.sum(:credit_limit), Account.sum(:credit_limit, :from => 'accounts')
314 assert_equal Account.sum(:credit_limit, :conditions => "credit_limit
> 50"),
315 Account.sum(:credit_limit, :from => 'accounts', :conditions => "credit_limit
> 50")
318 def test_average_with_from_option
319 assert_equal Account.average(:credit_limit), Account.average(:credit_limit, :from => 'accounts')
320 assert_equal Account.average(:credit_limit, :conditions => "credit_limit
> 50"),
321 Account.average(:credit_limit, :from => 'accounts', :conditions => "credit_limit
> 50")
324 def test_minimum_with_from_option
325 assert_equal Account.minimum(:credit_limit), Account.minimum(:credit_limit, :from => 'accounts')
326 assert_equal Account.minimum(:credit_limit, :conditions => "credit_limit
> 50"),
327 Account.minimum(:credit_limit, :from => 'accounts', :conditions => "credit_limit
> 50")
330 def test_maximum_with_from_option
331 assert_equal Account.maximum(:credit_limit), Account.maximum(:credit_limit, :from => 'accounts')
332 assert_equal Account.maximum(:credit_limit, :conditions => "credit_limit
> 50"),
333 Account.maximum(:credit_limit, :from => 'accounts', :conditions => "credit_limit
> 50")
336 def test_from_option_with_specified_index
337 if Edge.connection.adapter_name == 'MySQL'
338 assert_equal Edge.count(:all), Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)')
339 assert_equal Edge.count(:all, :conditions => 'sink_id < 5'),
340 Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)', :conditions => 'sink_id < 5')
344 def test_from_option_with_table_different_than_class
345 assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')