support Informix common tests over shared mem
[dbsrgits/DBIx-Class-Schema-Loader.git] / t / 10_10informix_common.t
CommitLineData
bc5afe55 1use strict;
c4a69b87 2use warnings;
3use Test::More;
4use Test::Exception;
5use Try::Tiny;
6use File::Path 'rmtree';
7use DBIx::Class::Schema::Loader 'make_schema_at';
5975bbe6 8use DBIx::Class::Schema::Loader::Utils 'split_name';
9use String::ToIdentifier::EN::Unicode 'to_identifier';
10use namespace::clean;
c4a69b87 11
bc5afe55 12use lib qw(t/lib);
c4a69b87 13
14use dbixcsl_common_tests ();
15use dbixcsl_test_dir '$tdir';
16
17use constant EXTRA_DUMP_DIR => "$tdir/informix_extra_dump";
bc5afe55 18
b511f36e 19# to support " quoted identifiers
20BEGIN { $ENV{DELIMIDENT} = 'y' }
21
bc5afe55 22# This test doesn't run over a shared memory connection, because of the single connection limit.
23
24my $dsn = $ENV{DBICTEST_INFORMIX_DSN} || '';
25my $user = $ENV{DBICTEST_INFORMIX_USER} || '';
26my $password = $ENV{DBICTEST_INFORMIX_PASS} || '';
27
c4a69b87 28my ($schema, $extra_schema); # for cleanup in END for extra tests
29
bc5afe55 30my $tester = dbixcsl_common_tests->new(
31 vendor => 'Informix',
c7e6dc1f 32 auto_inc_pk => 'serial primary key',
bc5afe55 33 null => '',
c7e6dc1f 34 default_function => 'current year to fraction(5)',
35 default_function_def => 'datetime year to fraction(5) default current year to fraction(5)',
bc5afe55 36 dsn => $dsn,
37 user => $user,
38 password => $password,
b511f36e 39 loader_options => { preserve_case => 1 },
40 quote_char => '"',
c7e6dc1f 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 },
c4a69b87 132 extra => {
5975bbe6 133 count => 26 * 2,
c4a69b87 134 run => sub {
135 ($schema) = @_;
136
137 SKIP: {
5975bbe6 138 skip 'Set the DBICTEST_INFORMIX_EXTRADB_DSN, _USER and _PASS environment variables to run the multi-database tests', 26 * 2
c4a69b87 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;
c4a69b87 147
148 $dbh1->do(<<'EOF');
149 CREATE TABLE informix_loader_test4 (
150 id SERIAL PRIMARY KEY,
151 value VARCHAR(100)
152 )
153EOF
154 $dbh1->do(<<'EOF');
155 CREATE TABLE informix_loader_test5 (
156 id SERIAL PRIMARY KEY,
157 value VARCHAR(100),
5975bbe6 158 four_id INTEGER REFERENCES informix_loader_test4 (id)
c4a69b87 159 )
160EOF
5975bbe6 161 $dbh1->do(<<'EOF');
162ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
163EOF
e8c7b904 164
165 my $db1 = db_name($schema);
166
167 $dbh1->disconnect;
168
169 my $dbh2 = $extra_schema->storage->dbh;
170
5975bbe6 171 $dbh2->do(<<'EOF');
172 CREATE TABLE informix_loader_test5 (
173 pk SERIAL PRIMARY KEY,
174 value VARCHAR(100),
175 four_id INTEGER
176 )
177EOF
178 $dbh2->do(<<'EOF');
179ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
180EOF
c4a69b87 181 $dbh2->do(<<"EOF");
182 CREATE TABLE informix_loader_test6 (
183 id SERIAL PRIMARY KEY,
184 value VARCHAR(100)
185 )
186EOF
187 $dbh2->do(<<"EOF");
188 CREATE TABLE informix_loader_test7 (
189 id SERIAL PRIMARY KEY,
190 value VARCHAR(100),
191 six_id INTEGER UNIQUE REFERENCES informix_loader_test6 (id)
192 )
193EOF
c4a69b87 194
5975bbe6 195 my $db2 = db_name($extra_schema);
196
e8c7b904 197 $dbh2->disconnect;
198
5975bbe6 199 my $db1_moniker = join '', map ucfirst lc, split_name to_identifier $db1;
200 my $db2_moniker = join '', map ucfirst lc, split_name to_identifier $db2;
201
202 foreach my $db_schema ({ $db1 => '%', $db2 => '%' }, { '%' => '%' }) {
203 lives_and {
204 my @warns;
205 local $SIG{__WARN__} = sub {
206 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/
207 || $_[0] =~ /unreferencable/;
208 };
209
210 make_schema_at(
211 'InformixMultiDatabase',
212 {
213 naming => 'current',
214 db_schema => $db_schema,
5975bbe6 215 dump_directory => EXTRA_DUMP_DIR,
216 quiet => 1,
217 },
218 [ $dsn, $user, $password ],
219 );
c4a69b87 220
e8c7b904 221 InformixMultiDatabase->storage->disconnect;
222
5975bbe6 223 diag join "\n", @warns if @warns;
c4a69b87 224
5975bbe6 225 is @warns, 0;
226 } "dumped schema for databases $db1 and $db2 with no warnings";
c4a69b87 227
5975bbe6 228 my $test_schema;
c4a69b87 229
5975bbe6 230 lives_and {
231 ok $test_schema = InformixMultiDatabase->connect($dsn, $user, $password);
232 } 'connected test schema';
c4a69b87 233
5975bbe6 234 my ($rsrc, $rs, $row, $rel_info, %uniqs);
c4a69b87 235
5975bbe6 236 lives_and {
4c2e2ce9 237 ok $rsrc = $test_schema->source("InformixLoaderTest4");
5975bbe6 238 } 'got source for table in database one';
c4a69b87 239
5975bbe6 240 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
241 'column in database one';
c4a69b87 242
5975bbe6 243 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
244 'column in database one';
c4a69b87 245
5975bbe6 246 is try { $rsrc->column_info('value')->{size} }, 100,
247 'column in database one';
c4a69b87 248
5975bbe6 249 lives_and {
4c2e2ce9 250 ok $rs = $test_schema->resultset("InformixLoaderTest4");
5975bbe6 251 } 'got resultset for table in database one';
c4a69b87 252
5975bbe6 253 lives_and {
254 ok $row = $rs->create({ value => 'foo' });
255 } 'executed SQL on table in database one';
c4a69b87 256
5975bbe6 257 $rel_info = try { $rsrc->relationship_info("informix_loader_test5") };
c4a69b87 258
5975bbe6 259 is_deeply $rel_info->{cond}, {
260 'foreign.four_id' => 'self.id'
261 }, 'relationship in database one';
c4a69b87 262
5975bbe6 263 is $rel_info->{attrs}{accessor}, 'single',
264 'relationship in database one';
c4a69b87 265
5975bbe6 266 is $rel_info->{attrs}{join_type}, 'LEFT',
267 'relationship in database one';
c4a69b87 268
5975bbe6 269 lives_and {
270 ok $rsrc = $test_schema->source("${db1_moniker}InformixLoaderTest5");
271 } 'got source for table in database one';
c4a69b87 272
5975bbe6 273 %uniqs = try { $rsrc->unique_constraints };
c4a69b87 274
5975bbe6 275 is keys %uniqs, 2,
276 'got unique and primary constraint in database one';
c4a69b87 277
5975bbe6 278 delete $uniqs{primary};
c4a69b87 279
5975bbe6 280 is_deeply ((values %uniqs)[0], ['four_id'],
281 'correct unique constraint in database one');
c4a69b87 282
5975bbe6 283 lives_and {
4c2e2ce9 284 ok $rsrc = $test_schema->source("InformixLoaderTest6");
5975bbe6 285 } 'got source for table in database two';
c4a69b87 286
5975bbe6 287 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
288 'column in database two introspected correctly';
c4a69b87 289
5975bbe6 290 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
291 'column in database two introspected correctly';
c4a69b87 292
5975bbe6 293 is try { $rsrc->column_info('value')->{size} }, 100,
294 'column in database two introspected correctly';
c4a69b87 295
5975bbe6 296 lives_and {
4c2e2ce9 297 ok $rs = $test_schema->resultset("InformixLoaderTest6");
5975bbe6 298 } 'got resultset for table in database two';
c4a69b87 299
5975bbe6 300 lives_and {
301 ok $row = $rs->create({ value => 'foo' });
302 } 'executed SQL on table in database two';
c4a69b87 303
5975bbe6 304 $rel_info = try { $rsrc->relationship_info('informix_loader_test7') };
c4a69b87 305
5975bbe6 306 is_deeply $rel_info->{cond}, {
307 'foreign.six_id' => 'self.id'
308 }, 'relationship in database two';
c4a69b87 309
5975bbe6 310 is $rel_info->{attrs}{accessor}, 'single',
311 'relationship in database two';
c4a69b87 312
5975bbe6 313 is $rel_info->{attrs}{join_type}, 'LEFT',
314 'relationship in database two';
315
316 lives_and {
4c2e2ce9 317 ok $rsrc = $test_schema->source("InformixLoaderTest7");
5975bbe6 318 } 'got source for table in database two';
319
320 %uniqs = try { $rsrc->unique_constraints };
321
322 is keys %uniqs, 2,
323 'got unique and primary constraint in database two';
324
325 delete $uniqs{primary};
326
327 is_deeply ((values %uniqs)[0], ['six_id'],
328 'correct unique constraint in database two');
329 }
c4a69b87 330 }
331 },
332 },
bc5afe55 333);
334
335if( !$dsn ) {
336 $tester->skip_tests('You need to set the DBICTEST_INFORMIX_DSN, _USER, and _PASS environment variables');
337}
338else {
339 $tester->run_tests();
340}
c4a69b87 341
5975bbe6 342sub db_name {
343 my $schema = shift;
344
345 # When we clone the schema, it still references the original loader, which
346 # references the original schema.
347 local $schema->loader->{schema} = $schema;
348
349 return $schema->loader->_current_db;
e8c7b904 350
351 $schema->storage->disconnect;
5975bbe6 352}
353
c4a69b87 354END {
355 if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
356 if (my $dbh2 = try { $extra_schema->storage->dbh }) {
c4a69b87 357
358 try {
359 $dbh2->do('DROP TABLE informix_loader_test7');
360 $dbh2->do('DROP TABLE informix_loader_test6');
5975bbe6 361 $dbh2->do('DROP TABLE informix_loader_test5');
e8c7b904 362 }
363 catch {
364 die "Error dropping test tables: $_";
365 };
366
367 $dbh2->disconnect;
368 }
369
370 if (my $dbh1 = try { $schema->storage->dbh }) {
371
372 try {
c4a69b87 373 $dbh1->do('DROP TABLE informix_loader_test5');
374 $dbh1->do('DROP TABLE informix_loader_test4');
375 }
376 catch {
377 die "Error dropping test tables: $_";
378 };
e8c7b904 379
380 $dbh1->disconnect;
c4a69b87 381 }
382
383 rmtree EXTRA_DUMP_DIR;
384 }
385}
bc5afe55 386# vim:et sts=4 sw=4 tw=0: