2008-01-21 17:20:51 +00:00
|
|
|
require "cases/helper"
|
2008-01-18 07:31:37 +00:00
|
|
|
require 'models/company'
|
|
|
|
require 'models/topic'
|
2008-06-11 23:26:35 +00:00
|
|
|
require 'models/edge'
|
2009-08-09 00:10:01 +00:00
|
|
|
require 'models/club'
|
|
|
|
require 'models/organization'
|
2006-02-25 23:06:04 +00:00
|
|
|
|
|
|
|
Company.has_many :accounts
|
|
|
|
|
2007-05-30 06:57:04 +00:00
|
|
|
class NumericData < ActiveRecord::Base
|
|
|
|
self.table_name = 'numeric_data'
|
|
|
|
end
|
|
|
|
|
2008-01-21 17:20:51 +00:00
|
|
|
class CalculationsTest < ActiveRecord::TestCase
|
2006-02-25 23:06:04 +00:00
|
|
|
fixtures :companies, :accounts, :topics
|
|
|
|
|
|
|
|
def test_should_sum_field
|
2006-08-29 17:06:27 +00:00
|
|
|
assert_equal 318, Account.sum(:credit_limit)
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_average_field
|
|
|
|
value = Account.average(:credit_limit)
|
2010-05-12 04:47:58 +00:00
|
|
|
assert_equal 53.0, value
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
|
|
|
|
2007-05-30 06:57:04 +00:00
|
|
|
def test_should_return_nil_as_average
|
|
|
|
assert_nil NumericData.average(:bank_balance)
|
|
|
|
end
|
2009-04-29 22:39:53 +00:00
|
|
|
|
2008-11-13 16:45:57 +00:00
|
|
|
def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal
|
2009-12-28 22:58:43 +00:00
|
|
|
assert_equal 0, NumericData.scoped.send(:type_cast_calculated_value, 0, nil, 'avg')
|
|
|
|
assert_equal 53.0, NumericData.scoped.send(:type_cast_calculated_value, 53, nil, 'avg')
|
2008-11-13 16:45:57 +00:00
|
|
|
end
|
2007-05-30 06:57:04 +00:00
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
def test_should_get_maximum_of_field
|
|
|
|
assert_equal 60, Account.maximum(:credit_limit)
|
|
|
|
end
|
|
|
|
|
2006-04-25 05:25:04 +00:00
|
|
|
def test_should_get_maximum_of_field_with_include
|
2010-03-08 00:53:21 +00:00
|
|
|
assert_equal 55, Account.maximum(:credit_limit, :include => :firm, :conditions => "companies.name != 'Summit'")
|
2006-04-25 05:25:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_get_maximum_of_field_with_scoped_include
|
2009-12-28 22:07:23 +00:00
|
|
|
Account.send :with_scope, :find => { :include => :firm, :conditions => "companies.name != 'Summit'" } do
|
2010-03-08 00:53:21 +00:00
|
|
|
assert_equal 55, Account.maximum(:credit_limit)
|
2006-04-25 05:25:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
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)
|
2006-03-01 16:25:14 +00:00
|
|
|
[1,6,2].each { |firm_id| assert c.keys.include?(firm_id) }
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_group_by_summed_field
|
|
|
|
c = Account.sum(:credit_limit, :group => :firm_id)
|
2006-03-01 16:25:14 +00:00
|
|
|
assert_equal 50, c[1]
|
|
|
|
assert_equal 105, c[6]
|
|
|
|
assert_equal 60, c[2]
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
|
|
|
|
2006-03-14 14:59:14 +00:00
|
|
|
def test_should_order_by_grouped_field
|
|
|
|
c = Account.sum(:credit_limit, :group => :firm_id, :order => "firm_id")
|
2006-08-29 17:06:27 +00:00
|
|
|
assert_equal [1, 2, 6, 9], c.keys.compact
|
2006-03-14 14:59:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_order_by_calculation
|
|
|
|
c = Account.sum(:credit_limit, :group => :firm_id, :order => "sum_credit_limit desc, firm_id")
|
2006-08-29 17:06:27 +00:00
|
|
|
assert_equal [105, 60, 53, 50, 50], c.keys.collect { |k| c[k] }
|
|
|
|
assert_equal [6, 2, 9, 1], c.keys.compact
|
2006-03-14 14:59:14 +00:00
|
|
|
end
|
|
|
|
|
2006-04-06 15:23:56 +00:00
|
|
|
def test_should_limit_calculation
|
|
|
|
c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL",
|
|
|
|
:group => :firm_id, :order => "firm_id", :limit => 2)
|
|
|
|
assert_equal [1, 2], c.keys.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_limit_calculation_with_offset
|
|
|
|
c = Account.sum(:credit_limit, :conditions => "firm_id IS NOT NULL",
|
|
|
|
:group => :firm_id, :order => "firm_id", :limit => 2, :offset => 1)
|
|
|
|
assert_equal [2, 6], c.keys.compact
|
|
|
|
end
|
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
def test_should_group_by_summed_field_having_condition
|
2008-01-18 07:30:42 +00:00
|
|
|
c = Account.sum(:credit_limit, :group => :firm_id,
|
2006-02-25 23:06:04 +00:00
|
|
|
:having => 'sum(credit_limit) > 50')
|
2006-03-01 16:25:14 +00:00
|
|
|
assert_nil c[1]
|
|
|
|
assert_equal 105, c[6]
|
|
|
|
assert_equal 60, c[2]
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
|
|
|
|
2009-03-06 22:29:36 +00:00
|
|
|
def test_should_group_by_summed_field_having_sanitized_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
|
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
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
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
def test_should_sum_field_with_conditions
|
|
|
|
assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6')
|
|
|
|
end
|
|
|
|
|
2008-04-08 05:20:33 +00:00
|
|
|
def test_should_return_zero_if_sum_conditions_return_nothing
|
|
|
|
assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2')
|
2008-06-02 03:00:15 +00:00
|
|
|
assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2')
|
2008-04-08 05:20:33 +00:00
|
|
|
end
|
|
|
|
|
2008-06-02 19:40:25 +00:00
|
|
|
def test_sum_should_return_valid_values_for_decimals
|
|
|
|
NumericData.create(:bank_balance => 19.83)
|
|
|
|
assert_equal 19.83, NumericData.sum(:bank_balance)
|
|
|
|
end
|
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
def test_should_group_by_summed_field_with_conditions
|
2008-01-18 07:30:42 +00:00
|
|
|
c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
|
2006-02-25 23:06:04 +00:00
|
|
|
:group => :firm_id)
|
2006-03-01 16:25:14 +00:00
|
|
|
assert_nil c[1]
|
|
|
|
assert_equal 105, c[6]
|
|
|
|
assert_equal 60, c[2]
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
def test_should_group_by_summed_field_with_conditions_and_having
|
2008-01-18 07:30:42 +00:00
|
|
|
c = Account.sum(:credit_limit, :conditions => 'firm_id > 1',
|
|
|
|
:group => :firm_id,
|
2006-02-25 23:06:04 +00:00
|
|
|
:having => 'sum(credit_limit) > 60')
|
2006-03-01 16:25:14 +00:00
|
|
|
assert_nil c[1]
|
|
|
|
assert_equal 105, c[6]
|
|
|
|
assert_nil c[2]
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_group_by_fields_with_table_alias
|
|
|
|
c = Account.sum(:credit_limit, :group => 'accounts.firm_id')
|
2006-03-01 16:25:14 +00:00
|
|
|
assert_equal 50, c[1]
|
|
|
|
assert_equal 105, c[6]
|
|
|
|
assert_equal 60, c[2]
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
def test_should_calculate_with_invalid_field
|
2006-08-29 17:06:27 +00:00
|
|
|
assert_equal 6, Account.calculate(:count, '*')
|
|
|
|
assert_equal 6, Account.calculate(:count, :all)
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
def test_should_calculate_grouped_with_invalid_field
|
|
|
|
c = Account.count(:all, :group => 'accounts.firm_id')
|
2006-03-01 16:25:14 +00:00
|
|
|
assert_equal 1, c[1]
|
|
|
|
assert_equal 2, c[6]
|
|
|
|
assert_equal 1, c[2]
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
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
|
2007-06-01 03:26:51 +00:00
|
|
|
|
2009-02-04 02:25:37 +00:00
|
|
|
def test_should_group_by_association_with_non_numeric_foreign_key
|
|
|
|
ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}])
|
|
|
|
|
|
|
|
firm = mock()
|
|
|
|
firm.expects(:id).returns("ABC")
|
|
|
|
firm.expects(:class).returns(Firm)
|
|
|
|
Company.expects(:find).with(["ABC"]).returns([firm])
|
|
|
|
|
|
|
|
column = mock()
|
|
|
|
column.expects(:name).at_least_once.returns(:firm_id)
|
|
|
|
column.expects(:type_cast).with("ABC").returns("ABC")
|
|
|
|
Account.expects(:columns).at_least_once.returns([column])
|
|
|
|
|
|
|
|
c = Account.count(:all, :group => :firm)
|
|
|
|
first_key = c.keys.first
|
|
|
|
assert_equal Firm, first_key.class
|
|
|
|
assert_equal 1, c[first_key]
|
2007-06-01 03:26:51 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2008-02-02 04:28:42 +00:00
|
|
|
def test_should_calculate_grouped_association_with_foreign_key_option
|
|
|
|
Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id'
|
|
|
|
c = Account.count(:all, :group => :another_firm)
|
|
|
|
assert_equal 1, c[companies(:first_firm)]
|
|
|
|
assert_equal 2, c[companies(:rails_core)]
|
|
|
|
assert_equal 1, c[companies(:first_client)]
|
|
|
|
end
|
|
|
|
|
2007-01-14 16:19:58 +00:00
|
|
|
def test_should_not_modify_options_when_using_includes
|
|
|
|
options = {:conditions => 'companies.id > 1', :include => :firm}
|
|
|
|
options_copy = options.dup
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2007-01-14 16:19:58 +00:00
|
|
|
Account.count(:all, options)
|
|
|
|
assert_equal options_copy, options
|
|
|
|
end
|
2006-02-25 23:06:04 +00:00
|
|
|
|
|
|
|
def test_should_calculate_grouped_by_function
|
2006-06-03 21:47:29 +00:00
|
|
|
c = Company.count(:all, :group => "UPPER(#{QUOTED_TYPE})")
|
2006-02-25 23:06:04 +00:00
|
|
|
assert_equal 2, c[nil]
|
|
|
|
assert_equal 1, c['DEPENDENTFIRM']
|
2009-06-30 22:55:09 +00:00
|
|
|
assert_equal 4, c['CLIENT']
|
2006-02-25 23:06:04 +00:00
|
|
|
assert_equal 2, c['FIRM']
|
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
def test_should_calculate_grouped_by_function_with_table_alias
|
2006-06-03 21:47:29 +00:00
|
|
|
c = Company.count(:all, :group => "UPPER(companies.#{QUOTED_TYPE})")
|
2006-02-25 23:41:51 +00:00
|
|
|
assert_equal 2, c[nil]
|
|
|
|
assert_equal 1, c['DEPENDENTFIRM']
|
2009-06-30 22:55:09 +00:00
|
|
|
assert_equal 4, c['CLIENT']
|
2006-02-25 23:41:51 +00:00
|
|
|
assert_equal 2, c['FIRM']
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-06-25 17:49:24 +00:00
|
|
|
def test_should_not_overshadow_enumerable_sum
|
|
|
|
assert_equal 6, [1, 2, 3].sum(&:abs)
|
|
|
|
end
|
2006-02-25 23:06:04 +00:00
|
|
|
|
|
|
|
def test_should_sum_scoped_field
|
|
|
|
assert_equal 15, companies(:rails_core).companies.sum(:id)
|
|
|
|
end
|
|
|
|
|
2009-08-09 00:10:01 +00:00
|
|
|
def test_should_sum_scoped_field_with_from
|
|
|
|
assert_equal Club.count, Organization.clubs.count
|
|
|
|
end
|
|
|
|
|
2006-02-25 23:06:04 +00:00
|
|
|
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
|
|
|
|
|
2009-04-29 22:35:21 +00:00
|
|
|
def test_should_group_by_summed_field_through_association_and_having
|
2006-02-25 23:06:04 +00:00
|
|
|
c = companies(:rails_core).companies.sum(:id, :group => :name,
|
|
|
|
:having => 'sum(id) > 7')
|
|
|
|
assert_nil c['Leetsoft']
|
|
|
|
assert_equal 8, c['Jadedpixel']
|
|
|
|
end
|
2006-03-01 16:25:14 +00:00
|
|
|
|
2006-06-03 21:19:36 +00:00
|
|
|
def test_should_count_selected_field_with_include
|
2006-08-29 17:06:27 +00:00
|
|
|
assert_equal 6, Account.count(:distinct => true, :include => :firm)
|
|
|
|
assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit)
|
2006-06-03 21:19:36 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2009-03-07 15:26:56 +00:00
|
|
|
def test_should_count_scoped_select
|
2009-03-07 16:36:40 +00:00
|
|
|
Account.update_all("credit_limit = NULL")
|
|
|
|
assert_equal 0, Account.scoped(:select => "credit_limit").count
|
2009-03-07 15:26:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_count_scoped_select_with_options
|
2009-03-07 16:36:40 +00:00
|
|
|
Account.update_all("credit_limit = NULL")
|
|
|
|
Account.last.update_attribute('credit_limit', 49)
|
|
|
|
Account.first.update_attribute('credit_limit', 51)
|
|
|
|
|
|
|
|
assert_equal 1, Account.scoped(:select => "credit_limit").count(:conditions => ['credit_limit >= 50'])
|
2009-03-07 15:26:56 +00:00
|
|
|
end
|
|
|
|
|
2008-04-04 12:06:22 +00:00
|
|
|
def test_should_count_manual_select_with_include
|
|
|
|
assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm)
|
|
|
|
end
|
|
|
|
|
2007-07-17 20:16:35 +00:00
|
|
|
def test_count_with_column_parameter
|
|
|
|
assert_equal 5, Account.count(:firm_id)
|
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2007-07-17 20:16:35 +00:00
|
|
|
def test_count_with_column_and_options_parameter
|
2010-06-23 16:10:53 +00:00
|
|
|
assert_equal 2, Account.count(:firm_id, :conditions => "credit_limit = 50 AND firm_id IS NOT NULL")
|
2007-07-17 20:16:35 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2010-08-25 08:52:00 +00:00
|
|
|
def test_should_count_field_in_joined_table
|
|
|
|
assert_equal 5, Account.count('companies.id', :joins => :firm)
|
|
|
|
assert_equal 4, Account.count('companies.id', :joins => :firm, :distinct => true)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_count_field_in_joined_table_with_group_by
|
2010-09-30 16:37:17 +00:00
|
|
|
c = Account.count('companies.id', :group => 'accounts.firm_id', :joins => :firm)
|
2010-08-25 08:52:00 +00:00
|
|
|
|
|
|
|
[1,6,2,9].each { |firm_id| assert c.keys.include?(firm_id) }
|
|
|
|
end
|
|
|
|
|
2006-09-26 17:02:45 +00:00
|
|
|
def test_count_with_no_parameters_isnt_deprecated
|
|
|
|
assert_not_deprecated { Account.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_count_with_too_many_parameters_raises
|
|
|
|
assert_raise(ArgumentError) { Account.count(1, 2, 3) }
|
|
|
|
end
|
2008-03-17 04:02:34 +00:00
|
|
|
|
|
|
|
def test_should_sum_expression
|
2009-03-22 22:07:23 +00:00
|
|
|
# Oracle adapter returns floating point value 636.0 after SUM
|
|
|
|
if current_adapter?(:OracleAdapter)
|
|
|
|
assert_equal 636, Account.sum("2 * credit_limit")
|
|
|
|
else
|
2010-05-22 19:38:04 +00:00
|
|
|
assert_equal 636, Account.sum("2 * credit_limit").to_i
|
2009-03-22 22:07:23 +00:00
|
|
|
end
|
2008-03-17 04:02:34 +00:00
|
|
|
end
|
2008-06-11 23:26:35 +00:00
|
|
|
|
|
|
|
def test_count_with_from_option
|
|
|
|
assert_equal Company.count(:all), Company.count(:all, :from => 'companies')
|
|
|
|
assert_equal Account.count(:all, :conditions => "credit_limit = 50"),
|
|
|
|
Account.count(:all, :from => 'accounts', :conditions => "credit_limit = 50")
|
|
|
|
assert_equal Company.count(:type, :conditions => {:type => "Firm"}),
|
|
|
|
Company.count(:type, :conditions => {:type => "Firm"}, :from => 'companies')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_sum_with_from_option
|
|
|
|
assert_equal Account.sum(:credit_limit), Account.sum(:credit_limit, :from => 'accounts')
|
|
|
|
assert_equal Account.sum(:credit_limit, :conditions => "credit_limit > 50"),
|
|
|
|
Account.sum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_average_with_from_option
|
|
|
|
assert_equal Account.average(:credit_limit), Account.average(:credit_limit, :from => 'accounts')
|
|
|
|
assert_equal Account.average(:credit_limit, :conditions => "credit_limit > 50"),
|
|
|
|
Account.average(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_minimum_with_from_option
|
|
|
|
assert_equal Account.minimum(:credit_limit), Account.minimum(:credit_limit, :from => 'accounts')
|
|
|
|
assert_equal Account.minimum(:credit_limit, :conditions => "credit_limit > 50"),
|
|
|
|
Account.minimum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_maximum_with_from_option
|
|
|
|
assert_equal Account.maximum(:credit_limit), Account.maximum(:credit_limit, :from => 'accounts')
|
|
|
|
assert_equal Account.maximum(:credit_limit, :conditions => "credit_limit > 50"),
|
|
|
|
Account.maximum(:credit_limit, :from => 'accounts', :conditions => "credit_limit > 50")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_from_option_with_specified_index
|
2010-08-02 08:37:57 +00:00
|
|
|
if Edge.connection.adapter_name == 'MySQL' or Edge.connection.adapter_name == 'Mysql2'
|
2008-06-11 23:26:35 +00:00
|
|
|
assert_equal Edge.count(:all), Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)')
|
|
|
|
assert_equal Edge.count(:all, :conditions => 'sink_id < 5'),
|
|
|
|
Edge.count(:all, :from => 'edges USE INDEX(unique_edge_index)', :conditions => 'sink_id < 5')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_from_option_with_table_different_than_class
|
|
|
|
assert_equal Account.count(:all), Company.count(:all, :from => 'accounts')
|
|
|
|
end
|
2010-10-01 11:27:02 +00:00
|
|
|
|
|
|
|
def test_distinct_is_honored_when_used_with_count_operation_after_group
|
|
|
|
# Count the number of authors for approved topics
|
|
|
|
approved_topics_count = Topic.group(:approved).count(:author_name)[true]
|
|
|
|
assert_equal approved_topics_count, 3
|
|
|
|
# Count the number of distinct authors for approved Topics
|
|
|
|
distinct_authors_for_approved_count = Topic.group(:approved).count(:author_name, :distinct => true)[true]
|
|
|
|
assert_equal distinct_authors_for_approved_count, 2
|
|
|
|
end
|
2006-02-25 23:06:04 +00:00
|
|
|
end
|