diff options
Diffstat (limited to 'activerecord/test')
-rw-r--r-- | activerecord/test/cases/bind_parameter_test.rb | 57 | ||||
-rw-r--r-- | activerecord/test/cases/migration/column_attributes_test.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/migration_test.rb | 34 |
3 files changed, 86 insertions, 11 deletions
diff --git a/activerecord/test/cases/bind_parameter_test.rb b/activerecord/test/cases/bind_parameter_test.rb index ca97a5dc65..8c3fe0437c 100644 --- a/activerecord/test/cases/bind_parameter_test.rb +++ b/activerecord/test/cases/bind_parameter_test.rb @@ -34,6 +34,49 @@ if ActiveRecord::Base.connection.prepared_statements ActiveSupport::Notifications.unsubscribe(@subscription) end + def test_statement_cache + @connection.clear_cache! + + topics = Topic.where(id: 1) + assert_equal [1], topics.map(&:id) + assert_includes statement_cache, to_sql_key(topics.arel) + end + + def test_statement_cache_with_query_cache + @connection.enable_query_cache! + @connection.clear_cache! + + topics = Topic.where(id: 1) + assert_equal [1], topics.map(&:id) + assert_includes statement_cache, to_sql_key(topics.arel) + ensure + @connection.disable_query_cache! + end + + def test_statement_cache_with_find + @connection.clear_cache! + + topics = Topic.where(id: 1).limit(1) + assert_equal 1, Topic.find(1).id + assert_includes statement_cache, to_sql_key(topics.arel) + end + + def test_statement_cache_with_in_clause + @connection.clear_cache! + + topics = Topic.where(id: [1, 3]) + assert_equal [1, 3], topics.map(&:id) + assert_not_includes statement_cache, to_sql_key(topics.arel) + end + + def test_statement_cache_with_sql_string_literal + @connection.clear_cache! + + topics = Topic.where("topics.id = ?", 1) + assert_equal [1], topics.map(&:id) + assert_not_includes statement_cache, to_sql_key(topics.arel) + end + def test_too_many_binds bind_params_length = @connection.send(:bind_params_length) @@ -45,7 +88,8 @@ if ActiveRecord::Base.connection.prepared_statements end def test_too_many_binds_with_query_cache - Topic.connection.enable_query_cache! + @connection.enable_query_cache! + bind_params_length = @connection.send(:bind_params_length) topics = Topic.where(id: (1 .. bind_params_length + 1).to_a) assert_equal Topic.count, topics.count @@ -53,7 +97,7 @@ if ActiveRecord::Base.connection.prepared_statements topics = Topic.where.not(id: (1 .. bind_params_length + 1).to_a) assert_equal 0, topics.count ensure - Topic.connection.disable_query_cache! + @connection.disable_query_cache! end def test_bind_from_join_in_subquery @@ -90,6 +134,15 @@ if ActiveRecord::Base.connection.prepared_statements end private + def to_sql_key(arel) + sql = @connection.to_sql(arel) + @connection.respond_to?(:sql_key, true) ? @connection.send(:sql_key, sql) : sql + end + + def statement_cache + @connection.instance_variable_get(:@statements).send(:cache) + end + def assert_logs_binds(binds) payload = { name: "SQL", 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 |