aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/calculations_test.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2006-02-25 23:06:04 +0000
committerDavid Heinemeier Hansson <david@loudthinking.com>2006-02-25 23:06:04 +0000
commit99307b959b1c16ab1dd0400a7398019fe9d5494b (patch)
tree94a082a3c7f91c8a98596a8bcf29f2c99d4baa70 /activerecord/test/calculations_test.rb
parent1aea4704dca570e3ef7abe69349ae2f161c7244b (diff)
downloadrails-99307b959b1c16ab1dd0400a7398019fe9d5494b.tar.gz
rails-99307b959b1c16ab1dd0400a7398019fe9d5494b.tar.bz2
rails-99307b959b1c16ab1dd0400a7398019fe9d5494b.zip
Added calculations: Base.count, Base.average, Base.sum, Base.minimum, Base.maxmium, and the generic Base.calculate. All can be used with :group and :having. Calculations and statitics need no longer require custom SQL. #3958 [Rick Olson]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3646 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/test/calculations_test.rb')
-rw-r--r--activerecord/test/calculations_test.rb136
1 files changed, 136 insertions, 0 deletions
diff --git a/activerecord/test/calculations_test.rb b/activerecord/test/calculations_test.rb
new file mode 100644
index 0000000000..f4bc4f3c5b
--- /dev/null
+++ b/activerecord/test/calculations_test.rb
@@ -0,0 +1,136 @@
+require 'abstract_unit'
+require 'fixtures/company'
+require 'fixtures/topic'
+
+Company.has_many :accounts
+
+class CalculationsTest < Test::Unit::TestCase
+ fixtures :companies, :accounts, :topics
+
+ def test_should_sum_field
+ assert_equal 265, Account.sum(:credit_limit)
+ end
+
+ def test_should_average_field
+ value = Account.average(:credit_limit)
+ assert_equal 53, value
+ assert_kind_of Float, value
+ end
+
+ def test_should_get_maximum_of_field
+ assert_equal 60, Account.maximum(:credit_limit)
+ end
+
+ def test_should_get_minimum_of_field
+ assert_equal 50, Account.minimum(:credit_limit)
+ end
+
+ def test_should_group_by_field
+ c = Account.sum(:credit_limit, :group => :firm_id)
+ %w( 1 6 2 ).each { |firm_id| assert c.keys.include?(firm_id) }
+ end
+
+ def test_should_group_by_summed_field
+ c = Account.sum(:credit_limit, :group => :firm_id)
+ assert_equal 50, c['1']
+ assert_equal 105, c['6']
+ assert_equal 60, c['2']
+ end
+
+ def test_should_group_by_summed_field_having_condition
+ c = Account.sum(:credit_limit, :group => :firm_id,
+ :having => 'sum(credit_limit) > 50')
+ assert_nil c['1']
+ assert_equal 105, c['6']
+ assert_equal 60, c['2']
+ end
+
+ def test_should_group_by_summed_association
+ c = Account.sum(:credit_limit, :group => :firm)
+ assert_equal 50, c[companies(:first_firm)]
+ assert_equal 105, c[companies(:rails_core)]
+ assert_equal 60, c[companies(:first_client)]
+ end
+
+ def test_should_sum_field_with_conditions
+ assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
+ end
+
+ def test_should_group_by_summed_field_with_conditions
+ c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
+ :group => :firm_id)
+ assert_nil c['1']
+ assert_equal 105, c['6']
+ assert_equal 60, c['2']
+ end
+
+ def test_should_group_by_summed_field_with_conditions_and_having
+ c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
+ :group => :firm_id,
+ :having => 'sum(credit_limit) > 60')
+ assert_nil c['1']
+ assert_equal 105, c['6']
+ assert_nil c['2']
+ end
+
+ def test_should_group_by_fields_with_table_alias
+ c = Account.sum(:credit_limit, :group => 'accounts.firm_id')
+ assert_equal 50, c['1']
+ assert_equal 105, c['6']
+ assert_equal 60, c['2']
+ end
+
+ def test_should_calculate_with_invalid_field
+ assert_equal 5, Account.calculate(:count, '*')
+ assert_equal 5, Account.calculate(:count, :all)
+ end
+
+ def test_should_calculate_grouped_with_invalid_field
+ c = Account.count(:all, :group => 'accounts.firm_id')
+ assert_equal 1, c['1']
+ assert_equal 2, c['6']
+ assert_equal 1, c['2']
+ end
+
+ def test_should_calculate_grouped_association_with_invalid_field
+ c = Account.count(:all, :group => :firm)
+ assert_equal 1, c[companies(:first_firm)]
+ assert_equal 2, c[companies(:rails_core)]
+ assert_equal 1, c[companies(:first_client)]
+ end
+
+ def test_should_calculate_grouped_by_function
+ c = Company.count(:all, :group => 'UPPER(type)')
+ assert_equal 2, c[nil]
+ assert_equal 1, c['DEPENDENTFIRM']
+ assert_equal 3, c['CLIENT']
+ assert_equal 2, c['FIRM']
+ end
+
+ def test_should_calculate_grouped_by_function_with_table_alias
+ c = Topic.count(:all, :group => 'DATE(topics.written_on)')
+ assert_equal 1, c["2003-07-15"]
+ assert_equal 1, c["2003-07-16"]
+ end
+
+ def test_should_sum_scoped_field
+ assert_equal 15, companies(:rails_core).companies.sum(:id)
+ end
+
+ def test_should_sum_scoped_field_with_conditions
+ assert_equal 8, companies(:rails_core).companies.sum(:id, :conditions => 'id > 7')
+ end
+
+ def test_should_group_by_scoped_field
+ c = companies(:rails_core).companies.sum(:id, :group => :name)
+ assert_equal 7, c['Leetsoft']
+ assert_equal 8, c['Jadedpixel']
+ end
+
+ def test_should_group_by_summed_field_with_conditions_and_having
+ c = companies(:rails_core).companies.sum(:id, :group => :name,
+ :having => 'sum(id) > 7')
+ assert_nil c['Leetsoft']
+ assert_equal 8, c['Jadedpixel']
+ end
+end