add money type support
[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 = 35 + 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 # this is so we can set ->storage_type before connecting
33   my $schema = DBICTest::Schema->clone;
34
35   unless ($storage_type eq 'DBI::Sybase') { # autodetect
36     $schema->storage_type("::$storage_type");
37   }
38
39   $schema->connection($dsn, $user, $pass, {
40     AutoCommit => 1,
41     on_connect_call => [
42       [ blob_setup => log_on_update => 1 ], # this is a safer option
43     ],
44   });
45
46   $schema->storage->ensure_connected;
47
48   if ($storage_idx == 0 &&
49       $schema->storage->isa('DBIx::Class::Storage::DBI::Sybase::NoBindVars')) {
50 # no placeholders in this version of Sybase or DBD::Sybase (or using FreeTDS)
51       my $tb = Test::More->builder;
52       $tb->skip('no placeholders') for 1..$TESTS;
53       next;
54   }
55
56   isa_ok( $schema->storage, "DBIx::Class::Storage::$storage_type" );
57
58   $schema->storage->_dbh->disconnect;
59   lives_ok (sub { $schema->storage->dbh }, 'reconnect works');
60
61   $schema->storage->dbh_do (sub {
62       my ($storage, $dbh) = @_;
63       eval { $dbh->do("DROP TABLE artist") };
64       $dbh->do(<<'SQL');
65 CREATE TABLE artist (
66    artistid INT IDENTITY PRIMARY KEY,
67    name VARCHAR(100),
68    rank INT DEFAULT 13 NOT NULL,
69    charfield CHAR(10) NULL
70 )
71 SQL
72   });
73
74   my %seen_id;
75
76 # so we start unconnected
77   $schema->storage->disconnect;
78
79 # inserts happen in a txn, so we make sure they can nest
80   $schema->txn_begin;
81
82 # test primary key handling
83   my $new = $schema->resultset('Artist')->create({ name => 'foo' });
84   ok($new->artistid > 0, "Auto-PK worked");
85
86   $seen_id{$new->artistid}++;
87
88   for (1..6) {
89     $new = $schema->resultset('Artist')->create({ name => 'Artist ' . $_ });
90     is ( $seen_id{$new->artistid}, undef, "id for Artist $_ is unique" );
91     $seen_id{$new->artistid}++;
92   }
93
94   $schema->txn_commit;
95
96 # test simple count
97   is ($schema->resultset('Artist')->count, 7, 'count(*) of whole table ok');
98
99 # test LIMIT support
100   my $it = $schema->resultset('Artist')->search({
101     artistid => { '>' => 0 }
102   }, {
103     rows => 3,
104     order_by => 'artistid',
105   });
106
107   is( $it->count, 3, "LIMIT count ok" );
108
109   is( $it->next->name, "foo", "iterator->next ok" );
110   $it->next;
111   is( $it->next->name, "Artist 2", "iterator->next ok" );
112   is( $it->next, undef, "next past end of resultset ok" );
113
114 # now try with offset
115   $it = $schema->resultset('Artist')->search({}, {
116     rows => 3,
117     offset => 3,
118     order_by => 'artistid',
119   });
120
121   is( $it->count, 3, "LIMIT with offset count ok" );
122
123   is( $it->next->name, "Artist 3", "iterator->next ok" );
124   $it->next;
125   is( $it->next->name, "Artist 5", "iterator->next ok" );
126   is( $it->next, undef, "next past end of resultset ok" );
127
128 # now try a grouped count
129   $schema->resultset('Artist')->create({ name => 'Artist 6' })
130     for (1..6);
131
132   $it = $schema->resultset('Artist')->search({}, {
133     group_by => 'name'
134   });
135
136   is( $it->count, 7, 'COUNT of GROUP_BY ok' );
137
138 # mostly stolen from the blob stuff Nniuq wrote for t/73oracle.t
139   SKIP: {
140     skip 'TEXT/IMAGE support does not work with FreeTDS', 12
141       if $schema->storage->_using_freetds;
142
143     my $dbh = $schema->storage->dbh;
144     {
145       local $SIG{__WARN__} = sub {};
146       eval { $dbh->do('DROP TABLE bindtype_test') };
147
148       $dbh->do(qq[
149         CREATE TABLE bindtype_test 
150         (
151           id    INT   IDENTITY PRIMARY KEY,
152           bytea INT   NULL,
153           blob  IMAGE NULL,
154           clob  TEXT  NULL
155         )
156       ],{ RaiseError => 1, PrintError => 0 });
157     }
158
159     my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
160     $binstr{'large'} = $binstr{'small'} x 1024;
161
162     my $maxloblen = length $binstr{'large'};
163     note
164       "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
165     local $dbh->{'LongReadLen'} = $maxloblen * 2;
166
167     my $rs = $schema->resultset('BindType');
168     my $last_id;
169
170     foreach my $type (qw(blob clob)) {
171       foreach my $size (qw(small large)) {
172         no warnings 'uninitialized';
173
174         my $created = eval { $rs->create( { $type => $binstr{$size} } ) };
175         ok(!$@, "inserted $size $type without dying");
176         diag $@ if $@;
177
178         $last_id = $created->id if $created;
179
180         my $got = eval {
181           $rs->find($last_id)->$type
182         };
183         diag $@ if $@;
184         ok($got eq $binstr{$size}, "verified inserted $size $type");
185       }
186     }
187
188     # blob insert with explicit PK
189     # also a good opportunity to test IDENTITY_INSERT
190     {
191       local $SIG{__WARN__} = sub {};
192       eval { $dbh->do('DROP TABLE bindtype_test') };
193
194       $dbh->do(qq[
195         CREATE TABLE bindtype_test 
196         (
197           id    INT   IDENTITY PRIMARY KEY,
198           bytea INT   NULL,
199           blob  IMAGE NULL,
200           clob  TEXT  NULL
201         )
202       ],{ RaiseError => 1, PrintError => 0 });
203     }
204     my $created = eval { $rs->create( { id => 1, blob => $binstr{large} } ) };
205     ok(!$@, "inserted large blob without dying with manual PK");
206     diag $@ if $@;
207
208     my $got = eval {
209       $rs->find(1)->blob
210     };
211     diag $@ if $@;
212     ok($got eq $binstr{large}, "verified inserted large blob with manual PK");
213
214     # try a blob update
215     my $new_str = $binstr{large} . 'mtfnpy';
216     eval { $rs->search({ id => 1 })->update({ blob => $new_str }) };
217     ok !$@, 'updated blob successfully';
218     diag $@ if $@;
219     $got = eval {
220       $rs->find(1)->blob
221     };
222     diag $@ if $@;
223     ok($got eq $new_str, "verified updated blob");
224   }
225
226 # test MONEY column support
227   $schema->storage->dbh_do (sub {
228       my ($storage, $dbh) = @_;
229       eval { $dbh->do("DROP TABLE money_test") };
230       $dbh->do(<<'SQL');
231 CREATE TABLE money_test (
232    id INT IDENTITY PRIMARY KEY,
233    amount MONEY NULL
234 )
235 SQL
236   });
237
238   my $rs = $schema->resultset('Money');
239
240   my $row;
241   lives_ok {
242     $row = $rs->create({ amount => 100 });
243   } 'inserted a money value';
244
245   is eval { $rs->find($row->id)->amount }, 100, 'money value round-trip';
246
247   lives_ok {
248     $row->update({ amount => 200 });
249   } 'updated a money value';
250
251   is eval { $rs->find($row->id)->amount },
252     200, 'updated money value round-trip';
253
254   lives_ok {
255     $row->update({ amount => undef });
256   } 'updated a money value to NULL';
257
258   my $null_amount = eval { $rs->find($row->id)->amount };
259   ok(
260     (($null_amount == undef) && (not $@)),
261     'updated money value to NULL round-trip'
262   );
263   diag $@ if $@;
264 }
265
266 # clean up our mess
267 END {
268   if (my $dbh = eval { $schema->storage->_dbh }) {
269     eval { $dbh->do("DROP TABLE $_") }
270       for qw/artist bindtype_test money_test/;
271   }
272 }