File: 05a-rules-mysql.t

package info (click to toggle)
libalzabo-perl 0.92-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,172 kB
  • ctags: 794
  • sloc: perl: 15,060; makefile: 4
file content (157 lines) | stat: -rw-r--r-- 4,489 bytes parent folder | download | duplicates (5)
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
#!/usr/bin/perl -w

use strict;

use File::Spec;

use lib '.', File::Spec->catdir( File::Spec->curdir, 't', 'lib' );

use Alzabo::Test::Utils;

use Test::More;


use Alzabo::Create;


unless ( eval { require DBD::mysql } && ! $@ )
{
    plan skip_all => 'needs DBD::mysql';
    exit;
}

plan tests => 26;


my $new_schema;
eval_ok( sub { $new_schema = Alzabo::Create::Schema->new( name => 'hello there',
                                                          rdbms => 'MySQL' ) },
	 "Make a new MySQL schema named 'hello there'" );

{
    eval { Alzabo::Create::Schema->new( name => 'hello:there',
                                        rdbms => 'MySQL' ); };

    my $e = $@;
    isa_ok( $e, 'Alzabo::Exception::RDBMSRules',
            "Exceptiont thrown from attempt to create a MySQL schema named 'hello:there'" );
}

{
    eval { $new_schema->make_table( name => 'x' x 65 ) };
    my $e = $@;
    isa_ok( $e, 'Alzabo::Exception::RDBMSRules',
            "Exception thrown from attempt to create a table in MySQL with a 65 character name" );
}

my $table;
{
    $table = $new_schema->make_table( name => 'quux' );
    $table->make_column( name => 'foo',
                         type => 'int',
                         attributes => [ 'unsigned' ],
                         null => 1,
                       );

    my $sql = join '', $new_schema->rules->table_sql($table);
    like( $sql, qr/int(?:eger)\s+unsigned/i,
          "Unsigned attribute should come right after type" );
}

{
    eval { $table->make_column( name => 'foo2',
                                type => 'text',
                                length => 1,
                              ); };
    my $e = $@;
    isa_ok( $e, 'Alzabo::Exception::RDBMSRules',
            "Exception thrown from attempt to make 'text' column with a length parameter" );
}

{
    eval { $table->make_column( name => 'var_no_len',
                                type => 'varchar' ) };
    my $e = $@;
    isa_ok( $e, 'Alzabo::Exception::RDBMSRules',
            "Exception thrown from attempt to make 'varchar' column with no length parameter" );
}

{
    foreach my $type ( qw( DATE DATETIME TIMESTAMP ) )
    {
        my $col = $table->make_column( name => "col_$type",
                                       type => $type,
                                     );

        ok( $col->is_date, "$type is date" );
    };
}

{
    foreach my $type ( qw( DATETIME TIMESTAMP ) )
    {
        my $col = $table->make_column( name => "col2_$type",
                                       type => $type,
                                     );

        ok( $col->is_datetime, "$type is date" );
    };
}

{
    foreach my $type ( qw( DECIMAL NUMERIC FLOAT DOUBLE REAL ) )
    {
        my $col = $table->make_column( name => "col2_$type",
                                       type => $type,
                                     );

        ok( $col->is_numeric, "$type is numeric" );
        ok( $col->is_floating_point, "$type is floating point" );
    };
}

{
    my $col = $table->make_column( name => 'int1',
                                   type => 'integer',
                                   default => 27,
                                 );

    is( $new_schema->rules->_default_for_column($col), 27, 'default is 27' );
}

{
    my $col = $table->make_column( name => 'vc1',
                                   type => 'varchar',
                                   length => 20,
                                   default => 'hello',
                                 );

    is( $new_schema->rules->_default_for_column($col), q|"hello"|, 'default is "hello" (with quotes)' );
}

{
    my $col = $table->make_column( name => 'dt1',
                                   type => 'datetime',
                                   default => 'NOW()',
                                   default_is_raw => 1,
                                 );

    is( $new_schema->rules->_default_for_column($col), q|NOW()|, 'default is NOW()' );
}

{
    my $col = eval { $table->make_column( name => 'vb1',
                                          type => 'varbinary',
                                        ) };

    like( $@, qr/must have a length/, 'length is required for (var)binary' );
}

{
    my $col = $table->make_column( name   => 'vb2',
                                   type   => 'varbinary',
                                   length => 10,
                                 );

    is( $col->length, 10, 'column length is 10' );
}