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