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