From 32089cbcc9ca3fb935f783e7a7ef2b60b7d43006 Mon Sep 17 00:00:00 2001 From: Wes Oldenbeuving Date: Wed, 5 Nov 2008 18:27:23 +0100 Subject: Ensure ActiveRecord::ConnectionPool.connected? handles undefined connections. [#936 state:resolved] Signed-off-by: Pratik Naik --- activerecord/test/cases/pooled_connections_test.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/pooled_connections_test.rb b/activerecord/test/cases/pooled_connections_test.rb index 078ca1d679..3e8c617a89 100644 --- a/activerecord/test/cases/pooled_connections_test.rb +++ b/activerecord/test/cases/pooled_connections_test.rb @@ -73,6 +73,14 @@ class PooledConnectionsTest < ActiveRecord::TestCase assert ActiveRecord::ConnectionAdapters::AbstractAdapter === conn conn_pool.checkin(conn) end + + def test_undefined_connection_returns_false + old_handler = ActiveRecord::Base.connection_handler + ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new + assert_equal false, ActiveRecord::Base.connected? + ensure + ActiveRecord::Base.connection_handler = old_handler + end end unless %w(FrontBase).include? ActiveRecord::Base.connection.adapter_name class AllowConcurrencyDeprecatedTest < ActiveRecord::TestCase -- cgit v1.2.3 From 0832bc63f4047d0ad0171c411460db23450213aa Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 6 Nov 2008 01:29:09 +0530 Subject: Make sure ActiveRecord::Base.connected? doesn't raise an exception for defined connections --- activerecord/test/cases/pooled_connections_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/pooled_connections_test.rb b/activerecord/test/cases/pooled_connections_test.rb index 3e8c617a89..36b45868b9 100644 --- a/activerecord/test/cases/pooled_connections_test.rb +++ b/activerecord/test/cases/pooled_connections_test.rb @@ -74,6 +74,11 @@ class PooledConnectionsTest < ActiveRecord::TestCase conn_pool.checkin(conn) end + def test_not_connected_defined_connection_reutnrs_false + ActiveRecord::Base.establish_connection(@connection) + assert ! ActiveRecord::Base.connected? + end + def test_undefined_connection_returns_false old_handler = ActiveRecord::Base.connection_handler ActiveRecord::Base.connection_handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new -- cgit v1.2.3 From 32a5cfcd7f8d14407f0c00ce2cdc82b1b568438e Mon Sep 17 00:00:00 2001 From: Tekin Suleyman Date: Thu, 6 Nov 2008 21:06:40 +0000 Subject: Added tests for HABTM associations with counter_sql Signed-off-by: Michael Koziarski [#1102 state:committed] --- .../has_and_belongs_to_many_associations_test.rb | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index 2949f1d304..b5bedf3704 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -68,6 +68,16 @@ class DeveloperWithSymbolsForKeys < ActiveRecord::Base :foreign_key => "developer_id" end +class DeveloperWithCounterSQL < ActiveRecord::Base + set_table_name 'developers' + has_and_belongs_to_many :projects, + :class_name => "DeveloperWithCounterSQL", + :join_table => "developers_projects", + :association_foreign_key => "project_id", + :foreign_key => "developer_id", + :counter_sql => 'SELECT COUNT(*) AS count_all FROM projects INNER JOIN developers_projects ON projects.id = developers_projects.project_id WHERE developers_projects.developer_id =#{id}' +end + class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase fixtures :accounts, :companies, :categories, :posts, :categories_posts, :developers, :projects, :developers_projects, :parrots, :pirates, :treasures, :price_estimates, :tags, :taggings @@ -739,6 +749,19 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_nothing_raised { david.projects.count(:all, :conditions => '1=1') } end + def test_count + david = Developer.find(1) + assert_equal 2, david.projects.count + end + + def test_count_with_counter_sql + developer = DeveloperWithCounterSQL.create(:name => 'tekin') + developer.project_ids = [projects(:active_record).id] + developer.save + developer.reload + assert_equal 1, developer.projects.count + end + uses_mocha 'mocking Post.transaction' do def test_association_proxy_transaction_method_starts_transaction_in_association_class Post.expects(:transaction) -- cgit v1.2.3 From b0ee1bdf2650d7a8380d4e9be58bba8d9c5bd40e Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 7 Nov 2008 15:40:56 -0500 Subject: Remove fixtures from Test::Unit::TestCase. Mix in AR::TestFixtures instead. --- activerecord/test/cases/helper.rb | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index f7bdac8013..13988d5392 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -5,7 +5,6 @@ require 'config' require 'test/unit' require 'active_record' -require 'active_record/fixtures' require 'active_record/test_case' require 'connection' @@ -48,15 +47,11 @@ class << ActiveRecord::Base end unless ENV['FIXTURE_DEBUG'] - module Test #:nodoc: - module Unit #:nodoc: - class << TestCase #:nodoc: - def try_to_load_dependency_with_silence(*args) - ActiveRecord::Base.logger.silence { try_to_load_dependency_without_silence(*args)} - end - - alias_method_chain :try_to_load_dependency, :silence - end + module ActiveRecord::TestFixtures::ClassMethods + def try_to_load_dependency_with_silence(*args) + ActiveRecord::Base.logger.silence { try_to_load_dependency_without_silence(*args)} end + + alias_method_chain :try_to_load_dependency, :silence end -end \ No newline at end of file +end -- cgit v1.2.3 From 1d803e51890e842f0c25ee3a016ed0311f2fa1b4 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 7 Nov 2008 16:22:56 -0500 Subject: Update AR tests --- activerecord/test/cases/fixtures_test.rb | 8 ++++---- activerecord/test/cases/validations_i18n_test.rb | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/fixtures_test.rb b/activerecord/test/cases/fixtures_test.rb index 6ba7597f56..ed2915b023 100644 --- a/activerecord/test/cases/fixtures_test.rb +++ b/activerecord/test/cases/fixtures_test.rb @@ -641,15 +641,15 @@ end class FixtureLoadingTest < ActiveRecord::TestCase uses_mocha 'reloading_fixtures_through_accessor_methods' do def test_logs_message_for_failed_dependency_load - Test::Unit::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError) + ActiveRecord::TestCase.expects(:require_dependency).with(:does_not_exist).raises(LoadError) ActiveRecord::Base.logger.expects(:warn) - Test::Unit::TestCase.try_to_load_dependency(:does_not_exist) + ActiveRecord::TestCase.try_to_load_dependency(:does_not_exist) end def test_does_not_logs_message_for_successful_dependency_load - Test::Unit::TestCase.expects(:require_dependency).with(:works_out_fine) + ActiveRecord::TestCase.expects(:require_dependency).with(:works_out_fine) ActiveRecord::Base.logger.expects(:warn).never - Test::Unit::TestCase.try_to_load_dependency(:works_out_fine) + ActiveRecord::TestCase.try_to_load_dependency(:works_out_fine) end end end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index 42246f18b6..b2df98ca0a 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -2,7 +2,7 @@ require "cases/helper" require 'models/topic' require 'models/reply' -class ActiveRecordValidationsI18nTests < Test::Unit::TestCase +class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase def setup reset_callbacks Topic @topic = Topic.new -- cgit v1.2.3 From dd77733f2fdb6dde2be7115fc31ad1dcbfccb5a1 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 8 Nov 2008 00:24:36 -0500 Subject: Timeout the connection pool monitor on ruby 1.8 only --- activerecord/test/cases/pooled_connections_test.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/pooled_connections_test.rb b/activerecord/test/cases/pooled_connections_test.rb index 36b45868b9..2a5e9509b3 100644 --- a/activerecord/test/cases/pooled_connections_test.rb +++ b/activerecord/test/cases/pooled_connections_test.rb @@ -28,10 +28,13 @@ class PooledConnectionsTest < ActiveRecord::TestCase end end - def test_pooled_connection_checkout - checkout_connections - assert_equal @connections.length, 2 - assert_equal @timed_out, 2 + # Will deadlock due to lack of Monitor timeouts in 1.9 + if RUBY_VERSION < '1.9' + def test_pooled_connection_checkout + checkout_connections + assert_equal @connections.length, 2 + assert_equal @timed_out, 2 + end end def checkout_checkin_connections(pool_size, threads) -- cgit v1.2.3 From 57d795bad43d4a3e5eef7151099a8e40808a1031 Mon Sep 17 00:00:00 2001 From: Ken Collins Date: Thu, 13 Nov 2008 10:45:57 -0600 Subject: Make sure any Fixnum returned by a DB sum is type cast to a Float before standard converstion to a BigDecimal [#8994 state:resolved] Signed-off-by: Joshua Peek --- activerecord/test/cases/calculations_test.rb | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 0fa61500c0..8bd0dd0f6e 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -25,6 +25,11 @@ class CalculationsTest < ActiveRecord::TestCase 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 + assert_equal 0, NumericData.send(:type_cast_calculated_value, 0, nil, 'avg') + assert_equal 53.0, NumericData.send(:type_cast_calculated_value, 53, nil, 'avg') + end def test_should_get_maximum_of_field assert_equal 60, Account.maximum(:credit_limit) -- cgit v1.2.3 From db7daa04b858f224b8b34a5dc94fe5804c1c6400 Mon Sep 17 00:00:00 2001 From: Amos King Date: Mon, 10 Nov 2008 09:26:19 -0600 Subject: Fix typo in pool_conections_test [#1350 state:committed] Signed-off-by: David Heinemeier Hansson --- activerecord/test/cases/pooled_connections_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/pooled_connections_test.rb b/activerecord/test/cases/pooled_connections_test.rb index 2a5e9509b3..2649a9358a 100644 --- a/activerecord/test/cases/pooled_connections_test.rb +++ b/activerecord/test/cases/pooled_connections_test.rb @@ -77,7 +77,7 @@ class PooledConnectionsTest < ActiveRecord::TestCase conn_pool.checkin(conn) end - def test_not_connected_defined_connection_reutnrs_false + def test_not_connected_defined_connection_returns_false ActiveRecord::Base.establish_connection(@connection) assert ! ActiveRecord::Base.connected? end -- cgit v1.2.3 From d3fd9971093101712e4cc97ccc534631888b673d Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Sat, 15 Nov 2008 01:59:12 -0500 Subject: fix assignment to has_one :through associations. Signed-off-by: Michael Koziarski --- .../has_one_through_associations_test.rb | 40 +++++++++++++++++++++- activerecord/test/fixtures/organizations.yml | 5 +++ activerecord/test/models/member.rb | 2 ++ activerecord/test/models/member_detail.rb | 4 +++ activerecord/test/models/organization.rb | 4 +++ activerecord/test/schema/schema.rb | 10 ++++++ 6 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 activerecord/test/fixtures/organizations.yml create mode 100644 activerecord/test/models/member_detail.rb create mode 100644 activerecord/test/models/organization.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_one_through_associations_test.rb b/activerecord/test/cases/associations/has_one_through_associations_test.rb index ff4021fe02..7d418de965 100644 --- a/activerecord/test/cases/associations/has_one_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_one_through_associations_test.rb @@ -3,9 +3,11 @@ require 'models/club' require 'models/member' require 'models/membership' require 'models/sponsor' +require 'models/organization' +require 'models/member_detail' class HasOneThroughAssociationsTest < ActiveRecord::TestCase - fixtures :members, :clubs, :memberships, :sponsors + fixtures :members, :clubs, :memberships, :sponsors, :organizations def setup @member = members(:groucho) @@ -120,4 +122,40 @@ class HasOneThroughAssociationsTest < ActiveRecord::TestCase clubs(:moustache_club).send(:private_method) @member.club.send(:private_method) end + + def test_assigning_to_has_one_through_preserves_decorated_join_record + @organization = organizations(:nsa) + assert_difference 'MemberDetail.count', 1 do + @member_detail = MemberDetail.new(:extra_data => 'Extra') + @member.member_detail = @member_detail + @member.organization = @organization + end + assert_equal @organization, @member.organization + assert @organization.members.include?(@member) + assert_equal 'Extra', @member.member_detail.extra_data + end + + def test_reassigning_has_one_through + @organization = organizations(:nsa) + @new_organization = organizations(:discordians) + + assert_difference 'MemberDetail.count', 1 do + @member_detail = MemberDetail.new(:extra_data => 'Extra') + @member.member_detail = @member_detail + @member.organization = @organization + end + assert_equal @organization, @member.organization + assert_equal 'Extra', @member.member_detail.extra_data + assert @organization.members.include?(@member) + assert !@new_organization.members.include?(@member) + + assert_no_difference 'MemberDetail.count' do + @member.organization = @new_organization + end + assert_equal @new_organization, @member.organization + assert_equal 'Extra', @member.member_detail.extra_data + assert !@organization.members.include?(@member) + assert @new_organization.members.include?(@member) + end + end diff --git a/activerecord/test/fixtures/organizations.yml b/activerecord/test/fixtures/organizations.yml new file mode 100644 index 0000000000..25295bff87 --- /dev/null +++ b/activerecord/test/fixtures/organizations.yml @@ -0,0 +1,5 @@ +nsa: + name: No Such Agency +discordians: + name: Discordians + diff --git a/activerecord/test/models/member.rb b/activerecord/test/models/member.rb index 688725f200..77a37abb38 100644 --- a/activerecord/test/models/member.rb +++ b/activerecord/test/models/member.rb @@ -6,4 +6,6 @@ class Member < ActiveRecord::Base has_one :favourite_club, :through => :memberships, :conditions => ["memberships.favourite = ?", true], :source => :club has_one :sponsor, :as => :sponsorable has_one :sponsor_club, :through => :sponsor + has_one :member_detail + has_one :organization, :through => :member_detail end \ No newline at end of file diff --git a/activerecord/test/models/member_detail.rb b/activerecord/test/models/member_detail.rb new file mode 100644 index 0000000000..e731454556 --- /dev/null +++ b/activerecord/test/models/member_detail.rb @@ -0,0 +1,4 @@ +class MemberDetail < ActiveRecord::Base + belongs_to :member + belongs_to :organization +end diff --git a/activerecord/test/models/organization.rb b/activerecord/test/models/organization.rb new file mode 100644 index 0000000000..d79d5037c8 --- /dev/null +++ b/activerecord/test/models/organization.rb @@ -0,0 +1,4 @@ +class Organization < ActiveRecord::Base + has_many :member_details + has_many :members, :through => :member_details +end \ No newline at end of file diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index ab5c7c520b..6217e3bc1c 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -197,6 +197,12 @@ ActiveRecord::Schema.define do t.string :name end + create_table :member_details, :force => true do |t| + t.integer :member_id + t.integer :organization_id + t.string :extra_data + end + create_table :memberships, :force => true do |t| t.datetime :joined_on t.integer :club_id, :member_id @@ -249,6 +255,10 @@ ActiveRecord::Schema.define do t.integer :shipping_customer_id end + create_table :organizations, :force => true do |t| + t.string :name + end + create_table :owners, :primary_key => :owner_id ,:force => true do |t| t.string :name end -- cgit v1.2.3 From 160b8a83444cd3784cba2396435ef06dba234122 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 15 Nov 2008 12:30:02 -0800 Subject: Set up fixtures for AR tests --- activerecord/test/cases/helper.rb | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 13988d5392..02b39d7480 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -6,6 +6,7 @@ require 'test/unit' require 'active_record' require 'active_record/test_case' +require 'active_record/fixtures' require 'connection' # Show backtraces for deprecated behavior for quicker cleanup. @@ -55,3 +56,10 @@ unless ENV['FIXTURE_DEBUG'] alias_method_chain :try_to_load_dependency, :silence end end + +class ActiveSupport::TestCase + include ActiveRecord::TestFixtures + self.fixture_path = FIXTURES_ROOT + self.use_instantiated_fixtures = false + self.use_transactional_fixtures = true +end -- cgit v1.2.3 From 5fe543b6294df44d0e7670b78347564874a2f5e2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sat, 15 Nov 2008 12:31:54 -0800 Subject: Add create_fixtures method for tests --- activerecord/test/cases/helper.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 02b39d7480..2382bfe4fe 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -62,4 +62,8 @@ class ActiveSupport::TestCase self.fixture_path = FIXTURES_ROOT self.use_instantiated_fixtures = false self.use_transactional_fixtures = true + + def create_fixtures(*table_names, &block) + Fixtures.create_fixtures(ActiveSupport::TestCase.fixture_path, table_names, {}, &block) + end end -- cgit v1.2.3 From ff594b2bc94ff2a942fe6ca05672387722dee686 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 16 Nov 2008 16:01:18 +0100 Subject: =?UTF-8?q?Added=20default=5Fscope=20to=20Base=20[#1381=20state:co?= =?UTF-8?q?mmitted]=20(Pawe=C5=82=20Kondzior)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- activerecord/test/cases/method_scoping_test.rb | 38 ++++++++++++++++++++++++++ activerecord/test/models/developer.rb | 12 ++++++++ 2 files changed, 50 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index ff10bfaf3e..79b24cd4fd 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,6 +522,44 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end +class DefaultScopingTest < ActiveRecord::TestCase + fixtures :developers + + def test_default_scope + expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_method_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_nested_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_nested_exclusive_scope + expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_overwriting_default_scope + expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } + assert_equal expected, received + end +end + =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index c08476f728..1844014011 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,3 +77,15 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end + +class DeveloperOrderedBySalary < ActiveRecord::Base + self.table_name = 'developers' + default_scope :order => "salary DESC" + + def self.all_ordered_by_name + with_scope(:find => { :order => "name DESC" }) do + find(:all) + end + end + +end -- cgit v1.2.3 From ca23287b448c2e007a5c93e43e762a10e0007b7a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 16 Nov 2008 16:35:52 +0100 Subject: =?UTF-8?q?Revert=20"Added=20default=5Fscope=20to=20Base=20[#1381?= =?UTF-8?q?=20state:committed]=20(Pawe=C5=82=20Kondzior)"=20--=20won't=20g?= =?UTF-8?q?el=20with=20threads.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit ff594b2bc94ff2a942fe6ca05672387722dee686. --- activerecord/test/cases/method_scoping_test.rb | 38 -------------------------- activerecord/test/models/developer.rb | 12 -------- 2 files changed, 50 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 79b24cd4fd..ff10bfaf3e 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,44 +522,6 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end -class DefaultScopingTest < ActiveRecord::TestCase - fixtures :developers - - def test_default_scope - expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } - assert_equal expected, received - end - - def test_method_scope - expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } - assert_equal expected, received - end - - def test_nested_scope - expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do - DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } - end - assert_equal expected, received - end - - def test_nested_exclusive_scope - expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do - DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } - end - assert_equal expected, received - end - - def test_overwriting_default_scope - expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary } - received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } - assert_equal expected, received - end -end - =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 1844014011..c08476f728 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,15 +77,3 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end - -class DeveloperOrderedBySalary < ActiveRecord::Base - self.table_name = 'developers' - default_scope :order => "salary DESC" - - def self.all_ordered_by_name - with_scope(:find => { :order => "name DESC" }) do - find(:all) - end - end - -end -- cgit v1.2.3 From 2530d0eea8eaecd2c61f99225f050ff47973e9a0 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sun, 16 Nov 2008 23:36:41 +0530 Subject: =?UTF-8?q?Added=20default=5Fscope=20to=20Base=20[#1381=20state:co?= =?UTF-8?q?mmitted]=20(Pawe=C5=82=20Kondzior)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- activerecord/test/cases/method_scoping_test.rb | 61 ++++++++++++++++++++++++++ activerecord/test/models/developer.rb | 12 +++++ 2 files changed, 73 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index ff10bfaf3e..4ac0018144 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -522,6 +522,67 @@ class HasAndBelongsToManyScopingTest< ActiveRecord::TestCase end +class DefaultScopingTest < ActiveRecord::TestCase + fixtures :developers + + def test_default_scope + expected = Developer.find(:all, :order => 'salary DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_default_scoping_with_threads + scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + + 2.times do + Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join + end + end + + def test_default_scoping_with_inheritance + scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + + # Inherit a class having a default scope and define a new default scope + klass = Class.new(DeveloperOrderedBySalary) + klass.send :default_scope, {} + + # Scopes added on children should append to parent scope + expected_klass_scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}, {:create=>nil, :find=>{}}] + assert_equal expected_klass_scope, klass.send(:scoped_methods) + + # Parent should still have the original scope + assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) + end + + def test_method_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.all_ordered_by_name.collect { |dev| dev.salary } + assert_equal expected, received + end + + def test_nested_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_scope(:find => { :order => 'name DESC'}) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_nested_exclusive_scope + expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do + DeveloperOrderedBySalary.find(:all).collect { |dev| dev.salary } + end + assert_equal expected, received + end + + def test_overwriting_default_scope + expected = Developer.find(:all, :order => 'salary').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.find(:all, :order => 'salary').collect { |dev| dev.salary } + assert_equal expected, received + end +end + =begin # We disabled the scoping for has_one and belongs_to as we can't think of a proper use case diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index c08476f728..0c20f97502 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -77,3 +77,15 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base raise if projects.empty? end end + +class DeveloperOrderedBySalary < ActiveRecord::Base + self.table_name = 'developers' + default_scope :order => "salary DESC" + + def self.all_ordered_by_name + with_scope(:find => { :order => "name DESC" }) do + find(:all) + end + end + +end -- cgit v1.2.3 From 32cb2345a54b9ab38461b2d4bb0dbd1706f2800e Mon Sep 17 00:00:00 2001 From: Tom Stuart Date: Mon, 17 Nov 2008 20:10:59 +0000 Subject: Fix default_scope to work in combination with named scopes Signed-off-by: David Heinemeier Hansson --- activerecord/test/cases/method_scoping_test.rb | 12 +++++++++--- activerecord/test/models/developer.rb | 14 +++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/method_scoping_test.rb b/activerecord/test/cases/method_scoping_test.rb index 4ac0018144..6372b4f6aa 100644 --- a/activerecord/test/cases/method_scoping_test.rb +++ b/activerecord/test/cases/method_scoping_test.rb @@ -532,7 +532,7 @@ class DefaultScopingTest < ActiveRecord::TestCase end def test_default_scoping_with_threads - scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + scope = [{ :create => {}, :find => { :order => 'salary DESC' } }] 2.times do Thread.new { assert_equal scope, DeveloperOrderedBySalary.send(:scoped_methods) }.join @@ -540,14 +540,14 @@ class DefaultScopingTest < ActiveRecord::TestCase end def test_default_scoping_with_inheritance - scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}] + scope = [{ :create => {}, :find => { :order => 'salary DESC' } }] # Inherit a class having a default scope and define a new default scope klass = Class.new(DeveloperOrderedBySalary) klass.send :default_scope, {} # Scopes added on children should append to parent scope - expected_klass_scope = [{:create=>nil, :find=>{:order=>"salary DESC"}}, {:create=>nil, :find=>{}}] + expected_klass_scope = [{ :create => {}, :find => { :order => 'salary DESC' }}, { :create => {}, :find => {} }] assert_equal expected_klass_scope, klass.send(:scoped_methods) # Parent should still have the original scope @@ -568,6 +568,12 @@ class DefaultScopingTest < ActiveRecord::TestCase assert_equal expected, received end + def test_named_scope + expected = Developer.find(:all, :order => 'name DESC').collect { |dev| dev.salary } + received = DeveloperOrderedBySalary.by_name.find(:all).collect { |dev| dev.salary } + assert_equal expected, received + end + def test_nested_exclusive_scope expected = Developer.find(:all, :limit => 100).collect { |dev| dev.salary } received = DeveloperOrderedBySalary.with_exclusive_scope(:find => { :limit => 100 }) do diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 0c20f97502..92039a4f54 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -79,13 +79,13 @@ class DeveloperWithBeforeDestroyRaise < ActiveRecord::Base end class DeveloperOrderedBySalary < ActiveRecord::Base - self.table_name = 'developers' - default_scope :order => "salary DESC" + self.table_name = 'developers' + default_scope :order => 'salary DESC' + named_scope :by_name, :order => 'name DESC' - def self.all_ordered_by_name - with_scope(:find => { :order => "name DESC" }) do - find(:all) - end + def self.all_ordered_by_name + with_scope(:find => { :order => 'name DESC' }) do + find(:all) end - + end end -- cgit v1.2.3 From 12118963acacc9c869bdd41ef8480a1a4e06d358 Mon Sep 17 00:00:00 2001 From: Sven Fuchs Date: Tue, 18 Nov 2008 09:59:46 +0100 Subject: use :en as a default locale (in favor of :en-US) Signed-off-by: David Heinemeier Hansson --- activerecord/test/cases/i18n_test.rb | 12 +-- activerecord/test/cases/validations_i18n_test.rb | 96 ++++++++++++------------ 2 files changed, 54 insertions(+), 54 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/i18n_test.rb b/activerecord/test/cases/i18n_test.rb index ea06e377e3..b1db662eca 100644 --- a/activerecord/test/cases/i18n_test.rb +++ b/activerecord/test/cases/i18n_test.rb @@ -9,32 +9,32 @@ class ActiveRecordI18nTests < Test::Unit::TestCase end def test_translated_model_attributes - I18n.backend.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } + I18n.backend.store_translations 'en', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } assert_equal 'topic title attribute', Topic.human_attribute_name('title') end def test_translated_model_attributes_with_sti - I18n.backend.store_translations 'en-US', :activerecord => {:attributes => {:reply => {:title => 'reply title attribute'} } } + I18n.backend.store_translations 'en', :activerecord => {:attributes => {:reply => {:title => 'reply title attribute'} } } assert_equal 'reply title attribute', Reply.human_attribute_name('title') end def test_translated_model_attributes_with_sti_fallback - I18n.backend.store_translations 'en-US', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } + I18n.backend.store_translations 'en', :activerecord => {:attributes => {:topic => {:title => 'topic title attribute'} } } assert_equal 'topic title attribute', Reply.human_attribute_name('title') end def test_translated_model_names - I18n.backend.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} } + I18n.backend.store_translations 'en', :activerecord => {:models => {:topic => 'topic model'} } assert_equal 'topic model', Topic.human_name end def test_translated_model_names_with_sti - I18n.backend.store_translations 'en-US', :activerecord => {:models => {:reply => 'reply model'} } + I18n.backend.store_translations 'en', :activerecord => {:models => {:reply => 'reply model'} } assert_equal 'reply model', Reply.human_name end def test_translated_model_names_with_sti_fallback - I18n.backend.store_translations 'en-US', :activerecord => {:models => {:topic => 'topic model'} } + I18n.backend.store_translations 'en', :activerecord => {:models => {:topic => 'topic model'} } assert_equal 'topic model', Reply.human_name end end diff --git a/activerecord/test/cases/validations_i18n_test.rb b/activerecord/test/cases/validations_i18n_test.rb index b2df98ca0a..f59e3f7001 100644 --- a/activerecord/test/cases/validations_i18n_test.rb +++ b/activerecord/test/cases/validations_i18n_test.rb @@ -9,7 +9,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase @old_load_path, @old_backend = I18n.load_path, I18n.backend I18n.load_path.clear I18n.backend = I18n::Backend::Simple.new - I18n.backend.store_translations('en-US', :activerecord => {:errors => {:messages => {:custom => nil}}}) + I18n.backend.store_translations('en', :activerecord => {:errors => {:messages => {:custom => nil}}}) end def teardown @@ -165,7 +165,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase def test_errors_full_messages_translates_human_attribute_name_for_model_attributes @topic.errors.instance_variable_set :@errors, { 'title' => ['empty'] } I18n.expects(:translate).with(:"topic.title", :default => ['Title'], :scope => [:activerecord, :attributes], :count => 1).returns('Title') - @topic.errors.full_messages :locale => 'en-US' + @topic.errors.full_messages :locale => 'en' end end @@ -429,8 +429,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_confirmation_of w/o mocha def test_validates_confirmation_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:confirmation => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:confirmation => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} Topic.validates_confirmation_of :title @topic.title_confirmation = 'foo' @@ -439,7 +439,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_confirmation_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}} Topic.validates_confirmation_of :title @topic.title_confirmation = 'foo' @@ -450,8 +450,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_acceptance_of w/o mocha def test_validates_acceptance_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:accepted => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:accepted => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} Topic.validates_acceptance_of :title, :allow_nil => false @topic.valid? @@ -459,7 +459,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_acceptance_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}} Topic.validates_acceptance_of :title, :allow_nil => false @topic.valid? @@ -469,8 +469,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_presence_of w/o mocha def test_validates_presence_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:blank => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:blank => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} Topic.validates_presence_of :title @topic.valid? @@ -478,7 +478,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_presence_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:blank => 'global message'}}} Topic.validates_presence_of :title @topic.valid? @@ -488,8 +488,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_length_of :within w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:too_short => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:too_short => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} Topic.validates_length_of :title, :within => 3..5 @topic.valid? @@ -497,7 +497,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}} Topic.validates_length_of :title, :within => 3..5 @topic.valid? @@ -507,8 +507,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_length_of :is w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -516,7 +516,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -526,8 +526,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_uniqueness_of w/o mocha def test_validates_length_of_within_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -535,7 +535,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_length_of_within_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}} Topic.validates_length_of :title, :is => 5 @topic.valid? @@ -546,8 +546,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_format_of w/o mocha def test_validates_format_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:invalid => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:invalid => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/ @topic.valid? @@ -555,7 +555,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_format_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/ @topic.valid? @@ -565,8 +565,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_inclusion_of w/o mocha def test_validates_inclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:inclusion => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:inclusion => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} Topic.validates_inclusion_of :title, :in => %w(a b c) @topic.valid? @@ -574,7 +574,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_inclusion_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}} Topic.validates_inclusion_of :title, :in => %w(a b c) @topic.valid? @@ -584,8 +584,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_exclusion_of w/o mocha def test_validates_exclusion_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:exclusion => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:exclusion => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} Topic.validates_exclusion_of :title, :in => %w(a b c) @topic.title = 'a' @@ -594,7 +594,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_exclusion_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}} Topic.validates_exclusion_of :title, :in => %w(a b c) @topic.title = 'a' @@ -605,8 +605,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_numericality_of without :only_integer w/o mocha def test_validates_numericality_of_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title @topic.title = 'a' @@ -615,7 +615,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_numericality_of_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true @topic.title = 'a' @@ -626,8 +626,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_numericality_of with :only_integer w/o mocha def test_validates_numericality_of_only_integer_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true @topic.title = 'a' @@ -636,7 +636,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_numericality_of_only_integer_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true @topic.title = 'a' @@ -647,8 +647,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_numericality_of :odd w/o mocha def test_validates_numericality_of_odd_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:odd => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:odd => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :odd => true @topic.title = 0 @@ -657,7 +657,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_numericality_of_odd_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:odd => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :odd => true @topic.title = 0 @@ -668,8 +668,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_numericality_of :less_than w/o mocha def test_validates_numericality_of_less_than_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:less_than => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:less_than => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0 @topic.title = 1 @@ -678,7 +678,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_numericality_of_less_than_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}} Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0 @topic.title = 1 @@ -690,8 +690,8 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase # validates_associated w/o mocha def test_validates_associated_finds_custom_model_key_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}} - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_associated :replies replied_topic.valid? @@ -699,7 +699,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_associated_finds_global_default_translation - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}} Topic.validates_associated :replies replied_topic.valid? @@ -707,7 +707,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validations_with_message_symbol_must_translate - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:messages => {:custom_error => "I am a custom error"}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:custom_error => "I am a custom error"}}} Topic.validates_presence_of :title, :message => :custom_error @topic.title = nil @topic.valid? @@ -715,7 +715,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_with_message_symbol_must_translate_per_attribute - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}} Topic.validates_presence_of :title, :message => :custom_error @topic.title = nil @topic.valid? @@ -723,7 +723,7 @@ class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase end def test_validates_with_message_symbol_must_translate_per_model - I18n.backend.store_translations 'en-US', :activerecord => {:errors => {:models => {:topic => {:custom_error => "I am a custom error"}}}} + I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:custom_error => "I am a custom error"}}}} Topic.validates_presence_of :title, :message => :custom_error @topic.title = nil @topic.valid? @@ -743,7 +743,7 @@ class ActiveRecordValidationsGenerateMessageI18nTests < Test::Unit::TestCase def setup reset_callbacks Topic @topic = Topic.new - I18n.backend.store_translations :'en-US', { + I18n.backend.store_translations :'en', { :activerecord => { :errors => { :messages => { -- cgit v1.2.3 From 8e4624be9e55cd0865b307be70a1e6287ec033ca Mon Sep 17 00:00:00 2001 From: Ken Collins Date: Wed, 19 Nov 2008 11:09:44 -0500 Subject: Remove SQL Server cases from tests for latest adapter work to pass rails expected behavior. Signed-off-by: Michael Koziarski --- activerecord/test/cases/adapter_test_sqlserver.rb | 95 ---------------------- activerecord/test/cases/associations/eager_test.rb | 2 +- activerecord/test/cases/base_test.rb | 13 ++- activerecord/test/cases/binary_test.rb | 6 +- activerecord/test/cases/defaults_test.rb | 2 +- activerecord/test/cases/inheritance_test.rb | 4 +- activerecord/test/cases/locking_test.rb | 4 +- activerecord/test/cases/migration_test.rb | 10 +-- .../test/cases/table_name_test_sqlserver.rb | 23 ------ .../test/schema/sqlserver_specific_schema.rb | 5 -- 10 files changed, 15 insertions(+), 149 deletions(-) delete mode 100644 activerecord/test/cases/adapter_test_sqlserver.rb delete mode 100644 activerecord/test/cases/table_name_test_sqlserver.rb delete mode 100644 activerecord/test/schema/sqlserver_specific_schema.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/adapter_test_sqlserver.rb b/activerecord/test/cases/adapter_test_sqlserver.rb deleted file mode 100644 index ea270fb7ee..0000000000 --- a/activerecord/test/cases/adapter_test_sqlserver.rb +++ /dev/null @@ -1,95 +0,0 @@ -require "cases/helper" -require 'models/default' -require 'models/post' -require 'models/task' - -class SqlServerAdapterTest < ActiveRecord::TestCase - class TableWithRealColumn < ActiveRecord::Base; end - - fixtures :posts, :tasks - - def setup - @connection = ActiveRecord::Base.connection - end - - def teardown - @connection.execute("SET LANGUAGE us_english") rescue nil - end - - def test_real_column_has_float_type - assert_equal :float, TableWithRealColumn.columns_hash["real_number"].type - end - - # SQL Server 2000 has a bug where some unambiguous date formats are not - # correctly identified if the session language is set to german - def test_date_insertion_when_language_is_german - @connection.execute("SET LANGUAGE deutsch") - - assert_nothing_raised do - Task.create(:starting => Time.utc(2000, 1, 31, 5, 42, 0), :ending => Date.new(2006, 12, 31)) - end - end - - def test_indexes_with_descending_order - # Make sure we have an index with descending order - @connection.execute "CREATE INDEX idx_credit_limit ON accounts (credit_limit DESC)" rescue nil - assert_equal ["credit_limit"], @connection.indexes('accounts').first.columns - ensure - @connection.execute "DROP INDEX accounts.idx_credit_limit" - end - - def test_execute_without_block_closes_statement - assert_all_statements_used_are_closed do - @connection.execute("SELECT 1") - end - end - - def test_execute_with_block_closes_statement - assert_all_statements_used_are_closed do - @connection.execute("SELECT 1") do |sth| - assert !sth.finished?, "Statement should still be alive within block" - end - end - end - - def test_insert_with_identity_closes_statement - assert_all_statements_used_are_closed do - @connection.insert("INSERT INTO accounts ([id], [firm_id],[credit_limit]) values (999, 1, 50)") - end - end - - def test_insert_without_identity_closes_statement - assert_all_statements_used_are_closed do - @connection.insert("INSERT INTO accounts ([firm_id],[credit_limit]) values (1, 50)") - end - end - - def test_active_closes_statement - assert_all_statements_used_are_closed do - @connection.active? - end - end - - def assert_all_statements_used_are_closed(&block) - existing_handles = [] - ObjectSpace.each_object(DBI::StatementHandle) {|handle| existing_handles << handle} - GC.disable - - yield - - used_handles = [] - ObjectSpace.each_object(DBI::StatementHandle) {|handle| used_handles << handle unless existing_handles.include? handle} - - assert_block "No statements were used within given block" do - used_handles.size > 0 - end - - ObjectSpace.each_object(DBI::StatementHandle) do |handle| - assert_block "Statement should have been closed within given block" do - handle.finished? - end - end - ensure - GC.enable - end -end diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 5f43975d5a..a4f1f65f9a 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -667,7 +667,7 @@ class EagerAssociationTest < ActiveRecord::TestCase end def test_count_with_include - if current_adapter?(:SQLServerAdapter, :SybaseAdapter) + if current_adapter?(:SybaseAdapter) assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "len(comments.body) > 15") elsif current_adapter?(:OpenBaseAdapter) assert_equal 3, authors(:david).posts_with_comments.count(:conditions => "length(FETCHBLOB(comments.body)) > 15") diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index da9f2742d8..5f54931d00 100755 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -428,9 +428,6 @@ class BasicsTest < ActiveRecord::TestCase end def test_preserving_date_objects - # SQL Server doesn't have a separate column type just for dates, so all are returned as time - return true if current_adapter?(:SQLServerAdapter) - if current_adapter?(:SybaseAdapter, :OracleAdapter) # Sybase ctlib does not (yet?) support the date type; use datetime instead. # Oracle treats all dates/times as Time. @@ -777,8 +774,8 @@ class BasicsTest < ActiveRecord::TestCase end end - # Oracle, SQLServer, and Sybase do not have a TIME datatype. - unless current_adapter?(:SQLServerAdapter, :OracleAdapter, :SybaseAdapter) + # Oracle, and Sybase do not have a TIME datatype. + unless current_adapter?(:OracleAdapter, :SybaseAdapter) def test_utc_as_time_zone Topic.default_timezone = :utc attributes = { "bonus_time" => "5:42:00AM" } @@ -1157,8 +1154,8 @@ class BasicsTest < ActiveRecord::TestCase end def test_attributes_on_dummy_time - # Oracle, SQL Server, and Sybase do not have a TIME datatype. - return true if current_adapter?(:SQLServerAdapter, :OracleAdapter, :SybaseAdapter) + # Oracle, and Sybase do not have a TIME datatype. + return true if current_adapter?(:OracleAdapter, :SybaseAdapter) attributes = { "bonus_time" => "5:42:00AM" @@ -1874,7 +1871,7 @@ class BasicsTest < ActiveRecord::TestCase assert_equal "integer", xml.elements["//parent-id"].attributes['type'] assert_equal "true", xml.elements["//parent-id"].attributes['nil'] - if current_adapter?(:SybaseAdapter, :SQLServerAdapter, :OracleAdapter) + if current_adapter?(:SybaseAdapter, :OracleAdapter) assert_equal last_read_in_current_timezone, xml.elements["//last-read"].text assert_equal "datetime" , xml.elements["//last-read"].attributes['type'] else diff --git a/activerecord/test/cases/binary_test.rb b/activerecord/test/cases/binary_test.rb index 7131532c05..8545ba97cc 100644 --- a/activerecord/test/cases/binary_test.rb +++ b/activerecord/test/cases/binary_test.rb @@ -1,13 +1,9 @@ require "cases/helper" -# Without using prepared statements, it makes no sense to test -# BLOB data with SQL Server, because the length of a statement is -# limited to 8KB. -# # Without using prepared statements, it makes no sense to test # BLOB data with DB2 or Firebird, because the length of a statement # is limited to 32KB. -unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :DB2Adapter, :FirebirdAdapter) +unless current_adapter?(:SybaseAdapter, :DB2Adapter, :FirebirdAdapter) require 'models/binary' class BinaryTest < ActiveRecord::TestCase diff --git a/activerecord/test/cases/defaults_test.rb b/activerecord/test/cases/defaults_test.rb index ee84cb8af8..38e4853dc0 100644 --- a/activerecord/test/cases/defaults_test.rb +++ b/activerecord/test/cases/defaults_test.rb @@ -78,7 +78,7 @@ class DefaultTest < ActiveRecord::TestCase end end - if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter) + if current_adapter?(:PostgreSQLAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter) def test_default_integers default = Default.new assert_instance_of Fixnum, default.positive_integer diff --git a/activerecord/test/cases/inheritance_test.rb b/activerecord/test/cases/inheritance_test.rb index a39415618d..3f59eb9706 100644 --- a/activerecord/test/cases/inheritance_test.rb +++ b/activerecord/test/cases/inheritance_test.rb @@ -59,13 +59,13 @@ class InheritanceTest < ActiveRecord::TestCase def test_a_bad_type_column #SQLServer need to turn Identity Insert On before manually inserting into the Identity column - if current_adapter?(:SQLServerAdapter, :SybaseAdapter) + if current_adapter?(:SybaseAdapter) Company.connection.execute "SET IDENTITY_INSERT companies ON" end Company.connection.insert "INSERT INTO companies (id, #{QUOTED_TYPE}, name) VALUES(100, 'bad_class!', 'Not happening')" #We then need to turn it back Off before continuing. - if current_adapter?(:SQLServerAdapter, :SybaseAdapter) + if current_adapter?(:SybaseAdapter) Company.connection.execute "SET IDENTITY_INSERT companies OFF" end assert_raises(ActiveRecord::SubclassNotFound) { Company.find(100) } diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index 0a14b1d906..077cac7747 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -200,9 +200,9 @@ end # blocks, so separate script called by Kernel#system is needed. # (See exec vs. async_exec in the PostgreSQL adapter.) -# TODO: The SQL Server, Sybase, and OpenBase adapters currently have no support for pessimistic locking +# TODO: The Sybase, and OpenBase adapters currently have no support for pessimistic locking -unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :OpenBaseAdapter) +unless current_adapter?(:SybaseAdapter, :OpenBaseAdapter) class PessimisticLockingTest < ActiveRecord::TestCase self.use_transactional_fixtures = false fixtures :people, :readers diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index ac44dd7ffe..2ec3d40332 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -271,9 +271,9 @@ if ActiveRecord::Base.connection.supports_migrations? Person.connection.drop_table table_name rescue nil end - # SQL Server, Sybase, and SQLite3 will not allow you to add a NOT NULL + # Sybase, and SQLite3 will not allow you to add a NOT NULL # column to a table without a default value. - unless current_adapter?(:SQLServerAdapter, :SybaseAdapter, :SQLiteAdapter) + unless current_adapter?(:SybaseAdapter, :SQLiteAdapter) def test_add_column_not_null_without_default Person.connection.create_table :testings do |t| t.column :foo, :string @@ -410,7 +410,7 @@ if ActiveRecord::Base.connection.supports_migrations? assert_equal Fixnum, bob.age.class assert_equal Time, bob.birthday.class - if current_adapter?(:SQLServerAdapter, :OracleAdapter, :SybaseAdapter) + if current_adapter?(:OracleAdapter, :SybaseAdapter) # Sybase, and Oracle don't differentiate between date/time assert_equal Time, bob.favorite_day.class else @@ -851,10 +851,6 @@ if ActiveRecord::Base.connection.supports_migrations? # - SQLite3 stores a float, in violation of SQL assert_kind_of BigDecimal, b.value_of_e assert_equal BigDecimal("2.71828182845905"), b.value_of_e - elsif current_adapter?(:SQLServer) - # - SQL Server rounds instead of truncating - assert_kind_of Fixnum, b.value_of_e - assert_equal 3, b.value_of_e else # - SQL standard is an integer assert_kind_of Fixnum, b.value_of_e diff --git a/activerecord/test/cases/table_name_test_sqlserver.rb b/activerecord/test/cases/table_name_test_sqlserver.rb deleted file mode 100644 index fbf38a130e..0000000000 --- a/activerecord/test/cases/table_name_test_sqlserver.rb +++ /dev/null @@ -1,23 +0,0 @@ -require "cases/helper" -require 'active_record/schema' - -if ActiveRecord::Base.connection.supports_migrations? - class Order < ActiveRecord::Base - self.table_name = '[order]' - end - - class TableNameTest < ActiveRecord::TestCase - self.use_transactional_fixtures = false - - # Ensures Model.columns works when using SQLServer escape characters. - # Enables legacy schemas using SQL reserved words as table names. - # Should work with table names with spaces as well ('table name'). - def test_escaped_table_name - assert_nothing_raised do - ActiveRecord::Base.connection.select_all 'SELECT * FROM [order]' - end - assert_equal '[order]', Order.table_name - assert_equal 5, Order.columns.length - end - end -end diff --git a/activerecord/test/schema/sqlserver_specific_schema.rb b/activerecord/test/schema/sqlserver_specific_schema.rb deleted file mode 100644 index cd8aca2fe5..0000000000 --- a/activerecord/test/schema/sqlserver_specific_schema.rb +++ /dev/null @@ -1,5 +0,0 @@ -ActiveRecord::Schema.define do - create_table :table_with_real_columns, :force => true do |t| - t.column :real_number, :real - end -end \ No newline at end of file -- cgit v1.2.3 From 7254d23764f7abe8023f3daeb07d99ea1c8e777a Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Mon, 24 Nov 2008 11:14:24 -0600 Subject: Autoload ActiveRecord files --- activerecord/test/cases/ar_schema_test.rb | 1 - activerecord/test/cases/schema_dumper_test.rb | 1 - 2 files changed, 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/ar_schema_test.rb b/activerecord/test/cases/ar_schema_test.rb index 431dc7a141..4c1589d965 100644 --- a/activerecord/test/cases/ar_schema_test.rb +++ b/activerecord/test/cases/ar_schema_test.rb @@ -1,5 +1,4 @@ require "cases/helper" -require 'active_record/schema' if ActiveRecord::Base.connection.supports_migrations? diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index ee7e285a73..17e4c755ce 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -1,5 +1,4 @@ require "cases/helper" -require 'active_record/schema_dumper' require 'stringio' -- cgit v1.2.3 From 9a4d557713acb0fc8e80f61af18094034aca029a Mon Sep 17 00:00:00 2001 From: Paul Date: Wed, 26 Nov 2008 15:21:12 +0100 Subject: Ensure hash conditions on referenced tables are considered when eager loading with limit/offset. [#1404 state:resolved] Signed-off-by: Pratik Naik --- activerecord/test/cases/associations/eager_test.rb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index a4f1f65f9a..3c8408d14b 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -385,12 +385,28 @@ class EagerAssociationTest < ActiveRecord::TestCase assert_equal count, posts.size end - def test_eager_with_has_many_and_limit_ond_high_offset + def test_eager_with_has_many_and_limit_and_high_offset posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ]) assert_equal 0, posts.size end - def test_count_eager_with_has_many_and_limit_ond_high_offset + def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_array_conditions + assert_queries(1) do + posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, + :conditions => [ "authors.name = ? and comments.body = ?", 'David', 'go crazy' ]) + assert_equal 0, posts.size + end + end + + def test_eager_with_has_many_and_limit_and_high_offset_and_multiple_hash_conditions + assert_queries(1) do + posts = Post.find(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, + :conditions => { 'authors.name' => 'David', 'comments.body' => 'go crazy' }) + assert_equal 0, posts.size + end + end + + def test_count_eager_with_has_many_and_limit_and_high_offset posts = Post.count(:all, :include => [ :author, :comments ], :limit => 2, :offset => 10, :conditions => [ "authors.name = ?", 'David' ]) assert_equal 0, posts end -- cgit v1.2.3 From 97403ad5fdfcdfb2110c6f8fd0ebf43b7afc4859 Mon Sep 17 00:00:00 2001 From: miloops Date: Fri, 21 Nov 2008 19:20:33 -0300 Subject: Add :having option to find, to use in combination with grouped finds. Also added to has_many and has_and_belongs_to_many associations. Signed-off-by: Michael Koziarski [#1028 state:committed] --- .../associations/has_and_belongs_to_many_associations_test.rb | 5 +++++ activerecord/test/cases/associations/has_many_associations_test.rb | 5 +++++ activerecord/test/cases/finder_test.rb | 7 +++++++ activerecord/test/models/author.rb | 1 + activerecord/test/models/category.rb | 1 + activerecord/test/models/project.rb | 1 + 6 files changed, 20 insertions(+) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb index b5bedf3704..2f08e09d43 100644 --- a/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb @@ -658,6 +658,11 @@ class HasAndBelongsToManyAssociationsTest < ActiveRecord::TestCase assert_equal 1, categories(:technology).posts_gruoped_by_title.size end + def test_find_scoped_grouped_having + assert_equal 2, projects(:active_record).well_payed_salary_groups.size + assert projects(:active_record).well_payed_salary_groups.all? { |g| g.salary > 10000 } + end + def test_get_ids assert_equal projects(:active_record, :action_controller).map(&:id).sort, developers(:david).project_ids.sort assert_equal [projects(:active_record).id], developers(:jamis).project_ids diff --git a/activerecord/test/cases/associations/has_many_associations_test.rb b/activerecord/test/cases/associations/has_many_associations_test.rb index 59784e1bcb..816ceb6855 100644 --- a/activerecord/test/cases/associations/has_many_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_associations_test.rb @@ -255,6 +255,11 @@ class HasManyAssociationsTest < ActiveRecord::TestCase assert_equal 2, companies(:first_firm).clients_grouped_by_name.length end + def test_find_scoped_grouped_having + assert_equal 1, authors(:david).popular_grouped_posts.length + assert_equal 0, authors(:mary).popular_grouped_posts.length + end + def test_adding force_signal37_to_load_all_clients_of_firm natural = Client.new("name" => "Natural Company") diff --git a/activerecord/test/cases/finder_test.rb b/activerecord/test/cases/finder_test.rb index 153880afbd..d4d770b04e 100644 --- a/activerecord/test/cases/finder_test.rb +++ b/activerecord/test/cases/finder_test.rb @@ -175,6 +175,13 @@ class FinderTest < ActiveRecord::TestCase assert_equal 4, developers.map(&:salary).uniq.size end + def test_find_with_group_and_having + developers = Developer.find(:all, :group => "salary", :having => "sum(salary) > 10000", :select => "salary") + assert_equal 3, developers.size + assert_equal 3, developers.map(&:salary).uniq.size + assert developers.all? { |developer| developer.salary > 10000 } + end + def test_find_with_entire_select_statement topics = Topic.find_by_sql "SELECT * FROM topics WHERE author_name = 'Mary'" diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index e5b19ff9e4..4ffac4fe32 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -1,6 +1,7 @@ class Author < ActiveRecord::Base has_many :posts has_many :posts_with_comments, :include => :comments, :class_name => "Post" + has_many :popular_grouped_posts, :include => :comments, :class_name => "Post", :group => "type", :having => "SUM(comments_count) > 1", :select => "type" has_many :posts_with_comments_sorted_by_comment_id, :include => :comments, :class_name => "Post", :order => 'comments.id' has_many :posts_sorted_by_id_limited, :class_name => "Post", :order => 'posts.id', :limit => 1 has_many :posts_with_categories, :include => :categories, :class_name => "Post" diff --git a/activerecord/test/models/category.rb b/activerecord/test/models/category.rb index 4e9d247a4e..5efce6aaa6 100644 --- a/activerecord/test/models/category.rb +++ b/activerecord/test/models/category.rb @@ -14,6 +14,7 @@ class Category < ActiveRecord::Base :class_name => 'Post', :conditions => { :title => 'Yet Another Testing Title' } + has_and_belongs_to_many :popular_grouped_posts, :class_name => "Post", :group => "posts.type", :having => "sum(comments.post_id) > 2", :include => :comments has_and_belongs_to_many :posts_gruoped_by_title, :class_name => "Post", :group => "title", :select => "title" def self.what_are_you diff --git a/activerecord/test/models/project.rb b/activerecord/test/models/project.rb index 44c692b5e7..550d4ae23c 100644 --- a/activerecord/test/models/project.rb +++ b/activerecord/test/models/project.rb @@ -13,6 +13,7 @@ class Project < ActiveRecord::Base :after_add => Proc.new {|o, r| o.developers_log << "after_adding#{r.id || ''}"}, :before_remove => Proc.new {|o, r| o.developers_log << "before_removing#{r.id}"}, :after_remove => Proc.new {|o, r| o.developers_log << "after_removing#{r.id}"} + has_and_belongs_to_many :well_payed_salary_groups, :class_name => "Developer", :group => "salary", :having => "SUM(salary) > 10000", :select => "SUM(salary) as salary" attr_accessor :developers_log -- cgit v1.2.3