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