1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
| #!/usr/bin/env perl
use Test::Most;
use Test::DatabaseRow;
use Test::Group;
use DBIx::Simple;
use SQL::Abstract;
use Const::Fast;
const my $DATABASE => 'testdb';
const my $TABLE => 't1';
const my $COLUMN => 'c1';
const my $DBHOST => '127.0.0.1';
const my $DBUSER => 'root';
const my $DBPASS => q();
my $db = DBIx::Simple->connect(
"DBI:mysql:host=$DBHOST",
$DBUSER, $DBPASS,
{
mysql_enable_utf8 => 1,
mysql_auto_reconnect => 1,
}
) or die;
$db->abstract = SQL::Abstract->new( { quota_char => '`', name_sep => '.' } );
local $Test::DatabaseRow::dbh = $db->dbh;
subtest "enum column에 컬럼 추가가 발생한경우(POST)" => sub {
my %h = (
1 => { n => 'a', i => 1 },
2 => { n => 'b', i => 2 },
3 => { n => 'c', i => 3 },
);
initialize(%h);
# +-------+-------------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
# +-------+-------------------+------+-----+---------+----------------+
# | id | int(11) unsigned | NO | PRI | NULL | auto_increment |
# | c1 | enum('a','b','c') | YES | | NULL | |
# +-------+-------------------+------+-----+---------+----------------+
insert_values(%h);
test_column_value(%h);
test_column_index(%h);
query("ALTER TABLE `$TABLE` CHANGE `$COLUMN` `$COLUMN` ENUM('a','b','c','x','y','z') NULL DEFAULT NULL");
# +-------+-------------------------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
# +-------+-------------------------------+------+-----+---------+----------------+
# | c1 | enum('a','b','c','x','y','z') | YES | | NULL | |
# +-------+-------------------------------+------+-----+---------+----------------+
test_column_value(%h);
test_column_index(%h);
};
subtest "enum column에 컬럼 추가가 발생한경우(PRE)" => sub {
my %h = (
1 => { n => 'a', i => 1 },
2 => { n => 'b', i => 2 },
3 => { n => 'c', i => 3 },
);
initialize(%h);
# +-------+-------------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
# +-------+-------------------+------+-----+---------+----------------+
# | id | int(11) unsigned | NO | PRI | NULL | auto_increment |
# | c1 | enum('a','b','c') | YES | | NULL | |
# +-------+-------------------+------+-----+---------+----------------+
insert_values(%h);
test_column_value(%h);
test_column_index(%h);
query("ALTER TABLE `$TABLE` CHANGE `$COLUMN` `$COLUMN` ENUM('x','y','z','a','b','c') NULL DEFAULT NULL");
# +-------+-------------------------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
# +-------+-------------------------------+------+-----+---------+----------------+
# | c1 | enum('x','y','z','a','b','c') | YES | | NULL | |
# +-------+-------------------------------+------+-----+---------+----------------+
test_column_value(%h);
test_column_index(%h); # Fail
};
subtest "enum column에 숫자 이름의 컬럼이 들어간 경우(1,2,3)" => sub {
my %h = (
1 => { n => '1', i => 1 },
2 => { n => '2', i => 2 },
3 => { n => '3', i => 3 },
);
initialize(%h);
# +-------+-------------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
# +-------+-------------------+------+-----+---------+----------------+
# | id | int(11) unsigned | NO | PRI | NULL | auto_increment |
# | c1 | enum('1','2','3') | YES | | NULL | |
# +-------+-------------------+------+-----+---------+----------------+
insert_values(%h);
test_column_value(%h);
test_column_index(%h);
query("INSERT INTO t1(id,c1) VALUES(4,1)");
all_row_ok(
table => $TABLE,
where => [ id => 4 ],
tests => [ $COLUMN => 1 ],
description => "if input just 1"
);
query("INSERT INTO t1(id,c1) VALUES(5,'1')");
all_row_ok(
table => $TABLE,
where => [ id => 5 ],
tests => [ $COLUMN => 1 ],
description => "if input '1'"
);
};
subtest "enum column에 숫자 이름의 컬럼이 들어간 경우(0,1,2)" => sub {
my %h = (
1 => { n => '0', i => 1 },
2 => { n => '1', i => 2 },
3 => { n => '2', i => 3 },
);
initialize(%h);
# +-------+-------------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
# +-------+-------------------+------+-----+---------+----------------+
# | id | int(11) unsigned | NO | PRI | NULL | auto_increment |
# | c1 | enum('0','1','2') | YES | | NULL | |
# +-------+-------------------+------+-----+---------+----------------+
insert_values(%h);
test_column_value(%h);
test_column_index(%h); #Fail
query("INSERT INTO t1(id,c1) VALUES(4,1)");
all_row_ok(
table => $TABLE,
where => [ id => 4 ],
tests => [ $COLUMN => 1 ],
description => "if input just 1"
);
query("INSERT INTO t1(id,c1) VALUES(5,'1')");
all_row_ok(
table => $TABLE,
where => [ id => 5 ],
tests => [ $COLUMN => 1 ],
description => "if input '1'"
);
};
done_testing();
sub query($) {
my ($query) = @;
$db->query($query) && print STDERR ">> ", $query, "\n";
}
sub initialize {
my %h = @;
my @ = map { sprintf qq('$h{$_}->{n}') } sort keys %h;
my $enum = sprintf(q{ENUM(%s)},join(',',@) );
print STDERR "Database : $DATABASE\n";
query("DROP DATABASE IF EXISTS `$DATABASE`");
query("CREATE DATABASE `$DATABASE` DEFAULT CHARACTER SET `utf8`");
query("USE `$DATABASE`");
query("CREATE TABLE `$TABLE` (id INT(11) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT) DEFAULT CHARACTER SET `utf8` ENGINE = `InnoDB`");
query("ALTER TABLE `$TABLE` ADD `$COLUMN` $enum NULL DEFAULT NULL AFTER `id`");
print STDERR '-' x 80, "\n";
}
sub insert_values {
my %h = @;
foreach my $k ( sort keys %h ) {
$db->insert( $TABLE, { id => $k, $COLUMN => $h{$k}->{'n'} } );
}
}
sub test_column_value {
my %h = @;
foreach my $k ( sort keys %h ) {
all_row_ok(
table => $TABLE,
where => [ id => $k ],
tests => [ $COLUMN => $h{$k}->{n} ],
description => "row $k // $COLUMN is $h{$k}->{'n'}"
);
}
}
sub test_column_index {
my %h = @;
foreach my $k ( sort keys %h ) {
all_row_ok(
sql => [ "SELECT $COLUMN + 0 as i FROM $TABLE WHERE id = ?", $k ],
tests => [ i => $h{$k}->{'i'} ],
description => "$k: index of $COLUMN is $h{$k}->{'i'}"
);
}
}
|