change_table_test.rb 8.3 KB
Newer Older
1 2 3 4 5 6
require "cases/migration/helper"

module ActiveRecord
  class Migration
    class TableTest < ActiveRecord::TestCase
      def setup
7
        @connection = Minitest::Mock.new
8 9
      end

G
Guo Xiang Tan 已提交
10
      teardown do
11 12 13 14
        assert @connection.verify
      end

      def with_change_table
15
        yield ActiveRecord::Base.connection.update_table_definition(:delete_me, @connection)
16 17 18 19
      end

      def test_references_column_type_adds_id
        with_change_table do |t|
20
          @connection.expect :add_reference, nil, [:delete_me, :customer, {}]
21 22 23 24 25 26
          t.references :customer
        end
      end

      def test_remove_references_column_type_removes_id
        with_change_table do |t|
27
          @connection.expect :remove_reference, nil, [:delete_me, :customer, {}]
28 29 30 31 32 33
          t.remove_references :customer
        end
      end

      def test_add_belongs_to_works_like_add_references
        with_change_table do |t|
34
          @connection.expect :add_reference, nil, [:delete_me, :customer, {}]
35 36 37 38 39 40
          t.belongs_to :customer
        end
      end

      def test_remove_belongs_to_works_like_remove_references
        with_change_table do |t|
41
          @connection.expect :remove_reference, nil, [:delete_me, :customer, {}]
42 43 44 45 46 47
          t.remove_belongs_to :customer
        end
      end

      def test_references_column_type_with_polymorphic_adds_type
        with_change_table do |t|
48 49
          @connection.expect :add_reference, nil, [:delete_me, :taggable, polymorphic: true]
          t.references :taggable, polymorphic: true
50 51 52 53 54
        end
      end

      def test_remove_references_column_type_with_polymorphic_removes_type
        with_change_table do |t|
55 56
          @connection.expect :remove_reference, nil, [:delete_me, :taggable, polymorphic: true]
          t.remove_references :taggable, polymorphic: true
57 58 59 60 61
        end
      end

      def test_references_column_type_with_polymorphic_and_options_null_is_false_adds_table_flag
        with_change_table do |t|
62 63
          @connection.expect :add_reference, nil, [:delete_me, :taggable, polymorphic: true, null: false]
          t.references :taggable, polymorphic: true, null: false
64 65 66 67 68
        end
      end

      def test_remove_references_column_type_with_polymorphic_and_options_null_is_false_removes_table_flag
        with_change_table do |t|
69 70
          @connection.expect :remove_reference, nil, [:delete_me, :taggable, polymorphic: true, null: false]
          t.remove_references :taggable, polymorphic: true, null: false
71 72 73
        end
      end

74 75 76 77 78 79 80 81 82 83 84 85 86 87
      def test_references_column_type_with_polymorphic_and_type
        with_change_table do |t|
          @connection.expect :add_reference, nil, [:delete_me, :taggable, polymorphic: true, type: :string]
          t.references :taggable, polymorphic: true, type: :string
        end
      end

      def test_remove_references_column_type_with_polymorphic_and_type
        with_change_table do |t|
          @connection.expect :remove_reference, nil, [:delete_me, :taggable, polymorphic: true, type: :string]
          t.remove_references :taggable, polymorphic: true, type: :string
        end
      end

88 89
      def test_timestamps_creates_updated_at_and_created_at
        with_change_table do |t|
90 91
          @connection.expect :add_timestamps, nil, [:delete_me, null: true]
          t.timestamps null: true
92 93 94 95 96
        end
      end

      def test_remove_timestamps_creates_updated_at_and_created_at
        with_change_table do |t|
97 98
          @connection.expect :remove_timestamps, nil, [:delete_me, { null: true }]
          t.remove_timestamps({ null: true })
99 100 101
        end
      end

102 103 104 105 106 107 108
      def test_primary_key_creates_primary_key_column
        with_change_table do |t|
          @connection.expect :add_column, nil, [:delete_me, :id, :primary_key, primary_key: true, first: true]
          t.primary_key :id, first: true
        end
      end

109 110
      def test_integer_creates_integer_column
        with_change_table do |t|
111 112
          @connection.expect :add_column, nil, [:delete_me, :foo, :integer, {}]
          @connection.expect :add_column, nil, [:delete_me, :bar, :integer, {}]
