Bump version to accomodate pre-beta testers
[dbsrgits/DBIx-Class.git] / t / 749sqlanywhere.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
f200d74b 3use strict;
4use warnings;
5
6use Test::More;
7use Test::Exception;
4b3515a6 8use Try::Tiny;
199fbc45 9use DBIx::Class::Optional::Dependencies ();
bbf6a9a5 10use DBIx::Class::_Util 'scope_guard';
c0329273 11
f200d74b 12use DBICTest;
13
199fbc45 14my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SQLANYWHERE_${_}" } qw/DSN USER PASS/};
15my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_SQLANYWHERE_ODBC_${_}" } qw/DSN USER PASS/};
16
17plan skip_all => 'Test needs ' .
18 (join ' or ', map { $_ ? $_ : () }
19 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_sqlanywhere'),
20 DBIx::Class::Optional::Dependencies->req_missing_for('test_rdbms_sqlanywhere_odbc'))
21 unless
22 $dsn && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_sqlanywhere')
23 or
24 $dsn2 && DBIx::Class::Optional::Dependencies->req_ok_for('test_rdbms_sqlanywhere_odbc')
25 or
26 (not $dsn || $dsn2);
27
c7e85630 28DBICTest::Schema->load_classes('ArtistGUID');
29
b341186f 30# tests stolen from 748informix.t
f200d74b 31
8ebb1b58 32plan skip_all => <<'EOF' unless $dsn || $dsn2;
374f18f2 33Set $ENV{DBICTEST_SQLANYWHERE_DSN} and/or $ENV{DBICTEST_SQLANYWHERE_ODBC_DSN},
cf7b6654 34_USER and _PASS to run these tests
35EOF
36
37my @info = (
38 [ $dsn, $user, $pass ],
39 [ $dsn2, $user2, $pass2 ],
40);
41
548d1627 42my $schema;
f200d74b 43
cf7b6654 44foreach my $info (@info) {
45 my ($dsn, $user, $pass) = @$info;
f200d74b 46
cf7b6654 47 next unless $dsn;
f200d74b 48
548d1627 49 $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
9cf3db6f 50 auto_savepoint => 1
51 });
f200d74b 52
bbf6a9a5 53 my $guard = scope_guard { cleanup($schema) };
cf7b6654 54
548d1627 55 my $dbh = $schema->storage->dbh;
cf7b6654 56
57 eval { $dbh->do("DROP TABLE artist") };
58
59 $dbh->do(<<EOF);
60 CREATE TABLE artist (
61 artistid INT IDENTITY PRIMARY KEY,
62 name VARCHAR(255) NULL,
63 charfield CHAR(10) NULL,
64 rank INT DEFAULT 13
65 )
ed720bc5 66EOF
f200d74b 67
cf7b6654 68 my $ars = $schema->resultset('Artist');
69 is ( $ars->count, 0, 'No rows at first' );
f200d74b 70
71# test primary key handling
cf7b6654 72 my $new = $ars->create({ name => 'foo' });
73 ok($new->artistid, "Auto-PK worked");
f200d74b 74
75# test explicit key spec
cf7b6654 76 $new = $ars->create ({ name => 'bar', artistid => 66 });
77 is($new->artistid, 66, 'Explicit PK worked');
78 $new->discard_changes;
79 is($new->artistid, 66, 'Explicit PK assigned');
f200d74b 80
9cf3db6f 81# test savepoints
b9889595 82 throws_ok {
9cf3db6f 83 $schema->txn_do(sub {
84 eval {
85 $schema->txn_do(sub {
86 $ars->create({ name => 'in_savepoint' });
87 die "rolling back savepoint";
88 });
89 };
90 ok ((not $ars->search({ name => 'in_savepoint' })->first),
91 'savepoint rolled back');
92 $ars->create({ name => 'in_outer_txn' });
93 die "rolling back outer txn";
94 });
b9889595 95 } qr/rolling back outer txn/,
9cf3db6f 96 'correct exception for rollback';
97
98 ok ((not $ars->search({ name => 'in_outer_txn' })->first),
99 'outer txn rolled back');
100
f200d74b 101# test populate
cf7b6654 102 lives_ok (sub {
103 my @pop;
104 for (1..2) {
105 push @pop, { name => "Artist_$_" };
106 }
107 $ars->populate (\@pop);
108 });
f200d74b 109
110# test populate with explicit key
cf7b6654 111 lives_ok (sub {
112 my @pop;
113 for (1..2) {
114 push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
115 }
116 $ars->populate (\@pop);
117 });
f200d74b 118
119# count what we did so far
cf7b6654 120 is ($ars->count, 6, 'Simple count works');
f200d74b 121
122# test LIMIT support
cf7b6654 123 my $lim = $ars->search( {},
124 {
125 rows => 3,
126 offset => 4,
127 order_by => 'artistid'
128 }
129 );
130 is( $lim->count, 2, 'ROWS+OFFSET count ok' );
131 is( $lim->all, 2, 'Number of ->all objects matches count' );
f200d74b 132
133# test iterator
cf7b6654 134 $lim->reset;
135 is( $lim->next->artistid, 101, "iterator->next ok" );
136 is( $lim->next->artistid, 102, "iterator->next ok" );
137 is( $lim->next, undef, "next past end of resultset ok" );
f200d74b 138
ed720bc5 139# test empty insert
cf7b6654 140 {
141 local $ars->result_source->column_info('artistid')->{is_auto_increment} = 0;
ed720bc5 142
cf7b6654 143 lives_ok { $ars->create({}) }
144 'empty insert works';
145 }
ed720bc5 146
b341186f 147# test blobs (stolen from 73oracle.t)
cf7b6654 148 eval { $dbh->do('DROP TABLE bindtype_test') };
149 $dbh->do(qq[
150 CREATE TABLE bindtype_test
151 (
f3a9ea3d 152 id INT NOT NULL PRIMARY KEY,
153 bytea INT NULL,
154 blob LONG BINARY NULL,
155 clob LONG VARCHAR NULL,
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}