test/cases/calculations_test.rb in ibm_db-1.0.2 vs test/cases/calculations_test.rb in ibm_db-1.0.5

- old
+ new

@@ -90,10 +90,18 @@ assert_nil c[1] assert_equal 105, c[6] assert_equal 60, c[2] end + 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 + 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)] @@ -154,28 +162,27 @@ assert_equal 1, c[companies(:first_firm)] assert_equal 2, c[companies(:rails_core)] assert_equal 1, c[companies(:first_client)] end - uses_mocha 'group_by_non_numeric_foreign_key_association' do - def test_should_group_by_association_with_non_numeric_foreign_key - ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}]) + 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]) + 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]) + 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) - assert_equal Firm, c.first.first.class - assert_equal 1, c.first.last - end + c = Account.count(:all, :group => :firm) + first_key = c.keys.first + assert_equal Firm, first_key.class + assert_equal 1, c[first_key] end 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) @@ -246,16 +253,29 @@ # :include is only valid on :count Company.send(:validate_calculation_options, :count, :include => true) end - assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) } - assert_raises(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) } + assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :sum, :foo => :bar) } + assert_raise(ArgumentError) { Company.send(:validate_calculation_options, :count, :foo => :bar) } end 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_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) + + assert_equal 1, Account.scoped(:select => "credit_limit").count(:conditions => ['credit_limit >= 50']) end def test_should_count_manual_select_with_include assert_equal 6, Account.count(:select => "DISTINCT accounts.id", :include => :firm) end