Really fix @INC munging of the taint test
[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::Schema->connect($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             clob            text         NULL,
45             a_memo          text         NULL
46         );
47     ],{ RaiseError => 1, PrintError => 1 });
48 }
49
50 $schema->storage->debug(0); # these tests spew up way too much stuff, disable trace
51
52 my $big_long_string = "\x00\x01\x02 abcd" x 125000;
53
54 my $new;
55 # test inserting a row
56 {
57   $new = $schema->resultset('BindType')->create({ bytea => $big_long_string });
58
59   ok($new->id, "Created a bytea row");
60   ok($new->bytea eq $big_long_string, "Set the blob correctly.");
61 }
62
63 # test retrieval of the bytea column
64 {
65   my $row = $schema->resultset('BindType')->find({ id => $new->id });
66   ok($row->get_column('bytea') eq $big_long_string, "Created the blob correctly.");
67 }
68
69 {
70   my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
71
72   # search on the bytea column (select)
73   {
74     my $row = $rs->first;
75     is($row ? $row->id : undef, $new->id, "Found the row searching on the bytea column.");
76   }
77
78   # search on the bytea column (update)
79   {
80     my $new_big_long_string = $big_long_string . "2";
81     $schema->txn_do(sub {
82       $rs->update({ bytea => $new_big_long_string });
83       my $row = $schema->resultset('BindType')->find({ id => $new->id });
84       ok( ($row ? $row->get_column('bytea') : '') eq $new_big_long_string,
85         "Updated the row correctly (searching on the bytea column)."
86       );
87       $schema->txn_rollback;
88     });
89   }
90
91   # search on the bytea column (delete)
92   {
93     $schema->txn_do(sub {
94       $rs->delete;
95       my $row = $schema->resultset('BindType')->find({ id => $new->id });
96       is($row, undef, "Deleted the row correctly (searching on the bytea column).");
97       $schema->txn_rollback;
98     });
99   }
100
101   # create with blob from $rs
102   $new = $rs->create({});
103   ok($new->bytea eq $big_long_string, 'Object has bytea value from $rs');
104   $new->discard_changes;
105   ok($new->bytea eq $big_long_string, 'bytea value made it to db');
106 }
107
108 # test inserting a row via populate() (bindtype propagation through execute_for_fetch)
109 # use a new $dbh to ensure no leakage due to prepare_cached
110 {
111   my $cnt = 4;
112
113   $schema->storage->_dbh(undef);
114   my $rs = $schema->resultset('BindType');
115   $rs->delete;
116
117   $rs->populate([
118     [qw/id bytea/],
119     map { [
120       \[ '?', [ {} => $_ ] ],
121       "pop_${_}_" . $big_long_string,
122     ]} (1 .. $cnt)
123   ]);
124
125   is($rs->count, $cnt, 'All rows were correctly inserted');
126   for (1..$cnt) {
127     my $r = $rs->find({ bytea => "pop_${_}_" . $big_long_string });
128     is ($r->id, $_, "Row $_ found after find() on the blob");
129
130   }
131 }
132
133 done_testing;
134
135 eval { $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE bindtype_test") } ) };
136