class ActiveRecord::AdapterTest

Public Instance Methods

setup() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 11
def setup
  @connection = ActiveRecord::Base.connection
end
test_charset() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 111
def test_charset
  assert_not_nil @connection.charset
  assert_not_equal "character_set_database", @connection.charset
  assert_equal @connection.show_variable("character_set_database"), @connection.charset
end
test_collation() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 117
def test_collation
  assert_not_nil @connection.collation
  assert_not_equal "collation_database", @connection.collation
  assert_equal @connection.show_variable("collation_database"), @connection.collation
end
test_create_record_with_pk_as_zero() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 29
def test_create_record_with_pk_as_zero
  Book.create(id: 0)
  assert_equal 0, Book.find(0).id
  assert_nothing_raised { Book.destroy(0) }
end
test_current_database() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 104
def test_current_database
  if @connection.respond_to?(:current_database)
    assert_equal ARTest.connection_config["arunit"]["database"], @connection.current_database
  end
end
test_data_source_exists?() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 69
def test_data_source_exists?
  assert @connection.data_source_exists?("accounts")
  assert @connection.data_source_exists?(:accounts)
  assert_not @connection.data_source_exists?("nonexistingtable")
  assert_not @connection.data_source_exists?("'")
  assert_not @connection.data_source_exists?(nil)
end
test_data_sources() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 61
def test_data_sources
  data_sources = @connection.data_sources
  assert_includes data_sources, "accounts"
  assert_includes data_sources, "authors"
  assert_includes data_sources, "tasks"
  assert_includes data_sources, "topics"
end
test_exceptions_from_notifications_are_not_translated() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 196
def test_exceptions_from_notifications_are_not_translated
  original_error = StandardError.new("This StandardError shouldn't get translated")
  subscriber = ActiveSupport::Notifications.subscribe("sql.active_record") { raise original_error }
  actual_error = assert_raises(StandardError) do
    @connection.execute("SELECT * FROM posts")
  end

  assert_equal original_error, actual_error

ensure
  ActiveSupport::Notifications.unsubscribe(subscriber) if subscriber
end
test_indexes() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 77
def test_indexes
  idx_name = "accounts_idx"

  indexes = @connection.indexes("accounts")
  assert indexes.empty?

  @connection.add_index :accounts, :firm_id, name: idx_name
  indexes = @connection.indexes("accounts")
  assert_equal "accounts", indexes.first.table
  assert_equal idx_name, indexes.first.name
  assert !indexes.first.unique
  assert_equal ["firm_id"], indexes.first.columns
ensure
  @connection.remove_index(:accounts, name: idx_name) rescue nil
