~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activerecord/test/cases/adapter_test.rb

  • Committer: Richard Lee (Canonical)
  • Date: 2010-10-15 15:17:58 UTC
  • mfrom: (190.1.3 use-case-mapper)
  • Revision ID: richard.lee@canonical.com-20101015151758-wcvmfxrexsongf9d
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
67
 
    def test_not_specifying_database_name_for_cross_database_selects
68
 
      assert_nothing_raised do
69
 
        ActiveRecord::Base.establish_connection({
70
 
          :adapter  => 'mysql',
71
 
          :username => 'rails'
72
 
        })
73
 
        ActiveRecord::Base.connection.execute "SELECT activerecord_unittest.pirates.*, activerecord_unittest2.courses.* FROM activerecord_unittest.pirates, activerecord_unittest2.courses"
74
 
      end
75
 
 
76
 
      ActiveRecord::Base.establish_connection 'arunit'
77
 
    end
78
 
  end
79
 
 
80
 
  if current_adapter?(:PostgreSQLAdapter)
81
 
    def test_encoding
82
 
      assert_not_nil @connection.encoding
83
 
    end
84
 
  end
85
 
 
86
 
  def test_table_alias
87
 
    def @connection.test_table_alias_length() 10; end
88
 
    class << @connection
89
 
      alias_method :old_table_alias_length, :table_alias_length
90
 
      alias_method :table_alias_length,     :test_table_alias_length
91
 
    end
92
 
 
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')
96
 
 
97
 
    class << @connection
98
 
      remove_method :table_alias_length
99
 
      alias_method :table_alias_length, :old_table_alias_length
100
 
    end
101
 
  end
102
 
 
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'
107
 
 
108
 
    def test_reset_empty_table_with_custom_pk
109
 
      Movie.delete_all
110
 
      Movie.connection.reset_pk_sequence! 'movies'
111
 
      assert_equal 1, Movie.create(:name => 'fight club').id
112
 
    end
113
 
 
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')
119
 
        sub.id = 'bob drake'
120
 
        assert_nothing_raised { sub.save! }
121
 
      end
122
 
    end
123
 
  end
124
 
 
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)
130
 
    else
131
 
      assert_equal " LIMIT 1 OFFSET 7", @connection.add_limit_offset!("", :limit=>sql_inject, :offset=>7)
132
 
    end
133
 
  end
134
 
 
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)
140
 
    else
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)
143
 
    end
144
 
  end
145
 
end