forked from movabletype/movabletype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddl-tests.pl
553 lines (457 loc) · 20 KB
/
ddl-tests.pl
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
#!/usr/bin/perl
# Movable Type (r) Open Source (C) 2001-2011 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
use strict;
use warnings;
use Data::Dumper;
use English qw( -no_watch_vars );
$OUTPUT_AUTOFLUSH = 1;
# Run this script as a symlink, in the form of 99-driver.t, ie:
# ln -s driver-tests.pl 99-driver.t
BEGIN {
# Set config to driver-test.cfg when run as /path/to/99-driver.t
$ENV{MT_CONFIG} = "$1-test.cfg"
if __FILE__ =~ m{ ([^\\/-]+) \.t \z }xms;
}
use Test::More;
use lib 't/lib';
use MT::Test;
use Test::Class;
BEGIN {
plan skip_all => "Configuration file $ENV{MT_CONFIG} not found"
if !-r $ENV{MT_CONFIG};
}
package Ddltest;
use base qw( MT::Object );
__PACKAGE__->install_properties({
column_defs => {
id => 'integer not null auto_increment',
string_25 => 'string(25)',
string_25_nn => 'string(25) not null',
string_255 => 'string(255)',
string_1024 => 'string(1024)',
int_bool => 'boolean',
int_bool_nn => 'boolean not null',
int_small => 'smallint',
int_small_nn => 'smallint not null',
int_med => 'integer',
int_med_nn => 'integer not null',
int_big => 'bigint',
int_big_nn => 'bigint not null',
float => 'float',
float_nn => 'float not null',
text => 'text',
text_nn => 'text not null',
blob => 'blob',
blob_nn => 'blob not null',
datetime => 'datetime',
datetime_nn => 'datetime not null',
},
indexes => {
string_25_nn => 1,
int_small_nn => 1,
string_dt => {
columns => [ qw( string_25 datetime_nn ) ],
},
},
audit => 1,
datasource => 'ddltest',
primary_key => 'id',
cacheable => 0,
});
package Ddltest::Multikey;
use base qw( MT::Object );
__PACKAGE__->install_properties({
column_defs => {
fkey => 'integer not null',
type => 'string(255) not null',
value => 'string(1024)',
},
datasource => 'multikey',
primary_key => [ qw( fkey type ) ],
cacheable => 0,
});
package Ddltest::InvalidType;
use base qw( MT::Object );
__PACKAGE__->install_properties({
column_defs => {
id => 'integer not null auto_increment',
boo => 'asfdasf',
},
datasource => 'ddltest_invalidtype',
primary_key => 'id',
cacheable => 0,
});
package Ddltest::ShortIndex;
use base qw( MT::Object );
__PACKAGE__->install_properties({
column_defs => {
id => 'integer not null auto_increment',
foo => 'string(25)',
bar => 'string(25)',
},
indexes => {
foo => 1,
short => {
columns => [ 'foo(10)' ],
},
short_short => {
columns => [ qw( foo(10) bar(10) ) ],
},
short_multi => {
columns => [ qw( foo bar(10) ) ],
},
},
datasource => 'ddltest_shortindex',
primary_key => 'id',
cacheable => 0,
});
package Ddltest::Fixable;
use base qw( MT::Object );
__PACKAGE__->install_properties({
column_defs => {
id => 'integer not null auto_increment',
foo => 'string(10)',
bar => 'string(10)',
baz => 'string(10)',
},
datasource => 'ddltest_fixable',
primary_key => 'id',
cacheable => 0,
});
package Test::DDL;
use base qw( Test::Class MT::Test );
use Test::More;
sub startup : Test(startup) {
my $self = shift;
my $driver = MT::Object->dbi_driver;
my $dbh = $driver->rw_handle;
my $ddl_class = $driver->dbd->ddl_class;
# The table may exist from a previous test, so delete it if it does.
eval {
if ($driver->table_exists('Ddltest')) {
my $sql = $driver->dbd->ddl_class->drop_table_sql('Ddltest');
$driver->rw_handle->do($sql);
}
};
}
sub _00_table_does_not_exist : Tests(3) {
my $self = shift;
my $driver = MT::Object->dbi_driver;
my $dbh = $driver->rw_handle;
my $ddl_class = $driver->dbd->ddl_class;
ok(!$driver->table_exists('Ddltest'), 'Ddltest table does not yet exist');
ok(!defined $ddl_class->column_defs('Ddltest'), 'Ddltest table has no column defs');
# postgres might return an empty set of indexes, but I guess that's okay.
my $index_defs = $ddl_class->index_defs('Ddltest');
ok(!defined $index_defs || !%$index_defs, 'Ddltest table has no index defs');
}
sub _01_create_table : Tests(2) {
my $self = shift;
my $driver = MT::Object->dbi_driver;
my $dbh = $driver->rw_handle;
my $ddl_class = $driver->dbd->ddl_class;
my $create_sql = $ddl_class->create_table_sql('Ddltest');
ok($create_sql, 'Create Table SQL for Ddltest is available');
my $res = $dbh->do($create_sql);
ok($res, 'Driver could perform Create Table SQL for Ddltest');
diag($dbh->errstr || $DBI::errstr) if !$res;
}
sub _02_create_indexes : Tests(6) {
my $self = shift;
my $driver = MT::Object->dbi_driver;
my $dbh = $driver->rw_handle;
my $ddl_class = $driver->dbd->ddl_class;
my @index_sql = $ddl_class->index_table_sql('Ddltest');
ok(@index_sql, 'Index Table SQL for Ddltest is available');
is(scalar @index_sql, 3, 'Index Table SQL has three statements');
for my $index_sql (@index_sql) {
my $res = $dbh->do($index_sql);
ok($res, 'Driver could perform Index Table SQL for Ddltest');
if (!$res) {
diag($dbh->errstr || $DBI::errstr);
diag('SQL: ' . $index_sql);
}
}
}
sub _03_create_sequence : Tests(2) {
my $self = shift;
my $driver = MT::Object->dbi_driver;
my $dbh = $driver->rw_handle;
my $ddl_class = $driver->dbd->ddl_class;
$ddl_class->create_sequence('Ddltest');
pass('Created Ddltest sequence without dying');
$ddl_class->drop_sequence('Ddltest');
$ddl_class->create_sequence('Ddltest');
pass('Recreated Ddltest sequence after drop without dying');
}
sub _def {
my ($not_null, $type, %param) = @_;
my $def = {
not_null => $not_null,
type => $type,
%param,
};
return $def;
}
sub is_def {
my ($got, $expected, $reason) = @_;
my $ddl_class = MT::Object->driver->dbd->ddl_class;
my @fields;
if ( $ddl_class =~ m/Pg/xms ) {
@fields = qw( not_null key);
} else {
@fields = qw( not_null auto key);
}
for my $field (@fields) {
if ($expected->{$field} xor $got->{$field}) {
fail($reason);
diag($expected->{$field}
? "Expected $field but didn't get it"
: "Expected not $field but got it");
return;
}
}
if (!defined $got->{type}) {
fail($reason);
diag("Expected MT data type ", $expected->{type}, " but got no type at all");
return;
}
my $got_type = $ddl_class->type2db($got);
my $expected_type = $ddl_class->type2db($expected);
if (!defined $got_type || $expected_type ne $got_type) {
fail($reason);
diag("Expected db data type ", $expected_type, " but got ", $got_type);
return;
}
if (defined $expected->{size} && $expected->{size} != $got->{size}) {
fail($reason);
diag("Expected size ", $expected->{size}, " but got ", $got->{size});
return;
}
pass($reason);
}
sub class_defs : Tests(26) {
my $defs = Ddltest->column_defs();
ok($defs, 'Ddltest class DDL settings are defined');
is_def($defs->{id}, _def(1, 'integer', auto => 1, key => 1), 'Ddltest id column def is correct');
is_def($defs->{string_25}, _def(0, 'string', size => 25), 'Ddltest string_25 column def is correct');
is_def($defs->{string_25_nn}, _def(1, 'string', size => 25), 'Ddltest string_25_nn column def is correct');
is_def($defs->{string_255}, _def(0, 'string', size => 255), 'Ddltest string_255 column def is correct');
is_def($defs->{string_1024}, _def(0, 'string', size => 1024), 'Ddltest string_1024 column def is correct');
is_def($defs->{int_bool}, _def(0, 'boolean'), 'Ddltest int_bool column def is correct');
is_def($defs->{int_bool_nn}, _def(1, 'boolean'), 'Ddltest int_bool_nn column def is correct');
is_def($defs->{int_small}, _def(0, 'smallint'), 'Ddltest int_small column def is correct');
is_def($defs->{int_small_nn}, _def(1, 'smallint'), 'Ddltest int_small_nn column def is correct');
is_def($defs->{int_med}, _def(0, 'integer'), 'Ddltest int_med column def is correct');
is_def($defs->{int_med_nn}, _def(1, 'integer'), 'Ddltest int_med_nn column def is correct');
is_def($defs->{int_big}, _def(0, 'bigint'), 'Ddltest int_big column def is correct');
is_def($defs->{int_big_nn}, _def(1, 'bigint'), 'Ddltest int_big_nn column def is correct');
is_def($defs->{float}, _def(0, 'float'), 'Ddltest float column def is correct');
is_def($defs->{float_nn}, _def(1, 'float'), 'Ddltest float_nn column def is correct');
is_def($defs->{text}, _def(0, 'text'), 'Ddltest text column def is correct');
is_def($defs->{text_nn}, _def(1, 'text'), 'Ddltest text_nn column def is correct');
is_def($defs->{blob}, _def(0, 'blob'), 'Ddltest blob column def is correct');
is_def($defs->{blob_nn}, _def(1, 'blob'), 'Ddltest blob_nn column def is correct');
is_def($defs->{datetime}, _def(0, 'datetime'), 'Ddltest datetime column def is correct');
is_def($defs->{datetime_nn}, _def(1, 'datetime'), 'Ddltest datetime_nn column def is correct');
# audit fields
is_def($defs->{created_on}, _def(0, 'datetime'), 'Ddltest created_on column def is correct');
is_def($defs->{created_by}, _def(0, 'integer'), 'Ddltest created_by column def is correct');
is_def($defs->{modified_on}, _def(0, 'datetime'), 'Ddltest modified_on column def is correct');
is_def($defs->{modified_by}, _def(0, 'integer'), 'Ddltest modified_by column def is correct');
}
sub table_defs : Tests(26) {
my $defs = MT::Object->driver->dbd->ddl_class->column_defs('Ddltest');
ok($defs, 'Ddltest table DDL settings are defined');
is_def($defs->{id}, _def(1, 'integer', auto => 1, key => 1), 'Ddltest id column def is correct');
is_def($defs->{string_25}, _def(0, 'string', size => 25), 'Ddltest string_25 column def is correct');
is_def($defs->{string_25_nn}, _def(1, 'string', size => 25), 'Ddltest string_25_nn column def is correct');
is_def($defs->{string_255}, _def(0, 'string', size => 255), 'Ddltest string_255 column def is correct');
is_def($defs->{string_1024}, _def(0, 'string', size => 1024), 'Ddltest string_1024 column def is correct');
is_def($defs->{int_bool}, _def(0, 'boolean'), 'Ddltest int_bool column def is correct');
is_def($defs->{int_bool_nn}, _def(1, 'boolean'), 'Ddltest int_bool_nn column def is correct');
is_def($defs->{int_small}, _def(0, 'smallint'), 'Ddltest int_small column def is correct');
is_def($defs->{int_small_nn}, _def(1, 'smallint'), 'Ddltest int_small_nn column def is correct');
is_def($defs->{int_med}, _def(0, 'integer'), 'Ddltest int_med column def is correct');
is_def($defs->{int_med_nn}, _def(1, 'integer'), 'Ddltest int_med_nn column def is correct');
is_def($defs->{int_big}, _def(0, 'bigint'), 'Ddltest int_big column def is correct');
is_def($defs->{int_big_nn}, _def(1, 'bigint'), 'Ddltest int_big_nn column def is correct');
is_def($defs->{float}, _def(0, 'float'), 'Ddltest float column def is correct');
is_def($defs->{float_nn}, _def(1, 'float'), 'Ddltest float_nn column def is correct');
is_def($defs->{text}, _def(0, 'text'), 'Ddltest text column def is correct');
is_def($defs->{text_nn}, _def(1, 'text'), 'Ddltest text_nn column def is correct');
is_def($defs->{blob}, _def(0, 'blob'), 'Ddltest blob column def is correct');
is_def($defs->{blob_nn}, _def(1, 'blob'), 'Ddltest blob_nn column def is correct');
is_def($defs->{datetime}, _def(0, 'datetime'), 'Ddltest datetime column def is correct');
is_def($defs->{datetime_nn}, _def(1, 'datetime'), 'Ddltest datetime_nn column def is correct');
# audit fields
is_def($defs->{created_on}, _def(0, 'datetime'), 'Ddltest created_on column def is correct');
is_def($defs->{created_by}, _def(0, 'integer'), 'Ddltest created_by column def is correct');
is_def($defs->{modified_on}, _def(0, 'datetime'), 'Ddltest modified_on column def is correct');
is_def($defs->{modified_by}, _def(0, 'integer'), 'Ddltest modified_by column def is correct');
}
sub index_defs : Tests(5) {
my $index_defs = MT::Object->driver->dbd->ddl_class->index_defs('Ddltest');
ok($index_defs, 'Ddltest table has index defs');
is(keys %$index_defs, 3, 'Ddltest table has three indexes');
is($index_defs->{string_25_nn}, 1, 'Ddltest table has name index');
is($index_defs->{int_small_nn}, 1, 'Ddltest table has status index');
is_deeply($index_defs->{string_dt}, {
columns => [ qw( string_25 datetime_nn ) ]
}, 'Ddltest table has multi-column string_dt index');
}
sub multikey_defs : Tests(8) {
my $self = shift;
my $class_defs = Ddltest::Multikey->column_defs();
ok($class_defs, 'Multikey class DDL settings are defined');
# TODO: multikeys aren't reported as keys.
is_def($class_defs->{fkey}, _def(1, 'integer', auto => 0, key => 0), 'Multikey id column def is correct');
is_def($class_defs->{type}, _def(1, 'string', size => 255, key => 0), 'Multikey type column def is correct');
is_def($class_defs->{value}, _def(0, 'string', size => 1024, key => 0), 'Multikey value column def is correct');
$self->reset_table_for('Ddltest::Multikey');
my $table_defs = MT::Object->driver->dbd->ddl_class->column_defs('Ddltest::Multikey');
ok($table_defs, 'Multikey table DDL settings are defined');
is_def($table_defs->{fkey}, _def(1, 'integer', auto => 0, key => 1), 'Multikey id column def is correct');
is_def($table_defs->{type}, _def(1, 'string', size => 255, key => 1), 'Multikey type column def is correct');
is_def($table_defs->{value}, _def(0, 'string', size => 1024), 'Multikey value column def is correct');
}
sub multikey_unique : Tests(1) {
my $self = shift;
$self->reset_table_for('Ddltest::Multikey');
my $orig = Ddltest::Multikey->new();
$orig->set_values({
fkey => 7,
type => 'awesome',
value => 'these truths',
});
$orig->save or die "Could not save original multikey instance: ", $orig->errstr;
# Test empirically if the multifield primary key is uniquely constrained
# by trying to insert another.
my $driver = MT::Object->dbi_driver;
my $dbh = $driver->rw_handle;
my $dbd = $driver->dbd;
my $table_name = Ddltest::Multikey->table_name;
my $sql = join q{ }, 'INSERT INTO', $table_name, '(',
join(q{, }, map { $dbd->db_column_name($table_name, $_) } qw( fkey type value )),
') VALUES (?, ?, ?)';
my $ret = eval { $dbh->do($sql, {}, 7, 'awesome', 'self-evident') ? 1 : 0 };
my $err = $ret ? q{}
: defined $ret ? $dbh->errstr
: $@
;
ok(!$ret, q{Duplicate multikey instance wouldn't insert});
diag('Saving error: ', $err) if !$ret;
}
sub invalid_type : Tests(3) {
my $self = shift;
my $driver = MT::Object->dbi_driver;
my $dbh = $driver->rw_handle;
my $ddl_class = $driver->dbd->ddl_class;
ok(!$driver->table_exists('Ddltest::InvalidType'), 'Ddltest::InvalidType table does not yet exist');
ok(!defined $ddl_class->column_defs('Ddltest::InvalidType'), 'Ddltest::InvalidType table has no column defs');
ok(!eval { $ddl_class->create_table_sql('Ddltest::InvalidType') }, 'Ddltest::InvalidType cannot make creation sql');
}
sub short_index : Tests(4) {
my $self = shift;
my $ddl_class = MT::Object->driver->dbd->ddl_class;
SKIP: {
skip('Only mysql supports shortened indexes', 4)
if $ddl_class !~ m{ mysql \z }xms;
$self->reset_table_for('Ddltest::ShortIndex');
my $index_defs = $ddl_class->index_defs('Ddltest::ShortIndex');
is($index_defs->{foo}, 1, 'Class with short indexes can have regular indexes too');
is_deeply($index_defs->{short}, {
columns => [ 'foo' ],
sizes => { foo => 10 },
}, 'Class can have a single column with a shortened index');
is_deeply($index_defs->{short_short}, {
columns => [ qw( foo bar ) ],
sizes => {
foo => 10,
bar => 10,
},
}, 'Class can have multiple shortened columns in an index');
is_deeply($index_defs->{short_multi}, {
columns => [ qw( foo bar ) ],
sizes => {
bar => 10,
},
}, 'Class can have mixed shortened and regular columns in an index');
}
}
sub fixable : Tests(12) {
my $self = shift;
my $driver = MT::Object->dbi_driver;
my $dbh = $driver->rw_handle;
my $ddl_class = $driver->dbd->ddl_class;
$self->reset_table_for('Ddltest::Fixable');
for my $i (1..5) {
my $obj = Ddltest::Fixable->new;
$obj->foo($i);
$obj->save or die "Could not add test Fixable record: " . $obj->errstr;
}
my $defs = $ddl_class->column_defs('Ddltest::Fixable');
ok($defs->{baz}, 'Ddltest::Fixable table has baz column after creation');
my @sql;
my $res;
SKIP: {
skip("Driver cannot drop columns", 2) unless $ddl_class->can_drop_column;
@sql = $ddl_class->drop_column_sql('Ddltest::Fixable', 'baz');
ok(@sql, 'Ddltest::Fixable can have column dropping sql');
SQL: for my $sql (@sql) {
$res = $dbh->do($sql);
last SQL if !$res;
}
ok($res, 'Ddltest::Fixable could have its column dropped');
}
{
local Ddltest::Fixable->properties->{column_defs}->{borf} = 'string(10)';
@sql = $ddl_class->add_column_sql('Ddltest::Fixable', 'borf');
ok(@sql, 'Ddltest::Fixable can have column adding sql');
SQL: for my $sql (@sql) {
$res = $dbh->do($sql);
diag(($dbh->errstr || $DBI::errstr) . "( sql $sql)") if !$res;
last SQL if !$res;
}
ok($res, 'Ddltest::Fixable could have a column added');
}
$defs = $ddl_class->column_defs('Ddltest::Fixable');
SKIP: {
skip("Driver cannot drop columns", 1) unless $ddl_class->can_drop_column;
ok(!$defs->{baz}, 'Ddltest::Fixable did indeed have a column dropped');
}
ok( $defs->{borf}, 'Ddltest::Fixable did indeed have a column added');
STMT: for my $stmt ($ddl_class->fix_class('Ddltest::Fixable')) {
$res = $dbh->do($stmt);
if (!$res) {
diag($dbh->errstr || $DBI::errstr);
last STMT;
}
}
ok($res, q{All Ddltest::Fixable's table-fixing statements were performed});
$defs = $ddl_class->column_defs('Ddltest::Fixable');
ok( $defs->{baz}, 'Ddltest::Fixable regrew its dropped column after a fix_class');
ok(!$defs->{borf}, 'Ddltest::Fixable lost its extra column after a fix_class');
my @objs = Ddltest::Fixable->load();
is(scalar @objs, 5, 'There are still 5 Ddltest::Fixable records');
is_deeply([ sort map { $_->foo } @objs ], [ 1..5 ], 'Ddltest::Fixable records survived with values intact');
}
sub _00_drop_table_test : Test(shutdown => 3) {
my $self = shift;
my $driver = MT::Object->dbi_driver;
my $dbh = $driver->rw_handle;
my $ddl_class = $driver->dbd->ddl_class;
my $drop_sql = $ddl_class->drop_table_sql('Ddltest');
ok($drop_sql, 'Drop Table SQL for Ddltest is available');
my $res = $dbh->do($drop_sql);
ok($res, 'Driver could perform Drop Table SQL for Ddltest');
diag($dbh->errstr || $DBI::errstr) if !$res;
ok(!defined $ddl_class->column_defs('Ddltest'), 'Ddltest table no longer exists');
}
package main;
Test::DDL->runtests();
1;