~michaelforrest/use-case-mapper/trunk

« back to all changes in this revision

Viewing changes to vendor/rails/activerecord/test/cases/schema_test_postgresql.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 SchemaTest < ActiveRecord::TestCase
4
 
  self.use_transactional_fixtures = false
5
 
 
6
 
  SCHEMA_NAME = 'test_schema'
7
 
  SCHEMA2_NAME = 'test_schema2'
8
 
  TABLE_NAME = 'things'
9
 
  CAPITALIZED_TABLE_NAME = 'Things'
10
 
  INDEX_A_NAME = 'a_index_things_on_name'
11
 
  INDEX_B_NAME = 'b_index_things_on_different_columns_in_each_schema'
12
 
  INDEX_A_COLUMN = 'name'
13
 
  INDEX_B_COLUMN_S1 = 'email'
14
 
  INDEX_B_COLUMN_S2 = 'moment'
15
 
  COLUMNS = [
16
 
    'id integer',
17
 
    'name character varying(50)',
18
 
    'email character varying(50)',
19
 
    'moment timestamp without time zone default now()'
20
 
  ]
21
 
 
22
 
  class Thing1 < ActiveRecord::Base
23
 
    set_table_name "test_schema.things"
24
 
  end
25
 
 
26
 
  class Thing2 < ActiveRecord::Base
27
 
    set_table_name "test_schema2.things"
28
 
  end
29
 
 
30
 
  class Thing3 < ActiveRecord::Base
31
 
    set_table_name 'test_schema."things.table"'
32
 
  end
33
 
 
34
 
  class Thing4 < ActiveRecord::Base
35
 
    set_table_name 'test_schema."Things"'
36
 
  end
37
 
 
38
 
  def setup
39
 
    @connection = ActiveRecord::Base.connection
40
 
    @connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
41
 
    @connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{TABLE_NAME}.table\" (#{COLUMNS.join(',')})"
42
 
    @connection.execute "CREATE TABLE #{SCHEMA_NAME}.\"#{CAPITALIZED_TABLE_NAME}\" (#{COLUMNS.join(',')})"
43
 
    @connection.execute "CREATE SCHEMA #{SCHEMA2_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
44
 
    @connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_A_COLUMN});"
45
 
    @connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_A_COLUMN});"
46
 
    @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_B_COLUMN_S1});"
