2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 12:26:20 -04:00
|
|
|
require "cases/helper"
|
|
|
|
require "models/post"
|
|
|
|
require "models/comment"
|
2013-09-04 00:46:33 -04:00
|
|
|
|
|
|
|
module ActiveRecord
|
2019-02-05 14:37:54 -05:00
|
|
|
module DelegationTests
|
2013-12-13 19:11:39 -05:00
|
|
|
ARRAY_DELEGATES = [
|
2015-12-01 18:41:50 -05:00
|
|
|
:+, :-, :|, :&, :[], :shuffle,
|
2015-05-27 23:37:21 -04:00
|
|
|
:all?, :collect, :compact, :detect, :each, :each_cons, :each_with_index,
|
2013-12-13 19:11:39 -05:00
|
|
|
:exclude?, :find_all, :flat_map, :group_by, :include?, :length,
|
2017-08-23 04:42:21 -04:00
|
|
|
:map, :none?, :one?, :partition, :reject, :reverse, :rotate,
|
|
|
|
:sample, :second, :sort, :sort_by, :slice, :third, :index, :rindex,
|
2017-02-04 08:49:47 -05:00
|
|
|
:to_ary, :to_set, :to_xml, :to_yaml, :join,
|
2017-03-10 09:32:05 -05:00
|
|
|
:in_groups, :in_groups_of, :to_sentence, :to_formatted_s, :as_json
|
2013-12-13 19:11:39 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
ARRAY_DELEGATES.each do |method|
|
2013-12-12 23:10:03 -05:00
|
|
|
define_method "test_delegates_#{method}_to_Array" do
|
2013-12-03 15:30:13 -05:00
|
|
|
assert_respond_to target, method
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
2019-02-05 14:37:54 -05:00
|
|
|
|
|
|
|
def test_not_respond_to_arel_method
|
|
|
|
assert_not_respond_to target, :exists
|
|
|
|
end
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
|
2017-05-05 15:52:59 -04:00
|
|
|
class DelegationAssociationTest < ActiveRecord::TestCase
|
2019-02-05 14:37:54 -05:00
|
|
|
include DelegationTests
|
2017-05-05 15:52:59 -04:00
|
|
|
|
2013-09-04 00:46:33 -04:00
|
|
|
def target
|
2017-08-24 08:00:00 -04:00
|
|
|
Post.new.comments
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
2013-12-12 23:10:03 -05:00
|
|
|
end
|
2013-09-04 00:46:33 -04:00
|
|
|
|
2017-05-05 15:52:59 -04:00
|
|
|
class DelegationRelationTest < ActiveRecord::TestCase
|
2019-02-05 14:37:54 -05:00
|
|
|
include DelegationTests
|
2013-09-04 00:46:33 -04:00
|
|
|
|
2013-12-12 23:10:03 -05:00
|
|
|
def target
|
|
|
|
Comment.all
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|
|
|
|
end
|
2018-03-12 14:43:41 -04:00
|
|
|
|
|
|
|
class QueryingMethodsDelegationTest < ActiveRecord::TestCase
|
2019-03-07 04:30:41 -05:00
|
|
|
QUERYING_METHODS =
|
|
|
|
ActiveRecord::Batches.public_instance_methods(false) +
|
|
|
|
ActiveRecord::Calculations.public_instance_methods(false) +
|
2020-10-02 21:52:07 -04:00
|
|
|
ActiveRecord::FinderMethods.public_instance_methods(false) - [:include?, :member?, :raise_record_not_found_exception!] +
|
2019-03-07 05:07:07 -05:00
|
|
|
ActiveRecord::SpawnMethods.public_instance_methods(false) - [:spawn, :merge!] +
|
2019-03-07 04:30:41 -05:00
|
|
|
ActiveRecord::QueryMethods.public_instance_methods(false).reject { |method|
|
2020-05-06 00:43:17 -04:00
|
|
|
method.end_with?("=", "!", "value", "values", "clause")
|
2019-04-04 17:09:44 -04:00
|
|
|
} - [:reverse_order, :arel, :extensions, :construct_join_dependency] + [
|
2019-03-07 04:30:41 -05:00
|
|
|
:any?, :many?, :none?, :one?,
|
|
|
|
:first_or_create, :first_or_create!, :first_or_initialize,
|
|
|
|
:find_or_create_by, :find_or_create_by!, :find_or_initialize_by,
|
|
|
|
:create_or_find_by, :create_or_find_by!,
|
2019-04-05 03:13:40 -04:00
|
|
|
:destroy_all, :delete_all, :update_all, :touch_all, :delete_by, :destroy_by
|
2019-03-07 04:30:41 -05:00
|
|
|
]
|
2018-03-12 14:43:41 -04:00
|
|
|
|
|
|
|
def test_delegate_querying_methods
|
|
|
|
klass = Class.new(ActiveRecord::Base) do
|
|
|
|
self.table_name = "posts"
|
|
|
|
end
|
|
|
|
|
2019-03-07 04:30:41 -05:00
|
|
|
assert_equal QUERYING_METHODS.sort, ActiveRecord::Querying::QUERYING_METHODS.sort
|
|
|
|
|
2018-03-12 14:43:41 -04:00
|
|
|
QUERYING_METHODS.each do |method|
|
|
|
|
assert_respond_to klass.all, method
|
|
|
|
assert_respond_to klass, method
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-09-04 00:46:33 -04:00
|
|
|
end
|