Revert 2c2bc4e5 - it is entirely superseded by cb551b07, 2baba3d9 and 83eef562
[dbsrgits/DBIx-Class.git] / t / 749sqlanywhere.t
CommitLineData
f200d74b 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
548d1627 6use Scope::Guard ();
4b3515a6 7use Try::Tiny;
199fbc45 8use DBIx::Class::Optional::Dependencies ();
f200d74b 9use lib qw(t/lib);
10use DBICTest;
11
199fbc45 12my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SQLANYWHERE_${_}" } qw/DSN USER PASS/};
13my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_SQLANYWHERE_ODBC_${_}" } qw/DSN USER PASS/};
14
15plan skip_all => 'Test needs ' .
16 (join ' or ', map { $_ ? $_ : () }
17 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_sqlanywhere'),
18 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_sqlanywhere_odbc'))
19 unless
20 $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_sqlanywhere')
21 or
22 $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_sqlanywhere_odbc')
23 or
24 (not $dsn || $dsn2);
25
c7e85630 26DBICTest::Schema->load_classes('ArtistGUID');
27
b341186f 28# tests stolen from 748informix.t
f200d74b 29
8ebb1b58 30plan skip_all => <<'EOF' unless $dsn || $dsn2;
374f18f2 31Set $ENV{DBICTEST_SQLANYWHERE_DSN} and/or $ENV{DBICTEST_SQLANYWHERE_ODBC_DSN},
cf7b6654 32_USER and _PASS to run these tests
33EOF
34
35my @info = (
36 [ $dsn, $user, $pass ],
37 [ $dsn2, $user2, $pass2 ],
38);
39
548d1627 40my $schema;
f200d74b 41
cf7b6654 42foreach my $info (@info) {
43 my ($dsn, $user, $pass) = @$info;
f200d74b 44
cf7b6654 45 next unless $dsn;
f200d74b 46
548d1627 47 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
9cf3db6f 48 auto_savepoint => 1
49 });
f200d74b 50
65d35121 51 my $guard = Scope::Guard->new(sub{ cleanup($schema) });
cf7b6654 52
548d1627 53 my $dbh = $schema->storage->dbh;
cf7b6654 54
55 eval { $dbh->do("DROP TABLE artist") };
56
57 $dbh->do(<<EOF);
58 CREATE TABLE artist (
59 artistid INT IDENTITY PRIMARY KEY,
60 name VARCHAR(255) NULL,
61 charfield CHAR(10) NULL,
62 rank INT DEFAULT 13
63 )
ed720bc5 64EOF
f200d74b 65
cf7b6654 66 my $ars = $schema->resultset('Artist');
67 is ( $ars->count, 0, 'No rows at first' );
f200d74b 68
69# test primary key handling
cf7b6654 70 my $new = $ars->create({ name => 'foo' });
71 ok($new->artistid, "Auto-PK worked");
f200d74b 72
73# test explicit key spec
cf7b6654 74 $new = $ars->create ({ name => 'bar', artistid => 66 });
75 is($new->artistid, 66, 'Explicit PK worked');
76 $new->discard_changes;
77 is($new->artistid, 66, 'Explicit PK assigned');
f200d74b 78
9cf3db6f 79# test savepoints
b9889595 80 throws_ok {
9cf3db6f 81 $schema->txn_do(sub {
82 eval {
83 $schema->txn_do(sub {
84 $ars->create({ name => 'in_savepoint' });
85 die "rolling back savepoint";
86 });
87 };
88 ok ((not $ars->search({ name => 'in_savepoint' })->first),
89 'savepoint rolled back');
90 $ars->create({ name => 'in_outer_txn' });
91 die "rolling back outer txn";
92 });
b9889595 93 } qr/rolling back outer txn/,
9cf3db6f 94 'correct exception for rollback';
95
96 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
97 'outer txn rolled back');
98
f200d74b 99# test populate
cf7b6654 100 lives_ok (sub {
101 my @pop;
102 for (1..2) {
103 push @pop, { name => "Artist_$_" };
104 }
105 $ars->populate (\@pop);
106 });
f200d74b 107
108# test populate with explicit key
cf7b6654 109 lives_ok (sub {
110 my @pop;
111 for (1..2) {
112 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
113 }
114 $ars->populate (\@pop);
115 });
f200d74b 116
117# count what we did so far
cf7b6654 118 is ($ars->count, 6, 'Simple count works');
f200d74b 119
120# test LIMIT support
cf7b6654 121 my $lim = $ars->search( {},
122 {
123 rows => 3,
124 offset => 4,
125 order_by => 'artistid'
126 }
127 );
128 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
129 is( $lim->all, 2, 'Number of ->all objects matches count' );
f200d74b 130
131# test iterator
cf7b6654 132 $lim->reset;
133 is( $lim->next->artistid, 101, "iterator->next ok" );
134 is( $lim->next->artistid, 102, "iterator->next ok" );
135 is( $lim->next, undef, "next past end of resultset ok" );
f200d74b 136
ed720bc5 137# test empty insert
cf7b6654 138 {
139 local $ars->result_source->column_info('artistid')->{is_auto_increment} = 0;
ed720bc5 140
cf7b6654 141 lives_ok { $ars->create({}) }
142 'empty insert works';
143 }
ed720bc5 144
b341186f 145# test blobs (stolen from 73oracle.t)
cf7b6654 146 eval { $dbh->do('DROP TABLE bindtype_test') };
147 $dbh->do(qq[
148 CREATE TABLE bindtype_test
149 (
f3a9ea3d 150 id INT NOT NULL PRIMARY KEY,
151 bytea INT NULL,
152 blob LONG BINARY NULL,
74b5397c 153 blob2 LONG BINARY NULL,
f3a9ea3d 154 clob LONG VARCHAR NULL,
74b5397c 155 clob2 LONG VARCHAR NULL,
f3a9ea3d 156 a_memo INT NULL
cf7b6654 157 )
158 ],{ RaiseError => 1, PrintError => 1 });
159
160 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
161 $binstr{'large'} = $binstr{'small'} x 1024;
162
163 my $maxloblen = length $binstr{'large'};
164 local $dbh->{'LongReadLen'} = $maxloblen;
165
166 my $rs = $schema->resultset('BindType');
167 my $id = 0;
168
169 foreach my $type (qw( blob clob )) {
170 foreach my $size (qw( small large )) {
171 $id++;
b341186f 172
ed720bc5 173# turn off horrendous binary DBIC_TRACE output
cf7b6654 174 local $schema->storage->{debug} = 0;
ed720bc5 175
cf7b6654 176 lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
177 "inserted $size $type without dying";
b341186f 178
cf7b6654 179 ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
180 }
b341186f 181 }
8273e845 182
548d1627 183 my @uuid_types = qw/uniqueidentifier uniqueidentifierstr/;
184
4b3515a6 185# test uniqueidentifiers (and the cursor_class).
186
548d1627 187 for my $uuid_type (@uuid_types) {
188 local $schema->source('ArtistGUID')->column_info('artistid')->{data_type}
189 = $uuid_type;
190
191 local $schema->source('ArtistGUID')->column_info('a_guid')->{data_type}
192 = $uuid_type;
193
194 $schema->storage->dbh_do (sub {
195 my ($storage, $dbh) = @_;
b1bdb76d 196 eval { $dbh->do("DROP TABLE artist_guid") };
548d1627 197 $dbh->do(<<"SQL");
b1bdb76d 198CREATE TABLE artist_guid (
548d1627 199 artistid $uuid_type NOT NULL,
200 name VARCHAR(100),
201 rank INT NOT NULL DEFAULT '13',
202 charfield CHAR(10) NULL,
203 a_guid $uuid_type,
204 primary key(artistid)
205)
206SQL
207 });
208
4b3515a6 209 local $TODO = 'something wrong with uniqueidentifierstr over ODBC'
210 if $dsn =~ /:ODBC:/ && $uuid_type eq 'uniqueidentifierstr';
211
548d1627 212 my $row;
213 lives_ok {
214 $row = $schema->resultset('ArtistGUID')->create({ name => 'mtfnpy' })
215 } 'created a row with a GUID';
216
217 ok(
218 eval { $row->artistid },
219 'row has GUID PK col populated',
220 );
221 diag $@ if $@;
222
223 ok(
224 eval { $row->a_guid },
225 'row has a GUID col with auto_nextval populated',
226 );
227 diag $@ if $@;
228
4b3515a6 229 my $row_from_db = try { $schema->resultset('ArtistGUID')
230 ->search({ name => 'mtfnpy' })->first }
231 catch { diag $_ };
232
233 is try { $row_from_db->artistid }, $row->artistid,
234 'PK GUID round trip (via ->search->next)';
235
236 is try { $row_from_db->a_guid }, $row->a_guid,
237 'NON-PK GUID round trip (via ->search->next)';
238
239 $row_from_db = try { $schema->resultset('ArtistGUID')
240 ->find($row->artistid) }
241 catch { diag $_ };
242
243 is try { $row_from_db->artistid }, $row->artistid,
244 'PK GUID round trip (via ->find)';
245
246 is try { $row_from_db->a_guid }, $row->a_guid,
247 'NON-PK GUID round trip (via ->find)';
248
249 ($row_from_db) = try { $schema->resultset('ArtistGUID')
250 ->search({ name => 'mtfnpy' })->all }
251 catch { diag $_ };
548d1627 252
4b3515a6 253 is try { $row_from_db->artistid }, $row->artistid,
254 'PK GUID round trip (via ->search->all)';
548d1627 255
4b3515a6 256 is try { $row_from_db->a_guid }, $row->a_guid,
257 'NON-PK GUID round trip (via ->search->all)';
548d1627 258 }
b341186f 259}
f200d74b 260
261done_testing;
262
548d1627 263sub cleanup {
65d35121 264 my $schema = shift;
b1bdb76d 265 eval { $schema->storage->dbh->do("DROP TABLE $_") }
266 for qw/artist artist_guid bindtype_test/;
f200d74b 267}