3
class AdapterTest < ActiveRecord::TestCase
5
@connection = ActiveRecord::Base.connection
9
tables = @connection.tables
10
assert tables.include?("accounts")
11
assert tables.include?("authors")
12
assert tables.include?("tasks")
13
assert tables.include?("topics")
16
def test_table_exists?
17
assert @connection.table_exists?("accounts")
18
assert !@connection.table_exists?("nonexistingtable")
22
idx_name = "accounts_idx"
24
if @connection.respond_to?(:indexes)
25
indexes = @connection.indexes("accounts")
28
@connection.add_index :accounts, :firm_id, :name => idx_name
29
indexes = @connection.indexes("accounts")
30
assert_equal "accounts", indexes.first.table
31
# OpenBase does not have the concept of a named index
32
# Indexes are merely properties of columns.
33
assert_equal idx_name, indexes.first.name unless current_adapter?(:OpenBaseAdapter)
34
assert !indexes.first.unique
35
assert_equal ["firm_id"], indexes.first.columns
37
warn "#{@connection.class} does not respond to #indexes"
41
@connection.remove_index(:accounts, :name => idx_name) rescue nil
44
def test_current_database
45
if @connection.respond_to?(:current_database)
46
assert_equal ENV['ARUNIT_DB_NAME'] || "activerecord_unittest", @connection.current_database
50
if current_adapter?(:MysqlAdapter)
52
assert_not_nil @connection.charset
53
assert_not_equal 'character_set_database', @connection.charset
54
assert_equal @connection.show_variable('character_set_database'), @connection.charset
58
assert_not_nil @connection.collation
59
assert_not_equal 'collation_database', @connection.collation
60
assert_equal @connection.show_variable('collation_database'), @connection.collation
63
def test_show_nonexistent_variable_returns_nil
64
assert_nil @connection.show_variable('foo_bar_baz')
67
def test_not_specifying_database_name_for_cross_database_selects
68
assert_nothing_raised do
69
ActiveRecord::Base.establish_connection({
73
ActiveRecord::Base.connection.execute "SELECT activerecord_unittest.pirates.*, activerecord_unittest2.courses.* FROM activerecord_unittest.pirates, activerecord_unittest2.courses"
76
ActiveRecord::Base.establish_connection 'arunit'
80
if current_adapter?(:PostgreSQLAdapter)
82
assert_not_nil @connection.encoding
87
def @connection.test_table_alias_length() 10; end
89
alias_method :old_table_alias_length, :table_alias_length
90
alias_method :table_alias_length, :test_table_alias_length
93
assert_equal 'posts', @connection.table_alias_for('posts')
94
assert_equal 'posts_comm', @connection.table_alias_for('posts_comments')
95
assert_equal 'dbo_posts', @connection.table_alias_for('dbo.posts')
98
remove_method :table_alias_length
99
alias_method :table_alias_length, :old_table_alias_length
103
# test resetting sequences in odd tables in postgreSQL
104
if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
105
require 'models/movie'
106
require 'models/subscriber'
108
def test_reset_empty_table_with_custom_pk
110
Movie.connection.reset_pk_sequence! 'movies'
111
assert_equal 1, Movie.create(:name => 'fight club').id
114
if ActiveRecord::Base.connection.adapter_name != "FrontBase"
115
def test_reset_table_with_non_integer_pk
116
Subscriber.delete_all
117
Subscriber.connection.reset_pk_sequence! 'subscribers'
118
sub = Subscriber.new(:name => 'robert drake')
120
assert_nothing_raised { sub.save! }
125
def test_add_limit_offset_should_sanitize_sql_injection_for_limit_without_comas
126
sql_inject = "1 select * from schema"
127
assert_equal " LIMIT 1", @connection.add_limit_offset!("", :limit=>sql_inject)
128
if current_adapter?(:MysqlAdapter)
129
assert_equal " LIMIT 7, 1", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
131
assert_equal " LIMIT 1 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
135
def test_add_limit_offset_should_sanitize_sql_injection_for_limit_with_comas
136
sql_inject = "1, 7 procedure help()"
137
if current_adapter?(:MysqlAdapter)
138
assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit=>sql_inject)
139
assert_equal " LIMIT 7, 1", @connection.add_limit_offset!("", :limit=> '1 ; DROP TABLE USERS', :offset=>7)
141
assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit=>sql_inject)
142
assert_equal " LIMIT 1,7 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)