another enum test for MySQL
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_02mysql_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     quote_char       => '`',
24     no_inline_rels   => 1,
25     no_implicit_rels => 1,
26     data_types  => {
27         # http://dev.mysql.com/doc/refman/5.5/en/data-type-overview.html
28         # Numeric Types
29         'bit'         => { data_type => 'bit', size => 1 },
30         'bit(11)'     => { data_type => 'bit', size => 11 },
31
32         'bool'        => { data_type => 'tinyint' },
33         'boolean'     => { data_type => 'tinyint' },
34         'tinyint'     => { data_type => 'tinyint' },
35         'tinyint unsigned'
36                       => { data_type => 'tinyint',   extra => { unsigned => 1 } },
37         'smallint'    => { data_type => 'smallint' },
38         'smallint unsigned'
39                       => { data_type => 'smallint',  extra => { unsigned => 1 } },
40         'mediumint'   => { data_type => 'mediumint' },
41         'mediumint unsigned'
42                       => { data_type => 'mediumint', extra => { unsigned => 1 } },
43         'int'         => { data_type => 'integer' },
44         'int unsigned'
45                       => { data_type => 'integer',   extra => { unsigned => 1 } },
46         'integer'     => { data_type => 'integer' },
47         'integer unsigned'
48                       => { data_type => 'integer',   extra => { unsigned => 1 } },
49         'integer not null'
50                       => { data_type => 'integer' },
51         'bigint'      => { data_type => 'bigint' },
52         'bigint unsigned'
53                       => { data_type => 'bigint',    extra => { unsigned => 1 } },
54
55         'serial'      => { data_type => 'bigint', is_auto_increment => 1, extra => { unsigned => 1 } },
56
57         'float'       => { data_type => 'float' },
58         'float unsigned'
59                       => { data_type => 'float',     extra => { unsigned => 1 } },
60         'double'      => { data_type => 'double precision' },
61         'double unsigned'
62                       => { data_type => 'double precision', extra => { unsigned => 1 } },
63         'double precision' =>
64                          { data_type => 'double precision' },
65         'double precision unsigned'
66                       => { data_type => 'double precision', extra => { unsigned => 1 } },
67
68         # we skip 'real' because its alias depends on the 'REAL AS FLOAT' setting
69
70         'float(2)'    => { data_type => 'float' },
71         'float(24)'   => { data_type => 'float' },
72         'float(25)'   => { data_type => 'double precision' },
73
74         'float(3,3)'  => { data_type => 'float', size => [3,3] },
75         'double(3,3)' => { data_type => 'double precision', size => [3,3] },
76         'double precision(3,3)'
77                       => { data_type => 'double precision', size => [3,3] },
78
79         'decimal'     => { data_type => 'decimal' },
80         'decimal unsigned'
81                       => { data_type => 'decimal', extra => { unsigned => 1 } },
82         'dec'         => { data_type => 'decimal' },
83         'numeric'     => { data_type => 'decimal' },
84         'fixed'       => { data_type => 'decimal' },
85
86         'decimal(3)'   => { data_type => 'decimal', size => [3,0] },
87
88         'decimal(3,3)' => { data_type => 'decimal', size => [3,3] },
89         'dec(3,3)'     => { data_type => 'decimal', size => [3,3] },
90         'numeric(3,3)' => { data_type => 'decimal', size => [3,3] },
91         'fixed(3,3)'   => { data_type => 'decimal', size => [3,3] },
92
93         # Date and Time Types
94         'date'        => { data_type => 'date', datetime_undef_if_invalid => 1 },
95         'datetime'    => { data_type => 'datetime', datetime_undef_if_invalid => 1 },
96         'timestamp default current_timestamp'
97                       => { data_type => 'timestamp', default_value => \'current_timestamp', datetime_undef_if_invalid => 1 },
98         'time'        => { data_type => 'time' },
99         'year'        => { data_type => 'year' },
100         'year(4)'     => { data_type => 'year' },
101         'year(2)'     => { data_type => 'year', size => 2 },
102
103         # String Types
104         'char'         => { data_type => 'char',      size => 1  },
105         'char(11)'     => { data_type => 'char',      size => 11 },
106         'varchar(20)'  => { data_type => 'varchar',   size => 20 },
107         'binary'       => { data_type => 'binary',    size => 1  },
108         'binary(11)'   => { data_type => 'binary',    size => 11 },
109         'varbinary(20)'=> { data_type => 'varbinary', size => 20 },
110
111         'tinyblob'    => { data_type => 'tinyblob' },
112         'tinytext'    => { data_type => 'tinytext' },
113         'blob'        => { data_type => 'blob' },
114
115         # text(M) types will map to the appropriate type, length is not stored
116         'text'        => { data_type => 'text' },
117
118         'mediumblob'  => { data_type => 'mediumblob' },
119         'mediumtext'  => { data_type => 'mediumtext' },
120         'longblob'    => { data_type => 'longblob' },
121         'longtext'    => { data_type => 'longtext' },
122
123         "enum('foo','bar','baz')"
124                       => { data_type => 'enum', extra => { list => [qw/foo bar baz/] } },
125         "set('foo','bar','baz')"
126                       => { data_type => 'set',  extra => { list => [qw/foo bar baz/] } },
127
128         # RT#68717
129         "enum('11,10 (<500)/0 DUN','4,90 (<120)/0 EUR') NOT NULL default '11,10 (<500)/0 DUN'"
130                       => { data_type => 'enum', extra => { list => ['11,10 (<500)/0 DUN', '4,90 (<120)/0 EUR'] }, default_value => '11,10 (<500)/0 DUN' },
131         "set('11_10 (<500)/0 DUN','4_90 (<120)/0 EUR') NOT NULL default '11_10 (<500)/0 DUN'"
132                       => { data_type => 'set', extra => { list => ['11_10 (<500)/0 DUN', '4_90 (<120)/0 EUR'] }, default_value => '11_10 (<500)/0 DUN' },
133         "enum('19,90 (<500)/0 EUR','4,90 (<120)/0 EUR','7,90 (<200)/0 CHF','300 (<6000)/0 CZK','4,90 (<100)/0 EUR','39 (<900)/0 DKK','299 (<5000)/0 EEK','9,90 (<250)/0 EUR','3,90 (<100)/0 GBP','3000 (<70000)/0 HUF','4000 (<70000)/0 JPY','13,90 (<200)/0 LVL','99 (<2500)/0 NOK','39 (<1000)/0 PLN','1000 (<20000)/0 RUB','49 (<2500)/0 SEK','29 (<600)/0 USD','19,90 (<600)/0 EUR','0 EUR','0 CHF') NOT NULL default '19,90 (<500)/0 EUR'"
134                       => { data_type => 'enum', extra => { list => ['19,90 (<500)/0 EUR','4,90 (<120)/0 EUR','7,90 (<200)/0 CHF','300 (<6000)/0 CZK','4,90 (<100)/0 EUR','39 (<900)/0 DKK','299 (<5000)/0 EEK','9,90 (<250)/0 EUR','3,90 (<100)/0 GBP','3000 (<70000)/0 HUF','4000 (<70000)/0 JPY','13,90 (<200)/0 LVL','99 (<2500)/0 NOK','39 (<1000)/0 PLN','1000 (<20000)/0 RUB','49 (<2500)/0 SEK','29 (<600)/0 USD','19,90 (<600)/0 EUR','0 EUR','0 CHF'] }, default_value => '19,90 (<500)/0 EUR' },
135     },
136     extra => {
137         create => [
138             q{
139                 CREATE TABLE `mysql_loader-test1` (
140                     id INT AUTO_INCREMENT PRIMARY KEY,
141                     value varchar(100)
142                 )
143             },
144             q{
145                 CREATE VIEW mysql_loader_test2 AS SELECT * FROM `mysql_loader-test1`
146             },
147         ],
148         pre_drop_ddl => [ 'DROP VIEW mysql_loader_test2', ],
149         drop => [ 'mysql_loader-test1', ],
150         count => 2,
151         run => sub {
152             my ($schema, $monikers, $classes) = @_;
153
154             is $monikers->{'mysql_loader-test1'}, 'MysqlLoaderTest1',
155                 'table with dash correctly monikerized';
156
157             my $rsrc = $schema->resultset($monikers->{mysql_loader_test2})->result_source;
158
159             is $rsrc->column_info('value')->{data_type}, 'varchar',
160                 'view introspected successfully';
161         },
162     },
163 );
164
165 if( !$dsn || !$user ) {
166     $tester->skip_tests('You need to set the DBICTEST_MYSQL_DSN, _USER, and _PASS environment variables');
167 }
168 else {
169     diag $skip_rels_msg if not $test_innodb;
170     $tester->run_tests();
171 }
172
173 # vim:et sts=4 sw=4 tw=0: