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