end
test_insert_update_delete_with_binds() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 247
def test_insert_update_delete_with_binds
  binds = [Relation::QueryAttribute.new("id", 1, Type.default_value)]
  bind_param = Arel::Nodes::BindParam.new(nil)

  id = @connection.insert("INSERT INTO events(id) VALUES (#{bind_param.to_sql})", nil, nil, nil, nil, binds)
  assert_equal 1, id

  @connection.update("UPDATE events SET title = 'foo' WHERE id = #{bind_param.to_sql}", nil, binds)
  result = @connection.select_all("SELECT * FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
  assert_equal({ "id" => 1, "title" => "foo" }, result.first)

  @connection.delete("DELETE FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
  result = @connection.select_all("SELECT * FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
  assert_nil result.first
end
test_insert_update_delete_with_legacy_binds() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 231
def test_insert_update_delete_with_legacy_binds
  binds = [[nil, 1]]
  bind_param = Arel::Nodes::BindParam.new(nil)

  id = @connection.insert("INSERT INTO events(id) VALUES (#{bind_param.to_sql})", nil, nil, nil, nil, binds)
  assert_equal 1, id

  @connection.update("UPDATE events SET title = 'foo' WHERE id = #{bind_param.to_sql}", nil, binds)
  result = @connection.select_all("SELECT * FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
  assert_equal({ "id" => 1, "title" => "foo" }, result.first)

  @connection.delete("DELETE FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
  result = @connection.select_all("SELECT * FROM events WHERE id = #{bind_param.to_sql}", nil, binds)
  assert_nil result.first
end
test_invalid_column() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 41
def test_invalid_column
  assert_not @connection.valid_type?(:foobar)
end
test_log_invalid_encoding() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 288
def test_log_invalid_encoding
  error = assert_raises RuntimeError do
    @connection.send :log, "SELECT 'ы' FROM DUAL" do
      raise "ы".dup.force_encoding(Encoding::ASCII_8BIT)
    end
  end

  assert_equal "ы", error.message
end
test_not_null_violations_are_translated_to_specific_exception() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 170
def test_not_null_violations_are_translated_to_specific_exception
  error = assert_raises(ActiveRecord::NotNullViolation) do
    Post.create
  end

  assert_not_nil error.cause
end
test_not_specifying_database_name_for_cross_database_selects() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 127
def test_not_specifying_database_name_for_cross_database_selects
  begin
    assert_nothing_raised do
      ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations["arunit"].except(:database))

      config = ARTest.connection_config
      ActiveRecord::Base.connection.execute(
        "SELECT #{config['arunit']['database']}.pirates.*, #{config['arunit2']['database']}.courses.* " \
        "FROM #{config['arunit']['database']}.pirates, #{config['arunit2']['database']}.courses"
      )
    end
  ensure
    ActiveRecord::Base.establish_connection :arunit
  end
end
test_numeric_value_out_of_ranges_are_translated_to_specific_exception() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 187
def test_numeric_value_out_of_ranges_are_translated_to_specific_exception
  error = assert_raises(ActiveRecord::RangeError) do
    Book.connection.create("INSERT INTO books(author_id) VALUES (9223372036854775808)")
  end

  assert_not_nil error.cause
end
test_remove_index_when_name_and_wrong_column_name_specified() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 93
def test_remove_index_when_name_and_wrong_column_name_specified
  index_name = "accounts_idx"

  @connection.add_index :accounts, :firm_id, name: index_name
  assert_raises ArgumentError do
    @connection.remove_index :accounts, name: index_name, column: :wrong_column_name
  end
ensure
  @connection.remove_index(:accounts, name: index_name)
end
test_select_all_always_return_activerecord_result() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 218
def test_select_all_always_return_activerecord_result
  result = @connection.select_all "SELECT * FROM posts"
  assert result.is_a?(ActiveRecord::Result)
end
test_select_all_with_legacy_binds() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 224
def test_select_all_with_legacy_binds
  post = Post.create!(title: "foo", body: "bar")
  expected = @connection.select_all("SELECT * FROM posts WHERE id = #{post.id}")
  result = @connection.select_all("SELECT * FROM posts WHERE id = #{Arel::Nodes::BindParam.new(nil).to_sql}", nil, [[nil, post.id]])
  assert_equal expected.to_hash, result.to_hash
end
test_select_methods_passing_a_association_relation() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 264
def test_select_methods_passing_a_association_relation
  author = Author.create!(name: "john")
  Post.create!(author: author, title: "foo", body: "bar")
  query = author.posts.where(title: "foo").select(:title)
  assert_equal({ "title" => "foo" }, @connection.select_one(query))
  assert @connection.select_all(query).is_a?(ActiveRecord::Result)
  assert_equal "foo", @connection.select_value(query)
  assert_equal ["foo"], @connection.select_values(query)
end
test_select_methods_passing_a_relation() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 274
def test_select_methods_passing_a_relation
  Post.create!(title: "foo", body: "bar")
  query = Post.where(title: "foo").select(:title)
  assert_equal({ "title" => "foo" }, @connection.select_one(query))
  assert @connection.select_all(query).is_a?(ActiveRecord::Result)
  assert_equal "foo", @connection.select_value(query)
  assert_equal ["foo"], @connection.select_values(query)
end
test_show_nonexistent_variable_returns_nil() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 123
def test_show_nonexistent_variable_returns_nil
  assert_nil @connection.show_variable("foo_bar_baz")
end
test_table_alias() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 144
def test_table_alias
  def @connection.test_table_alias_length() 10; end
  class << @connection
    alias_method :old_table_alias_length, :table_alias_length
    alias_method :table_alias_length,     :test_table_alias_length
  end

  assert_equal "posts",      @connection.table_alias_for("posts")
  assert_equal "posts_comm", @connection.table_alias_for("posts_comments")
  assert_equal "dbo_posts",  @connection.table_alias_for("dbo.posts")

  class << @connection
    remove_method :table_alias_length
    alias_method :table_alias_length, :old_table_alias_length
  end
end
test_table_exists?() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 53
def test_table_exists?
  assert @connection.table_exists?("accounts")
  assert @connection.table_exists?(:accounts)
  assert_not @connection.table_exists?("nonexistingtable")
  assert_not @connection.table_exists?("'")
  assert_not @connection.table_exists?(nil)
end
test_tables() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 45
def test_tables
  tables = @connection.tables
  assert_includes tables, "accounts"
  assert_includes tables, "authors"
  assert_includes tables, "tasks"
  assert_includes tables, "topics"
end
test_uniqueness_violations_are_translated_to_specific_exception() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 161
def test_uniqueness_violations_are_translated_to_specific_exception
  @connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
  error = assert_raises(ActiveRecord::RecordNotUnique) do
    @connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
  end

  assert_not_nil error.cause
end
test_update_prepared_statement() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 19
def test_update_prepared_statement
  b = Book.create(name: "my \x00 book")
  b.reload
  assert_equal "my \x00 book", b.name
  b.update_attributes(name: "my other \x00 book")
  b.reload
  assert_equal "my other \x00 book", b.name
end
test_valid_column() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 35
def test_valid_column
  @connection.native_database_types.each_key do |type|
    assert @connection.valid_type?(type)
  end
end
test_value_limit_violations_are_translated_to_specific_exception() click to toggle source
# File activerecord/test/cases/adapter_test.rb, line 179
def test_value_limit_violations_are_translated_to_specific_exception
  error = assert_raises(ActiveRecord::ValueTooLong) do
    Event.create(title: "abcdefgh")
  end

  assert_not_nil error.cause
end