47
 
    @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME}  USING btree (#{INDEX_B_COLUMN_S2});"
48
 
  end
49
 
 
50
 
  def teardown
51
 
    @connection.execute "DROP SCHEMA #{SCHEMA2_NAME} CASCADE"
52
 
    @connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE"
53
 
  end
54
 
 
55
 
  def test_with_schema_prefixed_table_name
56
 
    assert_nothing_raised do
57
 
      assert_equal COLUMNS, columns("#{SCHEMA_NAME}.#{TABLE_NAME}")
58
 
    end
59
 
  end
60
 
 
61
 
  def test_with_schema_prefixed_capitalized_table_name
62
 
    assert_nothing_raised do
63
 
      assert_equal COLUMNS, columns("#{SCHEMA_NAME}.#{CAPITALIZED_TABLE_NAME}")
64
 
    end
65
 
  end
66
 
 
67
 
  def test_with_schema_search_path
68
 
    assert_nothing_raised do
69
 
      with_schema_search_path(SCHEMA_NAME) do
70
 
        assert_equal COLUMNS, columns(TABLE_NAME)
71
 
      end
72
 
    end
73
 
  end
74
 
 
75
 
 
76
 
  def test_proper_encoding_of_table_name
77
 
    assert_equal '"table_name"', @connection.quote_table_name('table_name')
78
 
    assert_equal '"table.name"', @connection.quote_table_name('"table.name"')
79
 
    assert_equal '"schema_name"."table_name"', @connection.quote_table_name('schema_name.table_name')
80
 
    assert_equal '"schema_name"."table.name"', @connection.quote_table_name('schema_name."table.name"')
81
 
    assert_equal '"schema.name"."table_name"', @connection.quote_table_name('"schema.name".table_name')
82
 
    assert_equal '"schema.name"."table.name"', @connection.quote_table_name('"schema.name"."table.name"')
83
 
  end
84
 
 
85
 
  def test_classes_with_qualified_schema_name
86
 
    assert_equal 0, Thing1.count
87
 
    assert_equal 0, Thing2.count
88
 
    assert_equal 0, Thing3.count
89
 
    assert_equal 0, Thing4.count
90
 
 
91
 
    Thing1.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
92
 
    assert_equal 1, Thing1.count
93
 
    assert_equal 0, Thing2.count
94
 
    assert_equal 0, Thing3.count
95
 
    assert_equal 0, Thing4.count
96
 
 
97
 
    Thing2.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
98
 
    assert_equal 1, Thing1.count
99
 
    assert_equal 1, Thing2.count
100
 
    assert_equal 0, Thing3.count
101
 
    assert_equal 0, Thing4.count
102
 
 
103
 
    Thing3.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
104
 
    assert_equal 1, Thing1.count
105
 
    assert_equal 1, Thing2.count
106
 
    assert_equal 1, Thing3.count
107
 
    assert_equal 0, Thing4.count
108
 
 
109
 
    Thing4.create(:id => 1, :name => "thing1", :email => "thing1@localhost", :moment => Time.now)
110
 
    assert_equal 1, Thing1.count
111
 
    assert_equal 1, Thing2.count
112
 
    assert_equal 1, Thing3.count
113
 
    assert_equal 1, Thing4.count
114
 
  end
115
 
 
116
 
  def test_raise_on_unquoted_schema_name
117
 
    assert_raise(ActiveRecord::StatementInvalid) do
118
 
      with_schema_search_path '$user,public'
119
 
    end
120
 
  end
121
 
 
122
 
  def test_without_schema_search_path
123
 
    assert_raise(ActiveRecord::StatementInvalid) { columns(TABLE_NAME) }
124
 
  end
125
 
 
126
 
  def test_ignore_nil_schema_search_path
127
 
    assert_nothing_raised { with_schema_search_path nil }
128
 
  end
129
 
 
130
 
  def test_dump_indexes_for_schema_one
131
 
    do_dump_index_tests_for_schema(SCHEMA_NAME, INDEX_A_COLUMN, INDEX_B_COLUMN_S1)
132
 
  end
133
 
 
134
 
  def test_dump_indexes_for_schema_two
135
 
    do_dump_index_tests_for_schema(SCHEMA2_NAME, INDEX_A_COLUMN, INDEX_B_COLUMN_S2)
136
 
  end
137
 
 
138
 
  def test_with_uppercase_index_name
139
 
    ActiveRecord::Base.connection.execute "CREATE INDEX \"things_Index\" ON #{SCHEMA_NAME}.things (name)"
140
 
    assert_nothing_raised { ActiveRecord::Base.connection.remove_index :things, :name => "#{SCHEMA_NAME}.things_Index"}
141
 
 
142
 
    ActiveRecord::Base.connection.execute "CREATE INDEX \"things_Index\" ON #{SCHEMA_NAME}.things (name)"
143
 
    ActiveRecord::Base.connection.schema_search_path = SCHEMA_NAME
144
 
    assert_nothing_raised { ActiveRecord::Base.connection.remove_index :things, :name => "things_Index"}
145
 
    ActiveRecord::Base.connection.schema_search_path = "public"
146
 
  end
147
 
 
148
 
  private
149
 
    def columns(table_name)
150
 
      @connection.send(:column_definitions, table_name).map do |name, type, default|
151
 
        "#{name} #{type}" + (default ? " default #{default}" : '')
152
 
      end
153
 
    end
154
 
 
155
 
    def with_schema_search_path(schema_search_path)
156
 
      @connection.schema_search_path = schema_search_path
157
 
      yield if block_given?
158
 
    ensure
159
 
      @connection.schema_search_path = "'$user', public"
160
 
    end
161
 
 
162
 
    def do_dump_index_tests_for_schema(this_schema_name, first_index_column_name, second_index_column_name)
163
 
      with_schema_search_path(this_schema_name) do
164
 
        indexes = @connection.indexes(TABLE_NAME).sort_by {|i| i.name}
165
 
        assert_equal 2,indexes.size
166
 
 
167
 
        do_dump_index_assertions_for_one_index(indexes[0], INDEX_A_NAME, first_index_column_name)
168
 
        do_dump_index_assertions_for_one_index(indexes[1], INDEX_B_NAME, second_index_column_name)
169
 
      end
170
 
    end
171
 
 
172
 
    def do_dump_index_assertions_for_one_index(this_index, this_index_name, this_index_column)
173
 
      assert_equal TABLE_NAME, this_index.table
174
 
      assert_equal 1, this_index.columns.size
175
 
      assert_equal this_index_column, this_index.columns[0]
176
 
      assert_equal this_index_name, this_index.name
177
 
    end
178
 
end