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')
68 if current_adapter
?(:PostgreSQLAdapter)
70 assert_not_nil
@connection.encoding
75 def @connection.test_table_alias_length() 10; end
77 alias_method
:old_table_alias_length, :table_alias_length
78 alias_method
:table_alias_length, :test_table_alias_length
81 assert_equal
'posts', @connection.table_alias_for('posts')
82 assert_equal
'posts_comm', @connection.table_alias_for('posts_comments')
83 assert_equal
'dbo_posts', @connection.table_alias_for('dbo.posts')
86 remove_method
:table_alias_length
87 alias_method
:table_alias_length, :old_table_alias_length
91 # test resetting sequences in odd tables in postgreSQL
92 if ActiveRecord
::Base.connection
.respond_to
?(:reset_pk_sequence!)
93 require 'models/movie'
94 require 'models/subscriber'
96 def test_reset_empty_table_with_custom_pk
98 Movie
.connection
.reset_pk_sequence
! 'movies'
99 assert_equal
1, Movie
.create(:name => 'fight club').id
102 if ActiveRecord
::Base.connection
.adapter_name
!= "FrontBase"
103 def test_reset_table_with_non_integer_pk
104 Subscriber
.delete_all
105 Subscriber
.connection
.reset_pk_sequence
! 'subscribers'
106 sub
= Subscriber
.new(:name => 'robert drake')
108 assert_nothing_raised
{ sub
.save
! }
113 def test_add_limit_offset_should_sanitize_sql_injection_for_limit_without_comas
114 sql_inject
= "1 select * from schema"
115 assert_equal
" LIMIT 1", @connection.add_limit_offset
!("", :limit=>sql_inject
)
116 if current_adapter
?(:MysqlAdapter)
117 assert_equal
" LIMIT 7, 1", @connection.add_limit_offset
!("", :limit=>sql_inject
, :offset=>7)
119 assert_equal
" LIMIT 1 OFFSET 7", @connection.add_limit_offset
!("", :limit=>sql_inject
, :offset=>7)
123 def test_add_limit_offset_should_sanitize_sql_injection_for_limit_with_comas
124 sql_inject
= "1, 7 procedure help()"
125 if current_adapter
?(:MysqlAdapter)
126 assert_equal
" LIMIT 1,7", @connection.add_limit_offset
!("", :limit=>sql_inject
)
127 assert_equal
" LIMIT 7, 1", @connection.add_limit_offset
!("", :limit=> '1 ; DROP TABLE USERS', :offset=>7)
129 assert_equal
" LIMIT 1,7", @connection.add_limit_offset
!("", :limit=>sql_inject
)
130 assert_equal
" LIMIT 1,7 OFFSET 7", @connection.add_limit_offset
!("", :limit=>sql_inject
, :offset=>7)