| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | # Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd. |
|---|
| 4 | # This program is distributed under the terms of the |
|---|
| 5 | # GNU General Public License, version 2. |
|---|
| 6 | # |
|---|
| 7 | # $Id$ |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use warnings; |
|---|
| 11 | use Data::Dumper; |
|---|
| 12 | use English qw( -no_watch_vars ); |
|---|
| 13 | |
|---|
| 14 | $OUTPUT_AUTOFLUSH = 1; |
|---|
| 15 | |
|---|
| 16 | # Run this script as a symlink, in the form of 99-driver.t, ie: |
|---|
| 17 | # ln -s driver-tests.pl 99-driver.t |
|---|
| 18 | |
|---|
| 19 | BEGIN { |
|---|
| 20 | # Set config to driver-test.cfg when run as /path/to/99-driver.t |
|---|
| 21 | $ENV{MT_CONFIG} = "$1-test.cfg" |
|---|
| 22 | if __FILE__ =~ m{ ([^\\/-]+) \.t \z }xms; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | use Test::More; |
|---|
| 26 | use lib 't/lib'; |
|---|
| 27 | use MT::Test qw( :testdb ); |
|---|
| 28 | |
|---|
| 29 | BEGIN { |
|---|
| 30 | plan skip_all => "Configuration file $ENV{MT_CONFIG} not found" |
|---|
| 31 | if !-r $ENV{MT_CONFIG}; |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | plan tests => 30; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | package Ddltest; |
|---|
| 38 | |
|---|
| 39 | use base qw( MT::Object ); |
|---|
| 40 | |
|---|
| 41 | __PACKAGE__->install_properties({ |
|---|
| 42 | column_defs => { |
|---|
| 43 | id => 'integer not null auto_increment', |
|---|
| 44 | string_25 => 'string(25)', |
|---|
| 45 | string_25_nn => 'string(25) not null', |
|---|
| 46 | string_255 => 'string(255)', |
|---|
| 47 | string_1024 => 'string(1024)', |
|---|
| 48 | int_bool => 'boolean', |
|---|
| 49 | int_bool_nn => 'boolean not null', |
|---|
| 50 | int_small => 'smallint', |
|---|
| 51 | int_small_nn => 'smallint not null', |
|---|
| 52 | int_med => 'integer', |
|---|
| 53 | int_med_nn => 'integer not null', |
|---|
| 54 | int_big => 'bigint', |
|---|
| 55 | int_big_nn => 'bigint not null', |
|---|
| 56 | float => 'float', |
|---|
| 57 | float_nn => 'float not null', |
|---|
| 58 | text => 'text', |
|---|
| 59 | text_nn => 'text not null', |
|---|
| 60 | blob => 'blob', |
|---|
| 61 | blob_nn => 'blob not null', |
|---|
| 62 | datetime => 'datetime', |
|---|
| 63 | datetime_nn => 'datetime not null', |
|---|
| 64 | }, |
|---|
| 65 | indexes => { |
|---|
| 66 | name => 1, |
|---|
| 67 | status => 1, |
|---|
| 68 | created_on => 1, |
|---|
| 69 | }, |
|---|
| 70 | audit => 1, |
|---|
| 71 | datasource => 'ddltest', |
|---|
| 72 | primary_key => 'id', |
|---|
| 73 | cacheable => 0, |
|---|
| 74 | }); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | package main; |
|---|
| 78 | |
|---|
| 79 | my $driver = MT::Object->dbi_driver; |
|---|
| 80 | my $dbh = $driver->rw_handle; |
|---|
| 81 | my $ddl_class = $driver->dbd->ddl_class; |
|---|
| 82 | |
|---|
| 83 | # The table may exist from a previous test, so delete it if it does. |
|---|
| 84 | eval { |
|---|
| 85 | if ($driver->table_exists('Ddltest')) { |
|---|
| 86 | my $sql = $driver->dbd->ddl_class->drop_table_sql('Ddltest'); |
|---|
| 87 | $driver->rw_handle->do($sql); |
|---|
| 88 | } |
|---|
| 89 | }; |
|---|
| 90 | |
|---|
| 91 | ok(!$driver->table_exists('Ddltest'), 'Ddltest table does not yet exist'); |
|---|
| 92 | ok(!defined $ddl_class->column_defs('Ddltest'), 'Ddltest table has no column defs'); |
|---|
| 93 | |
|---|
| 94 | my $create_sql = $ddl_class->create_table_sql('Ddltest'); |
|---|
| 95 | ok($create_sql, 'Create Table SQL for Ddltest is available'); |
|---|
| 96 | my $res = $dbh->do($create_sql); |
|---|
| 97 | ok($res, 'Driver could perform Create Table SQL for Ddltest'); |
|---|
| 98 | diag($dbh->errstr || $DBI::errstr) if !$res; |
|---|
| 99 | |
|---|
| 100 | sub _def { |
|---|
| 101 | my ($auto, $not_null, $type, $size) = @_; |
|---|
| 102 | my $def = { |
|---|
| 103 | auto => $auto, |
|---|
| 104 | not_null => $not_null, |
|---|
| 105 | type => $type, |
|---|
| 106 | }; |
|---|
| 107 | $def->{size} = $size if defined $size; |
|---|
| 108 | return $def; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | my $defs = MT::Object->driver->dbd->ddl_class->column_defs('Ddltest'); |
|---|
| 112 | ok($defs, 'Ddltest DDL settings are defined'); |
|---|
| 113 | |
|---|
| 114 | sub is_def { |
|---|
| 115 | my ($got, $expected, $reason) = @_; |
|---|
| 116 | |
|---|
| 117 | for my $field (qw( not_null auto )) { |
|---|
| 118 | if ($expected->{$field} xor $got->{$field}) { |
|---|
| 119 | fail($reason); |
|---|
| 120 | diag($expected->{$field} |
|---|
| 121 | ? "Expected $field but didn't get it" |
|---|
| 122 | : "Expected not $field but got it"); |
|---|
| 123 | return; |
|---|
| 124 | } |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | if ($expected->{type} ne $got->{type}) { |
|---|
| 128 | fail($reason); |
|---|
| 129 | diag("Expected type ", $expected->{type}, " but got ", $got->{type}); |
|---|
| 130 | return; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | if (defined $expected->{size} && $expected->{size} != $got->{size}) { |
|---|
| 134 | fail($reason); |
|---|
| 135 | diag("Expected size ", $expected->{size}, " but got ", $got->{size}); |
|---|
| 136 | return; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | pass($reason); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | is_def($defs->{id}, _def(1, 1, 'integer'), 'Ddltest id column def is correct'); |
|---|
| 143 | |
|---|
| 144 | is_def($defs->{string_25}, _def(0, 0, 'string', 25), 'Ddltest string_25 column def is correct'); |
|---|
| 145 | is_def($defs->{string_25_nn}, _def(0, 1, 'string', 25), 'Ddltest string_25_nn column def is correct'); |
|---|
| 146 | is_def($defs->{string_255}, _def(0, 0, 'string', 255), 'Ddltest string_255 column def is correct'); |
|---|
| 147 | is_def($defs->{string_1024}, _def(0, 0, 'string', 1024), 'Ddltest string_1024 column def is correct'); |
|---|
| 148 | is_def($defs->{int_bool}, _def(0, 0, 'boolean'), 'Ddltest int_bool column def is correct'); |
|---|
| 149 | is_def($defs->{int_bool_nn}, _def(0, 1, 'boolean'), 'Ddltest int_bool_nn column def is correct'); |
|---|
| 150 | is_def($defs->{int_small}, _def(0, 0, 'smallint'), 'Ddltest int_small column def is correct'); |
|---|
| 151 | is_def($defs->{int_small_nn}, _def(0, 1, 'smallint'), 'Ddltest int_small_nn column def is correct'); |
|---|
| 152 | is_def($defs->{int_med}, _def(0, 0, 'integer'), 'Ddltest int_med column def is correct'); |
|---|
| 153 | is_def($defs->{int_med_nn}, _def(0, 1, 'integer'), 'Ddltest int_med_nn column def is correct'); |
|---|
| 154 | is_def($defs->{int_big}, _def(0, 0, 'bigint'), 'Ddltest int_big column def is correct'); |
|---|
| 155 | is_def($defs->{int_big_nn}, _def(0, 1, 'bigint'), 'Ddltest int_big_nn column def is correct'); |
|---|
| 156 | is_def($defs->{float}, _def(0, 0, 'float'), 'Ddltest float column def is correct'); |
|---|
| 157 | is_def($defs->{float_nn}, _def(0, 1, 'float'), 'Ddltest float_nn column def is correct'); |
|---|
| 158 | is_def($defs->{text}, _def(0, 0, 'text'), 'Ddltest text column def is correct'); |
|---|
| 159 | is_def($defs->{text_nn}, _def(0, 1, 'text'), 'Ddltest text_nn column def is correct'); |
|---|
| 160 | is_def($defs->{blob}, _def(0, 0, 'blob'), 'Ddltest blob column def is correct'); |
|---|
| 161 | is_def($defs->{blob_nn}, _def(0, 1, 'blob'), 'Ddltest blob_nn column def is correct'); |
|---|
| 162 | is_def($defs->{datetime}, _def(0, 0, 'datetime'), 'Ddltest datetime column def is correct'); |
|---|
| 163 | is_def($defs->{datetime_nn}, _def(0, 1, 'datetime'), 'Ddltest datetime_nn column def is correct'); |
|---|
| 164 | |
|---|
| 165 | # audit fields |
|---|
| 166 | is_def($defs->{created_on}, _def(0, 0, 'datetime'), 'Ddltest created_on column def is correct'); |
|---|
| 167 | is_def($defs->{created_by}, _def(0, 0, 'integer'), 'Ddltest created_by column def is correct'); |
|---|
| 168 | is_def($defs->{modified_on}, _def(0, 0, 'datetime'), 'Ddltest modified_on column def is correct'); |
|---|
| 169 | is_def($defs->{modified_by}, _def(0, 0, 'integer'), 'Ddltest modified_by column def is correct'); |
|---|
| 170 | |
|---|
| 171 | 1; |
|---|
| 172 | |
|---|