root/branches/release-38/t/ddl-tests.pl @ 2404

Revision 2404, 6.2 kB (checked in by mpaschal, 19 months ago)

Commit first ddl test while I have it

  • 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 => 30;
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        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
77package main;
78
79my $driver    = MT::Object->dbi_driver;
80my $dbh       = $driver->rw_handle;
81my $ddl_class = $driver->dbd->ddl_class;
82
83# The table may exist from a previous test, so delete it if it does.
84eval {
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
91ok(!$driver->table_exists('Ddltest'), 'Ddltest table does not yet exist');
92ok(!defined $ddl_class->column_defs('Ddltest'), 'Ddltest table has no column defs');
93
94my $create_sql = $ddl_class->create_table_sql('Ddltest');
95ok($create_sql, 'Create Table SQL for Ddltest is available');
96my $res = $dbh->do($create_sql);
97ok($res, 'Driver could perform Create Table SQL for Ddltest');
98diag($dbh->errstr || $DBI::errstr) if !$res;
99
100sub _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
111my $defs = MT::Object->driver->dbd->ddl_class->column_defs('Ddltest');
112ok($defs, 'Ddltest DDL settings are defined');
113
114sub 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
142is_def($defs->{id}, _def(1, 1, 'integer'), 'Ddltest id column def is correct');
143
144is_def($defs->{string_25},    _def(0, 0, 'string', 25),   'Ddltest string_25 column def is correct');
145is_def($defs->{string_25_nn}, _def(0, 1, 'string', 25),   'Ddltest string_25_nn column def is correct');
146is_def($defs->{string_255},   _def(0, 0, 'string', 255),  'Ddltest string_255 column def is correct');
147is_def($defs->{string_1024},  _def(0, 0, 'string', 1024), 'Ddltest string_1024 column def is correct');
148is_def($defs->{int_bool},     _def(0, 0, 'boolean'),      'Ddltest int_bool column def is correct');
149is_def($defs->{int_bool_nn},  _def(0, 1, 'boolean'),      'Ddltest int_bool_nn column def is correct');
150is_def($defs->{int_small},    _def(0, 0, 'smallint'),     'Ddltest int_small column def is correct');
151is_def($defs->{int_small_nn}, _def(0, 1, 'smallint'),     'Ddltest int_small_nn column def is correct');
152is_def($defs->{int_med},      _def(0, 0, 'integer'),      'Ddltest int_med column def is correct');
153is_def($defs->{int_med_nn},   _def(0, 1, 'integer'),      'Ddltest int_med_nn column def is correct');
154is_def($defs->{int_big},      _def(0, 0, 'bigint'),       'Ddltest int_big column def is correct');
155is_def($defs->{int_big_nn},   _def(0, 1, 'bigint'),       'Ddltest int_big_nn column def is correct');
156is_def($defs->{float},        _def(0, 0, 'float'),        'Ddltest float column def is correct');
157is_def($defs->{float_nn},     _def(0, 1, 'float'),        'Ddltest float_nn column def is correct');
158is_def($defs->{text},         _def(0, 0, 'text'),         'Ddltest text column def is correct');
159is_def($defs->{text_nn},      _def(0, 1, 'text'),         'Ddltest text_nn column def is correct');
160is_def($defs->{blob},         _def(0, 0, 'blob'),         'Ddltest blob column def is correct');
161is_def($defs->{blob_nn},      _def(0, 1, 'blob'),         'Ddltest blob_nn column def is correct');
162is_def($defs->{datetime},     _def(0, 0, 'datetime'),     'Ddltest datetime column def is correct');
163is_def($defs->{datetime_nn},  _def(0, 1, 'datetime'),     'Ddltest datetime_nn column def is correct');
164
165    # audit fields
166is_def($defs->{created_on},  _def(0, 0, 'datetime'), 'Ddltest created_on column def is correct');
167is_def($defs->{created_by},  _def(0, 0, 'integer'),  'Ddltest created_by column def is correct');
168is_def($defs->{modified_on}, _def(0, 0, 'datetime'), 'Ddltest modified_on column def is correct');
169is_def($defs->{modified_by}, _def(0, 0, 'integer'),  'Ddltest modified_by column def is correct');
170
1711;
172
Note: See TracBrowser for help on using the browser.