diff options
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/associations/has_many_through_associations_test.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/bind_parameter_test.rb | 25 | ||||
-rw-r--r-- | activerecord/test/cases/relation/select_test.rb | 5 | ||||
-rw-r--r-- | activerecord/test/cases/relations_test.rb | 32 | ||||
-rw-r--r-- | activerecord/test/cases/scoping/default_scoping_test.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/serialized_attribute_test.rb | 34 | ||||
-rw-r--r-- | activerecord/test/models/author.rb | 7 | ||||
-rw-r--r-- | activerecord/test/models/developer.rb | 1 | ||||
-rw-r--r-- | activerecord/test/models/post.rb | 1 | ||||
-rw-r--r-- | activerecord/test/models/topic.rb | 4 |
10 files changed, 78 insertions, 45 deletions
diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 6f23a832ef..67e013c6e0 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -56,11 +56,11 @@ class HasManyThroughAssociationsTest < ActiveRecord::TestCase end def test_preload_with_nested_association - posts = Post.preload(:author, :author_favorites).to_a + posts = Post.preload(:author, :author_favorites_with_scope).to_a assert_no_queries do posts.each(&:author) - posts.each(&:author_favorites) + posts.each(&:author_favorites_with_scope) end end diff --git a/activerecord/test/cases/bind_parameter_test.rb b/activerecord/test/cases/bind_parameter_test.rb index b89f054821..03cc0dffbc 100644 --- a/activerecord/test/cases/bind_parameter_test.rb +++ b/activerecord/test/cases/bind_parameter_test.rb @@ -54,17 +54,36 @@ if ActiveRecord::Base.connection.prepared_statements @connection.disable_query_cache! end + def test_statement_cache_with_find + @connection.clear_cache! + + assert_equal 1, Topic.find(1).id + assert_raises(RecordNotFound) { SillyReply.find(2) } + + topic_sql = cached_statement(Topic, Topic.primary_key) + assert_includes statement_cache, to_sql_key(topic_sql) + + e = assert_raise { cached_statement(SillyReply, SillyReply.primary_key) } + assert_equal "SillyReply has no cached statement by \"id\"", e.message + + replies = SillyReply.where(id: 2).limit(1) + assert_includes statement_cache, to_sql_key(replies.arel) + end + def test_statement_cache_with_find_by @connection.clear_cache! assert_equal 1, Topic.find_by!(id: 1).id - assert_equal 2, Reply.find_by!(id: 2).id + assert_raises(RecordNotFound) { SillyReply.find_by!(id: 2) } topic_sql = cached_statement(Topic, [:id]) assert_includes statement_cache, to_sql_key(topic_sql) - e = assert_raise { cached_statement(Reply, [:id]) } - assert_equal "Reply has no cached statement by [:id]", e.message + e = assert_raise { cached_statement(SillyReply, [:id]) } + assert_equal "SillyReply has no cached statement by [:id]", e.message + + replies = SillyReply.where(id: 2).limit(1) + assert_includes statement_cache, to_sql_key(replies.arel) end def test_statement_cache_with_in_clause diff --git a/activerecord/test/cases/relation/select_test.rb b/activerecord/test/cases/relation/select_test.rb index dec8a6925d..32e8f473ff 100644 --- a/activerecord/test/cases/relation/select_test.rb +++ b/activerecord/test/cases/relation/select_test.rb @@ -11,5 +11,10 @@ module ActiveRecord expected = Post.select(:title).to_sql assert_equal expected, Post.select(nil).select(:title).to_sql end + + def test_reselect + expected = Post.select(:title).to_sql + assert_equal expected, Post.select(:title, :body).reselect(:title).to_sql + end end end diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb index a7f09e6de0..85719cb8d0 100644 --- a/activerecord/test/cases/relations_test.rb +++ b/activerecord/test/cases/relations_test.rb @@ -182,27 +182,27 @@ class RelationTest < ActiveRecord::TestCase end end - def test_select_with_original_table_name_in_from + def test_select_with_from_includes_original_table_name relation = Comment.joins(:post).select(:id).order(:id) - subquery = Comment.from(Comment.table_name).joins(:post).select(:id).order(:id) + subquery = Comment.from("#{Comment.table_name} /*! USE INDEX (PRIMARY) */").joins(:post).select(:id).order(:id) assert_equal relation.map(&:id), subquery.map(&:id) end - def test_pluck_with_original_table_name_in_from + def test_pluck_with_from_includes_original_table_name relation = Comment.joins(:post).order(:id) - subquery = Comment.from(Comment.table_name).joins(:post).order(:id) + subquery = Comment.from("#{Comment.table_name} /*! USE INDEX (PRIMARY) */").joins(:post).order(:id) assert_equal relation.pluck(:id), subquery.pluck(:id) end - def test_select_with_quoted_original_table_name_in_from + def test_select_with_from_includes_quoted_original_table_name relation = Comment.joins(:post).select(:id).order(:id) - subquery = Comment.from(Comment.quoted_table_name).joins(:post).select(:id).order(:id) + subquery = Comment.from("#{Comment.quoted_table_name} /*! USE INDEX (PRIMARY) */").joins(:post).select(:id).order(:id) assert_equal relation.map(&:id), subquery.map(&:id) end - def test_pluck_with_quoted_original_table_name_in_from + def test_pluck_with_from_includes_quoted_original_table_name relation = Comment.joins(:post).order(:id) - subquery = Comment.from(Comment.quoted_table_name).joins(:post).order(:id) + subquery = Comment.from("#{Comment.quoted_table_name} /*! USE INDEX (PRIMARY) */").joins(:post).order(:id) assert_equal relation.pluck(:id), subquery.pluck(:id) end @@ -221,13 +221,25 @@ class RelationTest < ActiveRecord::TestCase def test_select_with_subquery_in_from_does_not_use_original_table_name relation = Comment.group(:type).select("COUNT(post_id) AS post_count, type") - subquery = Comment.from(relation).select("type", "post_count") + subquery = Comment.from(relation, "grouped_#{Comment.table_name}").select("type", "post_count") assert_equal(relation.map(&:post_count).sort, subquery.map(&:post_count).sort) end def test_group_with_subquery_in_from_does_not_use_original_table_name relation = Comment.group(:type).select("COUNT(post_id) AS post_count,type") - subquery = Comment.from(relation).group("type").average("post_count") + subquery = Comment.from(relation, "grouped_#{Comment.table_name}").group("type").average("post_count") + assert_equal(relation.map(&:post_count).sort, subquery.values.sort) + end + + def test_select_with_subquery_string_in_from_does_not_use_original_table_name + relation = Comment.group(:type).select("COUNT(post_id) AS post_count, type") + subquery = Comment.from("(#{relation.to_sql}) #{Comment.table_name}_grouped").select("type", "post_count") + assert_equal(relation.map(&:post_count).sort, subquery.map(&:post_count).sort) + end + + def test_group_with_subquery_string_in_from_does_not_use_original_table_name + relation = Comment.group(:type).select("COUNT(post_id) AS post_count,type") + subquery = Comment.from("(#{relation.to_sql}) #{Comment.table_name}_grouped").group("type").average("post_count") assert_equal(relation.map(&:post_count).sort, subquery.values.sort) end diff --git a/activerecord/test/cases/scoping/default_scoping_test.rb b/activerecord/test/cases/scoping/default_scoping_test.rb index 6281712df6..e7bdab58c6 100644 --- a/activerecord/test/cases/scoping/default_scoping_test.rb +++ b/activerecord/test/cases/scoping/default_scoping_test.rb @@ -408,18 +408,18 @@ class DefaultScopingTest < ActiveRecord::TestCase end def test_joins_not_affected_by_scope_other_than_default_or_unscoped - without_scope_on_post = Comment.joins(:post).to_a + without_scope_on_post = Comment.joins(:post).sort_by(&:id) with_scope_on_post = nil Post.where(id: [1, 5, 6]).scoping do - with_scope_on_post = Comment.joins(:post).to_a + with_scope_on_post = Comment.joins(:post).sort_by(&:id) end - assert_equal with_scope_on_post, without_scope_on_post + assert_equal without_scope_on_post, with_scope_on_post end def test_unscoped_with_joins_should_not_have_default_scope - assert_equal SpecialPostWithDefaultScope.unscoped { Comment.joins(:special_post_with_default_scope).to_a }, - Comment.joins(:post).to_a + assert_equal Comment.joins(:post).sort_by(&:id), + SpecialPostWithDefaultScope.unscoped { Comment.joins(:special_post_with_default_scope).sort_by(&:id) } end def test_sti_association_with_unscoped_not_affected_by_default_scope diff --git a/activerecord/test/cases/serialized_attribute_test.rb b/activerecord/test/cases/serialized_attribute_test.rb index fa136fe8da..ecf81b2042 100644 --- a/activerecord/test/cases/serialized_attribute_test.rb +++ b/activerecord/test/cases/serialized_attribute_test.rb @@ -1,28 +1,29 @@ # frozen_string_literal: true require "cases/helper" -require "models/topic" -require "models/reply" require "models/person" require "models/traffic_light" require "models/post" -require "bcrypt" class SerializedAttributeTest < ActiveRecord::TestCase fixtures :topics, :posts MyObject = Struct.new :attribute1, :attribute2 - # NOTE: Use a duplicate of Topic so attribute - # changes don't bleed into other tests - Topic = ::Topic.dup + class Topic < ActiveRecord::Base + serialize :content + end + + class ImportantTopic < Topic + serialize :important, Hash + end teardown do Topic.serialize("content") end def test_serialize_does_not_eagerly_load_columns - reset_column_information_of(Topic) + Topic.reset_column_information assert_no_queries do Topic.serialize(:content) end @@ -53,10 +54,10 @@ class SerializedAttributeTest < ActiveRecord::TestCase def test_serialized_attributes_from_database_on_subclass Topic.serialize :content, Hash - t = Reply.new(content: { foo: :bar }) + t = ImportantTopic.new(content: { foo: :bar }) assert_equal({ foo: :bar }, t.content) t.save! - t = Reply.last + t = ImportantTopic.last assert_equal({ foo: :bar }, t.content) end @@ -371,14 +372,13 @@ class SerializedAttributeTest < ActiveRecord::TestCase end def test_serialized_attribute_works_under_concurrent_initial_access - model = ::Topic.dup + model = Class.new(Topic) - topic = model.last + topic = model.create! topic.update group: "1" model.serialize :group, JSON - - reset_column_information_of(model) + model.reset_column_information # This isn't strictly necessary for the test, but a little bit of # knowledge of internals allows us to make failures far more likely. @@ -398,12 +398,4 @@ class SerializedAttributeTest < ActiveRecord::TestCase # raw string ("1"), or raise an exception. assert_equal [1] * threads.size, threads.map(&:value) end - - private - - def reset_column_information_of(topic_class) - topic_class.reset_column_information - # reset original topic to undefine attribute methods - ::Topic.reset_column_information - end end diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 3eb8a3a0fa..67be59a1fe 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -217,6 +217,13 @@ class AuthorAddress < ActiveRecord::Base end class AuthorFavorite < ActiveRecord::Base + belongs_to :author + belongs_to :favorite_author, class_name: "Author" +end + +class AuthorFavoriteWithScope < ActiveRecord::Base + self.table_name = "author_favorites" + default_scope { order(id: :asc) } belongs_to :author diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index ec48094207..c6574cf6e7 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -207,6 +207,7 @@ end class MultiplePoorDeveloperCalledJamis < ActiveRecord::Base self.table_name = "developers" + default_scope { } default_scope -> { where(name: "Jamis") } default_scope -> { where(salary: 50000) } end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index c6eb77dba4..53cbda83ed 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -78,6 +78,7 @@ class Post < ActiveRecord::Base has_many :comments_with_extend_2, extend: [NamedExtension, NamedExtension2], class_name: "Comment", foreign_key: "post_id" has_many :author_favorites, through: :author + has_many :author_favorites_with_scope, through: :author, class_name: "AuthorFavoriteWithScope", source: "author_favorites" has_many :author_categorizations, through: :author, source: :categorizations has_many :author_addresses, through: :author has_many :author_address_extra_with_address, diff --git a/activerecord/test/models/topic.rb b/activerecord/test/models/topic.rb index 0c8880a20e..77101090f2 100644 --- a/activerecord/test/models/topic.rb +++ b/activerecord/test/models/topic.rb @@ -119,10 +119,6 @@ class Topic < ActiveRecord::Base end end -class ImportantTopic < Topic - serialize :important, Hash -end - class DefaultRejectedTopic < Topic default_scope -> { where(approved: false) } end |