2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/default'
|
|
|
|
require 'models/entrant'
|
2006-02-09 13:06:29 -05:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class DefaultTest < ActiveRecord::TestCase
|
2006-11-20 03:54:42 -05:00
|
|
|
def test_nil_defaults_for_not_null_columns
|
|
|
|
column_defaults =
|
2008-05-31 15:36:07 -04:00
|
|
|
if current_adapter?(:MysqlAdapter) && Mysql.client_version < 50051
|
2006-11-20 06:44:31 -05:00
|
|
|
{ 'id' => nil, 'name' => '', 'course_id' => nil }
|
2006-11-20 03:54:42 -05:00
|
|
|
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
|
|
|
|
|
2007-01-12 03:46:01 -05:00
|
|
|
if current_adapter?(:MysqlAdapter)
|
|
|
|
# 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
|
2007-12-04 21:46:28 -05:00
|
|
|
# 0 in MySQL 4, nil in 5.
|
|
|
|
assert [0, nil].include?(klass.columns_hash['omit'].default)
|
2007-01-12 03:46:01 -05:00
|
|
|
assert !klass.columns_hash['omit'].null
|
|
|
|
|
2007-01-12 03:50:35 -05:00
|
|
|
assert_raise(ActiveRecord::StatementInvalid) { klass.create! }
|
2007-01-12 03:52:08 -05:00
|
|
|
|
|
|
|
assert_nothing_raised do
|
|
|
|
instance = klass.create!(:omit => 1)
|
|
|
|
assert_equal 0, instance.zero
|
|
|
|
assert_equal 1, instance.omit
|
|
|
|
end
|
2007-01-12 03:46:01 -05:00
|
|
|
ensure
|
|
|
|
klass.connection.drop_table(klass.table_name) rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-01-14 19:34:43 -05:00
|
|
|
if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter)
|
2006-02-09 13:06:29 -05:00
|
|
|
def test_default_integers
|
|
|
|
default = Default.new
|
2006-07-08 16:35:56 -04:00
|
|
|
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
|
2006-02-09 14:32:32 -05:00
|
|
|
end
|
2006-02-09 13:06:29 -05:00
|
|
|
end
|
2007-10-08 02:17:53 -04:00
|
|
|
|
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
def test_multiline_default_text
|
2008-02-21 22:26:21 -05:00
|
|
|
# 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)
|
2007-10-08 02:17:53 -04:00
|
|
|
end
|
|
|
|
end
|
2006-02-09 13:06:29 -05:00
|
|
|
end
|