Froze rails gems
[depot.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 if current_adapter?(:PostgreSQLAdapter)
69 def test_encoding
70 assert_not_nil @connection.encoding
71 end
72 end
73
74 def test_table_alias
75 def @connection.test_table_alias_length() 10; end
76 class << @connection
77 alias_method :old_table_alias_length, :table_alias_length
78 alias_method :table_alias_length, :test_table_alias_length
79 end
80
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')
84
85 class << @connection
86 remove_method :table_alias_length
87 alias_method :table_alias_length, :old_table_alias_length
88 end
89 end
90
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'
95
96 def test_reset_empty_table_with_custom_pk
97 Movie.delete_all
98 Movie.connection.reset_pk_sequence! 'movies'
99 assert_equal 1, Movie.create(:name => 'fight club').id
100 end
101
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')
107 sub.id = 'bob drake'
108 assert_nothing_raised { sub.save! }
109 end
110 end
111 end
112
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)
118 else
119 assert_equal " LIMIT 1 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
120 end
121 end
122
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)
128 else
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)
131 end
132 end
133 end