aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/column_definition_test.rb
blob: 783a374116f6342acb656fe48dbfee3b66f27298 (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
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 %Q{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 %Q{title varchar(20) DEFAULT 'Hello' NOT NULL}, @viz.accept(column_def)
      end

      if current_adapter?(:Mysql2Adapter)
        def test_should_set_default_for_mysql_binary_data_types
          type = SqlTypeMetadata.new(type: :binary, sql_type: "binary(1)")
          binary_column = AbstractMysqlAdapter::Column.new("title", "a", type)
          assert_equal "a", binary_column.default

          type = SqlTypeMetadata.new(type: :binary, sql_type: "varbinary")
          varbinary_column = AbstractMysqlAdapter::Column.new("title", "a", type)
          assert_equal "a", varbinary_column.default
        end

        def test_should_be_empty_string_default_for_mysql_binary_data_types
          type = SqlTypeMetadata.new(type: :binary, sql_type: "binary(1)")
          binary_column = AbstractMysqlAdapter::Column.new("title", "", type, false)
          assert_equal "", binary_column.default

          type = SqlTypeMetadata.new(type: :binary, sql_type: "varbinary")
          varbinary_column = AbstractMysqlAdapter::Column.new("title", "", type, false)
          assert_equal "", varbinary_column.default
        end

        def test_should_not_set_default_for_blob_and_text_data_types
          assert_raise ArgumentError do
            AbstractMysqlAdapter::Column.new("title", "a", SqlTypeMetadata.new(sql_type: "blob"))
          end

          text_type = AbstractMysqlAdapter::MysqlTypeMetadata.new(
            SqlTypeMetadata.new(type: :text))
          assert_raise ArgumentError do
            AbstractMysqlAdapter::Column.new("title", "Hello", text_type)
          end

          text_column = AbstractMysqlAdapter::Column.new("title", nil, text_type)
          assert_equal nil, text_column.default

          not_null_text_column = AbstractMysqlAdapter::Column.new("title", nil, text_type, false)
          assert_equal "", not_null_text_column.default
        end

        def test_has_default_should_return_false_for_blob_and_text_data_types
          binary_type = SqlTypeMetadata.new(sql_type: "blob")
          blob_column = AbstractMysqlAdapter::Column.new("title", nil, binary_type)
          assert !blob_column.has_default?

          text_type = SqlTypeMetadata.new(type: :text)
          text_column = AbstractMysqlAdapter::Column.new("title", nil, text_type)
          assert !text_column.has_default?
        end
      end
    end
  end
end