fe984bc313220a589ea0ed6e75772b4a296fdf3b
[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 lib qw(t/lib);
7 use DBICTest;
8
9 # tests stolen from 748informix.t
10
11 my ($dsn, $user, $pass)    = @ENV{map { "DBICTEST_SYBASE_ASA_${_}" }      qw/DSN USER PASS/};
12 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_SYBASE_ASA_ODBC_${_}" } qw/DSN USER PASS/};
13
14 plan skip_all => <<'EOF' unless $dsn || $dsn2;
15 Set $ENV{DBICTEST_SYBASE_ASA_DSN} and/or $ENV{DBICTEST_SYBASE_ASA_ODBC_DSN},
16 _USER and _PASS to run these tests
17 EOF
18
19 my @info = (
20   [ $dsn,  $user,  $pass  ],
21   [ $dsn2, $user2, $pass2 ],
22 );
23
24 my @handles_to_clean;
25
26 foreach my $info (@info) {
27   my ($dsn, $user, $pass) = @$info;
28
29   next unless $dsn;
30
31   my $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
32     auto_savepoint => 1
33   });
34
35   my $dbh = $schema->storage->dbh;
36
37   push @handles_to_clean, $dbh;
38
39   eval { $dbh->do("DROP TABLE artist") };
40
41   $dbh->do(<<EOF);
42   CREATE TABLE artist (
43     artistid INT IDENTITY PRIMARY KEY,
44     name VARCHAR(255) NULL,
45     charfield CHAR(10) NULL,
46     rank INT DEFAULT 13
47   )
48 EOF
49
50   my $ars = $schema->resultset('Artist');
51   is ( $ars->count, 0, 'No rows at first' );
52
53 # test primary key handling
54   my $new = $ars->create({ name => 'foo' });
55   ok($new->artistid, "Auto-PK worked");
56
57 # test explicit key spec
58   $new = $ars->create ({ name => 'bar', artistid => 66 });
59   is($new->artistid, 66, 'Explicit PK worked');
60   $new->discard_changes;
61   is($new->artistid, 66, 'Explicit PK assigned');
62
63 # test savepoints
64   throws_ok {
65     $schema->txn_do(sub {
66       eval {
67         $schema->txn_do(sub {
68           $ars->create({ name => 'in_savepoint' });
69           die "rolling back savepoint";
70         });
71       };
72       ok ((not $ars->search({ name => 'in_savepoint' })->first),
73         'savepoint rolled back');
74       $ars->create({ name => 'in_outer_txn' });
75       die "rolling back outer txn";
76     });
77   } qr/rolling back outer txn/,
78     'correct exception for rollback';
79
80   ok ((not $ars->search({ name => 'in_outer_txn' })->first),
81     'outer txn rolled back');
82
83 # test populate
84   lives_ok (sub {
85     my @pop;
86     for (1..2) {
87       push @pop, { name => "Artist_$_" };
88     }
89     $ars->populate (\@pop);
90   });
91
92 # test populate with explicit key
93   lives_ok (sub {
94     my @pop;
95     for (1..2) {
96       push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
97     }
98     $ars->populate (\@pop);
99   });
100
101 # count what we did so far
102   is ($ars->count, 6, 'Simple count works');
103
104 # test LIMIT support
105   my $lim = $ars->search( {},
106     {
107       rows => 3,
108       offset => 4,
109       order_by => 'artistid'
110     }
111   );
112   is( $lim->count, 2, 'ROWS+OFFSET count ok' );
113   is( $lim->all, 2, 'Number of ->all objects matches count' );
114
115 # test iterator
116   $lim->reset;
117   is( $lim->next->artistid, 101, "iterator->next ok" );
118   is( $lim->next->artistid, 102, "iterator->next ok" );
119   is( $lim->next, undef, "next past end of resultset ok" );
120
121 # test empty insert
122   {
123     local $ars->result_source->column_info('artistid')->{is_auto_increment} = 0;
124
125     lives_ok { $ars->create({}) }
126       'empty insert works';
127   }
128
129 # test blobs (stolen from 73oracle.t)
130   eval { $dbh->do('DROP TABLE bindtype_test') };
131   $dbh->do(qq[
132   CREATE TABLE bindtype_test
133   (
134     id    INT          NOT NULL PRIMARY KEY,
135     bytea INT          NULL,
136     blob  LONG BINARY  NULL,
137     clob  LONG VARCHAR NULL
138   )
139   ],{ RaiseError => 1, PrintError => 1 });
140
141   my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
142   $binstr{'large'} = $binstr{'small'} x 1024;
143
144   my $maxloblen = length $binstr{'large'};
145   local $dbh->{'LongReadLen'} = $maxloblen;
146
147   my $rs = $schema->resultset('BindType');
148   my $id = 0;
149
150   foreach my $type (qw( blob clob )) {
151     foreach my $size (qw( small large )) {
152       $id++;
153
154 # turn off horrendous binary DBIC_TRACE output
155       local $schema->storage->{debug} = 0;
156
157       lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
158       "inserted $size $type without dying";
159
160       ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
161     }
162   }
163 }
164
165 done_testing;
166
167 # clean up our mess
168 END {
169   foreach my $dbh (@handles_to_clean) {
170     eval { $dbh->do("DROP TABLE $_") } for qw/artist bindtype_test/;
171   }
172 }