test/cases/calculations_test.rb in db2-2.6.2 vs test/cases/calculations_test.rb in db2-2.7.0

- old
+ new

@@ -1,7 +1,8 @@ require "cases/helper" require 'models/company' +require "models/contract" require 'models/topic' require 'models/edge' require 'models/club' require 'models/organization' @@ -21,10 +22,22 @@ def test_should_average_field value = Account.average(:credit_limit) assert_equal 53.0, value end + def test_should_return_decimal_average_of_integer_field + return if current_adapter?(:IBM_DBAdapter) #average cannot be a decimal value when applied on integer field + value = Account.average(:id) + assert_equal 3.5, value + end + + def test_should_return_integer_average_if_db_returns_such + Account.connection.stubs :select_value => 3 + value = Account.average(:id) + assert_equal 3, value + end + def test_should_return_nil_as_average assert_nil NumericData.average(:bank_balance) end def test_type_cast_calculated_value_should_convert_db_averages_of_fixnum_class_to_decimal @@ -53,10 +66,23 @@ def test_should_group_by_field c = Account.sum(:credit_limit, :group => :firm_id) [1,6,2].each { |firm_id| assert c.keys.include?(firm_id) } end + def test_should_group_by_multiple_fields + c = Account.count(:all, :group => ['firm_id', :credit_limit]) + [ [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 + 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] @@ -83,10 +109,55 @@ 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 + 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 + return if current_adapter?(:OracleAdapter, :IBM_DBAdapter) + + queries = assert_sql { Account.limit(1).count } + assert_equal 1, queries.length + assert_match(/LIMIT/, queries.first) + end + + def test_offset_is_kept + return if current_adapter?(:OracleAdapter,:IBM_DBAdapter) + + queries = assert_sql { Account.offset(1).count } + assert_equal 1, queries.length + assert_match(/OFFSET/, queries.first) + end + + def test_limit_with_offset_is_kept + return if current_adapter?(:OracleAdapter,:IBM_DBAdapter) + + queries = assert_sql { Account.limit(1).offset(1).count } + assert_equal 1, queries.length + assert_match(/LIMIT/, queries.first) + assert_match(/OFFSET/, queries.first) + 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 + 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] @@ -99,10 +170,17 @@ assert_nil c[1] assert_equal 105, c[6] assert_equal 60, c[2] end + def test_should_group_by_summed_field_having_condition_from_select + c = Account.select("MIN(credit_limit) AS min_credit_limit").group(:firm_id).having("MIN(credit_limit) > 50").sum(:credit_limit) + assert_nil c[1] + assert_equal 60, c[2] + assert_equal 53, c[9] + 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)] @@ -164,15 +242,11 @@ assert_equal 2, c[companies(:rails_core)] assert_equal 1, c[companies(:first_client)] end def test_should_group_by_association_with_non_numeric_foreign_key - if( ActiveRecord::Base.connection.respond_to?(:pstmt_support_on) && ActiveRecord::Base.connection.pstmt_support_on == true ) - ActiveRecord::Base.connection.expects(:prepared_select).returns([{"count_all" => 1, "firm_id" => "ABC"}]) - else - ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}]) - end + 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]) @@ -252,19 +326,30 @@ def test_should_count_selected_field_with_include assert_equal 6, Account.count(:distinct => true, :include => :firm) assert_equal 4, Account.count(:distinct => true, :include => :firm, :select => :credit_limit) end + 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 + joined_count = Account.includes(:firm).where(:companies => {:name => '37signals'}).count + assert_equal 1, joined_count + end + def test_should_count_scoped_select Account.update_all("credit_limit = NULL") assert_equal 0, Account.scoped(:select => "credit_limit").count end def test_should_count_scoped_select_with_options Account.update_all("credit_limit = NULL") - Account.last.update_attribute('credit_limit', 49) - Account.first.update_attribute('credit_limit', 51) + Account.last.update_column('credit_limit', 49) + Account.first.update_column('credit_limit', 51) assert_equal 1, Account.scoped(:select => "credit_limit").count(:conditions => ['credit_limit >= 50']) end def test_should_count_manual_select_with_include @@ -319,10 +404,14 @@ 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_sum_array_compatibility + assert_equal Account.sum(:credit_limit), Account.sum(&:credit_limit) + 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 @@ -338,11 +427,11 @@ 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 - if Edge.connection.adapter_name == 'MySQL' + if Edge.connection.adapter_name == 'MySQL' or Edge.connection.adapter_name == 'Mysql2' 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 @@ -356,7 +445,38 @@ 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 + + def test_pluck + assert_equal [1,2,3,4], Topic.order(:id).pluck(:id) + end + + 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 + + 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 + assert_equal [1,2,3,4], Topic.order(:id).pluck("topics.id") end end