minor cleanups
[dbsrgits/DBIx-Class-Historic.git] / t / 746sybase.t
CommitLineData
a964a928 1use strict;
2use warnings;
b0b44f97 3no warnings 'uninitialized';
a964a928 4
5use Test::More;
e0b2344f 6use Test::Exception;
a964a928 7use lib qw(t/lib);
8use DBICTest;
9
10my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
11
b5453fbb 12my $TESTS = 29 + 2;
5703eb14 13
7d17f469 14if (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 {
5703eb14 20 plan tests => $TESTS*2;
7d17f469 21}
a964a928 22
6b1f5ef7 23my @storage_types = (
24 'DBI::Sybase',
25 'DBI::Sybase::NoBindVars',
26);
27my $schema;
5703eb14 28my $storage_idx = -1;
6b1f5ef7 29
30for my $storage_type (@storage_types) {
5703eb14 31 $storage_idx++;
6b1f5ef7 32 $schema = DBICTest::Schema->clone;
33
34 unless ($storage_type eq 'DBI::Sybase') { # autodetect
35 $schema->storage_type("::$storage_type");
36 }
b25da5cf 37 $schema->connection($dsn, $user, $pass, {
38 AutoCommit => 1,
39 on_connect_call => [
b25da5cf 40 [ blob_setup => log_on_update => 1 ], # this is a safer option
41 ],
42 });
a964a928 43
6b1f5ef7 44 $schema->storage->ensure_connected;
64f4e691 45 $schema->storage->_dbh->disconnect;
a964a928 46
5703eb14 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
6b1f5ef7 55 isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
56
64f4e691 57 lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
58
6b1f5ef7 59 $schema->storage->dbh_do (sub {
60 my ($storage, $dbh) = @_;
61 eval { $dbh->do("DROP TABLE artist") };
6b1f5ef7 62 $dbh->do(<<'SQL');
a964a928 63CREATE TABLE artist (
c5ce7cd6 64 artistid INT IDENTITY PRIMARY KEY,
a964a928 65 name VARCHAR(100),
66 rank INT DEFAULT 13 NOT NULL,
c5ce7cd6 67 charfield CHAR(10) NULL
a964a928 68)
c5ce7cd6 69SQL
6b1f5ef7 70 });
a964a928 71
6b1f5ef7 72 my %seen_id;
a964a928 73
6b1f5ef7 74# so we start unconnected
75 $schema->storage->disconnect;
a964a928 76
77# test primary key handling
6b1f5ef7 78 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
79 ok($new->artistid > 0, "Auto-PK worked");
a964a928 80
6b1f5ef7 81 $seen_id{$new->artistid}++;
a964a928 82
6b1f5ef7 83 for (1..6) {
a964a928 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}++;
6b1f5ef7 87 }
a964a928 88
aa56ff9a 89# test simple count
90 is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
91
92# test LIMIT support
e0b2344f 93 my $it = $schema->resultset('Artist')->search({
94 artistid => { '>' => 0 }
95 }, {
a964a928 96 rows => 3,
97 order_by => 'artistid',
6b1f5ef7 98 });
a964a928 99
b7505130 100 is( $it->count, 3, "LIMIT count ok" );
a964a928 101
6b1f5ef7 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" );
a964a928 106
a0348159 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
92e7a79b 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
5703eb14 131# mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
132 SKIP: {
b5453fbb 133 skip 'Need at least version 1.09 of DBD::Sybase to test TEXT/IMAGE', 12
5703eb14 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 }
e0b2344f 151
5703eb14 152 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
153 $binstr{'large'} = $binstr{'small'} x 1024;
e0b2344f 154
5703eb14 155 my $maxloblen = length $binstr{'large'};
156 note
157 "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
158 local $dbh->{'LongReadLen'} = $maxloblen;
e0b2344f 159
5703eb14 160 my $rs = $schema->resultset('BindType');
161 my $last_id;
e0b2344f 162
5703eb14 163 foreach my $type (qw(blob clob)) {
164 foreach my $size (qw(small large)) {
165 no warnings 'uninitialized';
bc54ca97 166
5703eb14 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 {
23419345 174 $rs->find($last_id)->$type
5703eb14 175 };
176 diag $@ if $@;
177 ok($got eq $binstr{$size}, "verified inserted $size $type");
178 }
179 }
bc54ca97 180
5703eb14 181 # try a blob update
182 TODO: {
183 local $TODO = 'updating TEXT/IMAGE does not work yet';
9b3dabe0 184
5703eb14 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 {
23419345 190 $rs->find($last_id)->blob
5703eb14 191 } eq $new_str, "verified updated blob" );
64f4e691 192 diag $@ if $@;
e0b2344f 193 }
7d17f469 194
5703eb14 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 $@;
7d17f469 213
5703eb14 214 my $got = eval {
23419345 215 $rs->find(1)->blob
5703eb14 216 };
7d17f469 217 diag $@ if $@;
5703eb14 218 ok($got eq $binstr{large}, "verified inserted large blob");
9b3dabe0 219 }
6b1f5ef7 220}
a964a928 221
222# clean up our mess
223END {
6b1f5ef7 224 if (my $dbh = eval { $schema->storage->_dbh }) {
225 $dbh->do('DROP TABLE artist');
5703eb14 226 eval { $dbh->do('DROP TABLE bindtype_test') };
6b1f5ef7 227 }
a964a928 228}