2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2008-07-14 11:12:54 -04:00
|
|
|
|
2011-01-31 17:38:20 -05:00
|
|
|
module ActiveRecord
|
|
|
|
module ConnectionAdapters
|
|
|
|
class ColumnDefinitionTest < ActiveRecord::TestCase
|
|
|
|
def setup
|
|
|
|
@adapter = AbstractAdapter.new(nil)
|
|
|
|
def @adapter.native_database_types
|
|
|
|
{:string => "varchar"}
|
|
|
|
end
|
2013-03-15 22:22:07 -04:00
|
|
|
@viz = @adapter.schema_creation
|
2011-01-31 17:38:20 -05:00
|
|
|
end
|
2009-08-09 23:43:56 -04:00
|
|
|
|
2011-01-31 17:38:20 -05:00
|
|
|
# Avoid column definitions in create table statements like:
|
|
|
|
# `title` varchar(255) DEFAULT NULL
|
|
|
|
def test_should_not_include_default_clause_when_default_is_null
|
2014-05-22 16:10:25 -04:00
|
|
|
column = Column.new("title", nil, Type::String.new(limit: 20))
|
2011-01-31 17:38:20 -05:00
|
|
|
column_def = ColumnDefinition.new(
|
2013-03-15 22:34:20 -04:00
|
|
|
column.name, "string",
|
2011-01-31 17:38:20 -05:00
|
|
|
column.limit, column.precision, column.scale, column.default, column.null)
|
2013-03-15 22:22:07 -04:00
|
|
|
assert_equal "title varchar(20)", @viz.accept(column_def)
|
2011-01-31 17:38:20 -05:00
|
|
|
end
|
2009-08-09 23:43:56 -04:00
|
|
|
|
2011-01-31 17:38:20 -05:00
|
|
|
def test_should_include_default_clause_when_default_is_present
|
2014-05-22 16:10:25 -04:00
|
|
|
column = Column.new("title", "Hello", Type::String.new(limit: 20))
|
2011-01-31 17:38:20 -05:00
|
|
|
column_def = ColumnDefinition.new(
|
2013-03-15 22:34:20 -04:00
|
|
|
column.name, "string",
|
2011-01-31 17:38:20 -05:00
|
|
|
column.limit, column.precision, column.scale, column.default, column.null)
|
2013-03-15 22:22:07 -04:00
|
|
|
assert_equal %Q{title varchar(20) DEFAULT 'Hello'}, @viz.accept(column_def)
|
2009-08-09 23:43:56 -04:00
|
|
|
end
|
|
|
|
|
2011-01-31 17:38:20 -05:00
|
|
|
def test_should_specify_not_null_if_null_option_is_false
|
2014-05-22 16:10:25 -04:00
|
|
|
column = Column.new("title", "Hello", Type::String.new(limit: 20), "varchar(20)", false)
|
2011-01-31 17:38:20 -05:00
|
|
|
column_def = ColumnDefinition.new(
|
2013-03-15 22:34:20 -04:00
|
|
|
column.name, "string",
|
2011-01-31 17:38:20 -05:00
|
|
|
column.limit, column.precision, column.scale, column.default, column.null)
|
2013-03-15 22:22:07 -04:00
|
|
|
assert_equal %Q{title varchar(20) DEFAULT 'Hello' NOT NULL}, @viz.accept(column_def)
|
2009-08-09 23:43:56 -04:00
|
|
|
end
|
|
|
|
|
2011-01-31 17:38:20 -05:00
|
|
|
if current_adapter?(:MysqlAdapter)
|
|
|
|
def test_should_set_default_for_mysql_binary_data_types
|
2014-05-17 14:24:13 -04:00
|
|
|
binary_column = MysqlAdapter::Column.new("title", "a", Type::Binary.new, "binary(1)")
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_equal "a", binary_column.default
|
2009-08-09 23:43:56 -04:00
|
|
|
|
2014-05-17 14:24:13 -04:00
|
|
|
varbinary_column = MysqlAdapter::Column.new("title", "a", Type::Binary.new, "varbinary(1)")
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_equal "a", varbinary_column.default
|
|
|
|
end
|
2009-08-09 23:43:56 -04:00
|
|
|
|
2011-01-31 17:38:20 -05:00
|
|
|
def test_should_not_set_default_for_blob_and_text_data_types
|
|
|
|
assert_raise ArgumentError do
|
2014-05-17 14:24:13 -04:00
|
|
|
MysqlAdapter::Column.new("title", "a", Type::Binary.new, "blob")
|
2011-01-31 17:38:20 -05:00
|
|
|
end
|
2009-08-09 23:43:56 -04:00
|
|
|
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_raise ArgumentError do
|
2014-05-17 14:24:13 -04:00
|
|
|
MysqlAdapter::Column.new("title", "Hello", Type::Text.new)
|
2011-01-31 17:38:20 -05:00
|
|
|
end
|
2010-03-31 17:05:34 -04:00
|
|
|
|
2014-05-17 14:24:13 -04:00
|
|
|
text_column = MysqlAdapter::Column.new("title", nil, Type::Text.new)
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_equal nil, text_column.default
|
2010-08-02 04:37:57 -04:00
|
|
|
|
2014-05-17 14:24:13 -04:00
|
|
|
not_null_text_column = MysqlAdapter::Column.new("title", nil, Type::Text.new, "text", false)
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_equal "", not_null_text_column.default
|
|
|
|
end
|
2010-08-02 04:37:57 -04:00
|
|
|
|
2014-02-05 03:51:34 -05:00
|
|
|
def test_has_default_should_return_false_for_blob_and_text_data_types
|
2014-05-17 14:24:13 -04:00
|
|
|
blob_column = MysqlAdapter::Column.new("title", nil, Type::Binary.new, "blob")
|
2011-01-31 17:38:20 -05:00
|
|
|
assert !blob_column.has_default?
|
2010-08-02 04:37:57 -04:00
|
|
|
|
2014-05-17 14:24:13 -04:00
|
|
|
text_column = MysqlAdapter::Column.new("title", nil, Type::Text.new)
|
2011-01-31 17:38:20 -05:00
|
|
|
assert !text_column.has_default?
|
|
|
|
end
|
2010-08-02 04:37:57 -04:00
|
|
|
end
|
|
|
|
|
2011-01-31 17:38:20 -05:00
|
|
|
if current_adapter?(:Mysql2Adapter)
|
|
|
|
def test_should_set_default_for_mysql_binary_data_types
|
2014-05-17 14:24:13 -04:00
|
|
|
binary_column = Mysql2Adapter::Column.new("title", "a", Type::Binary.new, "binary(1)")
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_equal "a", binary_column.default
|
2010-08-02 04:37:57 -04:00
|
|
|
|
2014-05-17 14:24:13 -04:00
|
|
|
varbinary_column = Mysql2Adapter::Column.new("title", "a", Type::Binary.new, "varbinary(1)")
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_equal "a", varbinary_column.default
|
|
|
|
end
|
2010-08-02 04:37:57 -04:00
|
|
|
|
2011-01-31 17:38:20 -05:00
|
|
|
def test_should_not_set_default_for_blob_and_text_data_types
|
|
|
|
assert_raise ArgumentError do
|
2014-05-17 14:24:13 -04:00
|
|
|
Mysql2Adapter::Column.new("title", "a", Type::Binary.new, "blob")
|
2011-01-31 17:38:20 -05:00
|
|
|
end
|
2010-08-02 04:37:57 -04:00
|
|
|
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_raise ArgumentError do
|
2014-05-17 14:24:13 -04:00
|
|
|
Mysql2Adapter::Column.new("title", "Hello", Type::Text.new)
|
2011-01-31 17:38:20 -05:00
|
|
|
end
|
2010-08-02 04:37:57 -04:00
|
|
|
|
2014-05-17 14:24:13 -04:00
|
|
|
text_column = Mysql2Adapter::Column.new("title", nil, Type::Text.new)
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_equal nil, text_column.default
|
2010-03-31 17:05:34 -04:00
|
|
|
|
2014-05-17 14:24:13 -04:00
|
|
|
not_null_text_column = Mysql2Adapter::Column.new("title", nil, Type::Text.new, "text", false)
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_equal "", not_null_text_column.default
|
|
|
|
end
|
|
|
|
|
2014-02-05 03:51:34 -05:00
|
|
|
def test_has_default_should_return_false_for_blob_and_text_data_types
|
2014-05-17 14:24:13 -04:00
|
|
|
blob_column = Mysql2Adapter::Column.new("title", nil, Type::Binary.new, "blob")
|
2011-01-31 17:38:20 -05:00
|
|
|
assert !blob_column.has_default?
|
2010-03-31 17:05:34 -04:00
|
|
|
|
2014-05-17 14:24:13 -04:00
|
|
|
text_column = Mysql2Adapter::Column.new("title", nil, Type::Text.new)
|
2011-01-31 17:38:20 -05:00
|
|
|
assert !text_column.has_default?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
def test_bigint_column_should_map_to_integer
|
2014-03-26 12:14:05 -04:00
|
|
|
oid = PostgreSQLAdapter::OID::Integer.new
|
2012-02-10 19:06:39 -05:00
|
|
|
bigint_column = PostgreSQLColumn.new('number', nil, oid, "bigint")
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_equal :integer, bigint_column.type
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_smallint_column_should_map_to_integer
|
2014-03-26 12:14:05 -04:00
|
|
|
oid = PostgreSQLAdapter::OID::Integer.new
|
2012-02-10 19:06:39 -05:00
|
|
|
smallint_column = PostgreSQLColumn.new('number', nil, oid, "smallint")
|
2011-01-31 17:38:20 -05:00
|
|
|
assert_equal :integer, smallint_column.type
|
|
|
|
end
|
|
|
|
end
|
2010-03-31 17:05:34 -04:00
|
|
|
end
|
|
|
|
end
|
2008-08-23 12:51:09 -04:00
|
|
|
end
|