2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2005-09-23 09:29:33 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class AdapterTest < ActiveRecord::TestCase
|
2005-09-23 09:29:33 -04:00
|
|
|
def setup
|
|
|
|
@connection = ActiveRecord::Base.connection
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tables
|
2008-05-06 19:08:23 -04:00
|
|
|
tables = @connection.tables
|
|
|
|
assert tables.include?("accounts")
|
|
|
|
assert tables.include?("authors")
|
|
|
|
assert tables.include?("tasks")
|
|
|
|
assert tables.include?("topics")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_table_exists?
|
|
|
|
assert @connection.table_exists?("accounts")
|
|
|
|
assert !@connection.table_exists?("nonexistingtable")
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_indexes
|
2006-03-17 22:54:04 -05:00
|
|
|
idx_name = "accounts_idx"
|
2007-05-25 20:20:37 -04:00
|
|
|
|
2005-09-23 09:29:33 -04:00
|
|
|
if @connection.respond_to?(:indexes)
|
|
|
|
indexes = @connection.indexes("accounts")
|
|
|
|
assert indexes.empty?
|
|
|
|
|
2006-01-03 23:17:23 -05:00
|
|
|
@connection.add_index :accounts, :firm_id, :name => idx_name
|
2005-09-23 09:29:33 -04:00
|
|
|
indexes = @connection.indexes("accounts")
|
|
|
|
assert_equal "accounts", indexes.first.table
|
2006-03-18 11:50:25 -05:00
|
|
|
# OpenBase does not have the concept of a named index
|
|
|
|
# Indexes are merely properties of columns.
|
|
|
|
assert_equal idx_name, indexes.first.name unless current_adapter?(:OpenBaseAdapter)
|
2005-09-23 09:29:33 -04:00
|
|
|
assert !indexes.first.unique
|
|
|
|
assert_equal ["firm_id"], indexes.first.columns
|
|
|
|
else
|
|
|
|
warn "#{@connection.class} does not respond to #indexes"
|
|
|
|
end
|
|
|
|
|
|
|
|
ensure
|
2006-03-17 22:54:04 -05:00
|
|
|
@connection.remove_index(:accounts, :name => idx_name) rescue nil
|
|
|
|
end
|
2007-05-25 20:20:37 -04:00
|
|
|
|
2006-03-17 22:54:04 -05:00
|
|
|
def test_current_database
|
|
|
|
if @connection.respond_to?(:current_database)
|
2006-04-04 12:33:31 -04:00
|
|
|
assert_equal ENV['ARUNIT_DB_NAME'] || "activerecord_unittest", @connection.current_database
|
2006-03-17 22:54:04 -05:00
|
|
|
end
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
2005-10-16 00:05:15 -04:00
|
|
|
|
2007-05-25 20:20:37 -04:00
|
|
|
if current_adapter?(:MysqlAdapter)
|
|
|
|
def test_charset
|
2007-06-20 14:30:35 -04:00
|
|
|
assert_not_nil @connection.charset
|
|
|
|
assert_not_equal 'character_set_database', @connection.charset
|
|
|
|
assert_equal @connection.show_variable('character_set_database'), @connection.charset
|
2007-05-25 20:20:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_collation
|
2007-06-20 14:30:35 -04:00
|
|
|
assert_not_nil @connection.collation
|
|
|
|
assert_not_equal 'collation_database', @connection.collation
|
|
|
|
assert_equal @connection.show_variable('collation_database'), @connection.collation
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_show_nonexistent_variable_returns_nil
|
|
|
|
assert_nil @connection.show_variable('foo_bar_baz')
|
2007-05-25 20:20:37 -04:00
|
|
|
end
|
2009-08-09 16:22:53 -04:00
|
|
|
|
|
|
|
def test_not_specifying_database_name_for_cross_database_selects
|
2010-05-25 09:11:23 -04:00
|
|
|
begin
|
|
|
|
assert_nothing_raised do
|
|
|
|
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['arunit'].except(:database))
|
|
|
|
ActiveRecord::Base.connection.execute "SELECT activerecord_unittest.pirates.*, activerecord_unittest2.courses.* FROM activerecord_unittest.pirates, activerecord_unittest2.courses"
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.establish_connection 'arunit'
|
2009-08-09 16:22:53 -04:00
|
|
|
end
|
|
|
|
end
|
2007-05-25 20:20:37 -04:00
|
|
|
end
|
|
|
|
|
2008-07-08 00:14:11 -04:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
def test_encoding
|
|
|
|
assert_not_nil @connection.encoding
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-18 00:43:35 -05:00
|
|
|
def test_table_alias
|
2006-03-18 02:31:01 -05:00
|
|
|
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
|
2007-05-25 20:20:37 -04:00
|
|
|
|
2006-03-18 00:43:35 -05:00
|
|
|
assert_equal 'posts', @connection.table_alias_for('posts')
|
|
|
|
assert_equal 'posts_comm', @connection.table_alias_for('posts_comments')
|
2006-03-25 23:13:25 -05:00
|
|
|
assert_equal 'dbo_posts', @connection.table_alias_for('dbo.posts')
|
2007-05-25 20:20:37 -04:00
|
|
|
|
2006-03-18 02:31:01 -05:00
|
|
|
class << @connection
|
2007-12-22 06:26:03 -05:00
|
|
|
remove_method :table_alias_length
|
2006-03-18 02:31:01 -05:00
|
|
|
alias_method :table_alias_length, :old_table_alias_length
|
|
|
|
end
|
2006-03-18 00:43:35 -05:00
|
|
|
end
|
|
|
|
|
2005-10-16 00:05:15 -04:00
|
|
|
# test resetting sequences in odd tables in postgreSQL
|
|
|
|
if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
|
2008-01-18 02:31:37 -05:00
|
|
|
require 'models/movie'
|
|
|
|
require 'models/subscriber'
|
2007-05-25 20:20:37 -04:00
|
|
|
|
2005-10-16 00:05:15 -04:00
|
|
|
def test_reset_empty_table_with_custom_pk
|
|
|
|
Movie.delete_all
|
|
|
|
Movie.connection.reset_pk_sequence! 'movies'
|
|
|
|
assert_equal 1, Movie.create(:name => 'fight club').id
|
|
|
|
end
|
|
|
|
|
2006-04-27 18:39:45 -04:00
|
|
|
if ActiveRecord::Base.connection.adapter_name != "FrontBase"
|
|
|
|
def test_reset_table_with_non_integer_pk
|
|
|
|
Subscriber.delete_all
|
|
|
|
Subscriber.connection.reset_pk_sequence! 'subscribers'
|
|
|
|
sub = Subscriber.new(:name => 'robert drake')
|
|
|
|
sub.id = 'bob drake'
|
|
|
|
assert_nothing_raised { sub.save! }
|
|
|
|
end
|
2005-10-16 00:05:15 -04:00
|
|
|
end
|
|
|
|
end
|
2006-03-18 11:50:25 -05:00
|
|
|
|
2009-04-04 19:19:29 -04:00
|
|
|
def test_uniqueness_violations_are_translated_to_specific_exception
|
|
|
|
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
|
|
|
|
assert_raises(ActiveRecord::RecordNotUnique) do
|
|
|
|
@connection.execute "INSERT INTO subscribers(nick) VALUES('me')"
|
|
|
|
end
|
|
|
|
end
|
2009-04-04 19:42:21 -04:00
|
|
|
|
|
|
|
def test_foreign_key_violations_are_translated_to_specific_exception
|
|
|
|
unless @connection.adapter_name == 'SQLite'
|
|
|
|
assert_raises(ActiveRecord::InvalidForeignKey) do
|
2009-07-23 13:02:04 -04:00
|
|
|
# Oracle adapter uses prefetched primary key values from sequence and passes them to connection adapter insert method
|
|
|
|
if @connection.prefetch_primary_key?
|
|
|
|
id_value = @connection.next_sequence_value(@connection.default_sequence_name("fk_test_has_fk", "id"))
|
|
|
|
@connection.execute "INSERT INTO fk_test_has_fk (id, fk_id) VALUES (#{id_value},0)"
|
|
|
|
else
|
|
|
|
@connection.execute "INSERT INTO fk_test_has_fk (fk_id) VALUES (0)"
|
|
|
|
end
|
2009-04-04 19:42:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-03-25 11:27:34 -04:00
|
|
|
|
|
|
|
def test_add_limit_offset_should_sanitize_sql_injection_for_limit_without_comas
|
|
|
|
sql_inject = "1 select * from schema"
|
2010-06-08 15:07:51 -04:00
|
|
|
assert_no_match(/schema/, @connection.add_limit_offset!("", :limit=>sql_inject))
|
|
|
|
assert_no_match(/schema/, @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7))
|
2010-03-25 11:27:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_limit_offset_should_sanitize_sql_injection_for_limit_with_comas
|
|
|
|
sql_inject = "1, 7 procedure help()"
|
2010-06-08 15:07:51 -04:00
|
|
|
assert_no_match(/procedure/, @connection.add_limit_offset!("", :limit=>sql_inject))
|
|
|
|
assert_no_match(/procedure/, @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7))
|
2010-03-25 11:27:34 -04:00
|
|
|
end
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|