84339ce8b740a06c7d61131b354bc4fddb4acc22
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 11mysql_common.t
1 use strict;
2 use Test::More;
3 use lib qw(t/lib);
4 use dbixcsl_common_tests;
5
6 my $dsn         = $ENV{DBICTEST_MYSQL_DSN} || '';
7 my $user        = $ENV{DBICTEST_MYSQL_USER} || '';
8 my $password    = $ENV{DBICTEST_MYSQL_PASS} || '';
9 my $test_innodb = $ENV{DBICTEST_MYSQL_INNODB} || 0;
10
11 my $skip_rels_msg = 'You need to set the DBICTEST_MYSQL_INNODB environment variable to test relationships.';
12
13 my $tester = dbixcsl_common_tests->new(
14     vendor           => 'Mysql',
15     auto_inc_pk      => 'INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT',
16     innodb           => $test_innodb ? q{Engine=InnoDB} : 0,
17     dsn              => $dsn,
18     user             => $user,
19     password         => $password,
20     connect_info_opts=> { on_connect_call => 'set_strict_mode' },
21     loader_options   => { preserve_case => 1 },
22     skip_rels        => $test_innodb ? 0 : $skip_rels_msg,
23     no_inline_rels   => 1,
24     no_implicit_rels => 1,
25     data_types  => {
26         # http://dev.mysql.com/doc/refman/5.5/en/data-type-overview.html
27         # Numeric Types
28         'bit'         => { data_type => 'bit', size => 1 },
29         'bit(11)'     => { data_type => 'bit', size => 11 },
30
31         'bool'        => { data_type => 'tinyint' },
32         'boolean'     => { data_type => 'tinyint' },
33         'tinyint'     => { data_type => 'tinyint' },
34         'tinyint unsigned'
35                       => { data_type => 'tinyint',   extra => { unsigned => 1 } },
36         'smallint'    => { data_type => 'smallint' },
37         'smallint unsigned'
38                       => { data_type => 'smallint',  extra => { unsigned => 1 } },
39         'mediumint'   => { data_type => 'mediumint' },
40         'mediumint unsigned'
41                       => { data_type => 'mediumint', extra => { unsigned => 1 } },
42         'int'         => { data_type => 'integer' },
43         'int unsigned'
44                       => { data_type => 'integer',   extra => { unsigned => 1 } },
45         'integer'     => { data_type => 'integer' },
46         'integer unsigned'
47                       => { data_type => 'integer',   extra => { unsigned => 1 } },
48         'bigint'      => { data_type => 'bigint' },
49         'bigint unsigned'
50                       => { data_type => 'bigint',    extra => { unsigned => 1 } },
51
52         'serial'      => { data_type => 'bigint', is_auto_increment => 1, extra => { unsigned => 1 } },
53
54         'float'       => { data_type => 'float' },
55         'float unsigned'
56                       => { data_type => 'float',     extra => { unsigned => 1 } },
57         'double'      => { data_type => 'double precision' },
58         'double unsigned'
59                       => { data_type => 'double precision', extra => { unsigned => 1 } },
60         'double precision' =>
61                          { data_type => 'double precision' },
62         'double precision unsigned'
63                       => { data_type => 'double precision', extra => { unsigned => 1 } },
64
65         # we skip 'real' because its alias depends on the 'REAL AS FLOAT' setting
66
67         'float(2)'    => { data_type => 'float' },
68         'float(24)'   => { data_type => 'float' },
69         'float(25)'   => { data_type => 'double precision' },
70
71         'float(3,3)'  => { data_type => 'float', size => [3,3] },
72         'double(3,3)' => { data_type => 'double precision', size => [3,3] },
73         'double precision(3,3)'
74                       => { data_type => 'double precision', size => [3,3] },
75
76         'decimal'     => { data_type => 'decimal' },
77         'decimal unsigned'
78                       => { data_type => 'decimal', extra => { unsigned => 1 } },
79         'dec'         => { data_type => 'decimal' },
80         'numeric'     => { data_type => 'decimal' },
81         'fixed'       => { data_type => 'decimal' },
82
83         'decimal(3)'   => { data_type => 'decimal', size => [3,0] },
84
85         'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
86         'dec(3,3)'     => { data_type => 'decimal', size => [3,3] },
87         'numeric(3,3)' => { data_type => 'decimal', size => [3,3] },
88         'fixed(3,3)'   => { data_type => 'decimal', size => [3,3] },
89
90         # Date and Time Types
91         'date'        => { data_type => 'date' },
92         'datetime'    => { data_type => 'datetime' },
93         'timestamp default current_timestamp'
94                       => { data_type => 'timestamp', default_value => \'current_timestamp' },
95         'time'        => { data_type => 'time' },
96         'year'        => { data_type => 'year' },
97         'year(4)'     => { data_type => 'year' },
98         'year(2)'     => { data_type => 'year', size => 2 },
99
100         # String Types
101         'char'         => { data_type => 'char',      size => 1  },
102         'char(11)'     => { data_type => 'char',      size => 11 },
103         'varchar(20)'  => { data_type => 'varchar',   size => 20 },
104         'binary'       => { data_type => 'binary',    size => 1  },
105         'binary(11)'   => { data_type => 'binary',    size => 11 },
106         'varbinary(20)'=> { data_type => 'varbinary', size => 20 },
107
108         'tinyblob'    => { data_type => 'tinyblob' },
109         'tinytext'    => { data_type => 'tinytext' },
110         'blob'        => { data_type => 'blob' },
111
112         # text(M) types will map to the appropriate type, length is not stored
113         'text'        => { data_type => 'text' },
114
115         'mediumblob'  => { data_type => 'mediumblob' },
116         'mediumtext'  => { data_type => 'mediumtext' },
117         'longblob'    => { data_type => 'longblob' },
118         'longtext'    => { data_type => 'longtext' },
119
120         "enum('foo', 'bar', 'baz')"
121                       => { data_type => 'enum', extra => { list => [qw/foo bar baz/] } },
122         "set('foo', 'bar', 'baz')"
123                       => { data_type => 'set',  extra => { list => [qw/foo bar baz/] } },
124     },
125     extra => {
126         create => [
127             q{
128                 CREATE TABLE mysql_loader_test1 (
129                     id INT AUTO_INCREMENT PRIMARY KEY,
130                     value varchar(100)
131                 )
132             },
133             q{
134                 CREATE VIEW mysql_loader_test2 AS SELECT * FROM mysql_loader_test1
135             },
136         ],
137         pre_drop_ddl => [ 'DROP VIEW mysql_loader_test2', ],
138         drop => [ 'mysql_loader_test1', ],
139         count => 1,
140         run => sub {
141             my ($schema, $monikers, $classes) = @_;
142
143             my $rsrc = $schema->resultset($monikers->{mysql_loader_test2})->result_source;
144
145             is $rsrc->column_info('value')->{data_type}, 'varchar',
146                 'view introspected successfully';
147         },
148     },
149 );
150
151 if( !$dsn || !$user ) {
152     $tester->skip_tests('You need to set the DBICTEST_MYSQL_DSN, _USER, and _PASS environment variables');
153 }
154 else {
155     diag $skip_rels_msg if not $test_innodb;
156     $tester->run_tests();
157 }
158
159 # vim:et sts=4 sw=4 tw=0: