test/cases/calculations_test.rb in ibm_db-0.10.0 vs test/cases/calculations_test.rb in ibm_db-1.0.0

- old
+ new

@@ -1,8 +1,9 @@ require "cases/helper" require 'models/company' require 'models/topic' +require 'models/edge' Company.has_many :accounts class NumericData < ActiveRecord::Base self.table_name = 'numeric_data' @@ -15,12 +16,12 @@ assert_equal 318, Account.sum(:credit_limit) end def test_should_average_field value = Account.average(:credit_limit) - assert_kind_of Float, value - assert_in_delta 53.0, value, 0.001 + assert_kind_of BigDecimal, value + assert_equal BigDecimal.new('53.0'), value end def test_should_return_nil_as_average assert_nil NumericData.average(:bank_balance) end @@ -97,12 +98,18 @@ assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6') end def test_should_return_zero_if_sum_conditions_return_nothing assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2') + assert_equal 0, companies(:rails_core).companies.sum(:id, :conditions => '1 = 2') end + def test_sum_should_return_valid_values_for_decimals + NumericData.create(:bank_balance => 19.83) + assert_equal 19.83, NumericData.sum(:bank_balance) + 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] @@ -267,9 +274,54 @@ def test_should_sum_expression if current_adapter?(:IBM_DBAdapter) && !ActiveRecord::Base.connection.servertype.class.name.include?('::IBM_IDS') assert_equal 636, Account.sum("2 * credit_limit") else - assert_equal "636", Account.sum("2 * credit_limit") + assert_equal '636', Account.sum("2 * credit_limit") end end + + 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 + if Edge.connection.adapter_name == 'MySQL' + 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 + end