aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/defaults_test.rb
blob: c63553ed51f3265b7d7a4cbfe95e8fecfffdffc9 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require "cases/helper"
require 'active_support/core_ext/object/inclusion'
require 'models/default'
require 'models/entrant'

class DefaultTest < ActiveRecord::TestCase
  def test_nil_defaults_for_not_null_columns
    column_defaults =
      if current_adapter?(:MysqlAdapter) && (Mysql.client_version < 50051 || (50100..50122).include?(Mysql.client_version))
        { 'id' => nil, 'name' => '',  'course_id' => nil }
      else
        { 'id' => nil, 'name' => nil, 'course_id' => nil }
      end

    column_defaults.each do |name, default|
      column = Entrant.columns_hash[name]
      assert !column.null, "#{name} column should be NOT NULL"
      assert_equal default, column.default, "#{name} column should be DEFAULT #{default.inspect}"
    end
  end

  if current_adapter?(:PostgreSQLAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter)
    def test_default_integers
      default = Default.new
      assert_instance_of Fixnum, default.positive_integer
      assert_equal 1, default.positive_integer
      assert_instance_of Fixnum, default.negative_integer
      assert_equal(-1, default.negative_integer)
      assert_instance_of BigDecimal, default.decimal_number
      assert_equal BigDecimal.new("2.78"), default.decimal_number
    end
  end

  if current_adapter?(:PostgreSQLAdapter)
    def test_multiline_default_text
      # older postgres versions represent the default with escapes ("\\012" for a newline)
      assert( "--- []\n\n" == Default.columns_hash['multiline_default'].default ||
               "--- []\\012\\012" == Default.columns_hash['multiline_default'].default)
    end
  end
end

if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
  class DefaultsTestWithoutTransactionalFixtures < ActiveRecord::TestCase
    # ActiveRecord::Base#create! (and #save and other related methods) will
    # open a new transaction. When in transactional fixtures mode, this will
    # cause Active Record to create a new savepoint. However, since MySQL doesn't
    # support DDL transactions, creating a table will result in any created
    # savepoints to be automatically released. This in turn causes the savepoint
    # release code in AbstractAdapter#transaction to fail.
    #
    # We don't want that to happen, so we disable transactional fixtures here.
    self.use_transactional_fixtures = false

    # MySQL 5 and higher is quirky with not null text/blob columns.
    # With MySQL Text/blob columns cannot have defaults. If the column is not
    # null MySQL will report that the column has a null default
    # but it behaves as though the column had a default of ''
    def test_mysql_text_not_null_defaults
      klass = Class.new(ActiveRecord::Base)
      klass.table_name = 'test_mysql_text_not_null_defaults'
      klass.connection.create_table klass.table_name do |t|
        t.column :non_null_text, :text, :null => false
        t.column :non_null_blob, :blob, :null => false
        t.column :null_text, :text, :null => true
        t.column :null_blob, :blob, :null => true
      end
      assert_equal '', klass.columns_hash['non_null_blob'].default
      assert_equal '', klass.columns_hash['non_null_text'].default

      assert_nil klass.columns_hash['null_blob'].default
      assert_nil klass.columns_hash['null_text'].default

      assert_nothing_raised do
        instance = klass.create!
        assert_equal '', instance.non_null_text
        assert_equal '', instance.non_null_blob
        assert_nil instance.null_text
        assert_nil instance.null_blob
      end
    ensure
      klass.connection.drop_table(klass.table_name) rescue nil
    end

    # MySQL uses an implicit default 0 rather than NULL unless in strict mode.
    # We use an implicit NULL so schema.rb is compatible with other databases.
    def test_mysql_integer_not_null_defaults
      klass = Class.new(ActiveRecord::Base)
      klass.table_name = 'test_integer_not_null_default_zero'
      klass.connection.create_table klass.table_name do |t|
        t.column :zero, :integer, :null => false, :default => 0
        t.column :omit, :integer, :null => false
      end

      assert_equal 0, klass.columns_hash['zero'].default
      assert !klass.columns_hash['zero'].null
      # 0 in MySQL 4, nil in 5.
      assert klass.columns_hash['omit'].default.in?([0, nil])
      assert !klass.columns_hash['omit'].null

      assert_raise(ActiveRecord::StatementInvalid) { klass.create! }

      assert_nothing_raised do
        instance = klass.create!(:omit => 1)
        assert_equal 0, instance.zero
        assert_equal 1, instance.omit
      end
    ensure
      klass.connection.drop_table(klass.table_name) rescue nil
    end
  end
end

if current_adapter?(:PostgreSQLAdapter)
  class DefaultsUsingMultipleSchemasAndDomainTest < ActiveSupport::TestCase
    def setup
      @connection = ActiveRecord::Base.connection

      @old_search_path = @connection.schema_search_path
      @connection.schema_search_path = "schema_1, pg_catalog"
      @connection.create_table "defaults" do |t|
        t.text "text_col", :default => "some value"
        t.string "string_col", :default => "some value"
      end
      Default.reset_column_information
    end

    def test_text_defaults_in_new_schema_when_overriding_domain
      assert_equal "some value", Default.new.text_col, "Default of text column was not correctly parse"
    end

    def test_string_defaults_in_new_schema_when_overriding_domain
      assert_equal "some value", Default.new.string_col, "Default of string column was not correctly parse"
    end

    def test_bpchar_defaults_in_new_schema_when_overriding_domain
      @connection.execute "ALTER TABLE defaults ADD bpchar_col bpchar DEFAULT 'some value'"
      Default.reset_column_information
      assert_equal "some value", Default.new.bpchar_col, "Default of bpchar column was not correctly parse"
    end

    def test_text_defaults_after_updating_column_default
      @connection.execute "ALTER TABLE defaults ALTER COLUMN text_col SET DEFAULT 'some text'::schema_1.text"
      assert_equal "some text", Default.new.text_col, "Default of text column was not correctly parse after updating default using '::text' since postgreSQL will add parens to the default in db"
    end

    def teardown
      @connection.schema_search_path = @old_search_path
      Default.reset_column_information
    end
  end
end