Merged updates from trunk into stable branch
[feedcatcher.git] / vendor / rails / activerecord / test / cases / calculations_test.rb
1 require "cases/helper"
2 require 'models/company'
3 require 'models/topic'
4 require 'models/edge'
5
6 Company.has_many :accounts
7
8 class NumericData < ActiveRecord::Base
9 self.table_name = 'numeric_data'
10 end
11
12 class CalculationsTest < ActiveRecord::TestCase
13 fixtures :companies, :accounts, :topics
14
15 def test_should_sum_field
16 assert_equal 318, Account.sum(:credit_limit)
17 end
18
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
23 end
24
25 def test_should_return_nil_as_average
26 assert_nil NumericData.average(:bank_balance)
27 end
28
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')
32 end
33
34 def test_should_get_maximum_of_field
35 assert_equal 60, Account.maximum(:credit_limit)
36 end
37
38 def test_should_get_maximum_of_field_with_include
39 assert_equal 50, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'")
40 end
41
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)
45 end
46 end
47
48 def test_should_get_minimum_of_field
49 assert_equal 50, Account.minimum(:credit_limit)
50 end
51
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) }
55 end
56
57 def test_should_group_by_summed_field
58 c = Account.sum(:credit_limit, :group => :firm_id)
59 assert_equal 50, c[1]
60 assert_equal 105, c[6]
61 assert_equal 60, c[2]
62 end
63
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
67 end
68
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
73 end
74
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
79 end
80
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
85 end
86
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')
90 assert_nil c[1]
91 assert_equal 105, c[6]
92 assert_equal 60, c[2]
93 end
94
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])
98 assert_nil c[1]
99 assert_equal 105, c[6]
100 assert_equal 60, c[2]
101 end
102
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)]
108 end
109
110 def test_should_sum_field_with_conditions
111 assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
112 end
113
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')
117 end
118
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)
122 end
123
124 def test_should_group_by_summed_field_with_conditions
125 c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
126 :group => :firm_id)
127 assert_nil c[1]
128 assert_equal 105, c[6]
129 assert_equal 60, c[2]
130 end
131
132 def test_should_group_by_summed_field_with_conditions_and_having
133 c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
134 :group => :firm_id,
135 :having => 'sum(credit_limit) > 60')
136 assert_nil c[1]
137 assert_equal 105, c[6]
138 assert_nil c[2]
139 end
140
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]
146 end
147
148 def test_should_calculate_with_invalid_field
149 assert_equal 6, Account.calculate(:count, '*')
150 assert_equal 6, Account.calculate(:count, :all)
151 end
152
153 def test_should_calculate_grouped_with_invalid_field
154 c = Account.count(:all, :group => 'accounts.firm_id')
155 assert_equal 1, c[1]
156 assert_equal 2, c[6]
157 assert_equal 1, c[2]
158 end
159
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)]
165 end
166
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"}])
169
170 firm = mock()
171 firm.expects(:id).returns("ABC")
172 firm.expects(:class).returns(Firm)
173 Company.expects(:find).with(["ABC"]).returns([firm])
174
175 column = mock()
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])
179
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]
184 end
185
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)]
192 end
193
194 def test_should_not_modify_options_when_using_includes
195 options = {:conditions => 'companies.id > 1', :include => :firm}
196 options_copy = options.dup
197
198 Account.count(:all, options)
199 assert_equal options_copy, options
200 end
201
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']
208 end
209
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']
216 end
217
218 def test_should_not_overshadow_enumerable_sum
219 assert_equal 6, [1, 2, 3].sum(&:abs)
220 end
221
222 def test_should_sum_scoped_field
223 assert_equal 15, companies(:rails_core).companies.sum(:id)
224 end
225
226 def test_should_sum_scoped_field_with_conditions
227 assert_equal 8, companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
228 end
229
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']
234 end
235
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']
241 end
242
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)
251 end
252 end
253
254 # :include is only valid on :count
255 Company.send(:validate_calculation_options, :count, :include => true)
256 end
257
258 assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
259 assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
260 end
261
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)
265 end
266
267 def test_should_count_scoped_select
268 Account.update_all("credit_limit = NULL")
269 assert_equal 0, Account.scoped(:select => "credit_limit").count
270 end
271
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)
276
277 assert_equal 1, Account.scoped(:select => "credit_limit").count(:conditions => ['credit_limit >= 50'])
278 end
279
280 def test_should_count_manual_select_with_include
281 assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm)
282 end
283
284 def test_count_with_column_parameter
285 assert_equal 5, Account.count(:firm_id)
286 end
287
288 def test_count_with_column_and_options_parameter
289 assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50")
290 end
291
292 def test_count_with_no_parameters_isnt_deprecated
293 assert_not_deprecated { Account.count }
294 end
295
296 def test_count_with_too_many_parameters_raises
297 assert_raise(ArgumentError) { Account.count(1, 2, 3) }
298 end
299
300 def test_should_sum_expression
301 assert_equal '636', Account.sum("2 * credit_limit")
302 end
303
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')
310 end
311
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")
316 end
317
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")
322 end
323
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")
328 end
329
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")
334 end
335
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')
341 end
342 end
343
344 def test_from_option_with_table_different_than_class
345 assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')
346 end
347
348 end