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