| 1 |
require 'abstract_unit' |
|---|
| 2 |
|
|---|
| 3 |
class AdapterTest < Test::Unit::TestCase |
|---|
| 4 |
def setup |
|---|
| 5 |
@connection = ActiveRecord::Base.connection |
|---|
| 6 |
end |
|---|
| 7 |
|
|---|
| 8 |
def test_tables |
|---|
| 9 |
if @connection.respond_to?(:tables) |
|---|
| 10 |
tables = @connection.tables |
|---|
| 11 |
assert tables.include?("accounts") |
|---|
| 12 |
assert tables.include?("authors") |
|---|
| 13 |
assert tables.include?("tasks") |
|---|
| 14 |
assert tables.include?("topics") |
|---|
| 15 |
else |
|---|
| 16 |
warn "#{@connection.class} does not respond to #tables" |
|---|
| 17 |
end |
|---|
| 18 |
end |
|---|
| 19 |
|
|---|
| 20 |
def test_indexes |
|---|
| 21 |
idx_name = "accounts_idx" |
|---|
| 22 |
|
|---|
| 23 |
if @connection.respond_to?(:indexes) |
|---|
| 24 |
indexes = @connection.indexes("accounts") |
|---|
| 25 |
assert indexes.empty? |
|---|
| 26 |
|
|---|
| 27 |
@connection.add_index :accounts, :firm_id, :name => idx_name |
|---|
| 28 |
indexes = @connection.indexes("accounts") |
|---|
| 29 |
assert_equal "accounts", indexes.first.table |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
assert_equal idx_name, indexes.first.name unless current_adapter?(:OpenBaseAdapter) |
|---|
| 33 |
assert !indexes.first.unique |
|---|
| 34 |
assert_equal ["firm_id"], indexes.first.columns |
|---|
| 35 |
else |
|---|
| 36 |
warn "#{@connection.class} does not respond to #indexes" |
|---|
| 37 |
end |
|---|
| 38 |
|
|---|
| 39 |
ensure |
|---|
| 40 |
@connection.remove_index(:accounts, :name => idx_name) rescue nil |
|---|
| 41 |
end |
|---|
| 42 |
|
|---|
| 43 |
def test_current_database |
|---|
| 44 |
if @connection.respond_to?(:current_database) |
|---|
| 45 |
assert_equal ENV['ARUNIT_DB_NAME'] || "activerecord_unittest", @connection.current_database |
|---|
| 46 |
end |
|---|
| 47 |
end |
|---|
| 48 |
|
|---|
| 49 |
if current_adapter?(:MysqlAdapter) |
|---|
| 50 |
def test_charset |
|---|
| 51 |
assert_not_nil @connection.charset |
|---|
| 52 |
assert_not_equal 'character_set_database', @connection.charset |
|---|
| 53 |
assert_equal @connection.show_variable('character_set_database'), @connection.charset |
|---|
| 54 |
end |
|---|
| 55 |
|
|---|
| 56 |
def test_collation |
|---|
| 57 |
assert_not_nil @connection.collation |
|---|
| 58 |
assert_not_equal 'collation_database', @connection.collation |
|---|
| 59 |
assert_equal @connection.show_variable('collation_database'), @connection.collation |
|---|
| 60 |
end |
|---|
| 61 |
|
|---|
| 62 |
def test_show_nonexistent_variable_returns_nil |
|---|
| 63 |
assert_nil @connection.show_variable('foo_bar_baz') |
|---|
| 64 |
end |
|---|
| 65 |
end |
|---|
| 66 |
|
|---|
| 67 |
def test_table_alias |
|---|
| 68 |
def @connection.test_table_alias_length() 10; end |
|---|
| 69 |
class << @connection |
|---|
| 70 |
alias_method :old_table_alias_length, :table_alias_length |
|---|
| 71 |
alias_method :table_alias_length, :test_table_alias_length |
|---|
| 72 |
end |
|---|
| 73 |
|
|---|
| 74 |
assert_equal 'posts', @connection.table_alias_for('posts') |
|---|
| 75 |
assert_equal 'posts_comm', @connection.table_alias_for('posts_comments') |
|---|
| 76 |
assert_equal 'dbo_posts', @connection.table_alias_for('dbo.posts') |
|---|
| 77 |
|
|---|
| 78 |
class << @connection |
|---|
| 79 |
alias_method :table_alias_length, :old_table_alias_length |
|---|
| 80 |
end |
|---|
| 81 |
end |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!) |
|---|
| 85 |
require 'fixtures/movie' |
|---|
| 86 |
require 'fixtures/subscriber' |
|---|
| 87 |
|
|---|
| 88 |
def test_reset_empty_table_with_custom_pk |
|---|
| 89 |
Movie.delete_all |
|---|
| 90 |
Movie.connection.reset_pk_sequence! 'movies' |
|---|
| 91 |
assert_equal 1, Movie.create(:name => 'fight club').id |
|---|
| 92 |
end |
|---|
| 93 |
|
|---|
| 94 |
if ActiveRecord::Base.connection.adapter_name != "FrontBase" |
|---|
| 95 |
def test_reset_table_with_non_integer_pk |
|---|
| 96 |
Subscriber.delete_all |
|---|
| 97 |
Subscriber.connection.reset_pk_sequence! 'subscribers' |
|---|
| 98 |
sub = Subscriber.new(:name => 'robert drake') |
|---|
| 99 |
sub.id = 'bob drake' |
|---|
| 100 |
assert_nothing_raised { sub.save! } |
|---|
| 101 |
end |
|---|
| 102 |
end |
|---|
| 103 |
end |
|---|
| 104 |
|
|---|
| 105 |
end |
|---|