aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/migration/references_statements_test.rb
blob: 769241ba1278a8fea436e8f81200e082fc8f8e15 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# frozen_string_literal: true

require "cases/migration/helper"

module ActiveRecord
  class Migration
    class ReferencesStatementsTest < ActiveRecord::TestCase
      include ActiveRecord::Migration::TestHelper

      self.use_transactional_tests = false

      def setup
        super
        @table_name = :test_models

        add_column table_name, :supplier_id, :integer
        add_index table_name, :supplier_id
      end

      def test_creates_reference_id_column
        add_reference table_name, :user
        assert column_exists?(table_name, :user_id, :integer)
      end

      def test_does_not_create_reference_type_column
        add_reference table_name, :taggable
        assert_not column_exists?(table_name, :taggable_type, :string)
      end

      def test_creates_reference_type_column
        add_reference table_name, :taggable, polymorphic: true
        assert column_exists?(table_name, :taggable_type, :string)
      end

      def test_does_not_create_reference_id_index_if_index_is_false
        add_reference table_name, :user, index: false
        assert_not index_exists?(table_name, :user_id)
      end

      def test_create_reference_id_index_even_if_index_option_is_not_passed
        add_reference table_name, :user
        assert index_exists?(table_name, :user_id)
      end

      def test_creates_polymorphic_index
        add_reference table_name, :taggable, polymorphic: true, index: true
        assert index_exists?(table_name, [:taggable_type, :taggable_id])
      end

      def test_creates_reference_type_column_with_default
        add_reference table_name, :taggable, polymorphic: { default: "Photo" }, index: true
        assert column_exists?(table_name, :taggable_type, :string, default: "Photo")
      end

      def test_creates_reference_type_column_with_not_null
        connection.create_table table_name, force: true do |t|
          t.references :taggable, null: false, polymorphic: true
        end
        assert column_exists?(table_name, :taggable_id, :integer, null: false)
        assert column_exists?(table_name, :taggable_type, :string, null: false)
      end

      def test_does_not_share_options_with_reference_type_column
        add_reference table_name, :taggable, type: :integer, limit: 2, polymorphic: true
        assert column_exists?(table_name, :taggable_id, :integer, limit: 2)
        assert column_exists?(table_name, :taggable_type, :string)
        assert_not column_exists?(table_name, :taggable_type, :string, limit: 2)
      end

      def test_creates_named_index
        add_reference table_name, :tag, index: { name: "index_taggings_on_tag_id" }
        assert index_exists?(table_name, :tag_id, name: "index_taggings_on_tag_id")
      end

      def test_creates_named_unique_index
        add_reference table_name, :tag, index: { name: "index_taggings_on_tag_id", unique: true }
        assert index_exists?(table_name, :tag_id, name: "index_taggings_on_tag_id", unique: true)
      end

      def test_creates_reference_id_with_specified_type
        add_reference table_name, :user, type: :string
        assert column_exists?(table_name, :user_id, :string)
      end

      def test_deletes_reference_id_column
        remove_reference table_name, :supplier
        assert_not column_exists?(table_name, :supplier_id, :integer)
      end

      def test_deletes_reference_id_index
        remove_reference table_name, :supplier
        assert_not index_exists?(table_name, :supplier_id)
      end

      def test_does_not_delete_reference_type_column
        with_polymorphic_column do
          remove_reference table_name, :supplier

          assert_not column_exists?(table_name, :supplier_id, :integer)
          assert column_exists?(table_name, :supplier_type, :string)
        end
      end

      def test_deletes_reference_type_column
        with_polymorphic_column do
          remove_reference table_name, :supplier, polymorphic: true
          assert_not column_exists?(table_name, :supplier_type, :string)
        end
      end

      def test_deletes_polymorphic_index
        with_polymorphic_column do
          remove_reference table_name, :supplier, polymorphic: true
          assert_not index_exists?(table_name, [:supplier_id, :supplier_type])
        end
      end

      def test_add_belongs_to_alias
        add_belongs_to table_name, :user
        assert column_exists?(table_name, :user_id, :integer)
      end

      def test_remove_belongs_to_alias
        remove_belongs_to table_name, :supplier
        assert_not column_exists?(table_name, :supplier_id, :integer)
      end

      private

        def with_polymorphic_column
          add_column table_name, :supplier_type, :string
          add_index table_name, [:supplier_id, :supplier_type]

          yield
        end
    end
  end
end