11 DBICTest::Schema->load_classes('ArtistGUID');
13 # tests stolen from 748informix.t
15 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SQLANYWHERE_${_}" } qw/DSN USER PASS/};
16 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_SQLANYWHERE_ODBC_${_}" } qw/DSN USER PASS/};
18 plan skip_all => <<'EOF' unless $dsn || $dsn2;
19 Set $ENV{DBICTEST_SQLANYWHERE_DSN} and/or $ENV{DBICTEST_SQLANYWHERE_ODBC_DSN},
20 _USER and _PASS to run these tests
24 [ $dsn, $user, $pass ],
25 [ $dsn2, $user2, $pass2 ],
30 foreach my $info (@info) {
31 my ($dsn, $user, $pass) = @$info;
35 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
39 my $guard = Scope::Guard->new(\&cleanup);
41 my $dbh = $schema->storage->dbh;
43 eval { $dbh->do("DROP TABLE artist") };
47 artistid INT IDENTITY PRIMARY KEY,
48 name VARCHAR(255) NULL,
49 charfield CHAR(10) NULL,
54 my $ars = $schema->resultset('Artist');
55 is ( $ars->count, 0, 'No rows at first' );
57 # test primary key handling
58 my $new = $ars->create({ name => 'foo' });
59 ok($new->artistid, "Auto-PK worked");
61 # test explicit key spec
62 $new = $ars->create ({ name => 'bar', artistid => 66 });
63 is($new->artistid, 66, 'Explicit PK worked');
64 $new->discard_changes;
65 is($new->artistid, 66, 'Explicit PK assigned');
72 $ars->create({ name => 'in_savepoint' });
73 die "rolling back savepoint";
76 ok ((not $ars->search({ name => 'in_savepoint' })->first),
77 'savepoint rolled back');
78 $ars->create({ name => 'in_outer_txn' });
79 die "rolling back outer txn";
81 } qr/rolling back outer txn/,
82 'correct exception for rollback';
84 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
85 'outer txn rolled back');
91 push @pop, { name => "Artist_$_" };
93 $ars->populate (\@pop);
96 # test populate with explicit key
100 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
102 $ars->populate (\@pop);
105 # count what we did so far
106 is ($ars->count, 6, 'Simple count works');
109 my $lim = $ars->search( {},
113 order_by => 'artistid'
116 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
117 is( $lim->all, 2, 'Number of ->all objects matches count' );
121 is( $lim->next->artistid, 101, "iterator->next ok" );
122 is( $lim->next->artistid, 102, "iterator->next ok" );
123 is( $lim->next, undef, "next past end of resultset ok" );
127 local $ars->result_source->column_info('artistid')->{is_auto_increment} = 0;
129 lives_ok { $ars->create({}) }
130 'empty insert works';
133 # test blobs (stolen from 73oracle.t)
134 eval { $dbh->do('DROP TABLE bindtype_test') };
136 CREATE TABLE bindtype_test
138 id INT NOT NULL PRIMARY KEY,
140 blob LONG BINARY NULL,
141 clob LONG VARCHAR NULL,
144 ],{ RaiseError => 1, PrintError => 1 });
146 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
147 $binstr{'large'} = $binstr{'small'} x 1024;
149 my $maxloblen = length $binstr{'large'};
150 local $dbh->{'LongReadLen'} = $maxloblen;
152 my $rs = $schema->resultset('BindType');
155 foreach my $type (qw( blob clob )) {
156 foreach my $size (qw( small large )) {
159 # turn off horrendous binary DBIC_TRACE output
160 local $schema->storage->{debug} = 0;
162 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
163 "inserted $size $type without dying";
165 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
169 my @uuid_types = qw/uniqueidentifier uniqueidentifierstr/;
171 # test uniqueidentifiers (and the cursor_class).
173 for my $uuid_type (@uuid_types) {
174 local $schema->source('ArtistGUID')->column_info('artistid')->{data_type}
177 local $schema->source('ArtistGUID')->column_info('a_guid')->{data_type}
180 $schema->storage->dbh_do (sub {
181 my ($storage, $dbh) = @_;
182 eval { $dbh->do("DROP TABLE artist_guid") };
184 CREATE TABLE artist_guid (
185 artistid $uuid_type NOT NULL,
187 rank INT NOT NULL DEFAULT '13',
188 charfield CHAR(10) NULL,
190 primary key(artistid)
195 local $TODO = 'something wrong with uniqueidentifierstr over ODBC'
196 if $dsn =~ /:ODBC:/ && $uuid_type eq 'uniqueidentifierstr';
200 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
201 } 'created a row with a GUID';
204 eval { $row->artistid },
205 'row has GUID PK col populated',
210 eval { $row->a_guid },
211 'row has a GUID col with auto_nextval populated',
215 my $row_from_db = try { $schema->resultset('ArtistGUID')
216 ->search({ name => 'mtfnpy' })->first }
219 is try { $row_from_db->artistid }, $row->artistid,
220 'PK GUID round trip (via ->search->next)';
222 is try { $row_from_db->a_guid }, $row->a_guid,
223 'NON-PK GUID round trip (via ->search->next)';
225 $row_from_db = try { $schema->resultset('ArtistGUID')
226 ->find($row->artistid) }
229 is try { $row_from_db->artistid }, $row->artistid,
230 'PK GUID round trip (via ->find)';
232 is try { $row_from_db->a_guid }, $row->a_guid,
233 'NON-PK GUID round trip (via ->find)';
235 ($row_from_db) = try { $schema->resultset('ArtistGUID')
236 ->search({ name => 'mtfnpy' })->all }
239 is try { $row_from_db->artistid }, $row->artistid,
240 'PK GUID round trip (via ->search->all)';
242 is try { $row_from_db->a_guid }, $row->a_guid,
243 'NON-PK GUID round trip (via ->search->all)';
250 eval { $schema->storage->dbh->do("DROP TABLE $_") }
251 for qw/artist artist_guid bindtype_test/;