00832c37529ea9cf7529d67fe21a94f90c24883b
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_10informix_common.t
1 use strict;
2 use warnings;
3 use Test::More;
4 use Test::Exception;
5 use Try::Tiny;
6 use File::Path 'rmtree';
7 use DBIx::Class::Schema::Loader 'make_schema_at';
8 use DBIx::Class::Schema::Loader::Utils 'split_name';
9 use String::ToIdentifier::EN::Unicode 'to_identifier';
10 use namespace::clean;
11
12 use lib qw(t/lib);
13
14 use dbixcsl_common_tests ();
15 use dbixcsl_test_dir '$tdir';
16
17 use constant EXTRA_DUMP_DIR => "$tdir/informix_extra_dump";
18
19 # to support " quoted identifiers
20 BEGIN { $ENV{DELIMIDENT} = 'y' }
21
22 # This test doesn't run over a shared memory connection, because of the single connection limit.
23
24 my $dsn      = $ENV{DBICTEST_INFORMIX_DSN} || '';
25 my $user     = $ENV{DBICTEST_INFORMIX_USER} || '';
26 my $password = $ENV{DBICTEST_INFORMIX_PASS} || '';
27
28 my ($schema, $extra_schema); # for cleanup in END for extra tests
29
30 my $tester = dbixcsl_common_tests->new(
31     vendor         => 'Informix',
32     auto_inc_pk    => 'serial primary key',
33     null           => '',
34     default_function     => 'current year to fraction(5)',
35     default_function_def => 'datetime year to fraction(5) default current year to fraction(5)',
36     dsn            => $dsn,
37     user           => $user,
38     password       => $password,
39     loader_options => { preserve_case => 1 },
40     quote_char     => '"',
41     data_types => {
42         # http://publib.boulder.ibm.com/infocenter/idshelp/v115/index.jsp?topic=/com.ibm.sqlr.doc/ids_sqr_094.htm
43
44         # Numeric Types
45         'int'              => { data_type => 'integer' },
46         integer            => { data_type => 'integer' },
47         int8               => { data_type => 'bigint' },
48         bigint             => { data_type => 'bigint' },
49         serial             => { data_type => 'integer', is_auto_increment => 1 },
50         bigserial          => { data_type => 'bigint',  is_auto_increment => 1 },
51         serial8            => { data_type => 'bigint',  is_auto_increment => 1 },
52         smallint           => { data_type => 'smallint' },
53         real               => { data_type => 'real' },
54         smallfloat         => { data_type => 'real' },
55         # just 'double' is a syntax error
56         'double precision' => { data_type => 'double precision' },
57         float              => { data_type => 'double precision' },
58         'float(1)'         => { data_type => 'double precision' },
59         'float(5)'         => { data_type => 'double precision' },
60         'float(10)'        => { data_type => 'double precision' },
61         'float(15)'        => { data_type => 'double precision' },
62         'float(16)'        => { data_type => 'double precision' },
63         numeric            => { data_type => 'numeric' },
64         decimal            => { data_type => 'numeric' },
65         dec                => { data_type => 'numeric' },
66         'numeric(6,3)'     => { data_type => 'numeric', size => [6,3] },
67         'decimal(6,3)'     => { data_type => 'numeric', size => [6,3] },
68         'dec(6,3)'         => { data_type => 'numeric', size => [6,3] },
69
70         # Boolean Type
71         # XXX this should map to 'boolean'
72         boolean            => { data_type => 'smallint' },
73
74         # Money Type
75         money              => { data_type => 'money' },
76         'money(3,3)'       => { data_type => 'numeric', size => [3,3] },
77
78         # Byte Type
79         byte               => { data_type => 'bytea', original => { data_type => 'byte' } },
80
81         # Character String Types
82         char               => { data_type => 'char', size => 1 },
83         'char(3)'          => { data_type => 'char', size => 3 },
84         character          => { data_type => 'char', size => 1 },
85         'character(3)'     => { data_type => 'char', size => 3 },
86         'varchar(3)'       => { data_type => 'varchar', size => 3 },
87         'character varying(3)'
88                            => { data_type => 'varchar', size => 3 },
89         # XXX min size not supported, colmin from syscolumns is NULL
90         'varchar(3,2)'     => { data_type => 'varchar', size => 3 },
91         'character varying(3,2)'
92                            => { data_type => 'varchar', size => 3 },
93         nchar              => { data_type => 'nchar', size => 1 },
94         'nchar(3)'         => { data_type => 'nchar', size => 3 },
95         'nvarchar(3)'      => { data_type => 'nvarchar', size => 3 },
96         'nvarchar(3,2)'    => { data_type => 'nvarchar', size => 3 },
97         'lvarchar(3)'      => { data_type => 'lvarchar', size => 3 },
98         'lvarchar(33)'     => { data_type => 'lvarchar', size => 33 },
99         text               => { data_type => 'text' },
100
101         # DateTime Types
102         date               => { data_type => 'date' },
103         'date default today'
104                            => { data_type => 'date', default_value => \'today' },
105         # XXX support all precisions
106         'datetime year to fraction(5)',
107                            => { data_type => 'datetime year to fraction(5)' },
108         'datetime year to fraction(5) default current year to fraction(5)',
109                            => { data_type => 'datetime year to fraction(5)', default_value => \'current year to fraction(5)' },
110         # XXX do interval
111
112         # Blob Types
113         # XXX no way to distinguish opaque types boolean, blob and clob
114         blob               => { data_type => 'blob' },
115         clob               => { data_type => 'blob' },
116
117         # IDSSECURITYLABEL Type
118         #
119         # This requires the DBSECADM privilege and a security policy on the
120         # table, things I know nothing about.
121 #        idssecuritylabel   => { data_type => 'idssecuritylabel' },
122
123         # List Types
124         # XXX need to introspect element type too
125         'list(varchar(20) not null)'
126                            => { data_type => 'list' },
127         'multiset(varchar(20) not null)'
128                            => { data_type => 'multiset' },
129         'set(varchar(20) not null)'
130                            => { data_type => 'set' },
131     },
132     extra => {
133         count => 26 * 2,
134         run   => sub {
135             ($schema) = @_;
136
137             SKIP: {
138                 skip 'Set the DBICTEST_INFORMIX_EXTRADB_DSN, _USER and _PASS environment variables to run the multi-database tests', 26 * 2
139                     unless $ENV{DBICTEST_INFORMIX_EXTRADB_DSN};
140
141                 $extra_schema = $schema->clone;
142                 $extra_schema->connection(@ENV{map "DBICTEST_INFORMIX_EXTRADB_$_",
143                     qw/DSN USER PASS/
144                 });
145
146                 my $dbh1 = $schema->storage->dbh;
147                 my $dbh2 = $extra_schema->storage->dbh;
148
149                 $dbh1->do(<<'EOF');
150                     CREATE TABLE informix_loader_test4 (
151                         id SERIAL PRIMARY KEY,
152                         value VARCHAR(100)
153                     )
154 EOF
155                 $dbh1->do(<<'EOF');
156                     CREATE TABLE informix_loader_test5 (
157                         id SERIAL PRIMARY KEY,
158                         value VARCHAR(100),
159                         four_id INTEGER REFERENCES informix_loader_test4 (id)
160                     )
161 EOF
162                 $dbh1->do(<<'EOF');
163 ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
164 EOF
165                 $dbh2->do(<<'EOF');
166                     CREATE TABLE informix_loader_test5 (
167                         pk SERIAL PRIMARY KEY,
168                         value VARCHAR(100),
169                         four_id INTEGER
170                     )
171 EOF
172                 $dbh2->do(<<'EOF');
173 ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
174 EOF
175                 $dbh2->do(<<"EOF");
176                     CREATE TABLE informix_loader_test6 (
177                         id SERIAL PRIMARY KEY,
178                         value VARCHAR(100)
179                     )
180 EOF
181                 $dbh2->do(<<"EOF");
182                     CREATE TABLE informix_loader_test7 (
183                         id SERIAL PRIMARY KEY,
184                         value VARCHAR(100),
185                         six_id INTEGER UNIQUE REFERENCES informix_loader_test6 (id)
186                     )
187 EOF
188
189                 my $db1 = db_name($schema);
190                 my $db2 = db_name($extra_schema);
191
192                 my $db1_moniker = join '', map ucfirst lc, split_name to_identifier $db1;
193                 my $db2_moniker = join '', map ucfirst lc, split_name to_identifier $db2;
194
195                 foreach my $db_schema ({ $db1 => '%', $db2 => '%' }, { '%' => '%' }) {
196                     lives_and {
197                         my @warns;
198                         local $SIG{__WARN__} = sub {
199                             push @warns, $_[0] unless $_[0] =~ /\bcollides\b/
200                                 || $_[0] =~ /unreferencable/;
201                         };
202      
203                         make_schema_at(
204                             'InformixMultiDatabase',
205                             {
206                                 naming => 'current',
207                                 db_schema => $db_schema,
208                                 moniker_parts => [qw/database name/],
209                                 dump_directory => EXTRA_DUMP_DIR,
210                                 quiet => 1,
211                             },
212                             [ $dsn, $user, $password ],
213                         );
214
215                         diag join "\n", @warns if @warns;
216
217                         is @warns, 0;
218                     } "dumped schema for databases $db1 and $db2 with no warnings";
219
220                     my $test_schema;
221
222                     lives_and {
223                         ok $test_schema = InformixMultiDatabase->connect($dsn, $user, $password);
224                     } 'connected test schema';
225
226                     my ($rsrc, $rs, $row, $rel_info, %uniqs);
227
228                     lives_and {
229                         ok $rsrc = $test_schema->source("${db1_moniker}InformixLoaderTest4");
230                     } 'got source for table in database one';
231
232                     is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
233                         'column in database one';
234
235                     is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
236                         'column in database one';
237
238                     is try { $rsrc->column_info('value')->{size} }, 100,
239                         'column in database one';
240
241                     lives_and {
242                         ok $rs = $test_schema->resultset("${db1_moniker}InformixLoaderTest4");
243                     } 'got resultset for table in database one';
244
245                     lives_and {
246                         ok $row = $rs->create({ value => 'foo' });
247                     } 'executed SQL on table in database one';
248
249                     $rel_info = try { $rsrc->relationship_info("informix_loader_test5") };
250
251                     is_deeply $rel_info->{cond}, {
252                         'foreign.four_id' => 'self.id'
253                     }, 'relationship in database one';
254
255                     is $rel_info->{attrs}{accessor}, 'single',
256                         'relationship in database one';
257
258                     is $rel_info->{attrs}{join_type}, 'LEFT',
259                         'relationship in database one';
260
261                     lives_and {
262                         ok $rsrc = $test_schema->source("${db1_moniker}InformixLoaderTest5");
263                     } 'got source for table in database one';
264
265                     %uniqs = try { $rsrc->unique_constraints };
266
267                     is keys %uniqs, 2,
268                         'got unique and primary constraint in database one';
269
270                     delete $uniqs{primary};
271
272                     is_deeply ((values %uniqs)[0], ['four_id'],
273                         'correct unique constraint in database one');
274
275                     lives_and {
276                         ok $rsrc = $test_schema->source("${db2_moniker}InformixLoaderTest6");
277                     } 'got source for table in database two';
278
279                     is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
280                         'column in database two introspected correctly';
281
282                     is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
283                         'column in database two introspected correctly';
284
285                     is try { $rsrc->column_info('value')->{size} }, 100,
286                         'column in database two introspected correctly';
287
288                     lives_and {
289                         ok $rs = $test_schema->resultset("${db2_moniker}InformixLoaderTest6");
290                     } 'got resultset for table in database two';
291
292                     lives_and {
293                         ok $row = $rs->create({ value => 'foo' });
294                     } 'executed SQL on table in database two';
295
296                     $rel_info = try { $rsrc->relationship_info('informix_loader_test7') };
297
298                     is_deeply $rel_info->{cond}, {
299                         'foreign.six_id' => 'self.id'
300                     }, 'relationship in database two';
301
302                     is $rel_info->{attrs}{accessor}, 'single',
303                         'relationship in database two';
304
305                     is $rel_info->{attrs}{join_type}, 'LEFT',
306                         'relationship in database two';
307
308                     lives_and {
309                         ok $rsrc = $test_schema->source("${db2_moniker}InformixLoaderTest7");
310                     } 'got source for table in database two';
311
312                     %uniqs = try { $rsrc->unique_constraints };
313
314                     is keys %uniqs, 2,
315                         'got unique and primary constraint in database two';
316
317                     delete $uniqs{primary};
318
319                     is_deeply ((values %uniqs)[0], ['six_id'],
320                         'correct unique constraint in database two');
321                 }
322             }
323         },
324     },
325 );
326
327 if( !$dsn ) {
328     $tester->skip_tests('You need to set the DBICTEST_INFORMIX_DSN, _USER, and _PASS environment variables');
329 }
330 else {
331     $tester->run_tests();
332 }
333
334 sub db_name {
335     my $schema = shift;
336
337     # When we clone the schema, it still references the original loader, which
338     # references the original schema.
339     local $schema->loader->{schema} = $schema;
340
341     return $schema->loader->_current_db;
342 }
343
344 END {
345     if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
346         if (my $dbh2 = try { $extra_schema->storage->dbh }) {
347             my $dbh1 = $schema->storage->dbh;
348
349             try {
350                 $dbh2->do('DROP TABLE informix_loader_test7');
351                 $dbh2->do('DROP TABLE informix_loader_test6');
352                 $dbh2->do('DROP TABLE informix_loader_test5');
353                 $dbh1->do('DROP TABLE informix_loader_test5');
354                 $dbh1->do('DROP TABLE informix_loader_test4');
355             }
356             catch {
357                 die "Error dropping test tables: $_";
358             };
359         }
360
361         rmtree EXTRA_DUMP_DIR;
362     }
363 }
364 # vim:et sts=4 sw=4 tw=0: