1 use DBIx::Class::Schema::Loader::Optional::Dependencies
2 -skip_all_without => 'test_rdbms_informix';
9 use File::Path 'rmtree';
10 use DBIx::Class::Schema::Loader 'make_schema_at';
11 use DBIx::Class::Schema::Loader::Utils 'split_name';
12 use String::ToIdentifier::EN::Unicode 'to_identifier';
17 use dbixcsl_common_tests ();
18 use dbixcsl_test_dir '$tdir';
20 use constant EXTRA_DUMP_DIR => "$tdir/informix_extra_dump";
22 # to support " quoted identifiers
23 BEGIN { $ENV{DELIMIDENT} = 'y' }
25 # This test doesn't run over a shared memory connection, because of the single connection limit.
27 my $dsn = $ENV{DBICTEST_INFORMIX_DSN} || '';
28 my $user = $ENV{DBICTEST_INFORMIX_USER} || '';
29 my $password = $ENV{DBICTEST_INFORMIX_PASS} || '';
31 my ($schema, $extra_schema); # for cleanup in END for extra tests
33 dbixcsl_common_tests->new(
35 auto_inc_pk => 'serial primary key',
37 default_function => 'current year to fraction(5)',
38 default_function_def => 'datetime year to fraction(5) default current year to fraction(5)',
41 password => $password,
42 loader_options => { preserve_case => 1 },
45 # http://publib.boulder.ibm.com/infocenter/idshelp/v115/index.jsp?topic=/com.ibm.sqlr.doc/ids_sqr_094.htm
48 'int' => { data_type => 'integer' },
49 integer => { data_type => 'integer' },
50 int8 => { data_type => 'bigint' },
51 bigint => { data_type => 'bigint' },
52 serial => { data_type => 'integer', is_auto_increment => 1 },
53 bigserial => { data_type => 'bigint', is_auto_increment => 1 },
54 serial8 => { data_type => 'bigint', is_auto_increment => 1 },
55 smallint => { data_type => 'smallint' },
56 real => { data_type => 'real' },
57 smallfloat => { data_type => 'real' },
58 # just 'double' is a syntax error
59 'double precision' => { data_type => 'double precision' },
60 float => { data_type => 'double precision' },
61 'float(1)' => { data_type => 'double precision' },
62 'float(5)' => { data_type => 'double precision' },
63 'float(10)' => { data_type => 'double precision' },
64 'float(15)' => { data_type => 'double precision' },
65 'float(16)' => { data_type => 'double precision' },
66 numeric => { data_type => 'numeric' },
67 decimal => { data_type => 'numeric' },
68 dec => { data_type => 'numeric' },
69 'numeric(6,3)' => { data_type => 'numeric', size => [6,3] },
70 'decimal(6,3)' => { data_type => 'numeric', size => [6,3] },
71 'dec(6,3)' => { data_type => 'numeric', size => [6,3] },
74 # XXX this should map to 'boolean'
75 boolean => { data_type => 'smallint' },
78 money => { data_type => 'money' },
79 'money(3,3)' => { data_type => 'numeric', size => [3,3] },
82 byte => { data_type => 'bytea', original => { data_type => 'byte' } },
84 # Character String Types
85 char => { data_type => 'char', size => 1 },
86 'char(3)' => { data_type => 'char', size => 3 },
87 character => { data_type => 'char', size => 1 },
88 'character(3)' => { data_type => 'char', size => 3 },
89 'varchar(3)' => { data_type => 'varchar', size => 3 },
90 'character varying(3)'
91 => { data_type => 'varchar', size => 3 },
92 # XXX min size not supported, colmin from syscolumns is NULL
93 'varchar(3,2)' => { data_type => 'varchar', size => 3 },
94 'character varying(3,2)'
95 => { data_type => 'varchar', size => 3 },
96 nchar => { data_type => 'nchar', size => 1 },
97 'nchar(3)' => { data_type => 'nchar', size => 3 },
98 'nvarchar(3)' => { data_type => 'nvarchar', size => 3 },
99 'nvarchar(3,2)' => { data_type => 'nvarchar', size => 3 },
100 'lvarchar(3)' => { data_type => 'lvarchar', size => 3 },
101 'lvarchar(33)' => { data_type => 'lvarchar', size => 33 },
102 text => { data_type => 'text' },
105 date => { data_type => 'date' },
107 => { data_type => 'date', default_value => \'today' },
108 # XXX support all precisions
109 'datetime year to fraction(5)',
110 => { data_type => 'datetime year to fraction(5)' },
111 'datetime year to fraction(5) default current year to fraction(5)',
112 => { data_type => 'datetime year to fraction(5)', default_value => \'current year to fraction(5)' },
116 # XXX no way to distinguish opaque types boolean, blob and clob
117 blob => { data_type => 'blob' },
118 clob => { data_type => 'blob' },
120 # IDSSECURITYLABEL Type
122 # This requires the DBSECADM privilege and a security policy on the
123 # table, things I know nothing about.
124 # idssecuritylabel => { data_type => 'idssecuritylabel' },
127 # XXX need to introspect element type too
128 'list(varchar(20) not null)'
129 => { data_type => 'list' },
130 'multiset(varchar(20) not null)'
131 => { data_type => 'multiset' },
132 'set(varchar(20) not null)'
133 => { data_type => 'set' },
141 skip 'Set the DBICTEST_INFORMIX_EXTRADB_DSN, _USER and _PASS environment variables to run the multi-database tests', 26 * 2
142 unless $ENV{DBICTEST_INFORMIX_EXTRADB_DSN};
144 $extra_schema = $schema->clone;
145 $extra_schema->connection(@ENV{map "DBICTEST_INFORMIX_EXTRADB_$_",
149 my $dbh1 = $schema->storage->dbh;
152 CREATE TABLE informix_loader_test4 (
153 id SERIAL PRIMARY KEY,
158 CREATE TABLE informix_loader_test5 (
159 id SERIAL PRIMARY KEY,
161 four_id INTEGER REFERENCES informix_loader_test4 (id)
165 ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
168 my $db1 = db_name($schema);
172 my $dbh2 = $extra_schema->storage->dbh;
175 CREATE TABLE informix_loader_test5 (
176 pk SERIAL PRIMARY KEY,
182 ALTER TABLE informix_loader_test5 ADD CONSTRAINT UNIQUE (four_id) CONSTRAINT loader_test5_uniq
185 CREATE TABLE informix_loader_test6 (
186 id SERIAL PRIMARY KEY,
191 CREATE TABLE informix_loader_test7 (
192 id SERIAL PRIMARY KEY,
194 six_id INTEGER UNIQUE REFERENCES informix_loader_test6 (id)
198 my $db2 = db_name($extra_schema);
202 my $db1_moniker = join '', map ucfirst lc, split_name to_identifier $db1;
203 my $db2_moniker = join '', map ucfirst lc, split_name to_identifier $db2;
205 foreach my $db_schema ({ $db1 => '%', $db2 => '%' }, { '%' => '%' }) {
208 local $SIG{__WARN__} = sub {
209 push @warns, $_[0] unless $_[0] =~ /\bcollides\b/
210 || $_[0] =~ /unreferencable/;
214 'InformixMultiDatabase',
217 db_schema => $db_schema,
218 dump_directory => EXTRA_DUMP_DIR,
221 [ $dsn, $user, $password ],
224 InformixMultiDatabase->storage->disconnect;
226 diag join "\n", @warns if @warns;
229 } "dumped schema for databases $db1 and $db2 with no warnings";
234 ok $test_schema = InformixMultiDatabase->connect($dsn, $user, $password);
235 } 'connected test schema';
237 my ($rsrc, $rs, $row, $rel_info, %uniqs);
240 ok $rsrc = $test_schema->source("InformixLoaderTest4");
241 } 'got source for table in database one';
243 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
244 'column in database one';
246 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
247 'column in database one';
249 is try { $rsrc->column_info('value')->{size} }, 100,
250 'column in database one';
253 ok $rs = $test_schema->resultset("InformixLoaderTest4");
254 } 'got resultset for table in database one';
257 ok $row = $rs->create({ value => 'foo' });
258 } 'executed SQL on table in database one';
260 $rel_info = try { $rsrc->relationship_info("informix_loader_test5") };
262 is_deeply $rel_info->{cond}, {
263 'foreign.four_id' => 'self.id'
264 }, 'relationship in database one';
266 is $rel_info->{attrs}{accessor}, 'single',
267 'relationship in database one';
269 is $rel_info->{attrs}{join_type}, 'LEFT',
270 'relationship in database one';
273 ok $rsrc = $test_schema->source("${db1_moniker}InformixLoaderTest5");
274 } 'got source for table in database one';
276 %uniqs = try { $rsrc->unique_constraints };
279 'got unique and primary constraint in database one';
281 delete $uniqs{primary};
283 is_deeply ((values %uniqs)[0], ['four_id'],
284 'correct unique constraint in database one');
287 ok $rsrc = $test_schema->source("InformixLoaderTest6");
288 } 'got source for table in database two';
290 is try { $rsrc->column_info('id')->{is_auto_increment} }, 1,
291 'column in database two introspected correctly';
293 is try { $rsrc->column_info('value')->{data_type} }, 'varchar',
294 'column in database two introspected correctly';
296 is try { $rsrc->column_info('value')->{size} }, 100,
297 'column in database two introspected correctly';
300 ok $rs = $test_schema->resultset("InformixLoaderTest6");
301 } 'got resultset for table in database two';
304 ok $row = $rs->create({ value => 'foo' });
305 } 'executed SQL on table in database two';
307 $rel_info = try { $rsrc->relationship_info('informix_loader_test7') };
309 is_deeply $rel_info->{cond}, {
310 'foreign.six_id' => 'self.id'
311 }, 'relationship in database two';
313 is $rel_info->{attrs}{accessor}, 'single',
314 'relationship in database two';
316 is $rel_info->{attrs}{join_type}, 'LEFT',
317 'relationship in database two';
320 ok $rsrc = $test_schema->source("InformixLoaderTest7");
321 } 'got source for table in database two';
323 %uniqs = try { $rsrc->unique_constraints };
326 'got unique and primary constraint in database two';
328 delete $uniqs{primary};
330 is_deeply ((values %uniqs)[0], ['six_id'],
331 'correct unique constraint in database two');
341 # When we clone the schema, it still references the original loader, which
342 # references the original schema.
343 local $schema->loader->{schema} = $schema;
345 return $schema->loader->_current_db;
347 $schema->storage->disconnect;
351 if (not $ENV{SCHEMA_LOADER_TESTS_NOCLEANUP}) {
352 if (my $dbh2 = try { $extra_schema->storage->dbh }) {
355 $dbh2->do('DROP TABLE informix_loader_test7');
356 $dbh2->do('DROP TABLE informix_loader_test6');
357 $dbh2->do('DROP TABLE informix_loader_test5');
360 die "Error dropping test tables: $_";
366 if (my $dbh1 = try { $schema->storage->dbh }) {
369 $dbh1->do('DROP TABLE informix_loader_test5');
370 $dbh1->do('DROP TABLE informix_loader_test4');
373 die "Error dropping test tables: $_";
379 rmtree EXTRA_DUMP_DIR;
382 # vim:et sts=4 sw=4 tw=0: