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_association
96 c = Account.sum(:credit_limit, :group => :firm)
97 assert_equal 50, c[companies(:first_firm)]
98 assert_equal 105, c[companies(:rails_core)]
99 assert_equal 60, c[companies(:first_client)]
102 def test_should_sum_field_with_conditions
103 assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
106 def test_should_return_zero_if_sum_conditions_return_nothing
107 assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2')
108 assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2')
111 def test_sum_should_return_valid_values_for_decimals
112 NumericData.create(:bank_balance => 19.83)
113 assert_equal 19.83, NumericData.sum(:bank_balance)
116 def test_should_group_by_summed_field_with_conditions
117 c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
120 assert_equal 105, c[6]
121 assert_equal 60, c[2]
124 def test_should_group_by_summed_field_with_conditions_and_having
125 c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
127 :having => 'sum(credit_limit) > 60')
129 assert_equal 105, c[6]
133 def test_should_group_by_fields_with_table_alias
134 c = Account.sum(:credit_limit, :group => 'accounts.firm_id')
135 assert_equal 50, c[1]
136 assert_equal 105, c[6]
137 assert_equal 60, c[2]
140 def test_should_calculate_with_invalid_field
141 assert_equal 6, Account.calculate(:count, '*')
142 assert_equal 6, Account.calculate(:count, :all)
145 def test_should_calculate_grouped_with_invalid_field
146 c = Account.count(:all, :group => 'accounts.firm_id')
152 def test_should_calculate_grouped_association_with_invalid_field
153 c = Account.count(:all, :group => :firm)
154 assert_equal 1, c[companies(:first_firm)]
155 assert_equal 2, c[companies(:rails_core)]
156 assert_equal 1, c[companies(:first_client)]
159 uses_mocha 'group_by_non_numeric_foreign_key_association' do
160 def test_should_group_by_association_with_non_numeric_foreign_key
161 ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all
" => 1, "firm_id
" => "ABC
"}])
164 firm.expects(:id).returns("ABC
")
165 firm.expects(:class).returns(Firm)
166 Company.expects(:find).with(["ABC
"]).returns([firm])
169 column.expects(:name).at_least_once.returns(:firm_id)
170 column.expects(:type_cast).with("ABC
").returns("ABC
")
171 Account.expects(:columns).at_least_once.returns([column])
173 c = Account.count(:all, :group => :firm)
174 assert_equal Firm, c.first.first.class
175 assert_equal 1, c.first.last
179 def test_should_calculate_grouped_association_with_foreign_key_option
180 Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id'
181 c = Account.count(:all, :group => :another_firm)
182 assert_equal 1, c[companies(:first_firm)]
183 assert_equal 2, c[companies(:rails_core)]
184 assert_equal 1, c[companies(:first_client)]
187 def test_should_not_modify_options_when_using_includes
188 options = {:conditions => 'companies.id > 1', :include => :firm}
189 options_copy = options.dup
191 Account.count(:all, options)
192 assert_equal options_copy, options
195 def test_should_calculate_grouped_by_function
196 c = Company.count(:all, :group => "UPPER(#{QUOTED_TYPE})")
197 assert_equal 2, c[nil]
198 assert_equal 1, c['DEPENDENTFIRM']
199 assert_equal 3, c['CLIENT']
200 assert_equal 2, c['FIRM']
203 def test_should_calculate_grouped_by_function_with_table_alias
204 c = Company.count(:all, :group => "UPPER(companies
.#{QUOTED_TYPE})")
205 assert_equal 2, c[nil]
206 assert_equal 1, c['DEPENDENTFIRM']
207 assert_equal 3, c['CLIENT']
208 assert_equal 2, c['FIRM']
211 def test_should_not_overshadow_enumerable_sum
212 assert_equal 6, [1, 2, 3].sum(&:abs)
215 def test_should_sum_scoped_field
216 assert_equal 15, companies(:rails_core).companies.sum(:id)
219 def test_should_sum_scoped_field_with_conditions
220 assert_equal 8, companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
223 def test_should_group_by_scoped_field
224 c = companies(:rails_core).companies.sum(:id, :group => :name)
225 assert_equal 7, c['Leetsoft']
226 assert_equal 8, c['Jadedpixel']
229 def test_should_group_by_summed_field_with_conditions_and_having
230 c = companies(:rails_core).companies.sum(:id, :group => :name,
231 :having => 'sum(id) > 7')
232 assert_nil c['Leetsoft']
233 assert_equal 8, c['Jadedpixel']
236 def test_should_reject_invalid_options
237 assert_nothing_raised do
238 [:count, :sum].each do |func|
239 # empty options are valid
240 Company.send(:validate_calculation_options, func)
241 # these options are valid for all calculations
242 [:select, :conditions, :joins, :order, :group, :having, :distinct].each do |opt|
243 Company.send(:validate_calculation_options, func, opt => true)
247 # :include is only valid on :count
248 Company.send(:validate_calculation_options, :count, :include => true)
251 assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
252 assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
255 def test_should_count_selected_field_with_include
256 assert_equal 6, Account.count(:distinct => true, :include => :firm)
257 assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
260 def test_should_count_manual_select_with_include
261 assert_equal 6, Account.count(:select => "DISTINCT accounts
.id
", :include => :firm)
264 def test_count_with_column_parameter
265 assert_equal 5, Account.count(:firm_id)
268 def test_count_with_column_and_options_parameter
269 assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit
= 50")
272 def test_count_with_no_parameters_isnt_deprecated
273 assert_not_deprecated { Account.count }
276 def test_count_with_too_many_parameters_raises
277 assert_raise(ArgumentError) { Account.count(1, 2, 3) }
280 def test_should_sum_expression
281 assert_equal '636', Account.sum("2 * credit_limit
")
284 def test_count_with_from_option
285 assert_equal Company.count(:all), Company.count(:all, :from => 'companies')
286 assert_equal Account.count(:all, :conditions => "credit_limit
= 50"),
287 Account.count(:all, :from => 'accounts', :conditions => "credit_limit
= 50")
288 assert_equal Company.count(:type, :conditions => {:type => "Firm
"}),
289 Company.count(:type, :conditions => {:type => "Firm
"}, :from => 'companies')
292 def test_sum_with_from_option
293 assert_equal Account.sum(:credit_limit), Account.sum(:credit_limit, :from => 'accounts')
294 assert_equal Account.sum(:credit_limit, :conditions => "credit_limit
> 50"),
295 Account.sum(:credit_limit, :from => 'accounts', :conditions => "credit_limit
> 50")
298 def test_average_with_from_option
299 assert_equal Account.average(:credit_limit), Account.average(:credit_limit, :from => 'accounts')
300 assert_equal Account.average(:credit_limit, :conditions => "credit_limit
> 50"),
301 Account.average(:credit_limit, :from => 'accounts', :conditions => "credit_limit
> 50")
304 def test_minimum_with_from_option
305 assert_equal Account.minimum(:credit_limit), Account.minimum(:credit_limit, :from => 'accounts')
306 assert_equal Account.minimum(:credit_limit, :conditions => "credit_limit
> 50"),
307 Account.minimum(:credit_limit, :from => 'accounts', :conditions => "credit_limit
> 50")
310 def test_maximum_with_from_option
311 assert_equal Account.maximum(:credit_limit), Account.maximum(:credit_limit, :from => 'accounts')
312 assert_equal Account.maximum(:credit_limit, :conditions => "credit_limit
> 50"),
313 Account.maximum(:credit_limit, :from => 'accounts', :conditions => "credit_limit
> 50")
316 def test_from_option_with_specified_index
317 if Edge.connection.adapter_name == 'MySQL'
318 assert_equal Edge.count(:all), Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)')
319 assert_equal Edge.count(:all, :conditions => 'sink_id < 5'),
320 Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)', :conditions => 'sink_id < 5')
324 def test_from_option_with_table_different_than_class
325 assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')