2008-07-14 11:12:54 -04:00
|
|
|
require "cases/helper"
|
|
|
|
|
|
|
|
class ColumnDefinitionTest < ActiveRecord::TestCase
|
|
|
|
def setup
|
|
|
|
@adapter = ActiveRecord::ConnectionAdapters::AbstractAdapter.new(nil)
|
|
|
|
def @adapter.native_database_types
|
|
|
|
{:string => "varchar"}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Avoid column definitions in create table statements like:
|
2008-08-23 12:51:09 -04:00
|
|
|
# `title` varchar(255) DEFAULT NULL
|
2008-07-14 11:12:54 -04:00
|
|
|
def test_should_not_include_default_clause_when_default_is_null
|
|
|
|
column = ActiveRecord::ConnectionAdapters::Column.new("title", nil, "varchar(20)")
|
|
|
|
column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
|
|
|
|
@adapter, column.name, "string",
|
|
|
|
column.limit, column.precision, column.scale, column.default, column.null)
|
2008-08-23 12:51:09 -04:00
|
|
|
assert_equal "title varchar(20)", column_def.to_sql
|
2008-07-14 11:12:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_include_default_clause_when_default_is_present
|
|
|
|
column = ActiveRecord::ConnectionAdapters::Column.new("title", "Hello", "varchar(20)")
|
|
|
|
column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
|
|
|
|
@adapter, column.name, "string",
|
|
|
|
column.limit, column.precision, column.scale, column.default, column.null)
|
2008-08-23 12:51:09 -04:00
|
|
|
assert_equal %Q{title varchar(20) DEFAULT 'Hello'}, column_def.to_sql
|
2008-07-14 11:12:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_specify_not_null_if_null_option_is_false
|
|
|
|
column = ActiveRecord::ConnectionAdapters::Column.new("title", "Hello", "varchar(20)", false)
|
|
|
|
column_def = ActiveRecord::ConnectionAdapters::ColumnDefinition.new(
|
|
|
|
@adapter, column.name, "string",
|
|
|
|
column.limit, column.precision, column.scale, column.default, column.null)
|
|
|
|
assert_equal %Q{title varchar(20) DEFAULT 'Hello' NOT NULL}, column_def.to_sql
|
|
|
|
end
|
2009-08-09 23:43:56 -04:00
|
|
|
|
|
|
|
if current_adapter?(:MysqlAdapter)
|
|
|
|
def test_should_set_default_for_mysql_binary_data_types
|
|
|
|
binary_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "a", "binary(1)")
|
|
|
|
assert_equal "a", binary_column.default
|
|
|
|
|
|
|
|
varbinary_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "a", "varbinary(1)")
|
|
|
|
assert_equal "a", varbinary_column.default
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_set_default_for_blob_and_text_data_types
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "a", "blob")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", "Hello", "text")
|
|
|
|
end
|
|
|
|
|
|
|
|
text_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "text")
|
|
|
|
assert_equal nil, text_column.default
|
|
|
|
|
|
|
|
not_null_text_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "text", false)
|
|
|
|
assert_equal "", not_null_text_column.default
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_default_should_return_false_for_blog_and_test_data_types
|
|
|
|
blob_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "blob")
|
|
|
|
assert !blob_column.has_default?
|
|
|
|
|
|
|
|
text_column = ActiveRecord::ConnectionAdapters::MysqlColumn.new("title", nil, "text")
|
|
|
|
assert !text_column.has_default?
|
|
|
|
end
|
|
|
|
end
|
2010-03-31 17:05:34 -04:00
|
|
|
|
2010-08-02 04:37:57 -04:00
|
|
|
if current_adapter?(:Mysql2Adapter)
|
|
|
|
def test_should_set_default_for_mysql_binary_data_types
|
|
|
|
binary_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "a", "binary(1)")
|
|
|
|
assert_equal "a", binary_column.default
|
|
|
|
|
|
|
|
varbinary_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "a", "varbinary(1)")
|
|
|
|
assert_equal "a", varbinary_column.default
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_set_default_for_blob_and_text_data_types
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "a", "blob")
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_raise ArgumentError do
|
|
|
|
ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", "Hello", "text")
|
|
|
|
end
|
|
|
|
|
|
|
|
text_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "text")
|
|
|
|
assert_equal nil, text_column.default
|
|
|
|
|
|
|
|
not_null_text_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "text", false)
|
|
|
|
assert_equal "", not_null_text_column.default
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_default_should_return_false_for_blog_and_test_data_types
|
|
|
|
blob_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "blob")
|
|
|
|
assert !blob_column.has_default?
|
|
|
|
|
|
|
|
text_column = ActiveRecord::ConnectionAdapters::Mysql2Column.new("title", nil, "text")
|
|
|
|
assert !text_column.has_default?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-31 17:05:34 -04:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
def test_bigint_column_should_map_to_integer
|
|
|
|
bigint_column = ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new('number', nil, "bigint")
|
2010-05-18 21:47:24 -04:00
|
|
|
assert_equal :integer, bigint_column.type
|
2010-03-31 17:05:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_smallint_column_should_map_to_integer
|
|
|
|
smallint_column = ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new('number', nil, "smallint")
|
2010-05-18 21:47:24 -04:00
|
|
|
assert_equal :integer, smallint_column.type
|
2010-03-31 17:05:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_uuid_column_should_map_to_string
|
|
|
|
uuid_column = ActiveRecord::ConnectionAdapters::PostgreSQLColumn.new('unique_id', nil, "uuid")
|
2010-05-18 21:47:24 -04:00
|
|
|
assert_equal :string, uuid_column.type
|
2010-03-31 17:05:34 -04:00
|
|
|
end
|
|
|
|
end
|
2008-08-23 12:51:09 -04:00
|
|
|
end
|