code cleanups
[dbsrgits/DBIx-Class.git] / t / 746sybase.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_SYBASE_${_}" } qw/DSN USER PASS/};
10
11 if (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 }
19
20 my @storage_types = (
21   'DBI::Sybase',
22   'DBI::Sybase::NoBindVars',
23 );
24 my $schema;
25
26 for 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   }
32   $schema->connection($dsn, $user, $pass, {
33     AutoCommit => 1,
34     on_connect_call => [
35       [ blob_setup => log_on_update => 1 ], # this is a safer option
36     ],
37   });
38
39   $schema->storage->ensure_connected;
40
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") };
46       $dbh->do(<<'SQL');
47 CREATE TABLE artist (
48    artistid INT IDENTITY PRIMARY KEY,
49    name VARCHAR(100),
50    rank INT DEFAULT 13 NOT NULL,
51    charfield CHAR(10) NULL
52 )
53 SQL
54   });
55
56   my %seen_id;
57
58 # so we start unconnected
59   $schema->storage->disconnect;
60
61 # test primary key handling
62   my $new = $schema->resultset('Artist')->create({ name => 'foo' });
63   ok($new->artistid > 0, "Auto-PK worked");
64
65   $seen_id{$new->artistid}++;
66
67   for (1..6) {
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}++;
71   }
72
73 # test simple count
74   is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
75
76 # test LIMIT support
77   my $it = $schema->resultset('Artist')->search({
78     artistid => { '>' => 0 }
79   }, {
80     rows => 3,
81     order_by => 'artistid',
82   });
83
84   is( $it->count, 3, "LIMIT count ok" );
85
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" );
90
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
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
115 # mostly stole the blob stuff Nniuq wrote for t/73oracle.t
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
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" );
154     }
155   }
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   }
170 }
171
172 # clean up our mess
173 END {
174   if (my $dbh = eval { $schema->storage->_dbh }) {
175     $dbh->do('DROP TABLE artist');
176     $dbh->do('DROP TABLE bindtype_test');
177   }
178 }