Skip to content

Commit 1db36c4

Browse files
committed
Clean up tables after each test.
Follow-Up to rails#14400 This ensures that all tables are removed after each test and thereby allowing us to run the tests in a random order.
1 parent 9d44b3f commit 1db36c4

File tree

2 files changed

+96
-92
lines changed

2 files changed

+96
-92
lines changed

activerecord/test/cases/adapters/mysql/connection_test.rb

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
require "cases/helper"
2+
require 'support/ddl_helper'
23

34
class MysqlConnectionTest < ActiveRecord::TestCase
5+
include DdlHelper
6+
47
class Klass < ActiveRecord::Base
58
end
69

@@ -69,59 +72,50 @@ def test_bind_value_substitute
6972
end
7073

7174
def test_exec_no_binds
72-
@connection.exec_query('drop table if exists ex')
73-
@connection.exec_query(<<-eosql)
74-
CREATE TABLE `ex` (`id` int(11) auto_increment PRIMARY KEY,
75-
`data` varchar(255))
76-
eosql
77-
result = @connection.exec_query('SELECT id, data FROM ex')
78-
assert_equal 0, result.rows.length
79-
assert_equal 2, result.columns.length
80-
assert_equal %w{ id data }, result.columns
75+
with_example_table do
76+
result = @connection.exec_query('SELECT id, data FROM ex')
77+
assert_equal 0, result.rows.length
78+
assert_equal 2, result.columns.length
79+
assert_equal %w{ id data }, result.columns
8180

82-
@connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
81+
@connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
8382

84-
# if there are no bind parameters, it will return a string (due to
85-
# the libmysql api)
86-
result = @connection.exec_query('SELECT id, data FROM ex')
87-
assert_equal 1, result.rows.length
88-
assert_equal 2, result.columns.length
83+
# if there are no bind parameters, it will return a string (due to
84+
# the libmysql api)
85+
result = @connection.exec_query('SELECT id, data FROM ex')
86+
assert_equal 1, result.rows.length
87+
assert_equal 2, result.columns.length
8988

90-
assert_equal [['1', 'foo']], result.rows
89+
assert_equal [['1', 'foo']], result.rows
90+
end
9191
end
9292

9393
def test_exec_with_binds
94-
@connection.exec_query('drop table if exists ex')
95-
@connection.exec_query(<<-eosql)
96-
CREATE TABLE `ex` (`id` int(11) auto_increment PRIMARY KEY,
97-
`data` varchar(255))
98-
eosql
99-
@connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
100-
result = @connection.exec_query(
101-
'SELECT id, data FROM ex WHERE id = ?', nil, [[nil, 1]])
94+
with_example_table do
95+
@connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
96+
result = @connection.exec_query(
97+
'SELECT id, data FROM ex WHERE id = ?', nil, [[nil, 1]])
10298

103-
assert_equal 1, result.rows.length
104-
assert_equal 2, result.columns.length
99+
assert_equal 1, result.rows.length
100+
assert_equal 2, result.columns.length
105101

106-
assert_equal [[1, 'foo']], result.rows
102+
assert_equal [[1, 'foo']], result.rows
103+
end
107104
end
108105

109106
def test_exec_typecasts_bind_vals
110-
@connection.exec_query('drop table if exists ex')
111-
@connection.exec_query(<<-eosql)
112-
CREATE TABLE `ex` (`id` int(11) auto_increment PRIMARY KEY,
113-
`data` varchar(255))
114-
eosql
115-
@connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
116-
column = @connection.columns('ex').find { |col| col.name == 'id' }
107+
with_example_table do
108+
@connection.exec_query('INSERT INTO ex (id, data) VALUES (1, "foo")')
109+
column = @connection.columns('ex').find { |col| col.name == 'id' }
117110

118-
result = @connection.exec_query(
119-
'SELECT id, data FROM ex WHERE id = ?', nil, [[column, '1-fuu']])
111+
result = @connection.exec_query(
112+
'SELECT id, data FROM ex WHERE id = ?', nil, [[column, '1-fuu']])
120113

