Froze rails gems
[depot.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_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)]
100 end
101
102 def test_should_sum_field_with_conditions
103 assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
104 end
105
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')
109 end
110
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)
114 end
115
116 def test_should_group_by_summed_field_with_conditions
117 c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
118 :group => :firm_id)
119 assert_nil c[1]
120 assert_equal 105, c[6]
121 assert_equal 60, c[2]
122 end
123
124 def test_should_group_by_summed_field_with_conditions_and_having
125 c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
126 :group => :firm_id,
127 :having => 'sum(credit_limit) > 60')
128 assert_nil c[1]
129 assert_equal 105, c[6]
130 assert_nil c[2]
131 end
132
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]
138 end
139
140 def test_should_calculate_with_invalid_field
141 assert_equal 6, Account.calculate(:count, '*')
142 assert_equal 6, Account.calculate(:count, :all)
143 end
144
145 def test_should_calculate_grouped_with_invalid_field
146 c = Account.count(:all, :group => 'accounts.firm_id')
147 assert_equal 1, c[1]
148 assert_equal 2, c[6]
149 assert_equal 1, c[2]
150 end
151
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)]
157 end
158
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"}])
162
163 firm = mock()
164 firm.expects(:id).returns("ABC")
165 firm.expects(:class).returns(Firm)
166 Company.expects(:find).with(["ABC"]).returns([firm])
167
168 column = mock()
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])
172
173 c = Account.count(:all, :group => :firm)
174 assert_equal Firm, c.first.first.class
175 assert_equal 1, c.first.last
176 end
177 end
178
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)]
185 end
186
187 def test_should_not_modify_options_when_using_includes
188 options = {:conditions => 'companies.id > 1', :include => :firm}
189 options_copy = options.dup
190
191 Account.count(:all, options)
192 assert_equal options_copy, options
193 end
194
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']
201 end
202
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']
209 end
210
211 def test_should_not_overshadow_enumerable_sum
212 assert_equal 6, [1, 2, 3].sum(&:abs)
213 end
214
215 def test_should_sum_scoped_field
216 assert_equal 15, companies(:rails_core).companies.sum(:id)
217 end
218
219 def test_should_sum_scoped_field_with_conditions
220 assert_equal 8, companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
221 end
222
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']
227 end
228
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']
234 end
235
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)
244 end
245 end
246
247 # :include is only valid on :count
248 Company.send(:validate_calculation_options, :count, :include => true)
249 end
250
251 assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) }
252 assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
253 end
254
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)
258 end
259
260 def test_should_count_manual_select_with_include
261 assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm)
262 end
263
264 def test_count_with_column_parameter
265 assert_equal 5, Account.count(:firm_id)
266 end
267
268 def test_count_with_column_and_options_parameter
269 assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50")
270 end
271
272 def test_count_with_no_parameters_isnt_deprecated
273 assert_not_deprecated { Account.count }
274 end
275
276 def test_count_with_too_many_parameters_raises
277 assert_raise(ArgumentError) { Account.count(1, 2, 3) }
278 end
279
280 def test_should_sum_expression
281 assert_equal '636', Account.sum("2 * credit_limit")
282 end
283
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')
290 end
291
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")
296 end
297
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")
302 end
303
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")
308 end
309
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")
314 end
315
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')
321 end
322 end
323
324 def test_from_option_with_table_different_than_class
325 assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')
326 end
327
328 end