From 67732efcb658807929be0e968642ee1d90b3c986 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 26 Feb 2019 02:30:41 +0900 Subject: Refactor `type_to_sql` to handle converting `limit` to `size` in itself Also, improving an argument error message for `limit`, extracting around `type_to_sql` code into schema statements, and more exercise tests. --- .../test/cases/migration/column_attributes_test.rb | 6 ++-- activerecord/test/cases/migration_test.rb | 34 ++++++++++++++++++---- 2 files changed, 31 insertions(+), 9 deletions(-) (limited to 'activerecord/test') diff --git a/activerecord/test/cases/migration/column_attributes_test.rb b/activerecord/test/cases/migration/column_attributes_test.rb index 3022121f4c..6f9190c110 100644 --- a/activerecord/test/cases/migration/column_attributes_test.rb +++ b/activerecord/test/cases/migration/column_attributes_test.rb @@ -177,10 +177,8 @@ module ActiveRecord if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter) def test_out_of_range_limit_should_raise assert_raise(ActiveRecordError) { add_column :test_models, :integer_too_big, :integer, limit: 10 } - - unless current_adapter?(:PostgreSQLAdapter) - assert_raise(ActiveRecordError) { add_column :test_models, :text_too_big, :text, limit: 0xfffffffff } - end + assert_raise(ActiveRecordError) { add_column :test_models, :text_too_big, :text, limit: 0xfffffffff } + assert_raise(ActiveRecordError) { add_column :test_models, :binary_too_big, :binary, limit: 0xfffffffff } end end end diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 02031e51ef..0ecd93412e 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -600,6 +600,18 @@ class MigrationTest < ActiveRecord::TestCase end end + def test_decimal_scale_without_precision_should_raise + e = assert_raise(ArgumentError) do + Person.connection.create_table :test_decimal_scales, force: true do |t| + t.decimal :scaleonly, scale: 10 + end + end + + assert_equal "Error adding decimal column: precision cannot be empty if scale is specified", e.message + ensure + Person.connection.drop_table :test_decimal_scales, if_exists: true + end + if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter) def test_out_of_range_integer_limit_should_raise e = assert_raise(ActiveRecord::ActiveRecordError, "integer limit didn't raise") do @@ -608,13 +620,11 @@ class MigrationTest < ActiveRecord::TestCase end end - assert_match(/No integer type has byte size 10/, e.message) + assert_includes e.message, "No integer type has byte size 10" ensure Person.connection.drop_table :test_integer_limits, if_exists: true end - end - if current_adapter?(:Mysql2Adapter) def test_out_of_range_text_limit_should_raise e = assert_raise(ActiveRecord::ActiveRecordError, "text limit didn't raise") do Person.connection.create_table :test_text_limits, force: true do |t| @@ -622,11 +632,25 @@ class MigrationTest < ActiveRecord::TestCase end end - assert_match(/No text type has byte length #{0xfffffffff}/, e.message) + assert_includes e.message, "No text type has byte size #{0xfffffffff}" + ensure + Person.connection.drop_table :test_text_limits, if_exists: true + end + + def test_out_of_range_binary_limit_should_raise + e = assert_raise(ActiveRecord::ActiveRecordError) do + Person.connection.create_table :test_text_limits, force: true do |t| + t.binary :bigbinary, limit: 0xfffffffff + end + end + + assert_includes e.message, "No binary type has byte size #{0xfffffffff}" ensure Person.connection.drop_table :test_text_limits, if_exists: true end + end + if current_adapter?(:Mysql2Adapter) def test_invalid_text_size_should_raise e = assert_raise(ArgumentError) do Person.connection.create_table :test_text_sizes, force: true do |t| @@ -634,7 +658,7 @@ class MigrationTest < ActiveRecord::TestCase end end - assert_match(/#{0xfffffffff} is invalid :size value\. Only :tiny, :medium, and :long are allowed\./, e.message) + assert_equal "#{0xfffffffff} is invalid :size value. Only :tiny, :medium, and :long are allowed.", e.message ensure Person.connection.drop_table :test_text_sizes, if_exists: true end -- cgit v1.2.3