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