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