change the (incorrect) version check to a check for FreeTDS
[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 (or using FreeTDS)
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 'TEXT/IMAGE support does not work with FreeTDS', 12
134       if $schema->storage->_using_freetds;
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 * 2;
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->find($last_id)->$type
175         };
176         diag $@ if $@;
177         ok($got eq $binstr{$size}, "verified inserted $size $type");
178       }
179     }
180
181     # blob insert with explicit PK
182     {
183       local $SIG{__WARN__} = sub {};
184       eval { $dbh->do('DROP TABLE bindtype_test') };
185
186       $dbh->do(qq[
187         CREATE TABLE bindtype_test 
188         (
189           id    INT   PRIMARY KEY,
190           bytea INT   NULL,
191           blob  IMAGE NULL,
192           clob  TEXT  NULL
193         )
194       ],{ RaiseError => 1, PrintError => 0 });
195     }
196     my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) };
197     ok(!$@, "inserted large blob without dying with manual PK");
198     diag $@ if $@;
199
200     my $got = eval {
201       $rs->find(1)->blob
202     };
203     diag $@ if $@;
204     ok($got eq $binstr{large}, "verified inserted large blob with manual PK");
205
206     # try a blob update
207     my $new_str = $binstr{large} . 'mtfnpy';
208     eval { $rs->search({ id => 1 })->update({ blob => $new_str }) };
209     ok !$@, 'updated blob successfully';
210     diag $@ if $@;
211     $got = eval {
212       $rs->find(1)->blob
213     };
214     diag $@ if $@;
215     ok($got eq $new_str, "verified updated blob");
216   }
217 }
218
219 # clean up our mess
220 END {
221   if (my $dbh = eval { $schema->storage->_dbh }) {
222     $dbh->do('DROP TABLE artist');
223     eval { $dbh->do('DROP TABLE bindtype_test')    };
224   }
225 }