aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/column_definition_test.rb
blob: 0dffe5e60b1402fa6a7642043c34857ef73633d0 (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
require "cases/helper"

module ActiveRecord
  module ConnectionAdapters
    class ColumnDefinitionTest < ActiveRecord::TestCase
      def setup
        @adapter = AbstractAdapter.new(nil)
        def @adapter.native_database_types
          { string: "varchar" }
        end
        @viz = @adapter.schema_creation
      end

      # Avoid column definitions in create table statements like:
      # `title` varchar(255) DEFAULT NULL
      def test_should_not_include_default_clause_when_default_is_null
        column = Column.new("title", nil, SqlTypeMetadata.new(limit: 20))
        column_def = ColumnDefinition.new(
          column.name, "string",
          column.limit, column.precision, column.scale, column.default, column.null)
        assert_equal "title varchar(20)", @viz.accept(column_def)
      end

      def test_should_include_default_clause_when_default_is_present
        column = Column.new("title", "Hello", SqlTypeMetadata.new(limit: 20))
        column_def = ColumnDefinition.new(
          column.name, "string",
          column.limit, column.precision, column.scale, column.default, column.null)
        assert_equal "title varchar(20) DEFAULT 'Hello'", @viz.accept(column_def)
      end

      def test_should_specify_not_null_if_null_option_is_false
        type_metadata = SqlTypeMetadata.new(limit: 20)
        column = Column.new("title", "Hello", type_metadata, false)
        column_def = ColumnDefinition.new(
          column.name, "string",
          column.limit, column.precision, column.scale, column.default, column.null)
        assert_equal "title varchar(20) DEFAULT 'Hello' NOT NULL", @viz.accept(column_def)
      end
    end
  end
end