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