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