121-
assert_equal 1, result.rows.length
122-
assert_equal 2, result.columns.length
114+
assert_equal 1, result.rows.length
115+
assert_equal 2, result.columns.length
123116

124-
assert_equal [[1, 'foo']], result.rows
117+
assert_equal [[1, 'foo']], result.rows
118+
end
125119
end
126120

127121
# Test that MySQL allows multiple results for stored procedures
@@ -174,4 +168,12 @@ def run_without_connection
174168
ActiveRecord::Base.establish_connection(original_connection)
175169
end
176170
end
171+
172+
def with_example_table(&block)
173+
definition ||= <<-SQL
174+
`id` int(11) auto_increment PRIMARY KEY,
175+
`data` varchar(255)
176+
SQL
177+
super(@connection, 'ex', definition, &block)
178+
end
177179
end
Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
# encoding: utf-8
22

33
require "cases/helper"
4+
require 'support/ddl_helper'
45

56
module ActiveRecord
67
module ConnectionAdapters
78
class MysqlAdapterTest < ActiveRecord::TestCase
9+
include DdlHelper
10+
811
def setup
912
@conn = ActiveRecord::Base.connection
10-
@conn.exec_query('drop table if exists ex')
11-
@conn.exec_query(<<-eosql)
12-
CREATE TABLE `ex` (
13-
`id` int(11) auto_increment PRIMARY KEY,
14-
`number` integer,
15-
`data` varchar(255))
16-
eosql
1713
end
1814

1915
def test_bad_connection_mysql
@@ -25,8 +21,10 @@ def test_bad_connection_mysql
2521
end
2622

2723
def test_valid_column
28-
column = @conn.columns('ex').find { |col| col.name == 'id' }
29-
assert @conn.valid_type?(column.type)
24+
with_example_table do
25+
column = @conn.columns('ex').find { |col| col.name == 'id' }
26+
assert @conn.valid_type?(column.type)
27+
end
3028
end
3129

3230
def test_invalid_column
@@ -38,31 +36,35 @@ def test_client_encoding
3836
end
3937

4038
def test_exec_insert_number
41-
insert(@conn, 'number' => 10)
39+
with_example_table do
40+
insert(@conn, 'number' => 10)
4241

43-
result = @conn.exec_query('SELECT number FROM ex WHERE number = 10')
42+
result = @conn.exec_query('SELECT number FROM ex WHERE number = 10')
4443

45-
assert_equal 1, result.rows.length
46-
# if there are no bind parameters, it will return a string (due to
47-
# the libmysql api)
48-
assert_equal '10', result.rows.last.last
44+
assert_equal 1, result.rows.length
45+
# if there are no bind parameters, it will return a string (due to
46+
# the libmysql api)
47+
assert_equal '10', result.rows.last.last
48+
end
4949
end
5050

5151
def test_exec_insert_string
52-
str = 'いただきます!'
53-
insert(@conn, 'number' => 10, 'data' => str)
52+
with_example_table do
53+
str = 'いただきます!'
54+
insert(@conn, 'number' => 10, 'data' => str)
5455

55-
result = @conn.exec_query('SELECT number, data FROM ex WHERE number = 10')
56+
result = @conn.exec_query('SELECT number, data FROM ex WHERE number = 10')
5657

57-
value = result.rows.last.last
58+
value = result.rows.last.last
5859

59-
# FIXME: this should probably be inside the mysql AR adapter?
60-
value.force_encoding(@conn.client_encoding)
60+
# FIXME: this should probably be inside the mysql AR adapter?
61+
value.force_encoding(@conn.client_encoding)
6162

62-
# The strings in this file are utf-8, so transcode to utf-8
63-
value.encode!(Encoding::UTF_8)
63+
# The strings in this file are utf-8, so transcode to utf-8
64+
value.encode!(Encoding::UTF_8)
6465

65-
assert_equal str, value
66+
assert_equal str, value
67+
end
6668
end
6769

