Updated to lastest rails + a patch of our own (in url_helper, ticket #294)
[gitorious:lachies-clone.git] / vendor / rails / activerecord / test / cases / adapter_test.rb
1 require "cases/helper"
2
3 class AdapterTest < ActiveRecord::TestCase
4   def setup
5     @connection = ActiveRecord::Base.connection
6   end
7
8   def test_tables
9     tables = @connection.tables
10     assert tables.include?("accounts")
11     assert tables.include?("authors")
12     assert tables.include?("tasks")
13     assert tables.include?("topics")
14   end
15
16   def test_table_exists?
17     assert @connection.table_exists?("accounts")
18     assert !@connection.table_exists?("nonexistingtable")
19   end
20
21   def test_indexes
22     idx_name = "accounts_idx"
23
24     if @connection.respond_to?(:indexes)
25       indexes = @connection.indexes("accounts")
26       assert indexes.empty?
27
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
36     else
37       warn "#{@connection.class} does not respond to #indexes"
38     end
39
40   ensure
41     @connection.remove_index(:accounts, :name => idx_name) rescue nil
42   end
43
44   def test_current_database
45     if @connection.respond_to?(:current_database)
46       assert_equal ENV['ARUNIT_DB_NAME'] || "activerecord_unittest", @connection.current_database
47     end
48   end
49
50   if current_adapter?(:MysqlAdapter)
51     def test_charset
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
55     end
56
57     def test_collation
58       assert_not_nil @connection.collation
59       assert_not_equal 'collation_database', @connection.collation
60       assert_equal @connection.show_variable('collation_database'), @connection.collation
61     end
62
63     def test_show_nonexistent_variable_returns_nil
64       assert_nil @connection.show_variable('foo_bar_baz')
65     end
66   end
67
68   def test_table_alias
69     def @connection.test_table_alias_length() 10; end
70     class << @connection
71       alias_method :old_table_alias_length, :table_alias_length
72       alias_method :table_alias_length,     :test_table_alias_length
73     end
74
75     assert_equal 'posts',      @connection.table_alias_for('posts')
76     assert_equal 'posts_comm', @connection.table_alias_for('posts_comments')
77     assert_equal 'dbo_posts',  @connection.table_alias_for('dbo.posts')
78
79     class << @connection
80       remove_method :table_alias_length
81       alias_method :table_alias_length, :old_table_alias_length
82     end
83   end
84
85   # test resetting sequences in odd tables in postgreSQL
86   if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
87     require 'models/movie'
88     require 'models/subscriber'
89
90     def test_reset_empty_table_with_custom_pk
91       Movie.delete_all
92       Movie.connection.reset_pk_sequence! 'movies'
93       assert_equal 1, Movie.create(:name => 'fight club').id
94     end
95
96     if ActiveRecord::Base.connection.adapter_name != "FrontBase"
97       def test_reset_table_with_non_integer_pk
98         Subscriber.delete_all
99         Subscriber.connection.reset_pk_sequence! 'subscribers'
100         sub = Subscriber.new(:name => 'robert drake')
101         sub.id = 'bob drake'
102         assert_nothing_raised { sub.save! }
103       end
104     end
105   end
106
107   def test_add_limit_offset_should_sanitize_sql_injection_for_limit_without_comas
108     sql_inject = "1 select * from schema"
109       assert_equal " LIMIT 1", @connection.add_limit_offset!("", :limit=>sql_inject)
110     if current_adapter?(:MysqlAdapter)
111       assert_equal " LIMIT 7, 1", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
112     else
113       assert_equal " LIMIT 1 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
114     end
115   end
116
117   def test_add_limit_offset_should_sanitize_sql_injection_for_limit_with_comas
118     sql_inject = "1, 7 procedure help()"
119     if current_adapter?(:MysqlAdapter)
120       assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit=>sql_inject)
121       assert_equal " LIMIT 7, 1", @connection.add_limit_offset!("", :limit=> '1 ; DROP TABLE USERS', :offset=>7)
122     else
123       assert_equal " LIMIT 1,7", @connection.add_limit_offset!("", :limit=>sql_inject)
124       assert_equal " LIMIT 1,7 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
125     end
126   end
127 end