113 114 115 116
          t.integer :foo, :bar
        end
      end

117 118 119 120 121 122 123 124
      def test_bigint_creates_bigint_column
        with_change_table do |t|
          @connection.expect :add_column, nil, [:delete_me, :foo, :bigint, {}]
          @connection.expect :add_column, nil, [:delete_me, :bar, :bigint, {}]
          t.bigint :foo, :bar
        end
      end

125 126
      def test_string_creates_string_column
        with_change_table do |t|
127 128
          @connection.expect :add_column, nil, [:delete_me, :foo, :string, {}]
          @connection.expect :add_column, nil, [:delete_me, :bar, :string, {}]
129 130 131 132
          t.string :foo, :bar
        end
      end

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
      if current_adapter?(:PostgreSQLAdapter)
        def test_json_creates_json_column
          with_change_table do |t|
            @connection.expect :add_column, nil, [:delete_me, :foo, :json, {}]
            @connection.expect :add_column, nil, [:delete_me, :bar, :json, {}]
            t.json :foo, :bar
          end
        end

        def test_xml_creates_xml_column
          with_change_table do |t|
            @connection.expect :add_column, nil, [:delete_me, :foo, :xml, {}]
            @connection.expect :add_column, nil, [:delete_me, :bar, :xml, {}]
            t.xml :foo, :bar
          end
        end
      end

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
      def test_column_creates_column
        with_change_table do |t|
          @connection.expect :add_column, nil, [:delete_me, :bar, :integer, {}]
          t.column :bar, :integer
        end
      end

      def test_column_creates_column_with_options
        with_change_table do |t|
          @connection.expect :add_column, nil, [:delete_me, :bar, :integer, {:null => false}]
          t.column :bar, :integer, :null => false
        end
      end

      def test_index_creates_index
        with_change_table do |t|
          @connection.expect :add_index, nil, [:delete_me, :bar, {}]
          t.index :bar
        end
      end

      def test_index_creates_index_with_options
        with_change_table do |t|
          @connection.expect :add_index, nil, [:delete_me, :bar, {:unique => true}]
          t.index :bar, :unique => true
        end
      end

      def test_index_exists
        with_change_table do |t|
          @connection.expect :index_exists?, nil, [:delete_me, :bar, {}]
          t.index_exists?(:bar)
        end
      end

      def test_index_exists_with_options
        with_change_table do |t|
          @connection.expect :index_exists?, nil, [:delete_me, :bar, {:unique => true}]
          t.index_exists?(:bar, :unique => true)
        end
      end

J
Jarek Radosz 已提交
193 194 195 196 197 198 199
      def test_rename_index_renames_index
        with_change_table do |t|
          @connection.expect :rename_index, nil, [:delete_me, :bar, :baz]
          t.rename_index :bar, :baz
        end
      end

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
      def test_change_changes_column
        with_change_table do |t|
          @connection.expect :change_column, nil, [:delete_me, :bar, :string, {}]
          t.change :bar, :string
        end
      end

      def test_change_changes_column_with_options
        with_change_table do |t|
          @connection.expect :change_column, nil, [:delete_me, :bar, :string, {:null => true}]
          t.change :bar, :string, :null => true
        end
      end

      def test_change_default_changes_column
        with_change_table do |t|
          @connection.expect :change_column_default, nil, [:delete_me, :bar, :string]
          t.change_default :bar, :string
        end
      end

      def test_remove_drops_single_column
        with_change_table do |t|
223
          @connection.expect :remove_columns, nil, [:delete_me, :bar]
224 225 226 227 228 229
          t.remove :bar
        end
      end

      def test_remove_drops_multiple_columns
        with_change_table do |t|
230
          @connection.expect :remove_columns, nil, [:delete_me, :bar, :baz]
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
          t.remove :bar, :baz
        end
      end

      def test_remove_index_removes_index_with_options
        with_change_table do |t|
          @connection.expect :remove_index, nil, [:delete_me, {:unique => true}]
          t.remove_index :unique => true
        end
      end

      def test_rename_renames_column
        with_change_table do |t|
          @connection.expect :rename_column, nil, [:delete_me, :bar, :baz]
          t.rename :bar, :baz
        end
      end
248 249 250 251 252 253

      def test_table_name_set
        with_change_table do |t|
          assert_equal :delete_me, t.name
        end
      end
254 255 256
    end
  end
end