Silence spurious warnings from todoified test
[dbsrgits/DBIx-Class.git] / t / 73oracle_blob.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
cb551b07 2use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_oracle';
3
1143e5bd 4use strict;
5use warnings;
6
7use Test::Exception;
8use Test::More;
9use Sub::Name;
10use Try::Tiny;
1143e5bd 11
74113bd1 12use DBICTest::Schema::BindType;
13BEGIN {
14 DBICTest::Schema::BindType->add_columns(
15 'blb2' => {
16 data_type => 'blob',
17 is_nullable => 1,
18 },
19 'clb2' => {
20 data_type => 'clob',
21 is_nullable => 1,
22 }
23 );
24}
25
1143e5bd 26use DBICTest;
1143e5bd 27
1143e5bd 28$ENV{NLS_SORT} = "BINARY";
29$ENV{NLS_COMP} = "BINARY";
30$ENV{NLS_LANG} = "AMERICAN";
31
cb551b07 32my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_ORA_${_}" } qw/DSN USER PASS/};
33
1143e5bd 34my $v = do {
af1f4f84 35 my $si = DBICTest::Schema->connect($dsn, $user, $pass)->storage->_server_info;
36 $si->{normalized_dbms_version}
37 or die "Unparseable Oracle server version: $si->{dbms_version}\n";
1143e5bd 38};
af1f4f84 39
1143e5bd 40##########
41# the recyclebin (new for 10g) sometimes comes in the way
42my $on_connect_sql = $v >= 10 ? ["ALTER SESSION SET recyclebin = OFF"] : [];
43
44# iterate all tests on following options
45my @tryopt = (
46 { on_connect_do => $on_connect_sql },
47 { quote_char => '"', on_connect_do => $on_connect_sql },
48);
49
50# keep a database handle open for cleanup
51my $dbh;
52
53my $schema;
54for my $opt (@tryopt) {
55 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, $opt);
56
57 $dbh = $schema->storage->dbh;
58 my $q = $schema->storage->sql_maker->quote_char || '';
59
60 do_creates($dbh, $q);
61
62 _run_blob_tests($schema, $opt);
63}
64
65sub _run_blob_tests {
66SKIP: {
1143e5bd 67 my ($schema, $opt) = @_;
68 my %binstr = ( 'small' => join('', map { chr($_) } ( 1 .. 127 )) );
69 $binstr{'large'} = $binstr{'small'} x 1024;
70
71 my $maxloblen = (length $binstr{'large'}) + 5;
72 note "Localizing LongReadLen to $maxloblen to avoid truncation of test data";
73 local $dbh->{'LongReadLen'} = $maxloblen;
74
75 my $rs = $schema->resultset('BindType');
76
77 if ($DBD::Oracle::VERSION eq '1.23') {
78 throws_ok { $rs->create({ id => 1, blob => $binstr{large} }) }
79 qr/broken/,
80 'throws on blob insert with DBD::Oracle == 1.23';
81 skip 'buggy BLOB support in DBD::Oracle 1.23', 1;
82 }
83
84 my $q = $schema->storage->sql_maker->quote_char || '';
85 local $TODO = 'Something is confusing column bindtype assignment when quotes are active'
86 . ': https://rt.cpan.org/Ticket/Display.html?id=64206'
87 if $q;
88
1143e5bd 89 my $id;
90 foreach my $size (qw( small large )) {
91 $id++;
92
49eeb48d 93 local $schema->storage->{debug} = 0
94 if $size eq 'large';
1143e5bd 95
96 my $str = $binstr{$size};
97 lives_ok {
74113bd1 98 $rs->create( { 'id' => $id, blob => "blob:$str", clob => "clob:$str", blb2 => "blb2:$str", clb2 => "clb2:$str" } )
1143e5bd 99 } "inserted $size without dying";
100
101 my %kids = %{$schema->storage->_dbh->{CachedKids}};
102 my @objs = $rs->search({ blob => "blob:$str", clob => "clob:$str" })->all;
103 is_deeply (
104 $schema->storage->_dbh->{CachedKids},
105 \%kids,
106 'multi-part LOB equality query was not cached',
107 ) if $size eq 'large';
108 is @objs, 1, 'One row found matching on both LOBs';
109 ok (try { $objs[0]->blob }||'' eq "blob:$str", 'blob inserted/retrieved correctly');
110 ok (try { $objs[0]->clob }||'' eq "clob:$str", 'clob inserted/retrieved correctly');
74113bd1 111 ok (try { $objs[0]->clb2 }||'' eq "clb2:$str", "clb2 inserted correctly");
112 ok (try { $objs[0]->blb2 }||'' eq "blb2:$str", "blb2 inserted correctly");
1143e5bd 113
4ca1fd6f 114 {
1143e5bd 115 local $TODO = '-like comparison on blobs not tested before ora 10 (fails on 8i)'
116 if $schema->storage->_server_info->{normalized_dbms_version} < 10;
117
118 lives_ok {
119 @objs = $rs->search({ clob => { -like => 'clob:%' } })->all;
120 ok (@objs, 'rows found matching CLOB with a LIKE query');
121 } 'Query with like on blob succeeds';
122 }
123
124 ok(my $subq = $rs->search(
125 { blob => "blob:$str", clob => "clob:$str" },
126 {
127 from => \ "(SELECT * FROM ${q}bindtype_test${q} WHERE ${q}id${q} != ?) ${q}me${q}",
7e175371 128 bind => [ [ {} => 12345678 ] ],
1143e5bd 129 }
130 )->get_column('id')->as_query);
131
132 @objs = $rs->search({ id => { -in => $subq } })->all;
133 is (@objs, 1, 'One row found matching on both LOBs as a subquery');
134
135 lives_ok {
136 $rs->search({ id => $id, blob => "blob:$str", clob => "clob:$str" })
74113bd1 137 ->update({ blob => 'updated blob', clob => 'updated clob', clb2 => 'updated clb2', blb2 => 'updated blb2' });
1143e5bd 138 } 'blob UPDATE with blobs in WHERE clause survived';
139
140 @objs = $rs->search({ blob => "updated blob", clob => 'updated clob' })->all;
141 is @objs, 1, 'found updated row';
142 ok (try { $objs[0]->blob }||'' eq "updated blob", 'blob updated/retrieved correctly');
143 ok (try { $objs[0]->clob }||'' eq "updated clob", 'clob updated/retrieved correctly');
74113bd1 144 ok (try { $objs[0]->clb2 }||'' eq "updated clb2", "clb2 updated correctly");
145 ok (try { $objs[0]->blb2 }||'' eq "updated blb2", "blb2 updated correctly");
1143e5bd 146
147 lives_ok {
148 $rs->search({ id => $id })
149 ->update({ blob => 're-updated blob', clob => 're-updated clob' });
150 } 'blob UPDATE without blobs in WHERE clause survived';
151
152 @objs = $rs->search({ blob => 're-updated blob', clob => 're-updated clob' })->all;
153 is @objs, 1, 'found updated row';
154 ok (try { $objs[0]->blob }||'' eq 're-updated blob', 'blob updated/retrieved correctly');
155 ok (try { $objs[0]->clob }||'' eq 're-updated clob', 'clob updated/retrieved correctly');
156
157 lives_ok {
158 $rs->search({ blob => "re-updated blob", clob => "re-updated clob" })
159 ->delete;
160 } 'blob DELETE with WHERE clause survived';
161 @objs = $rs->search({ blob => "re-updated blob", clob => 're-updated clob' })->all;
162 is @objs, 0, 'row deleted successfully';
163 }
4ca1fd6f 164}
1143e5bd 165
166 do_clean ($dbh);
167}
168
169done_testing;
170
171sub do_creates {
172 my ($dbh, $q) = @_;
173
174 do_clean($dbh);
175
74113bd1 176 $dbh->do("CREATE TABLE ${q}bindtype_test${q} (${q}id${q} integer NOT NULL PRIMARY KEY, ${q}bytea${q} integer NULL, ${q}blob${q} blob NULL, ${q}blb2${q} blob NULL, ${q}clob${q} clob NULL, ${q}clb2${q} clob NULL, ${q}a_memo${q} integer NULL)");
1143e5bd 177}
178
179# clean up our mess
180sub do_clean {
181
182 my $dbh = shift || return;
183
184 for my $q ('', '"') {
185 my @clean = (
186 "DROP TABLE ${q}bindtype_test${q}",
187 );
188 eval { $dbh -> do ($_) } for @clean;
189 }
190}
191
192END {
6918c70e 193 if ($dbh) {
1143e5bd 194 local $SIG{__WARN__} = sub {};
6918c70e 195 do_clean($dbh);
196 undef $dbh;
1143e5bd 197 }
198}