a1f980728c4b4638d8a51394582070551484bfa2
[dbsrgits/DBIx-Class.git] / t / 750firebird.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 use Scope::Guard ();
9
10 # tests stolen from 749sybase_asa.t
11
12 my ($dsn, $user, $pass)    = @ENV{map { "DBICTEST_FIREBIRD_${_}" }      qw/DSN USER PASS/};
13 my ($dsn2, $user2, $pass2) = @ENV{map { "DBICTEST_FIREBIRD_ODBC_${_}" } qw/DSN USER PASS/};
14
15 plan skip_all => <<'EOF' unless $dsn || $dsn2;
16 Set $ENV{DBICTEST_FIREBIRD_DSN} and/or $ENV{DBICTEST_FIREBIRD_ODBC_DSN},
17 _USER and _PASS to run these tests
18 EOF
19
20 my @info = (
21   [ $dsn,  $user,  $pass  ],
22   [ $dsn2, $user2, $pass2 ],
23 );
24
25 my $schema;
26
27 foreach my $conn_idx (0..$#info) {
28   my ($dsn, $user, $pass) = @{ $info[$conn_idx] || [] };
29
30   next unless $dsn;
31
32   $schema = DBICTest::Schema->connect($dsn, $user, $pass, {
33     auto_savepoint => 1,
34     quote_char     => q["],
35     name_sep       => q[.],
36   });
37   my $dbh = $schema->storage->dbh;
38
39   my $sg = Scope::Guard->new(\&cleanup);
40
41   eval { $dbh->do(q[DROP TABLE "artist"]) };
42   $dbh->do(<<EOF);
43   CREATE TABLE "artist" (
44     "artistid" INT PRIMARY KEY,
45     "name" VARCHAR(255),
46     "charfield" CHAR(10),
47     "rank" INT DEFAULT 13
48   )
49 EOF
50   eval { $dbh->do(q[DROP GENERATOR "gen_artist_artistid"]) };
51   $dbh->do('CREATE GENERATOR "gen_artist_artistid"');
52   eval { $dbh->do('DROP TRIGGER "artist_bi"') };
53   $dbh->do(<<EOF);
54   CREATE TRIGGER "artist_bi" FOR "artist"
55   ACTIVE BEFORE INSERT POSITION 0
56   AS
57   BEGIN
58    IF (NEW."artistid" IS NULL) THEN
59     NEW."artistid" = GEN_ID("gen_artist_artistid",1);
60   END
61 EOF
62
63   my $ars = $schema->resultset('Artist');
64   is ( $ars->count, 0, 'No rows at first' );
65
66 # test primary key handling
67   my $new = $ars->create({ name => 'foo' });
68   ok($new->artistid, "Auto-PK worked");
69
70 # test savepoints
71   eval {
72     $schema->txn_do(sub {
73       eval {
74         $schema->txn_do(sub {
75           $ars->create({ name => 'in_savepoint' });
76           die "rolling back savepoint";
77         });
78       };
79       ok ((not $ars->search({ name => 'in_savepoint' })->first),
80         'savepoint rolled back');
81       $ars->create({ name => 'in_outer_txn' });
82       die "rolling back outer txn";
83     });
84   };
85
86   like $@, qr/rolling back outer txn/,
87     'correct exception for rollback';
88
89   ok ((not $ars->search({ name => 'in_outer_txn' })->first),
90     'outer txn rolled back');
91
92 # test explicit key spec
93   $new = $ars->create ({ name => 'bar', artistid => 66 });
94   is($new->artistid, 66, 'Explicit PK worked');
95   $new->discard_changes;
96   is($new->artistid, 66, 'Explicit PK assigned');
97
98   lives_ok {
99     $new->update({ name => 'baz' })
100   } 'update survived';
101   $new->discard_changes;
102   is $new->name, 'baz', 'row updated';
103
104 # test populate
105   lives_ok (sub {
106     my @pop;
107     for (1..2) {
108       push @pop, { name => "Artist_$_" };
109     }
110     $ars->populate (\@pop);
111   });
112
113 # test populate with explicit key
114   lives_ok (sub {
115     my @pop;
116     for (1..2) {
117       push @pop, { name => "Artist_expkey_$_", artistid => 100 + $_ };
118     }
119     # XXX why does insert_bulk not work here?
120     my @foo = $ars->populate (\@pop);
121   });
122
123 # count what we did so far
124   is ($ars->count, 6, 'Simple count works');
125
126 # test UPDATE
127   lives_ok {
128     $schema->resultset('Artist')
129            ->search({name => 'foo'})
130            ->update({rank => 4 });
131   } 'Can update a column';
132
133   my ($updated) = $schema->resultset('Artist')->search({name => 'foo'});
134   is $updated->rank, 4, 'and the update made it to the database';
135
136
137 # test LIMIT support
138   my $lim = $ars->search( {},
139     {
140       rows => 3,
141       offset => 4,
142       order_by => 'artistid'
143     }
144   );
145   is( $lim->count, 2, 'ROWS+OFFSET count ok' );
146   is( $lim->all, 2, 'Number of ->all objects matches count' );
147
148 # test iterator
149   $lim->reset;
150   is( $lim->next->artistid, 101, "iterator->next ok" );
151   is( $lim->next->artistid, 102, "iterator->next ok" );
152   is( $lim->next, undef, "next past end of resultset ok" );
153
154 # test empty insert
155   {
156     local $ars->result_source->column_info('artistid')->{is_auto_increment} = 0;
157
158     lives_ok { $ars->create({}) }
159       'empty insert works';
160   }
161
162 # test blobs (stolen from 73oracle.t)
163   SKIP: {
164     eval { $dbh->do('DROP TABLE "bindtype_test2"') };
165     $dbh->do(q[
166     CREATE TABLE "bindtype_test2"
167     (
168       "id"     INT PRIMARY KEY,
169       "bytea"  INT,
170       "a_blob" BLOB,
171       "a_clob" BLOB SUB_TYPE TEXT
172     )
173     ]);
174
175     my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
176     $binstr{'large'} = $binstr{'small'} x 1024;
177
178     my $maxloblen = length $binstr{'large'};
179     local $dbh->{'LongReadLen'} = $maxloblen;
180
181     my $rs = $schema->resultset('BindType2');
182     my $id = 0;
183
184     foreach my $type (qw( a_blob a_clob )) {
185       foreach my $size (qw( small large )) {
186         $id++;
187
188 # turn off horrendous binary DBIC_TRACE output
189         local $schema->storage->{debug} = 0;
190
191         lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) }
192         "inserted $size $type without dying";
193
194         ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" );
195       }
196     }
197   }
198 }
199
200 done_testing;
201
202 # clean up our mess
203
204 sub cleanup {
205   my $dbh;
206   eval {
207     $schema->storage->disconnect; # to avoid object FOO is in use errors
208     $dbh = $schema->storage->dbh;
209   };
210   return unless $dbh;
211
212   eval { $dbh->do('DROP TRIGGER "artist_bi"') };
213   diag $@ if $@;
214
215   eval { $dbh->do('DROP GENERATOR "gen_artist_artistid"') };
216   diag $@ if $@;
217
218   foreach my $table (qw/artist bindtype_test/) {
219     eval { $dbh->do(qq[DROP TABLE "$table"]) };
220     #diag $@ if $@;
221   }
222 }