Commit | Line | Data |
3a8aae80 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use Test::More; |
5 | use Test::Exception; |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
dff4c3a3 |
8 | use Scope::Guard (); |
3a8aae80 |
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 | |
145b2a3d |
25 | my $schema; |
3a8aae80 |
26 | |
5c6ed0b5 |
27 | foreach my $conn_idx (0..$#info) { |
9633951d |
28 | my ($dsn, $user, $pass) = @{ $info[$conn_idx] || [] }; |
3a8aae80 |
29 | |
30 | next unless $dsn; |
31 | |
32323fc2 |
32 | $schema = DBICTest::Schema->connect($dsn, $user, $pass, { |
930689e6 |
33 | auto_savepoint => 1, |
34 | quote_char => q["], |
35 | name_sep => q[.], |
32323fc2 |
36 | }); |
3a8aae80 |
37 | my $dbh = $schema->storage->dbh; |
38 | |
dff4c3a3 |
39 | my $sg = Scope::Guard->new(\&cleanup); |
3a8aae80 |
40 | |
930689e6 |
41 | eval { $dbh->do(q[DROP TABLE "artist"]) }; |
3a8aae80 |
42 | $dbh->do(<<EOF); |
930689e6 |
43 | CREATE TABLE "artist" ( |
44 | "artistid" INT PRIMARY KEY, |
45 | "name" VARCHAR(255), |
46 | "charfield" CHAR(10), |
47 | "rank" INT DEFAULT 13 |
3a8aae80 |
48 | ) |
49 | EOF |
930689e6 |
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"') }; |
3a8aae80 |
53 | $dbh->do(<<EOF); |
930689e6 |
54 | CREATE TRIGGER "artist_bi" FOR "artist" |
3a8aae80 |
55 | ACTIVE BEFORE INSERT POSITION 0 |
56 | AS |
57 | BEGIN |
930689e6 |
58 | IF (NEW."artistid" IS NULL) THEN |
59 | NEW."artistid" = GEN_ID("gen_artist_artistid",1); |
3a8aae80 |
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 | |
32323fc2 |
70 | # test savepoints |
a499b173 |
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 | }; |
5c6ed0b5 |
85 | |
86 | like $@, qr/rolling back outer txn/, |
87 | 'correct exception for rollback'; |
88 | |
a499b173 |
89 | ok ((not $ars->search({ name => 'in_outer_txn' })->first), |
90 | 'outer txn rolled back'); |
32323fc2 |
91 | |
3a8aae80 |
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 | |
babd3d8e |
98 | lives_ok { |
99 | $new->update({ name => 'baz' }) |
100 | } 'update survived'; |
101 | $new->discard_changes; |
102 | is $new->name, 'baz', 'row updated'; |
103 | |
3a8aae80 |
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 | } |
17d59d97 |
119 | $ars->populate (\@pop); |
3a8aae80 |
120 | }); |
121 | |
122 | # count what we did so far |
123 | is ($ars->count, 6, 'Simple count works'); |
124 | |
72537d31 |
125 | # test UPDATE |
17d59d97 |
126 | lives_and { |
127 | $ars->search({ name => 'foo' })->update({ rank => 4 }); |
128 | |
129 | is $ars->search({ name => 'foo' })->first->rank, 4; |
72537d31 |
130 | } 'Can update a column'; |
131 | |
132 | my ($updated) = $schema->resultset('Artist')->search({name => 'foo'}); |
133 | is $updated->rank, 4, 'and the update made it to the database'; |
134 | |
135 | |
3a8aae80 |
136 | # test LIMIT support |
137 | my $lim = $ars->search( {}, |
138 | { |
139 | rows => 3, |
140 | offset => 4, |
141 | order_by => 'artistid' |
142 | } |
143 | ); |
144 | is( $lim->count, 2, 'ROWS+OFFSET count ok' ); |
145 | is( $lim->all, 2, 'Number of ->all objects matches count' ); |
146 | |
147 | # test iterator |
148 | $lim->reset; |
149 | is( $lim->next->artistid, 101, "iterator->next ok" ); |
150 | is( $lim->next->artistid, 102, "iterator->next ok" ); |
151 | is( $lim->next, undef, "next past end of resultset ok" ); |
152 | |
17d59d97 |
153 | # test multiple executing cursors |
154 | { |
155 | my $rs1 = $ars->search({}, { order_by => { -asc => 'artistid' }}); |
156 | my $rs2 = $ars->search({}, { order_by => { -desc => 'artistid' }}); |
157 | |
c13488fd |
158 | is $rs1->next->artistid, 1, 'multiple cursors'; |
159 | is $rs2->next->artistid, 102, 'multiple cursors'; |
17d59d97 |
160 | } |
161 | |
3a8aae80 |
162 | # test empty insert |
163 | { |
164 | local $ars->result_source->column_info('artistid')->{is_auto_increment} = 0; |
165 | |
166 | lives_ok { $ars->create({}) } |
167 | 'empty insert works'; |
168 | } |
169 | |
170 | # test blobs (stolen from 73oracle.t) |
145b2a3d |
171 | SKIP: { |
930689e6 |
172 | eval { $dbh->do('DROP TABLE "bindtype_test2"') }; |
145b2a3d |
173 | $dbh->do(q[ |
930689e6 |
174 | CREATE TABLE "bindtype_test2" |
145b2a3d |
175 | ( |
930689e6 |
176 | "id" INT PRIMARY KEY, |
177 | "bytea" INT, |
178 | "a_blob" BLOB, |
179 | "a_clob" BLOB SUB_TYPE TEXT |
145b2a3d |
180 | ) |
181 | ]); |
182 | |
145b2a3d |
183 | my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) ); |
184 | $binstr{'large'} = $binstr{'small'} x 1024; |
3a8aae80 |
185 | |
145b2a3d |
186 | my $maxloblen = length $binstr{'large'}; |
187 | local $dbh->{'LongReadLen'} = $maxloblen; |
3a8aae80 |
188 | |
90489c23 |
189 | my $rs = $schema->resultset('BindType2'); |
145b2a3d |
190 | my $id = 0; |
3a8aae80 |
191 | |
145b2a3d |
192 | foreach my $type (qw( a_blob a_clob )) { |
193 | foreach my $size (qw( small large )) { |
194 | $id++; |
3a8aae80 |
195 | |
196 | # turn off horrendous binary DBIC_TRACE output |
145b2a3d |
197 | local $schema->storage->{debug} = 0; |
3a8aae80 |
198 | |
145b2a3d |
199 | lives_ok { $rs->create( { 'id' => $id, $type => $binstr{$size} } ) } |
200 | "inserted $size $type without dying"; |
3a8aae80 |
201 | |
145b2a3d |
202 | ok($rs->find($id)->$type eq $binstr{$size}, "verified inserted $size $type" ); |
203 | } |
3a8aae80 |
204 | } |
205 | } |
206 | } |
207 | |
208 | done_testing; |
209 | |
210 | # clean up our mess |
dff4c3a3 |
211 | |
212 | sub cleanup { |
145b2a3d |
213 | my $dbh; |
214 | eval { |
215 | $schema->storage->disconnect; # to avoid object FOO is in use errors |
216 | $dbh = $schema->storage->dbh; |
217 | }; |
218 | return unless $dbh; |
219 | |
930689e6 |
220 | eval { $dbh->do('DROP TRIGGER "artist_bi"') }; |
145b2a3d |
221 | diag $@ if $@; |
222 | |
930689e6 |
223 | eval { $dbh->do('DROP GENERATOR "gen_artist_artistid"') }; |
145b2a3d |
224 | diag $@ if $@; |
225 | |
9633951d |
226 | foreach my $table (qw/artist bindtype_test/) { |
227 | eval { $dbh->do(qq[DROP TABLE "$table"]) }; |
145b2a3d |
228 | #diag $@ if $@; |
3a8aae80 |
229 | } |
230 | } |