11# encoding: utf-8
22
33require "cases/helper"
4+ require 'support/ddl_helper'
45
56module 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
145147end
0 commit comments