2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2012-09-05 18:00:07 -04:00
|
|
|
require 'models/club'
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/company'
|
2011-11-30 04:03:00 -05:00
|
|
|
require "models/contract"
|
2008-06-11 19:26:35 -04:00
|
|
|
require 'models/edge'
|
2009-08-08 20:10:01 -04:00
|
|
|
require 'models/organization'
|
2012-09-05 18:00:07 -04:00
|
|
|
require 'models/possession'
|
|
|
|
require 'models/topic'
|
2013-05-10 13:24:56 -04:00
|
|
|
require 'models/reply'
|
2012-10-17 18:51:17 -04:00
|
|
|
require 'models/minivan'
|
|
|
|
require 'models/speedometer'
|
2012-12-28 22:11:02 -05:00
|
|
|
require 'models/ship_part'
|
2014-12-09 09:20:22 -05:00
|
|
|
require 'models/treasure'
|
2006-02-25 18:06:04 -05:00
|
|
|
|
2007-05-30 02:57:04 -04:00
|
|
|
class NumericData < ActiveRecord::Base
|
|
|
|
self.table_name = 'numeric_data'
|
2014-05-24 08:30:26 -04:00
|
|
|
|
2014-06-07 09:09:45 -04:00
|
|
|
attribute :world_population, Type::Integer.new
|
|
|
|
attribute :my_house_population, Type::Integer.new
|
|
|
|
attribute :atoms_in_universe, Type::Integer.new
|
2007-05-30 02:57:04 -04:00
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class CalculationsTest < ActiveRecord::TestCase
|
2014-08-28 02:30:29 -04:00
|
|
|
fixtures :companies, :accounts, :topics, :speedometers, :minivans
|
2006-02-25 18:06:04 -05:00
|
|
|
|
|
|
|
def test_should_sum_field
|
2006-08-29 13:06:27 -04:00
|
|
|
assert_equal 318, Account.sum(:credit_limit)
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_average_field
|
|
|
|
value = Account.average(:credit_limit)
|
2010-05-12 00:47:58 -04:00
|
|
|
assert_equal 53.0, value
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
|
|
|
|
2013-05-01 14:33:11 -04:00
|
|
|
def test_should_resolve_aliased_attributes
|
|
|
|
assert_equal 318, Account.sum(:available_credit)
|
|
|
|
end
|
|
|
|
|
2011-01-10 11:55:32 -05:00
|
|
|
def test_should_return_decimal_average_of_integer_field
|
|
|
|
value = Account.average(:id)
|
|
|
|
assert_equal 3.5, value
|
|
|
|
end
|
|
|
|
|
2011-02-03 15:07:03 -05:00
|
|
|
def test_should_return_integer_average_if_db_returns_such
|
2012-12-28 22:11:02 -05:00
|
|
|
ShipPart.delete_all
|
|
|
|
ShipPart.create!(:id => 3, :name => 'foo')
|
|
|
|
value = ShipPart.average(:id)
|
2011-02-03 15:07:03 -05:00
|
|
|
assert_equal 3, value
|
|
|
|
end
|
|
|
|
|
2007-05-30 02:57:04 -04:00
|
|
|
def test_should_return_nil_as_average
|
|
|
|
assert_nil NumericData.average(:bank_balance)
|
|
|
|
end
|
2009-04-29 18:39:53 -04:00
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_get_maximum_of_field
|
|
|
|
assert_equal 60, Account.maximum(:credit_limit)
|
|
|
|
end
|
|
|
|
|
2006-04-25 01:25:04 -04:00
|
|
|
def test_should_get_maximum_of_field_with_include
|
2012-04-13 13:26:04 -04:00
|
|
|
assert_equal 55, Account.where("companies.name != 'Summit'").references(:companies).includes(:firm).maximum(:credit_limit)
|
2006-04-25 01:25:04 -04:00
|
|
|
end
|
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_get_minimum_of_field
|
|
|
|
assert_equal 50, Account.minimum(:credit_limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_group_by_field
|
2012-04-26 12:29:22 -04:00
|
|
|
c = Account.group(:firm_id).sum(:credit_limit)
|
2012-06-25 16:36:24 -04:00
|
|
|
[1,6,2].each do |firm_id|
|
|
|
|
assert c.keys.include?(firm_id), "Group #{c.inspect} does not contain firm_id #{firm_id}"
|
|
|
|
end
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
2011-03-03 23:26:45 -05:00
|
|
|
|
2012-06-24 08:39:54 -04:00
|
|
|
def test_should_group_by_arel_attribute
|
2012-06-25 16:36:24 -04:00
|
|
|
c = Account.group(Account.arel_table[:firm_id]).sum(:credit_limit)
|
|
|
|
[1,6,2].each do |firm_id|
|
|
|
|
assert c.keys.include?(firm_id), "Group #{c.inspect} does not contain firm_id #{firm_id}"
|
|
|
|
end
|
2012-06-24 08:39:54 -04:00
|
|
|
end
|
|
|
|
|
2010-11-15 21:33:21 -05:00
|
|
|
def test_should_group_by_multiple_fields
|
2012-04-26 12:29:22 -04:00
|
|
|
c = Account.group('firm_id', :credit_limit).count(:all)
|
2010-11-15 21:33:21 -05:00
|
|
|
[ [nil, 50], [1, 50], [6, 50], [6, 55], [9, 53], [2, 60] ].each { |firm_and_limit| assert c.keys.include?(firm_and_limit) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_group_by_multiple_fields_having_functions
|
|
|
|
c = Topic.group(:author_name, 'COALESCE(type, title)').count(:all)
|
|
|
|
assert_equal 1, c[["Carl", "The Third Topic of the day"]]
|
|
|
|
assert_equal 1, c[["Mary", "Reply"]]
|
|
|
|
assert_equal 1, c[["David", "The First Topic"]]
|
|
|
|
assert_equal 1, c[["Carl", "Reply"]]
|
|
|
|
end
|
2006-02-25 18:06:04 -05:00
|
|
|
|
|
|
|
def test_should_group_by_summed_field
|
2012-04-26 12:29:22 -04:00
|
|
|
c = Account.group(:firm_id).sum(:credit_limit)
|
2006-03-01 11:25:14 -05:00
|
|
|
assert_equal 50, c[1]
|
|
|
|
assert_equal 105, c[6]
|
|
|
|
assert_equal 60, c[2]
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
|
|
|
|
2006-03-14 09:59:14 -05:00
|
|
|
def test_should_order_by_grouped_field
|
2013-04-02 10:11:14 -04:00
|
|
|
c = Account.group(:firm_id).order("firm_id").sum(:credit_limit)
|
2006-08-29 13:06:27 -04:00
|
|
|
assert_equal [1, 2, 6, 9], c.keys.compact
|
2006-03-14 09:59:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_order_by_calculation
|
2013-04-02 10:11:14 -04:00
|
|
|
c = Account.group(:firm_id).order("sum_credit_limit desc, firm_id").sum(:credit_limit)
|
2006-08-29 13:06:27 -04: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 09:59:14 -05:00
|
|
|
end
|
|
|
|
|
2006-04-06 11:23:56 -04:00
|
|
|
def test_should_limit_calculation
|
2013-04-02 10:11:14 -04:00
|
|
|
c = Account.where("firm_id IS NOT NULL").group(:firm_id).order("firm_id").limit(2).sum(:credit_limit)
|
2006-04-06 11:23:56 -04:00
|
|
|
assert_equal [1, 2], c.keys.compact
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_limit_calculation_with_offset
|
2013-04-02 10:11:14 -04:00
|
|
|
c = Account.where("firm_id IS NOT NULL").group(:firm_id).order("firm_id").
|
|
|
|
limit(2).offset(1).sum(:credit_limit)
|
2006-04-06 11:23:56 -04:00
|
|
|
assert_equal [2, 6], c.keys.compact
|
|
|
|
end
|
|
|
|
|
2011-03-03 23:26:45 -05:00
|
|
|
def test_limit_should_apply_before_count
|
|
|
|
accounts = Account.limit(3).where('firm_id IS NOT NULL')
|
|
|
|
|
|
|
|
assert_equal 3, accounts.count(:firm_id)
|
|
|
|
assert_equal 3, accounts.select(:firm_id).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_count_should_shortcut_with_limit_zero
|
|
|
|
accounts = Account.limit(0)
|
|
|
|
|
|
|
|
assert_no_queries { assert_equal 0, accounts.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_limit_is_kept
|
2011-02-28 12:15:19 -05:00
|
|
|
return if current_adapter?(:OracleAdapter)
|
|
|
|
|
2011-03-03 23:26:45 -05:00
|
|
|
queries = assert_sql { Account.limit(1).count }
|
2011-02-25 18:38:59 -05:00
|
|
|
assert_equal 1, queries.length
|
|
|
|
assert_match(/LIMIT/, queries.first)
|
|
|
|
end
|
|
|
|
|
2011-03-03 23:26:45 -05:00
|
|
|
def test_offset_is_kept
|
|
|
|
return if current_adapter?(:OracleAdapter)
|
|
|
|
|
2011-02-25 18:38:59 -05:00
|
|
|
queries = assert_sql { Account.offset(1).count }
|
|
|
|
assert_equal 1, queries.length
|
2011-03-03 23:26:45 -05:00
|
|
|
assert_match(/OFFSET/, queries.first)
|
2011-02-25 18:38:59 -05:00
|
|
|
end
|
|
|
|
|
2011-03-03 23:26:45 -05:00
|
|
|
def test_limit_with_offset_is_kept
|
|
|
|
return if current_adapter?(:OracleAdapter)
|
|
|
|
|
|
|
|
queries = assert_sql { Account.limit(1).offset(1).count }
|
2011-02-25 18:38:59 -05:00
|
|
|
assert_equal 1, queries.length
|
2011-03-03 23:26:45 -05:00
|
|
|
assert_match(/LIMIT/, queries.first)
|
|
|
|
assert_match(/OFFSET/, queries.first)
|
2011-02-25 18:38:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_no_limit_no_offset
|
|
|
|
queries = assert_sql { Account.count }
|
|
|
|
assert_equal 1, queries.length
|
|
|
|
assert_no_match(/LIMIT/, queries.first)
|
|
|
|
assert_no_match(/OFFSET/, queries.first)
|
|
|
|
end
|
|
|
|
|
2013-05-13 13:31:00 -04:00
|
|
|
def test_count_on_invalid_columns_raises
|
|
|
|
e = assert_raises(ActiveRecord::StatementInvalid) {
|
|
|
|
Account.select("credit_limit, firm_name").count
|
|
|
|
}
|
|
|
|
|
2013-08-01 07:59:17 -04:00
|
|
|
assert_match %r{accounts}i, e.message
|
2013-05-13 13:31:00 -04:00
|
|
|
assert_match "credit_limit, firm_name", e.message
|
|
|
|
end
|
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_group_by_summed_field_having_condition
|
2013-04-02 10:11:14 -04:00
|
|
|
c = Account.group(:firm_id).having('sum(credit_limit) > 50').sum(:credit_limit)
|
2009-03-06 17:29:36 -05:00
|
|
|
assert_nil c[1]
|
|
|
|
assert_equal 105, c[6]
|
|
|
|
assert_equal 60, c[2]
|
|
|
|
end
|
|
|
|
|
2011-07-05 06:17:39 -04:00
|
|
|
def test_should_group_by_summed_field_having_condition_from_select
|
2011-08-05 14:20:15 -04:00
|
|
|
c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("MIN(credit_limit) > 50").sum(:credit_limit)
|
2011-07-05 06:17:39 -04:00
|
|
|
assert_nil c[1]
|
|
|
|
assert_equal 60, c[2]
|
|
|
|
assert_equal 53, c[9]
|
|
|
|
end
|
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_group_by_summed_association
|
2012-04-26 12:29:22 -04:00
|
|
|
c = Account.group(:firm).sum(:credit_limit)
|
2006-02-25 18:06:04 -05:00
|
|
|
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 02:30:42 -05:00
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_sum_field_with_conditions
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal 105, Account.where('firm_id = 6').sum(:credit_limit)
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
|
|
|
|
2008-04-08 01:20:33 -04:00
|
|
|
def test_should_return_zero_if_sum_conditions_return_nothing
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal 0, Account.where('1 = 2').sum(:credit_limit)
|
|
|
|
assert_equal 0, companies(:rails_core).companies.where('1 = 2').sum(:id)
|
2008-04-08 01:20:33 -04:00
|
|
|
end
|
|
|
|
|
2008-06-02 15:40:25 -04: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
|
|
|
|
|
2013-12-08 18:23:38 -05:00
|
|
|
def test_should_return_type_casted_values_with_group_and_expression
|
|
|
|
assert_equal 0.5, Account.group(:firm_name).sum('0.01 * credit_limit')['37signals']
|
|
|
|
end
|
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_group_by_summed_field_with_conditions
|
2013-04-02 10:11:14 -04:00
|
|
|
c = Account.where('firm_id > 1').group(:firm_id).sum(:credit_limit)
|
2006-03-01 11:25:14 -05:00
|
|
|
assert_nil c[1]
|
|
|
|
assert_equal 105, c[6]
|
|
|
|
assert_equal 60, c[2]
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_group_by_summed_field_with_conditions_and_having
|
2013-04-02 10:11:14 -04:00
|
|
|
c = Account.where('firm_id > 1').group(:firm_id).
|
|
|
|
having('sum(credit_limit) > 60').sum(:credit_limit)
|
2006-03-01 11:25:14 -05:00
|
|
|
assert_nil c[1]
|
|
|
|
assert_equal 105, c[6]
|
|
|
|
assert_nil c[2]
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_group_by_fields_with_table_alias
|
2012-04-26 12:29:22 -04:00
|
|
|
c = Account.group('accounts.firm_id').sum(:credit_limit)
|
2006-03-01 11:25:14 -05:00
|
|
|
assert_equal 50, c[1]
|
|
|
|
assert_equal 105, c[6]
|
|
|
|
assert_equal 60, c[2]
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_calculate_with_invalid_field
|
2006-08-29 13:06:27 -04:00
|
|
|
assert_equal 6, Account.calculate(:count, '*')
|
|
|
|
assert_equal 6, Account.calculate(:count, :all)
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_calculate_grouped_with_invalid_field
|
2012-04-26 12:29:22 -04:00
|
|
|
c = Account.group('accounts.firm_id').count(:all)
|
2006-03-01 11:25:14 -05:00
|
|
|
assert_equal 1, c[1]
|
|
|
|
assert_equal 2, c[6]
|
|
|
|
assert_equal 1, c[2]
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_calculate_grouped_association_with_invalid_field
|
2012-04-26 12:29:22 -04:00
|
|
|
c = Account.group(:firm).count(:all)
|
2006-02-25 18:06:04 -05:00
|
|
|
assert_equal 1, c[companies(:first_firm)]
|
|
|
|
assert_equal 2, c[companies(:rails_core)]
|
|
|
|
assert_equal 1, c[companies(:first_client)]
|
|
|
|
end
|
2007-05-31 23:26:51 -04:00
|
|
|
|
2009-02-03 21:25:37 -05:00
|
|
|
def test_should_group_by_association_with_non_numeric_foreign_key
|
2012-10-17 18:55:30 -04:00
|
|
|
Speedometer.create! id: 'ABC'
|
|
|
|
Minivan.create! id: 'OMG', speedometer_id: 'ABC'
|
2009-02-03 21:25:37 -05:00
|
|
|
|
2012-10-17 18:51:17 -04:00
|
|
|
c = Minivan.group(:speedometer).count(:all)
|
2009-02-03 21:25:37 -05:00
|
|
|
first_key = c.keys.first
|
2012-10-17 18:51:17 -04:00
|
|
|
assert_equal Speedometer, first_key.class
|
2009-02-03 21:25:37 -05:00
|
|
|
assert_equal 1, c[first_key]
|
2007-05-31 23:26:51 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-02-01 23:28:42 -05:00
|
|
|
def test_should_calculate_grouped_association_with_foreign_key_option
|
|
|
|
Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id'
|
2012-04-26 12:29:22 -04:00
|
|
|
c = Account.group(:another_firm).count(:all)
|
2008-02-01 23:28:42 -05:00
|
|
|
assert_equal 1, c[companies(:first_firm)]
|
|
|
|
assert_equal 2, c[companies(:rails_core)]
|
|
|
|
assert_equal 1, c[companies(:first_client)]
|
|
|
|
end
|
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_calculate_grouped_by_function
|
2012-04-26 12:29:22 -04:00
|
|
|
c = Company.group("UPPER(#{QUOTED_TYPE})").count(:all)
|
2006-02-25 18:06:04 -05:00
|
|
|
assert_equal 2, c[nil]
|
|
|
|
assert_equal 1, c['DEPENDENTFIRM']
|
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where
newly created finder methods #second, #third, #forth, and #fifth
caused a NoMethodError error on reload associations and where we
were pulling the wrong element out of cached associations.
Examples:
some_book.authors.reload.second
# Before
# => NoMethodError: undefined method 'first' for nil:NilClass
# After
# => #<Author id: 2, name: "Sally Second", ...>
some_book.first.authors.first
some_book.first.authors.second
# Before
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 1, name: "Freddy First", ...>
# After
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 2, name: "Sally Second", ...>
Fixes #13783.
2014-01-21 17:34:39 -05:00
|
|
|
assert_equal 5, c['CLIENT']
|
2006-02-25 18:06:04 -05:00
|
|
|
assert_equal 2, c['FIRM']
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_calculate_grouped_by_function_with_table_alias
|
2012-04-26 12:29:22 -04:00
|
|
|
c = Company.group("UPPER(companies.#{QUOTED_TYPE})").count(:all)
|
2006-02-25 18:41:51 -05:00
|
|
|
assert_equal 2, c[nil]
|
|
|
|
assert_equal 1, c['DEPENDENTFIRM']
|
Ensure AR #second, #third, etc. finders work through associations
This commit fixes two regressions introduced in cafe31a078 where
newly created finder methods #second, #third, #forth, and #fifth
caused a NoMethodError error on reload associations and where we
were pulling the wrong element out of cached associations.
Examples:
some_book.authors.reload.second
# Before
# => NoMethodError: undefined method 'first' for nil:NilClass
# After
# => #<Author id: 2, name: "Sally Second", ...>
some_book.first.authors.first
some_book.first.authors.second
# Before
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 1, name: "Freddy First", ...>
# After
# => #<Author id: 1, name: "Freddy First", ...>
# => #<Author id: 2, name: "Sally Second", ...>
Fixes #13783.
2014-01-21 17:34:39 -05:00
|
|
|
assert_equal 5, c['CLIENT']
|
2006-02-25 18:41:51 -05:00
|
|
|
assert_equal 2, c['FIRM']
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2006-06-25 13:49:24 -04:00
|
|
|
def test_should_not_overshadow_enumerable_sum
|
|
|
|
assert_equal 6, [1, 2, 3].sum(&:abs)
|
|
|
|
end
|
2006-02-25 18:06:04 -05:00
|
|
|
|
|
|
|
def test_should_sum_scoped_field
|
|
|
|
assert_equal 15, companies(:rails_core).companies.sum(:id)
|
|
|
|
end
|
|
|
|
|
2009-08-08 20:10:01 -04:00
|
|
|
def test_should_sum_scoped_field_with_from
|
|
|
|
assert_equal Club.count, Organization.clubs.count
|
|
|
|
end
|
|
|
|
|
2006-02-25 18:06:04 -05:00
|
|
|
def test_should_sum_scoped_field_with_conditions
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal 8, companies(:rails_core).companies.where('id > 7').sum(:id)
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_group_by_scoped_field
|
2012-04-26 12:29:22 -04:00
|
|
|
c = companies(:rails_core).companies.group(:name).sum(:id)
|
2006-02-25 18:06:04 -05:00
|
|
|
assert_equal 7, c['Leetsoft']
|
|
|
|
assert_equal 8, c['Jadedpixel']
|
|
|
|
end
|
|
|
|
|
2009-04-29 18:35:21 -04:00
|
|
|
def test_should_group_by_summed_field_through_association_and_having
|
2012-04-26 12:29:22 -04:00
|
|
|
c = companies(:rails_core).companies.group(:name).having('sum(id) > 7').sum(:id)
|
2006-02-25 18:06:04 -05:00
|
|
|
assert_nil c['Leetsoft']
|
|
|
|
assert_equal 8, c['Jadedpixel']
|
|
|
|
end
|
2006-03-01 11:25:14 -05:00
|
|
|
|
2006-06-03 17:19:36 -04:00
|
|
|
def test_should_count_selected_field_with_include
|
2013-03-12 05:52:25 -04:00
|
|
|
assert_equal 6, Account.includes(:firm).distinct.count
|
|
|
|
assert_equal 4, Account.includes(:firm).distinct.select(:credit_limit).count
|
2006-06-03 17:19:36 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2011-05-11 05:19:06 -04:00
|
|
|
def test_should_not_perform_joined_include_by_default
|
|
|
|
assert_equal Account.count, Account.includes(:firm).count
|
|
|
|
queries = assert_sql { Account.includes(:firm).count }
|
|
|
|
assert_no_match(/join/i, queries.last)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_perform_joined_include_when_referencing_included_tables
|
2012-01-13 18:42:07 -05:00
|
|
|
joined_count = Account.includes(:firm).where(:companies => {:name => '37signals'}).count
|
2011-05-11 05:19:06 -04:00
|
|
|
assert_equal 1, joined_count
|
|
|
|
end
|
|
|
|
|
2009-03-07 10:26:56 -05:00
|
|
|
def test_should_count_scoped_select
|
2009-03-07 11:36:40 -05:00
|
|
|
Account.update_all("credit_limit = NULL")
|
2013-04-02 10:11:14 -04:00
|
|
|
assert_equal 0, Account.select("credit_limit").count
|
2009-03-07 10:26:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_count_scoped_select_with_options
|
2009-03-07 11:36:40 -05:00
|
|
|
Account.update_all("credit_limit = NULL")
|
2012-07-24 19:20:03 -04:00
|
|
|
Account.last.update_columns('credit_limit' => 49)
|
|
|
|
Account.first.update_columns('credit_limit' => 51)
|
2009-03-07 11:36:40 -05:00
|
|
|
|
2013-04-02 10:11:14 -04:00
|
|
|
assert_equal 1, Account.select("credit_limit").where('credit_limit >= 50').count
|
2009-03-07 10:26:56 -05:00
|
|
|
end
|
|
|
|
|
2008-04-04 08:06:22 -04:00
|
|
|
def test_should_count_manual_select_with_include
|
2013-04-02 10:11:14 -04:00
|
|
|
assert_equal 6, Account.select("DISTINCT accounts.id").includes(:firm).count
|
2008-04-04 08:06:22 -04:00
|
|
|
end
|
|
|
|
|
2007-07-17 16:16:35 -04:00
|
|
|
def test_count_with_column_parameter
|
|
|
|
assert_equal 5, Account.count(:firm_id)
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2013-03-12 05:23:08 -04:00
|
|
|
def test_count_with_distinct
|
|
|
|
assert_equal 4, Account.select(:credit_limit).distinct.count
|
2013-01-23 10:28:17 -05:00
|
|
|
assert_equal 4, Account.select(:credit_limit).uniq.count
|
|
|
|
end
|
|
|
|
|
2013-05-01 14:33:11 -04:00
|
|
|
def test_count_with_aliased_attribute
|
|
|
|
assert_equal 6, Account.count(:available_credit)
|
|
|
|
end
|
|
|
|
|
2007-07-17 16:16:35 -04:00
|
|
|
def test_count_with_column_and_options_parameter
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal 2, Account.where("credit_limit = 50 AND firm_id IS NOT NULL").count(:firm_id)
|
2007-07-17 16:16:35 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2010-08-25 04:52:00 -04:00
|
|
|
def test_should_count_field_in_joined_table
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal 5, Account.joins(:firm).count('companies.id')
|
2013-03-12 05:52:25 -04:00
|
|
|
assert_equal 4, Account.joins(:firm).distinct.count('companies.id')
|
2010-08-25 04:52:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_count_field_in_joined_table_with_group_by
|
2013-04-02 10:11:14 -04:00
|
|
|
c = Account.group('accounts.firm_id').joins(:firm).count('companies.id')
|
2010-08-25 04:52:00 -04:00
|
|
|
|
|
|
|
[1,6,2,9].each { |firm_id| assert c.keys.include?(firm_id) }
|
|
|
|
end
|
|
|
|
|
2006-09-26 13:02:45 -04: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 00:02:34 -04:00
|
|
|
|
2014-04-07 10:07:46 -04:00
|
|
|
def test_count_with_order
|
|
|
|
assert_equal 6, Account.order(:credit_limit).count
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_count_with_reverse_order
|
|
|
|
assert_equal 6, Account.order(:credit_limit).reverse_order.count
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_count_with_where_and_order
|
|
|
|
assert_equal 1, Account.where(firm_name: '37signals').count
|
|
|
|
assert_equal 1, Account.where(firm_name: '37signals').order(:firm_name).count
|
|
|
|
assert_equal 1, Account.where(firm_name: '37signals').order(:firm_name).reverse_order.count
|
|
|
|
end
|
|
|
|
|
2008-03-17 00:02:34 -04:00
|
|
|
def test_should_sum_expression
|
2009-03-22 18:07:23 -04: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 15:38:04 -04:00
|
|
|
assert_equal 636, Account.sum("2 * credit_limit").to_i
|
2009-03-22 18:07:23 -04:00
|
|
|
end
|
2008-03-17 00:02:34 -04:00
|
|
|
end
|
2008-06-11 19:26:35 -04:00
|
|
|
|
2012-08-24 05:08:36 -04:00
|
|
|
def test_sum_expression_returns_zero_when_no_records_to_sum
|
|
|
|
assert_equal 0, Account.where('1 = 2').sum("2 * credit_limit")
|
|
|
|
end
|
|
|
|
|
2008-06-11 19:26:35 -04:00
|
|
|
def test_count_with_from_option
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal Company.count(:all), Company.from('companies').count(:all)
|
|
|
|
assert_equal Account.where("credit_limit = 50").count(:all),
|
|
|
|
Account.from('accounts').where("credit_limit = 50").count(:all)
|
|
|
|
assert_equal Company.where(:type => "Firm").count(:type),
|
|
|
|
Company.where(:type => "Firm").from('companies').count(:type)
|
2008-06-11 19:26:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sum_with_from_option
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal Account.sum(:credit_limit), Account.from('accounts').sum(:credit_limit)
|
|
|
|
assert_equal Account.where("credit_limit > 50").sum(:credit_limit),
|
|
|
|
Account.where("credit_limit > 50").from('accounts').sum(:credit_limit)
|
2008-06-11 19:26:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_average_with_from_option
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal Account.average(:credit_limit), Account.from('accounts').average(:credit_limit)
|
|
|
|
assert_equal Account.where("credit_limit > 50").average(:credit_limit),
|
|
|
|
Account.where("credit_limit > 50").from('accounts').average(:credit_limit)
|
2008-06-11 19:26:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_minimum_with_from_option
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal Account.minimum(:credit_limit), Account.from('accounts').minimum(:credit_limit)
|
|
|
|
assert_equal Account.where("credit_limit > 50").minimum(:credit_limit),
|
|
|
|
Account.where("credit_limit > 50").from('accounts').minimum(:credit_limit)
|
2008-06-11 19:26:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_maximum_with_from_option
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal Account.maximum(:credit_limit), Account.from('accounts').maximum(:credit_limit)
|
|
|
|
assert_equal Account.where("credit_limit > 50").maximum(:credit_limit),
|
|
|
|
Account.where("credit_limit > 50").from('accounts').maximum(:credit_limit)
|
2008-06-11 19:26:35 -04:00
|
|
|
end
|
|
|
|
|
2012-04-26 23:15:28 -04:00
|
|
|
def test_maximum_with_not_auto_table_name_prefix_if_column_included
|
|
|
|
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
|
|
|
|
|
2012-12-28 22:11:02 -05:00
|
|
|
assert_equal 7, Company.includes(:contracts).maximum(:developer_id)
|
2012-04-26 23:15:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_minimum_with_not_auto_table_name_prefix_if_column_included
|
|
|
|
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
|
|
|
|
|
2012-12-28 22:11:02 -05:00
|
|
|
assert_equal 7, Company.includes(:contracts).minimum(:developer_id)
|
2012-04-26 23:15:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_sum_with_not_auto_table_name_prefix_if_column_included
|
|
|
|
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
|
|
|
|
|
2012-12-28 22:11:02 -05:00
|
|
|
assert_equal 7, Company.includes(:contracts).sum(:developer_id)
|
2012-04-26 23:15:28 -04:00
|
|
|
end
|
|
|
|
|
2008-06-11 19:26:35 -04:00
|
|
|
def test_from_option_with_specified_index
|
2010-08-02 04:37:57 -04:00
|
|
|
if Edge.connection.adapter_name == 'MySQL' or Edge.connection.adapter_name == 'Mysql2'
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal Edge.count(:all), Edge.from('edges USE INDEX(unique_edge_index)').count(:all)
|
|
|
|
assert_equal Edge.where('sink_id < 5').count(:all),
|
|
|
|
Edge.from('edges USE INDEX(unique_edge_index)').where('sink_id < 5').count(:all)
|
2008-06-11 19:26:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_from_option_with_table_different_than_class
|
2012-04-26 12:29:22 -04:00
|
|
|
assert_equal Account.count(:all), Company.from('accounts').count(:all)
|
2008-06-11 19:26:35 -04:00
|
|
|
end
|
2010-10-01 07:27:02 -04: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]
|
2014-01-18 00:51:34 -05:00
|
|
|
assert_equal approved_topics_count, 4
|
2010-10-01 07:27:02 -04:00
|
|
|
# Count the number of distinct authors for approved Topics
|
2013-03-12 05:52:25 -04:00
|
|
|
distinct_authors_for_approved_count = Topic.group(:approved).distinct.count(:author_name)[true]
|
2014-01-18 00:51:34 -05:00
|
|
|
assert_equal distinct_authors_for_approved_count, 3
|
2010-10-01 07:27:02 -04:00
|
|
|
end
|
2011-11-30 04:03:00 -05:00
|
|
|
|
|
|
|
def test_pluck
|
2014-01-18 00:51:34 -05:00
|
|
|
assert_equal [1,2,3,4,5], Topic.order(:id).pluck(:id)
|
2011-11-30 04:03:00 -05:00
|
|
|
end
|
|
|
|
|
2013-05-10 13:33:24 -04:00
|
|
|
def test_pluck_without_column_names
|
|
|
|
assert_equal [[1, "Firm", 1, nil, "37signals", nil, 1, nil, ""]],
|
|
|
|
Company.order(:id).limit(1).pluck
|
|
|
|
end
|
|
|
|
|
2011-11-30 04:03:00 -05:00
|
|
|
def test_pluck_type_cast
|
|
|
|
topic = topics(:first)
|
|
|
|
relation = Topic.where(:id => topic.id)
|
|
|
|
assert_equal [ topic.approved ], relation.pluck(:approved)
|
|
|
|
assert_equal [ topic.last_read ], relation.pluck(:last_read)
|
|
|
|
assert_equal [ topic.written_on ], relation.pluck(:written_on)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pluck_and_uniq
|
|
|
|
assert_equal [50, 53, 55, 60], Account.order(:credit_limit).uniq.pluck(:credit_limit)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pluck_in_relation
|
|
|
|
company = Company.first
|
|
|
|
contract = company.contracts.create!
|
|
|
|
assert_equal [contract.id], company.contracts.pluck(:id)
|
|
|
|
end
|
|
|
|
|
2013-05-01 14:33:11 -04:00
|
|
|
def test_pluck_on_aliased_attribute
|
|
|
|
assert_equal 'The First Topic', Topic.order(:id).pluck(:heading).first
|
|
|
|
end
|
|
|
|
|
2011-12-22 14:22:59 -05:00
|
|
|
def test_pluck_with_serialization
|
|
|
|
t = Topic.create!(:content => { :foo => :bar })
|
|
|
|
assert_equal [{:foo => :bar}], Topic.where(:id => t.id).pluck(:content)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pluck_with_qualified_column_name
|
2014-01-18 00:51:34 -05:00
|
|
|
assert_equal [1,2,3,4,5], Topic.order(:id).pluck("topics.id")
|
2011-12-22 14:22:59 -05:00
|
|
|
end
|
2012-02-08 09:37:27 -05:00
|
|
|
|
|
|
|
def test_pluck_auto_table_name_prefix
|
|
|
|
c = Company.create!(:name => "test", :contracts => [Contract.new])
|
|
|
|
assert_equal [c.id], Company.joins(:contracts).pluck(:id)
|
|
|
|
end
|
|
|
|
|
2012-04-26 23:15:28 -04:00
|
|
|
def test_pluck_if_table_included
|
|
|
|
c = Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
|
|
|
|
assert_equal [c.id], Company.includes(:contracts).where("contracts.id" => c.contracts.first).pluck(:id)
|
|
|
|
end
|
|
|
|
|
2012-02-08 09:37:27 -05:00
|
|
|
def test_pluck_not_auto_table_name_prefix_if_column_joined
|
2012-02-08 13:34:29 -05:00
|
|
|
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
|
2012-02-09 17:42:39 -05:00
|
|
|
assert_equal [7], Company.joins(:contracts).pluck(:developer_id)
|
2012-02-08 09:37:27 -05:00
|
|
|
end
|
2012-04-30 12:45:00 -04:00
|
|
|
|
2012-05-15 13:21:38 -04:00
|
|
|
def test_pluck_with_selection_clause
|
|
|
|
assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT credit_limit').sort
|
2012-05-15 20:51:27 -04:00
|
|
|
assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT accounts.credit_limit').sort
|
|
|
|
assert_equal [50, 53, 55, 60], Account.pluck('DISTINCT(credit_limit)').sort
|
2012-05-16 17:07:10 -04:00
|
|
|
|
|
|
|
# MySQL returns "SUM(DISTINCT(credit_limit))" as the column name unless
|
|
|
|
# an alias is provided. Without the alias, the column cannot be found
|
|
|
|
# and properly typecast.
|
|
|
|
assert_equal [50 + 53 + 55 + 60], Account.pluck('SUM(DISTINCT(credit_limit)) as credit_limit')
|
2012-05-15 13:21:38 -04:00
|
|
|
end
|
|
|
|
|
2012-04-30 12:45:00 -04:00
|
|
|
def test_plucks_with_ids
|
2012-08-03 06:51:52 -04:00
|
|
|
assert_equal Company.all.map(&:id).sort, Company.ids.sort
|
2012-04-30 12:45:00 -04:00
|
|
|
end
|
2012-04-26 23:15:28 -04:00
|
|
|
|
2013-05-10 13:24:56 -04:00
|
|
|
def test_pluck_with_includes_limit_and_empty_result
|
|
|
|
assert_equal [], Topic.includes(:replies).limit(0).pluck(:id)
|
|
|
|
assert_equal [], Topic.includes(:replies).limit(1).where('0 = 1').pluck(:id)
|
|
|
|
end
|
|
|
|
|
2012-04-26 23:15:28 -04:00
|
|
|
def test_pluck_not_auto_table_name_prefix_if_column_included
|
|
|
|
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
|
|
|
|
ids = Company.includes(:contracts).pluck(:developer_id)
|
|
|
|
assert_equal Company.count, ids.length
|
|
|
|
assert_equal [7], ids.compact
|
|
|
|
end
|
2012-05-26 13:17:05 -04:00
|
|
|
|
|
|
|
def test_pluck_multiple_columns
|
|
|
|
assert_equal [
|
|
|
|
[1, "The First Topic"], [2, "The Second Topic of the day"],
|
2014-01-18 00:51:34 -05:00
|
|
|
[3, "The Third Topic of the day"], [4, "The Fourth Topic of the day"],
|
|
|
|
[5, "The Fifth Topic of the day"]
|
2012-06-19 21:12:01 -04:00
|
|
|
], Topic.order(:id).pluck(:id, :title)
|
2012-05-26 13:17:05 -04:00
|
|
|
assert_equal [
|
|
|
|
[1, "The First Topic", "David"], [2, "The Second Topic of the day", "Mary"],
|
2014-01-18 00:51:34 -05:00
|
|
|
[3, "The Third Topic of the day", "Carl"], [4, "The Fourth Topic of the day", "Carl"],
|
|
|
|
[5, "The Fifth Topic of the day", "Jason"]
|
2012-06-19 21:12:01 -04:00
|
|
|
], Topic.order(:id).pluck(:id, :title, :author_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pluck_with_multiple_columns_and_selection_clause
|
|
|
|
assert_equal [[1, 50], [2, 50], [3, 50], [4, 60], [5, 55], [6, 53]],
|
|
|
|
Account.pluck('id, credit_limit')
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_pluck_with_multiple_columns_and_includes
|
|
|
|
Company.create!(:name => "test", :contracts => [Contract.new(:developer_id => 7)])
|
|
|
|
companies_and_developers = Company.order('companies.id').includes(:contracts).pluck(:name, :developer_id)
|
|
|
|
|
|
|
|
assert_equal Company.count, companies_and_developers.length
|
|
|
|
assert_equal ["37signals", nil], companies_and_developers.first
|
|
|
|
assert_equal ["test", 7], companies_and_developers.last
|
2012-05-26 13:17:05 -04:00
|
|
|
end
|
2012-09-05 18:00:07 -04:00
|
|
|
|
|
|
|
def test_pluck_with_reserved_words
|
|
|
|
Possession.create!(:where => "Over There")
|
|
|
|
|
|
|
|
assert_equal ["Over There"], Possession.pluck(:where)
|
|
|
|
end
|
2012-11-11 11:15:26 -05:00
|
|
|
|
|
|
|
def test_pluck_replaces_select_clause
|
|
|
|
taks_relation = Topic.select(:approved, :id).order(:id)
|
2014-01-18 00:51:34 -05:00
|
|
|
assert_equal [1,2,3,4,5], taks_relation.pluck(:id)
|
|
|
|
assert_equal [false, true, true, true, true], taks_relation.pluck(:approved)
|
2012-11-11 11:15:26 -05:00
|
|
|
end
|
2014-06-11 16:32:00 -04:00
|
|
|
|
|
|
|
def test_pluck_columns_with_same_name
|
|
|
|
expected = [["The First Topic", "The Second Topic of the day"], ["The Third Topic of the day", "The Fourth Topic of the day"]]
|
|
|
|
actual = Topic.joins(:replies)
|
|
|
|
.pluck('topics.title', 'replies_topics.title')
|
|
|
|
assert_equal expected, actual
|
|
|
|
end
|
2014-12-09 09:20:22 -05:00
|
|
|
|
|
|
|
def test_calculation_with_polymorphic_relation
|
2014-12-11 15:34:46 -05:00
|
|
|
part = ShipPart.create!(name: "has trinket")
|
|
|
|
part.trinkets.create!
|
|
|
|
|
|
|
|
assert_equal part.id, ShipPart.joins(:trinkets).sum(:id)
|
2014-12-09 09:20:22 -05:00
|
|
|
end
|
|
|
|
|
2014-12-11 16:54:03 -05:00
|
|
|
def test_pluck_joined_with_polymorphic_relation
|
|
|
|
part = ShipPart.create!(name: "has trinket")
|
|
|
|
part.trinkets.create!
|
|
|
|
|
|
|
|
assert_equal [part.id], ShipPart.joins(:trinkets).pluck(:id)
|
|
|
|
end
|
|
|
|
|
2014-12-11 15:34:46 -05:00
|
|
|
def test_grouped_calculation_with_polymorphic_relation
|
|
|
|
part = ShipPart.create!(name: "has trinket")
|
|
|
|
part.trinkets.create!
|
|
|
|
|
|
|
|
assert_equal({ "has trinket" => part.id }, ShipPart.joins(:trinkets).group("ship_parts.name").sum(:id))
|
|
|
|
end
|
2006-02-25 18:06:04 -05:00
|
|
|
end
|