2 require 'models/default'
3 require 'models/entrant'
5 class DefaultTest
< ActiveRecord
::TestCase
6 def test_nil_defaults_for_not_null_columns
8 if current_adapter
?(:MysqlAdapter) && (Mysql
.client_version
< 50051 || (50100..50122).include?(Mysql
.client_version
))
9 { 'id' => nil, 'name' => '', 'course_id' => nil }
11 { 'id' => nil, 'name' => nil, 'course_id' => nil }
14 column_defaults
.each
do |name
, default
|
15 column
= Entrant
.columns_hash
[name
]
16 assert
!column
.null
, "#{name} column should be NOT NULL"
17 assert_equal default
, column
.default
, "#{name} column should be DEFAULT #{default.inspect}"
21 if current_adapter
?(:MysqlAdapter)
23 #MySQL 5 and higher is quirky with not null text/blob columns.
24 #With MySQL Text/blob columns cannot have defaults. If the column is not null MySQL will report that the column has a null default
25 #but it behaves as though the column had a default of ''
26 def test_mysql_text_not_null_defaults
27 klass
= Class
.new(ActiveRecord
::Base)
28 klass
.table_name
= 'test_mysql_text_not_null_defaults'
29 klass
.connection
.create_table klass
.table_name
do |t
|
30 t
.column
:non_null_text, :text, :null => false
31 t
.column
:non_null_blob, :blob, :null => false
32 t
.column
:null_text, :text, :null => true
33 t
.column
:null_blob, :blob, :null => true
35 assert_equal
'', klass
.columns_hash
['non_null_blob'].default
36 assert_equal
'', klass
.columns_hash
['non_null_text'].default
38 assert_equal
nil, klass
.columns_hash
['null_blob'].default
39 assert_equal
nil, klass
.columns_hash
['null_text'].default
41 assert_nothing_raised
do
42 instance
= klass
.create
!
43 assert_equal
'', instance
.non_null_text
44 assert_equal
'', instance
.non_null_blob
45 assert_nil instance
.null_text
46 assert_nil instance
.null_blob
49 klass
.connection
.drop_table(klass
.table_name
) rescue nil
53 # MySQL uses an implicit default 0 rather than NULL unless in strict mode.
54 # We use an implicit NULL so schema.rb is compatible with other databases.
55 def test_mysql_integer_not_null_defaults
56 klass
= Class
.new(ActiveRecord
::Base)
57 klass
.table_name
= 'test_integer_not_null_default_zero'
58 klass
.connection
.create_table klass
.table_name
do |t
|
59 t
.column
:zero, :integer, :null => false, :default => 0
60 t
.column
:omit, :integer, :null => false
63 assert_equal
0, klass
.columns_hash
['zero'].default
64 assert
!klass
.columns_hash
['zero'].null
65 # 0 in MySQL 4, nil in 5.
66 assert
[0, nil].include?(klass
.columns_hash
['omit'].default
)
67 assert
!klass
.columns_hash
['omit'].null
69 assert_raise(ActiveRecord
::StatementInvalid) { klass
.create
! }
71 assert_nothing_raised
do
72 instance
= klass
.create
!(:omit => 1)
73 assert_equal
0, instance
.zero
74 assert_equal
1, instance
.omit
77 klass
.connection
.drop_table(klass
.table_name
) rescue nil
81 if current_adapter
?(:PostgreSQLAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter)
82 def test_default_integers
84 assert_instance_of Fixnum
, default
.positive_integer
85 assert_equal
1, default
.positive_integer
86 assert_instance_of Fixnum
, default
.negative_integer
87 assert_equal
-1, default
.negative_integer
88 assert_instance_of BigDecimal
, default
.decimal_number
89 assert_equal BigDecimal
.new("2.78"), default
.decimal_number
93 if current_adapter
?(:PostgreSQLAdapter)
94 def test_multiline_default_text
95 # older postgres versions represent the default with escapes ("\\012" for a newline)
96 assert ( "--- []\n\n" == Default
.columns_hash
['multiline_default'].default
||
97 "--- []\\012\\012" == Default
.columns_hash
['multiline_default'].default
)