3 class Group
< ActiveRecord
::Base
4 Group
.table_name
= 'group'
5 belongs_to
:select, :class_name => 'Select'
9 class Select
< ActiveRecord
::Base
10 Select
.table_name
= 'select'
14 class Values
< ActiveRecord
::Base
15 Values
.table_name
= 'values'
18 class Distinct
< ActiveRecord
::Base
19 Distinct
.table_name
= 'distinct'
20 has_and_belongs_to_many
:selects
21 has_many
:values, :through => :groups
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
28 @connection = ActiveRecord
::Base.connection
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
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'
41 drop_tables_directly
['group', 'select', 'values', 'distinct', 'distincts_selects', 'order']
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
53 # rename tables with reserved-word names
54 def test_rename_tables
55 assert_nothing_raised
{ @connection.rename_table(:group, :order) }
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) }
66 # dump structure of table with reserved word name
67 def test_structure_dump
68 assert_nothing_raised
{ @connection.structure_dump
}
71 # introspect table with reserved word name
73 assert_nothing_raised
{ @connection.columns(:group) }
74 assert_nothing_raised
{ @connection.indexes(:group) }
78 self.use_instantiated_fixtures
= true
79 self.use_transactional_fixtures
= false
84 f
= create_test_fixtures
:select, :distinct, :group, :values, :distincts_selects
86 assert_nothing_raised
{
88 x
.delete_existing_fixtures
92 assert_nothing_raised
{
99 #activerecord model class with reserved-word table name
100 def test_activerecord_model
101 create_test_fixtures
:select, :distinct, :group, :values, :distincts_selects
103 assert_nothing_raised
{ x
= Group
.new
}
105 assert_nothing_raised
{ x
.save
}
107 assert_nothing_raised
{ x
.save
}
108 assert_nothing_raised
{ y
= Group
.find_by_order('y') }
109 assert_nothing_raised
{ y
= Group
.find(1) }
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
117 assert_nothing_raised
{ v
= Group
.find(1).values
}
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
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])
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
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])
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
}
146 def test_calculations_work_with_reserved_words
147 assert_nothing_raised
{ Group
.count
}
150 def test_associations_work_with_reserved_words
151 assert_nothing_raised
{ Select
.find(:all, :include => [:groups]) }
154 #the following functions were added to DRY test cases
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
)
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}`")
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} )")