6870
def test_tables_quoting
@@ -74,46 +76,37 @@ def test_tables_quoting
7476
end
7577

7678
def test_pk_and_sequence_for
77-
pk, seq = @conn.pk_and_sequence_for('ex')
78-
assert_equal 'id', pk
79-
assert_equal @conn.default_sequence_name('ex', 'id'), seq
79+
with_example_table do
80+
pk, seq = @conn.pk_and_sequence_for('ex')
81+
assert_equal 'id', pk
82+
assert_equal @conn.default_sequence_name('ex', 'id'), seq
83+
end
8084
end
8185

8286
def test_pk_and_sequence_for_with_non_standard_primary_key
83-
@conn.exec_query('drop table if exists ex_with_non_standard_pk')
84-
@conn.exec_query(<<-eosql)
85-
CREATE TABLE `ex_with_non_standard_pk` (
86-
`code` INT(11) auto_increment,
87-
PRIMARY KEY (`code`))
88-
eosql
89-
pk, seq = @conn.pk_and_sequence_for('ex_with_non_standard_pk')
90-
assert_equal 'code', pk
91-
assert_equal @conn.default_sequence_name('ex_with_non_standard_pk', 'code'), seq
87+
with_example_table '`code` INT(11) auto_increment, PRIMARY KEY (`code`)' do
88+
pk, seq = @conn.pk_and_sequence_for('ex')
89+
assert_equal 'code', pk
90+
assert_equal @conn.default_sequence_name('ex', 'code'), seq
91+
end
9292
end
9393

9494
def test_pk_and_sequence_for_with_custom_index_type_pk
95-
@conn.exec_query('drop table if exists ex_with_custom_index_type_pk')
96-
@conn.exec_query(<<-eosql)
97-
CREATE TABLE `ex_with_custom_index_type_pk` (
98-
`id` INT(11) auto_increment,
99-
PRIMARY KEY USING BTREE (`id`))
100-
eosql
101-
pk, seq = @conn.pk_and_sequence_for('ex_with_custom_index_type_pk')
102-
assert_equal 'id', pk
103-
assert_equal @conn.default_sequence_name('ex_with_custom_index_type_pk', 'id'), seq
95+
with_example_table '`id` INT(11) auto_increment, PRIMARY KEY USING BTREE (`id`)' do
96+
pk, seq = @conn.pk_and_sequence_for('ex')
97+
assert_equal 'id', pk
98+
assert_equal @conn.default_sequence_name('ex', 'id'), seq
99+
end
104100
end
105101

106102
def test_tinyint_integer_typecasting
107-
@conn.exec_query('drop table if exists ex_with_non_boolean_tinyint_column')
108-
@conn.exec_query(<<-eosql)
109-
CREATE TABLE `ex_with_non_boolean_tinyint_column` (
110-
`status` TINYINT(4))
111-
eosql
112-
insert(@conn, { 'status' => 2 }, 'ex_with_non_boolean_tinyint_column')
103+
with_example_table '`status` TINYINT(4)' do
104+
insert(@conn, { 'status' => 2 }, 'ex')
113105

114-
result = @conn.exec_query('SELECT status FROM ex_with_non_boolean_tinyint_column')
106+
result = @conn.exec_query('SELECT status FROM ex')
115107

116-
assert_equal 2, result.column_types['status'].type_cast(result.last['status'])
108+
assert_equal 2, result.column_types['status'].type_cast(result.last['status'])
109+
end
117110
end
118111

119112
def test_supports_extensions
@@ -140,6 +133,15 @@ def insert(ctx, data, table='ex')
140133

141134
ctx.exec_insert(sql, 'SQL', binds)
142135
end
136+
137+
def with_example_table(definition = nil, &block)
138+
definition ||= <<-SQL
139+
`id` int(11) auto_increment PRIMARY KEY,
140+
`number` integer,
141+
`data` varchar(255)
142+
SQL
143+
super(@conn, 'ex', definition, &block)
144+
end
143145
end
144146
end
145147
end

0 commit comments

Comments
 (0)