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