Fix updating multiple CLOB/BLOB columns on Oracle
[dbsrgits/DBIx-Class.git] / t / 72pg_bytea.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use DBIx::Class::Optional::Dependencies ();
6 use Try::Tiny;
7 use lib qw(t/lib);
8 use DBICTest;
9
10 plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('rdbms_pg')
11   unless DBIx::Class::Optional::Dependencies->req_ok_for ('rdbms_pg');
12
13 my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
14
15 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
16   unless ($dsn && $dbuser);
17
18 my $schema = DBICTest->connect_schema($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
19
20 if ($schema->storage->_server_info->{normalized_dbms_version} >= 9.0) {
21   if (not try { DBD::Pg->VERSION('2.17.2') }) {
22     plan skip_all =>
23       'DBD::Pg < 2.17.2 does not work with Pg >= 9.0 BYTEA columns';
24   }
25 }
26 elsif (not try { DBD::Pg->VERSION('2.9.2') }) {
27   plan skip_all =>
28     'DBD::Pg < 2.9.2 does not work with BYTEA columns';
29 }
30
31 my $dbh = $schema->storage->dbh;
32
33 {
34     local $SIG{__WARN__} = sub {};
35     $dbh->do('DROP TABLE IF EXISTS bindtype_test');
36
37     # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
38     $dbh->do(qq[
39         CREATE TABLE bindtype_test
40         (
41             id              serial       NOT NULL   PRIMARY KEY,
42             bytea           bytea        NULL,
43             blob            bytea        NULL,
44             blob2           bytea        NULL,
45             clob            text         NULL,
46             clob2           text         NULL,
47             a_memo          text         NULL
48         );
49     ],{ RaiseError => 1, PrintError => 1 });
50 }
51
52 $schema->storage->debug(0); # these tests spew up way too much stuff, disable trace
53
54 my $big_long_string = "\x00\x01\x02 abcd" x 125000;
55
56 my $new;
57 # test inserting a row
58 {
59   $new = $schema->resultset('BindType')->create({ bytea => $big_long_string });
60
61   ok($new->id, "Created a bytea row");
62   ok($new->bytea eq $big_long_string, "Set the blob correctly.");
63 }
64
65 # test retrieval of the bytea column
66 {
67   my $row = $schema->resultset('BindType')->find({ id => $new->id });
68   ok($row->get_column('bytea') eq $big_long_string, "Created the blob correctly.");
69 }
70
71 {
72   my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
73
74   # search on the bytea column (select)
75   {
76     my $row = $rs->first;
77     is($row ? $row->id : undef, $new->id, "Found the row searching on the bytea column.");
78   }
79
80   # search on the bytea column (update)
81   {
82     my $new_big_long_string = $big_long_string . "2";
83     $schema->txn_do(sub {
84       $rs->update({ bytea => $new_big_long_string });
85       my $row = $schema->resultset('BindType')->find({ id => $new->id });
86       ok( ($row ? $row->get_column('bytea') : '') eq $new_big_long_string,
87         "Updated the row correctly (searching on the bytea column)."
88       );
89       $schema->txn_rollback;
90     });
91   }
92
93   # search on the bytea column (delete)
94   {
95     $schema->txn_do(sub {
96       $rs->delete;
97       my $row = $schema->resultset('BindType')->find({ id => $new->id });
98       is($row, undef, "Deleted the row correctly (searching on the bytea column).");
99       $schema->txn_rollback;
100     });
101   }
102
103   # create with blob from $rs
104   $new = $rs->create({});
105   ok($new->bytea eq $big_long_string, 'Object has bytea value from $rs');
106   $new->discard_changes;
107   ok($new->bytea eq $big_long_string, 'bytea value made it to db');
108 }
109
110 # test inserting a row via populate() (bindtype propagation through execute_for_fetch)
111 # use a new $dbh to ensure no leakage due to prepare_cached
112 {
113   my $cnt = 4;
114
115   $schema->storage->_dbh(undef);
116   my $rs = $schema->resultset('BindType');
117   $rs->delete;
118
119   $rs->populate([
120     [qw/id bytea/],
121     map { [
122       \[ '?', [ {} => $_ ] ],
123       "pop_${_}_" . $big_long_string,
124     ]} (1 .. $cnt)
125   ]);
126
127   is($rs->count, $cnt, 'All rows were correctly inserted');
128   for (1..$cnt) {
129     my $r = $rs->find({ bytea => "pop_${_}_" . $big_long_string });
130     is ($r->id, $_, "Row $_ found after find() on the blob");
131
132   }
133 }
134
135 done_testing;
136
137 eval { $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE bindtype_test") } ) };
138