Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / schema_test_postgresql.rb
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 INDEX_A_NAME = 'a_index_things_on_name'
10 INDEX_B_NAME = 'b_index_things_on_different_columns_in_each_schema'
11 INDEX_A_COLUMN = 'name'
12 INDEX_B_COLUMN_S1 = 'email'
13 INDEX_B_COLUMN_S2 = 'moment'
14 COLUMNS = [
15 'id integer',
16 'name character varying(50)',
17 'email character varying(50)',
18 'moment timestamp without time zone default now()'
19 ]
20
21 def setup
22 @connection = ActiveRecord::Base.connection
23 @connection.execute "CREATE SCHEMA #{SCHEMA_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
24 @connection.execute "CREATE SCHEMA #{SCHEMA2_NAME} CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
25 @connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});"
26 @connection.execute "CREATE INDEX #{INDEX_A_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_A_COLUMN});"
27 @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA_NAME}.#{TABLE_NAME} USING btree (#{INDEX_B_COLUMN_S1});"
28 @connection.execute "CREATE INDEX #{INDEX_B_NAME} ON #{SCHEMA2_NAME}.#{TABLE_NAME} USING btree (#{INDEX_B_COLUMN_S2});"
29 end
30
31 def teardown
32 @connection.execute "DROP SCHEMA #{SCHEMA2_NAME} CASCADE"
33 @connection.execute "DROP SCHEMA #{SCHEMA_NAME} CASCADE"
34 end
35
36 def test_with_schema_prefixed_table_name
37 assert_nothing_raised do
38 assert_equal COLUMNS, columns("#{SCHEMA_NAME}.#{TABLE_NAME}")
39 end
40 end
41
42 def test_with_schema_search_path
43 assert_nothing_raised do
44 with_schema_search_path(SCHEMA_NAME) do
45 assert_equal COLUMNS, columns(TABLE_NAME)
46 end
47 end
48 end
49
50 def test_raise_on_unquoted_schema_name
51 assert_raise(ActiveRecord::StatementInvalid) do
52 with_schema_search_path '$user,public'
53 end
54 end
55
56 def test_without_schema_search_path
57 assert_raise(ActiveRecord::StatementInvalid) { columns(TABLE_NAME) }
58 end
59
60 def test_ignore_nil_schema_search_path
61 assert_nothing_raised { with_schema_search_path nil }
62 end
63
64 def test_dump_indexes_for_schema_one
65 do_dump_index_tests_for_schema(SCHEMA_NAME, INDEX_A_COLUMN, INDEX_B_COLUMN_S1)
66 end
67
68 def test_dump_indexes_for_schema_two
69 do_dump_index_tests_for_schema(SCHEMA2_NAME, INDEX_A_COLUMN, INDEX_B_COLUMN_S2)
70 end
71
72 private
73 def columns(table_name)
74 @connection.send(:column_definitions, table_name).map do |name, type, default|
75 "#{name} #{type}" + (default ? " default #{default}" : '')
76 end
77 end
78
79 def with_schema_search_path(schema_search_path)
80 @connection.schema_search_path = schema_search_path
81 yield if block_given?
82 ensure
83 @connection.schema_search_path = "'$user', public"
84 end
85
86 def do_dump_index_tests_for_schema(this_schema_name, first_index_column_name, second_index_column_name)
87 with_schema_search_path(this_schema_name) do
88 indexes = @connection.indexes(TABLE_NAME).sort_by {|i| i.name}
89 assert_equal 2,indexes.size
90
91 do_dump_index_assertions_for_one_index(indexes[0], INDEX_A_NAME, first_index_column_name)
92 do_dump_index_assertions_for_one_index(indexes[1], INDEX_B_NAME, second_index_column_name)
93 end
94 end
95
96 def do_dump_index_assertions_for_one_index(this_index, this_index_name, this_index_column)
97 assert_equal TABLE_NAME, this_index.table
98 assert_equal 1, this_index.columns.size
99 assert_equal this_index_column, this_index.columns[0]
100 assert_equal this_index_name, this_index.name
101 end
102 end