Upgraded Rails and RSpec
[monkeycharger.git] / vendor / rails / activerecord / test / calculations_test.rb
blob298a1d1878a41da9c296edd9d2f207d273d62b74
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'
9 end
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)
16   end
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
22   end
24   def test_should_return_nil_as_average
25     assert_nil NumericData.average(:bank_balance)
26   end
28   def test_should_get_maximum_of_field
29     assert_equal 60, Account.maximum(:credit_limit)
30   end
32   def test_should_get_maximum_of_field_with_include
33     assert_equal 50, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'")
34   end
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)
39     end
40   end
42   def test_should_get_minimum_of_field
43     assert_equal 50, Account.minimum(:credit_limit)
44   end
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) }
49   end
51   def test_should_group_by_summed_field
52     c = Account.sum(:credit_limit, :group => :firm_id)
53     assert_equal 50,   c[1]
54     assert_equal 105,  c[6]
55     assert_equal 60,   c[2]
56   end
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
61   end
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
67   end
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
73   end
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
79   end
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')
84     assert_nil        c[1]
85     assert_equal 105, c[6]
86     assert_equal 60,  c[2]
87   end
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)]
94   end
95   
96   def test_should_sum_field_with_conditions
97     assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
98   end
100   def test_should_group_by_summed_field_with_conditions
101     c = Account.sum(:credit_limit, :conditions => 'firm_id > 1', 
102                                    :group => :firm_id)
103     assert_nil        c[1]
104     assert_equal 105, c[6]
105     assert_equal 60,  c[2]
106   end
107   
108   def test_should_group_by_summed_field_with_conditions_and_having
109     c = Account.sum(:credit_limit, :conditions => 'firm_id > 1', 
110                                    :group => :firm_id, 
111                                    :having => 'sum(credit_limit) > 60')
112     assert_nil        c[1]
113     assert_equal 105, c[6]
114     assert_nil        c[2]
115   end
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]
122   end
123   
124   def test_should_calculate_with_invalid_field
125     assert_equal 6, Account.calculate(:count, '*')
126     assert_equal 6, Account.calculate(:count, :all)
127   end
128   
129   def test_should_calculate_grouped_with_invalid_field
130     c = Account.count(:all, :group => 'accounts.firm_id')
131     assert_equal 1, c[1]
132     assert_equal 2, c[6]
133     assert_equal 1, c[2]
134   end
135   
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)]
141   end
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"}])
147       firm = mock()
148       firm.expects(:id).returns("ABC")
149       firm.expects(:class).returns(Firm)
150       Company.expects(:find).with(["ABC"]).returns([firm])
152       column = mock()
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
160     end
161   end
162   
163   def test_should_not_modify_options_when_using_includes
164     options = {:conditions => 'companies.id > 1', :include => :firm}
165     options_copy = options.dup
166     
167     Account.count(:all, options)
168     assert_equal options_copy, options
169   end
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']
177   end
178   
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']
185   end
186   
187   def test_should_not_overshadow_enumerable_sum
188     assert_equal 6, [1, 2, 3].sum(&:abs)
189   end
191   def test_should_sum_scoped_field
192     assert_equal 15, companies(:rails_core).companies.sum(:id)
193   end
195   def test_should_sum_scoped_field_with_conditions
196     assert_equal 8,  companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
197   end
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']
203   end
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']
210   end
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)
220         end
221       end
222       
223       # :include is only valid on :count
224       Company.send(:validate_calculation_options, :count, :include => true)
225     end
226     
227     assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum,   :foo => :bar) }
228     assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) }
229   end
230   
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)
234   end
235   
236   def test_count_with_column_parameter
237     assert_equal 5, Account.count(:firm_id)
238   end
239   
240   def test_count_with_column_and_options_parameter
241     assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50")
242   end
243   
244   def test_count_with_no_parameters_isnt_deprecated
245     assert_not_deprecated { Account.count }
246   end
248   def test_count_with_too_many_parameters_raises
249     assert_raise(ArgumentError) { Account.count(1, 2, 3) }
250   end