aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/relation/delegation_test.rb
blob: a8030c2d64fa2f0e06bd826979cc5cfc6ad56e48 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

require "cases/helper"
require "models/post"
require "models/comment"

module ActiveRecord
  module ArrayDelegationTests
    ARRAY_DELEGATES = [
      :+, :-, :|, :&, :[], :shuffle,
      :all?, :collect, :compact, :detect, :each, :each_cons, :each_with_index,
      :exclude?, :find_all, :flat_map, :group_by, :include?, :length,
      :map, :none?, :one?, :partition, :reject, :reverse, :rotate,
      :sample, :second, :sort, :sort_by, :slice, :third, :index, :rindex,
      :to_ary, :to_set, :to_xml, :to_yaml, :join,
      :in_groups, :in_groups_of, :to_sentence, :to_formatted_s, :as_json
    ]

    ARRAY_DELEGATES.each do |method|
      define_method "test_delegates_#{method}_to_Array" do
        assert_respond_to target, method
      end
    end
  end

  module DeprecatedArelDelegationTests
    AREL_METHODS = [
      :with, :orders, :froms, :project, :projections, :taken, :constraints, :exists, :locked, :where_sql,
      :ast, :source, :join_sources, :to_dot, :create_insert, :create_true, :create_false
    ]

    def test_deprecate_arel_delegation
      AREL_METHODS.each do |method|
        assert_deprecated { target.public_send(method) }
        assert_deprecated { target.public_send(method) }
      end
    end
  end

  class DelegationAssociationTest < ActiveRecord::TestCase
    include ArrayDelegationTests
    include DeprecatedArelDelegationTests

    def target
      Post.new.comments
    end
  end

  class DelegationRelationTest < ActiveRecord::TestCase
    include ArrayDelegationTests
    include DeprecatedArelDelegationTests

    def target
      Comment.all
    end
  end

  class QueryingMethodsDelegationTest < ActiveRecord::TestCase
    QUERYING_METHODS = [
      :find, :take, :take!, :first, :first!, :last, :last!, :exists?, :any?, :many?, :none?, :one?,
      :second, :second!, :third, :third!, :fourth, :fourth!, :fifth, :fifth!, :forty_two, :forty_two!, :third_to_last, :third_to_last!, :second_to_last, :second_to_last!,
      :first_or_create, :first_or_create!, :first_or_initialize,
      :find_or_create_by, :find_or_create_by!, :create_or_find_by, :create_or_find_by!, :find_or_initialize_by,
      :find_by, :find_by!,
      :destroy_all, :delete_all, :update_all,
      :find_each, :find_in_batches, :in_batches,
      :select, :group, :order, :except, :reorder, :limit, :offset, :joins, :left_joins, :left_outer_joins, :or,
      :where, :rewhere, :preload, :eager_load, :includes, :from, :lock, :readonly, :extending,
      :having, :create_with, :distinct, :references, :none, :unscope, :merge,
      :count, :average, :minimum, :maximum, :sum, :calculate,
      :pluck, :pick, :ids,
    ]

    def test_delegate_querying_methods
      klass = Class.new(ActiveRecord::Base) do
        self.table_name = "posts"
      end

      QUERYING_METHODS.each do |method|
        assert_respond_to klass.all, method
        assert_respond_to klass, method
      end
    end
  end
end