diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2017-07-11 17:40:24 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-11 17:40:24 -0400 |
commit | e319b01de8d42e1c8dd511d882b988121c81604f (patch) | |
tree | acca9fc78823050d6eca57374743a183f61fe559 | |
parent | cfb1e4dfd8813d3d5c75a15a750b3c53eebdea65 (diff) | |
parent | 23300f4440ae35ba7604476496e7867f5eff9dab (diff) | |
download | rails-e319b01de8d42e1c8dd511d882b988121c81604f.tar.gz rails-e319b01de8d42e1c8dd511d882b988121c81604f.tar.bz2 rails-e319b01de8d42e1c8dd511d882b988121c81604f.zip |
Merge pull request #29746 from kamipo/extract_fake_klass
Extract `FakeKlass` in `relation_test.rb` and `relation/mutation_test.rb`
-rw-r--r-- | activerecord/test/cases/relation/mutation_test.rb | 38 | ||||
-rw-r--r-- | activerecord/test/cases/relation_test.rb | 20 | ||||
-rw-r--r-- | activerecord/test/models/post.rb | 32 |
3 files changed, 38 insertions, 52 deletions
diff --git a/activerecord/test/cases/relation/mutation_test.rb b/activerecord/test/cases/relation/mutation_test.rb index 8e73baa70a..a37a2186ed 100644 --- a/activerecord/test/cases/relation/mutation_test.rb +++ b/activerecord/test/cases/relation/mutation_test.rb @@ -3,39 +3,6 @@ require "models/post" module ActiveRecord class RelationMutationTest < ActiveRecord::TestCase - FakeKlass = Struct.new(:table_name, :name) do - extend ActiveRecord::Delegation::DelegateCache - inherited self - - def connection - Post.connection - end - - def relation_delegate_class(klass) - self.class.relation_delegate_class(klass) - end - - def attribute_alias?(name) - false - end - - def sanitize_sql(sql) - sql - end - - def sanitize_sql_for_order(sql) - sql - end - - def arel_attribute(name, table) - table[name] - end - end - - def relation - @relation ||= Relation.new FakeKlass.new("posts"), Post.arel_table, Post.predicate_builder - end - (Relation::MULTI_VALUE_METHODS - [:references, :extending, :order, :unscope, :select]).each do |method| test "##{method}!" do assert relation.public_send("#{method}!", :foo).equal?(relation) @@ -167,5 +134,10 @@ module ActiveRecord relation.skip_query_cache! assert relation.skip_query_cache_value end + + private + def relation + @relation ||= Relation.new(FakeKlass, Post.arel_table, Post.predicate_builder) + end end end diff --git a/activerecord/test/cases/relation_test.rb b/activerecord/test/cases/relation_test.rb index a403824f1a..382aa17c34 100644 --- a/activerecord/test/cases/relation_test.rb +++ b/activerecord/test/cases/relation_test.rb @@ -8,24 +8,6 @@ module ActiveRecord class RelationTest < ActiveRecord::TestCase fixtures :posts, :comments, :authors, :author_addresses, :ratings - FakeKlass = Struct.new(:table_name, :name) do - extend ActiveRecord::Delegation::DelegateCache - - inherited self - - def self.connection - Post.connection - end - - def self.table_name - "fake_table" - end - - def self.sanitize_sql_for_order(sql) - sql - end - end - def test_construction relation = Relation.new(FakeKlass, :b, nil) assert_equal FakeKlass, relation.klass @@ -90,7 +72,7 @@ module ActiveRecord end def test_table_name_delegates_to_klass - relation = Relation.new(FakeKlass.new("posts"), :b, Post.predicate_builder) + relation = Relation.new(FakeKlass, :b, Post.predicate_builder) assert_equal "posts", relation.table_name end diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index ed64e0ee52..a99ab12b19 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -276,3 +276,35 @@ end class SubConditionalStiPost < ConditionalStiPost end + +class FakeKlass + extend ActiveRecord::Delegation::DelegateCache + + inherited self + + class << self + def connection + Post.connection + end + + def table_name + "posts" + end + + def attribute_alias?(name) + false + end + + def sanitize_sql(sql) + sql + end + + def sanitize_sql_for_order(sql) + sql + end + + def arel_attribute(name, table) + table[name] + end + end +end |