Froze rails gems
[depot.git] / vendor / rails / activerecord / test / cases / reserved_word_test_mysql.rb
1 require "cases/helper"
2
3 class Group < ActiveRecord::Base
4 Group.table_name = 'group'
5 belongs_to :select, :class_name => 'Select'
6 has_one :values
7 end
8
9 class Select < ActiveRecord::Base
10 Select.table_name = 'select'
11 has_many :groups
12 end
13
14 class Values < ActiveRecord::Base
15 Values.table_name = 'values'
16 end
17
18 class Distinct < ActiveRecord::Base
19 Distinct.table_name = 'distinct'
20 has_and_belongs_to_many :selects
21 has_many :values, :through => :groups
22 end
23
24 # a suite of tests to ensure the ConnectionAdapters#MysqlAdapter can handle tables with
25 # reserved word names (ie: group, order, values, etc...)
26 class MysqlReservedWordTest < ActiveRecord::TestCase
27 def setup
28 @connection = ActiveRecord::Base.connection
29
30 # we call execute directly here (and do similar below) because ActiveRecord::Base#create_table()
31 # will fail with these table names if these test cases fail
32
33 create_tables_directly 'group'=>'id int auto_increment primary key, `order` varchar(255), select_id int',
34 'select'=>'id int auto_increment primary key',
35 'values'=>'id int auto_increment primary key, group_id int',
36 'distinct'=>'id int auto_increment primary key',
37 'distincts_selects'=>'distinct_id int, select_id int'
38 end
39
40 def teardown
41 drop_tables_directly ['group', 'select', 'values', 'distinct', 'distincts_selects', 'order']
42 end
43
44 # create tables with reserved-word names and columns
45 def test_create_tables
46 assert_nothing_raised {
47 @connection.create_table :order do |t|
48 t.column :group, :string
49 end
50 }
51 end
52
53 # rename tables with reserved-word names
54 def test_rename_tables
55 assert_nothing_raised { @connection.rename_table(:group, :order) }
56 end
57
58 # alter column with a reserved-word name in a table with a reserved-word name
59 def test_change_columns
60 assert_nothing_raised { @connection.change_column_default(:group, :order, 'whatever') }
61 #the quoting here will reveal any double quoting issues in change_column's interaction with the column method in the adapter
62 assert_nothing_raised { @connection.change_column('group', 'order', :Int, :default => 0) }
63 assert_nothing_raised { @connection.rename_column(:group, :order, :values) }
64 end
65
66 # dump structure of table with reserved word name
67 def test_structure_dump
68 assert_nothing_raised { @connection.structure_dump }
69 end
70
71 # introspect table with reserved word name
72 def test_introspect
73 assert_nothing_raised { @connection.columns(:group) }
74 assert_nothing_raised { @connection.indexes(:group) }
75 end
76
77 #fixtures
78 self.use_instantiated_fixtures = true
79 self.use_transactional_fixtures = false
80
81 #fixtures :group
82
83 def test_fixtures
84 f = create_test_fixtures :select, :distinct, :group, :values, :distincts_selects
85
86 assert_nothing_raised {
87 f.each do |x|
88 x.delete_existing_fixtures
89 end
90 }
91
92 assert_nothing_raised {
93 f.each do |x|
94 x.insert_fixtures
95 end
96 }
97 end
98
99 #activerecord model class with reserved-word table name
100 def test_activerecord_model
101 create_test_fixtures :select, :distinct, :group, :values, :distincts_selects
102 x = nil
103 assert_nothing_raised { x = Group.new }
104 x.order = 'x'
105 assert_nothing_raised { x.save }
106 x.order = 'y'
107 assert_nothing_raised { x.save }
108 assert_nothing_raised { y = Group.find_by_order('y') }
109 assert_nothing_raised { y = Group.find(1) }
110 x = Group.find(1)
111 end
112
113 # has_one association with reserved-word table name
114 def test_has_one_associations
115 create_test_fixtures :select, :distinct, :group, :values, :distincts_selects
116 v = nil
117 assert_nothing_raised { v = Group.find(1).values }
118 assert_equal v.id, 2
119 end
120
121 # belongs_to association with reserved-word table name
122 def test_belongs_to_associations
123 create_test_fixtures :select, :distinct, :group, :values, :distincts_selects
124 gs = nil
125 assert_nothing_raised { gs = Select.find(2).groups }
126 assert_equal gs.length, 2
127 assert(gs.collect{|x| x.id}.sort == [2, 3])
128 end
129
130 # has_and_belongs_to_many with reserved-word table name
131 def test_has_and_belongs_to_many
132 create_test_fixtures :select, :distinct, :group, :values, :distincts_selects
133 s = nil
134 assert_nothing_raised { s = Distinct.find(1).selects }
135 assert_equal s.length, 2
136 assert(s.collect{|x|x.id}.sort == [1, 2])
137 end
138
139 # activerecord model introspection with reserved-word table and column names
140 def test_activerecord_introspection
141 assert_nothing_raised { Group.table_exists? }
142 assert_nothing_raised { Group.columns }
143 end
144
145 # Calculations
146 def test_calculations_work_with_reserved_words
147 assert_nothing_raised { Group.count }
148 end
149
150 def test_associations_work_with_reserved_words
151 assert_nothing_raised { Select.find(:all, :include => [:groups]) }
152 end
153
154 #the following functions were added to DRY test cases
155
156 private
157 # custom fixture loader, uses Fixtures#create_fixtures and appends base_path to the current file's path
158 def create_test_fixtures(*fixture_names)
159 Fixtures.create_fixtures(FIXTURES_ROOT + "/reserved_words", fixture_names)
160 end
161
162 # custom drop table, uses execute on connection to drop a table if it exists. note: escapes table_name
163 def drop_tables_directly(table_names, connection = @connection)
164 table_names.each do |name|
165 connection.execute("DROP TABLE IF EXISTS `#{name}`")
166 end
167 end
168
169 # custom create table, uses execute on connection to create a table, note: escapes table_name, does NOT escape columns
170 def create_tables_directly (tables, connection = @connection)
171 tables.each do |table_name, column_properties|
172 connection.execute("CREATE TABLE `#{table_name}` ( #{column_properties} )")
173 end
174 end
175
176 end