root/branches/release-39/t/ddl-tests.pl @ 2462

Revision 2462, 7.2 kB (checked in by mpaschal, 18 months ago)

Test index generation and introspection

  • Property svn:executable set to *
Line 
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
9use strict;
10use warnings;
11use Data::Dumper;
12use 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
19BEGIN {
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
25use Test::More;
26use lib 't/lib';
27use MT::Test qw( :testdb );
28
29BEGIN {
30    plan skip_all => "Configuration file $ENV{MT_CONFIG} not found"
31        if !-r $ENV{MT_CONFIG};
32}
33
34plan tests => 41;
35
36
37package Ddltest;
38
39use 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        string_25_nn => 1,
67        int_small_nn => 1,
68        string_dt  => {
69            columns => [ qw( string_25 datetime_nn ) ],
70        },
71    },
72    audit       => 1,
73    datasource  => 'ddltest',
74    primary_key => 'id',
75    cacheable   => 0,
76});
77
78
79package main;
80
81my $driver    = MT::Object->dbi_driver;
82my $dbh       = $driver->rw_handle;
83my $ddl_class = $driver->dbd->ddl_class;
84
85# The table may exist from a previous test, so delete it if it does.
86eval {
87    if ($driver->table_exists('Ddltest')) {
88        my $sql = $driver->dbd->ddl_class->drop_table_sql('Ddltest');
89        $driver->rw_handle->do($sql);
90    }
91};
92
93ok(!$driver->table_exists('Ddltest'), 'Ddltest table does not yet exist');
94ok(!defined $ddl_class->column_defs('Ddltest'), 'Ddltest table has no column defs');
95
96my $create_sql = $ddl_class->create_table_sql('Ddltest');
97ok($create_sql, 'Create Table SQL for Ddltest is available');
98my $res = $dbh->do($create_sql);
99ok($res, 'Driver could perform Create Table SQL for Ddltest');
100diag($dbh->errstr || $DBI::errstr) if !$res;
101
102sub _def {
103    my ($auto, $not_null, $type, $size) = @_;
104    my $def = {
105        auto => $auto,
106        not_null => $not_null,
107        type => $type,
108    };
109    $def->{size} = $size if defined $size;
110    return $def;
111}
112
113my $defs = MT::Object->driver->dbd->ddl_class->column_defs('Ddltest');
114ok($defs, 'Ddltest DDL settings are defined');
115
116sub is_def {
117    my ($got, $expected, $reason) = @_;
118
119    for my $field (qw( not_null auto )) {
120        if ($expected->{$field} xor $got->{$field}) {
121            fail($reason);
122            diag($expected->{$field}
123                ? "Expected $field but didn't get it"
124                : "Expected not $field but got it");
125            return;
126        }
127    }
128
129    if ($expected->{type} ne $got->{type}) {
130        fail($reason);
131        diag("Expected type ", $expected->{type}, " but got ", $got->{type});
132        return;
133    }
134
135    if (defined $expected->{size} && $expected->{size} != $got->{size}) {
136        fail($reason);
137        diag("Expected size ", $expected->{size}, " but got ", $got->{size});
138        return;
139    }
140
141    pass($reason);
142}
143
144is_def($defs->{id}, _def(1, 1, 'integer'), 'Ddltest id column def is correct');
145
146is_def($defs->{string_25},    _def(0, 0, 'string', 25),   'Ddltest string_25 column def is correct');
147is_def($defs->{string_25_nn}, _def(0, 1, 'string', 25),   'Ddltest string_25_nn column def is correct');
148is_def($defs->{string_255},   _def(0, 0, 'string', 255),  'Ddltest string_255 column def is correct');
149is_def($defs->{string_1024},  _def(0, 0, 'string', 1024), 'Ddltest string_1024 column def is correct');
150is_def($defs->{int_bool},     _def(0, 0, 'boolean'),      'Ddltest int_bool column def is correct');
151is_def($defs->{int_bool_nn},  _def(0, 1, 'boolean'),      'Ddltest int_bool_nn column def is correct');
152is_def($defs->{int_small},    _def(0, 0, 'smallint'),     'Ddltest int_small column def is correct');
153is_def($defs->{int_small_nn}, _def(0, 1, 'smallint'),     'Ddltest int_small_nn column def is correct');
154is_def($defs->{int_med},      _def(0, 0, 'integer'),      'Ddltest int_med column def is correct');
155is_def($defs->{int_med_nn},   _def(0, 1, 'integer'),      'Ddltest int_med_nn column def is correct');
156is_def($defs->{int_big},      _def(0, 0, 'bigint'),       'Ddltest int_big column def is correct');
157is_def($defs->{int_big_nn},   _def(0, 1, 'bigint'),       'Ddltest int_big_nn column def is correct');
158is_def($defs->{float},        _def(0, 0, 'float'),        'Ddltest float column def is correct');
159is_def($defs->{float_nn},     _def(0, 1, 'float'),        'Ddltest float_nn column def is correct');
160is_def($defs->{text},         _def(0, 0, 'text'),         'Ddltest text column def is correct');
161is_def($defs->{text_nn},      _def(0, 1, 'text'),         'Ddltest text_nn column def is correct');
162is_def($defs->{blob},         _def(0, 0, 'blob'),         'Ddltest blob column def is correct');
163is_def($defs->{blob_nn},      _def(0, 1, 'blob'),         'Ddltest blob_nn column def is correct');
164is_def($defs->{datetime},     _def(0, 0, 'datetime'),     'Ddltest datetime column def is correct');
165is_def($defs->{datetime_nn},  _def(0, 1, 'datetime'),     'Ddltest datetime_nn column def is correct');
166
167# audit fields
168is_def($defs->{created_on},  _def(0, 0, 'datetime'), 'Ddltest created_on column def is correct');
169is_def($defs->{created_by},  _def(0, 0, 'integer'),  'Ddltest created_by column def is correct');
170is_def($defs->{modified_on}, _def(0, 0, 'datetime'), 'Ddltest modified_on column def is correct');
171is_def($defs->{modified_by}, _def(0, 0, 'integer'),  'Ddltest modified_by column def is correct');
172
173
174ok(!defined MT::Object->driver->dbd->ddl_class->index_defs('Ddltest'), q{Ddltest table's indexes are not yet created});
175
176my @index_sql = $ddl_class->index_table_sql('Ddltest');
177ok(@index_sql, 'Index Table SQL for Ddltest is available');
178is(scalar @index_sql, 3, 'Index Table SQL has 4 statements');
179for my $index_sql (@index_sql) {
180    $res = $dbh->do($index_sql);
181    ok($res, 'Driver could perform Index Table SQL for Ddltest');
182    if (!$res) {
183        diag($dbh->errstr || $DBI::errstr);
184        diag('SQL: ' . $index_sql);
185    }
186}
187
188my $index_defs = MT::Object->driver->dbd->ddl_class->index_defs('Ddltest');
189ok($index_defs, 'Ddltest table has index defs');
190
191is(keys %$index_defs, 3, 'Ddltest table has three indexes');
192is($index_defs->{string_25_nn}, 1, 'Ddltest table has name index');
193is($index_defs->{int_small_nn}, 1, 'Ddltest table has status index');
194is_deeply($index_defs->{string_dt}, { columns => [ qw( string_25 datetime_nn ) ] }, 'Ddltest table has multi-column string_dt index');
195
1961;
197
Note: See TracBrowser for help on using the browser.