mangling _select_args turned out to be unnecessary
[dbsrgits/DBIx-Class.git] / t / 746sybase.t
1 use strict;
2 use warnings;  
3 no warnings 'uninitialized';
4
5 use Test::More;
6 use Test::Exception;
7 use lib qw(t/lib);
8 use DBICTest;
9
10 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
11
12 my $TESTS = 29 + 2;
13
14 if (not ($dsn && $user)) {
15   plan skip_all =>
16     'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test' .
17     "\nWarning: This test drops and creates the tables " .
18     "'artist' and 'bindtype_test'";
19 } else {
20   plan tests => $TESTS*2;
21 }
22
23 my @storage_types = (
24   'DBI::Sybase',
25   'DBI::Sybase::NoBindVars',
26 );
27 my $schema;
28 my $storage_idx = -1;
29
30 for my $storage_type (@storage_types) {
31   $storage_idx++;
32   $schema = DBICTest::Schema->clone;
33
34   unless ($storage_type eq 'DBI::Sybase') { # autodetect
35     $schema->storage_type("::$storage_type");
36   }
37   $schema->connection($dsn, $user, $pass, {
38     AutoCommit => 1,
39     on_connect_call => [
40       [ blob_setup => log_on_update => 1 ], # this is a safer option
41     ],
42   });
43
44   $schema->storage->ensure_connected;
45   $schema->storage->_dbh->disconnect;
46
47   if ($storage_idx == 0 &&
48       $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::NoBindVars')) {
49 # no placeholders in this version of Sybase or DBD::Sybase
50       my $tb = Test::More->builder;
51       $tb->skip('no placeholders') for 1..$TESTS;
52       next;
53   }
54
55   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
56
57   lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
58
59   $schema->storage->dbh_do (sub {
60       my ($storage, $dbh) = @_;
61       eval { $dbh->do("DROP TABLE artist") };
62       $dbh->do(<<'SQL');
63 CREATE TABLE artist (
64    artistid INT IDENTITY PRIMARY KEY,
65    name VARCHAR(100),
66    rank INT DEFAULT 13 NOT NULL,
67    charfield CHAR(10) NULL
68 )
69 SQL
70   });
71
72   my %seen_id;
73
74 # so we start unconnected
75   $schema->storage->disconnect;
76
77 # test primary key handling
78   my $new = $schema->resultset('Artist')->create({ name => 'foo' });
79   ok($new->artistid > 0, "Auto-PK worked");
80
81   $seen_id{$new->artistid}++;
82
83   for (1..6) {
84     $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
85     is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
86     $seen_id{$new->artistid}++;
87   }
88
89 # test simple count
90   is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
91
92 # test LIMIT support
93   my $it = $schema->resultset('Artist')->search({
94     artistid => { '>' => 0 }
95   }, {
96     rows => 3,
97     order_by => 'artistid',
98   });
99
100   is( $it->count, 3, "LIMIT count ok" );
101
102   is( $it->next->name, "foo", "iterator->next ok" );
103   $it->next;
104   is( $it->next->name, "Artist 2", "iterator->next ok" );
105   is( $it->next, undef, "next past end of resultset ok" );
106
107 # now try with offset
108   $it = $schema->resultset('Artist')->search({}, {
109     rows => 3,
110     offset => 3,
111     order_by => 'artistid',
112   });
113
114   is( $it->count, 3, "LIMIT with offset count ok" );
115
116   is( $it->next->name, "Artist 3", "iterator->next ok" );
117   $it->next;
118   is( $it->next->name, "Artist 5", "iterator->next ok" );
119   is( $it->next, undef, "next past end of resultset ok" );
120
121 # now try a grouped count
122   $schema->resultset('Artist')->create({ name => 'Artist 6' })
123     for (1..6);
124
125   $it = $schema->resultset('Artist')->search({}, {
126     group_by => 'name'
127   });
128
129   is( $it->count, 7, 'COUNT of GROUP_BY ok' );
130
131 # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
132   SKIP: {
133     skip 'Need at least version 1.09 of DBD::Sybase to test TEXT/IMAGE', 12
134         unless $DBD::Sybase::VERSION >= 1.09;
135
136     my $dbh = $schema->storage->dbh;
137     {
138       local $SIG{__WARN__} = sub {};
139       eval { $dbh->do('DROP TABLE bindtype_test') };
140
141       $dbh->do(qq[
142         CREATE TABLE bindtype_test 
143         (
144           id    INT   IDENTITY PRIMARY KEY,
145           bytea INT   NULL,
146           blob  IMAGE NULL,
147           clob  TEXT  NULL
148         )
149       ],{ RaiseError => 1, PrintError => 0 });
150     }
151
152     my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
153     $binstr{'large'} = $binstr{'small'} x 1024;
154
155     my $maxloblen = length $binstr{'large'};
156     note
157       "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
158     local $dbh->{'LongReadLen'} = $maxloblen;
159
160     my $rs = $schema->resultset('BindType');
161     my $last_id;
162
163     foreach my $type (qw(blob clob)) {
164       foreach my $size (qw(small large)) {
165         no warnings 'uninitialized';
166
167         my $created = eval { $rs->create( { $type => $binstr{$size} } ) };
168         ok(!$@, "inserted $size $type without dying");
169         diag $@ if $@;
170
171         $last_id = $created->id if $created;
172
173         my $got = eval {
174           $rs->search({ id => $last_id }, { select => [$type] })->single->$type
175         };
176         diag $@ if $@;
177         ok($got eq $binstr{$size}, "verified inserted $size $type");
178       }
179     }
180
181     # try a blob update
182     TODO: {
183       local $TODO = 'updating TEXT/IMAGE does not work yet';
184
185       my $new_str = $binstr{large} . 'foo';
186       eval { $rs->search({ id => $last_id })->update({ blob => $new_str }) };
187       ok !$@, 'updated blob successfully';
188       diag $@ if $@;
189       ok(eval {
190         $rs->search({ id => $last_id }, { select => ['blob'] })->single->blob
191       } eq $new_str, "verified updated blob" );
192       diag $@ if $@;
193     }
194
195     # blob insert with explicit PK
196     {
197       local $SIG{__WARN__} = sub {};
198       eval { $dbh->do('DROP TABLE bindtype_test') };
199
200       $dbh->do(qq[
201         CREATE TABLE bindtype_test 
202         (
203           id    INT   PRIMARY KEY,
204           bytea INT   NULL,
205           blob  IMAGE NULL,
206           clob  TEXT  NULL
207         )
208       ],{ RaiseError => 1, PrintError => 0 });
209     }
210     my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) };
211     ok(!$@, "inserted large blob without dying");
212     diag $@ if $@;
213
214     my $got = eval {
215       $rs->search({ id => 1 }, { select => ['blob'] })->single->blob
216     };
217     diag $@ if $@;
218     ok($got eq $binstr{large}, "verified inserted large blob");
219   }
220 }
221
222 # clean up our mess
223 END {
224   if (my $dbh = eval { $schema->storage->_dbh }) {
225     $dbh->do('DROP TABLE artist');
226     eval { $dbh->do('DROP TABLE bindtype_test')    };
227   }
228 }