Add import-time-skip support to OptDeps, switch most tests over to that
[dbsrgits/DBIx-Class.git] / t / 72pg_bytea.t
CommitLineData
cb551b07 1use DBIx::Class::Optional::Dependencies -skip_all_without => 'test_rdbms_pg';
2
6e399b4f 3use strict;
68de9438 4use warnings;
6e399b4f 5
6use Test::More;
9aec3ec6 7use Try::Tiny;
6e399b4f 8use lib qw(t/lib);
9use DBICTest;
10
9fdf90df 11my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
6e399b4f 12
6892eb09 13my $schema = DBICTest::Schema->connect($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
9fdf90df 14
9aec3ec6 15if ($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}
21elsif (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
eda28767 26my $dbh = $schema->storage->dbh;
9fdf90df 27
bb452615 28{
29 local $SIG{__WARN__} = sub {};
a65c1894 30 $dbh->do('DROP TABLE IF EXISTS bindtype_test');
6ec7d1bb 31
32 # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
bb452615 33 $dbh->do(qq[
f3a9ea3d 34 CREATE TABLE bindtype_test
bb452615 35 (
6ec7d1bb 36 id serial NOT NULL PRIMARY KEY,
37 bytea bytea NULL,
38 blob bytea NULL,
f3a9ea3d 39 clob text NULL,
40 a_memo text NULL
bb452615 41 );
42 ],{ RaiseError => 1, PrintError => 1 });
43}
9fdf90df 44
0007aedf 45$schema->storage->debug(0); # these tests spew up way too much stuff, disable trace
46
d7f20fdf 47my $big_long_string = "\x00\x01\x02 abcd" x 125000;
6ffb5be5 48
49my $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");
10f6d2b7 55 ok($new->bytea eq $big_long_string, "Set the blob correctly.");
6ffb5be5 56}
57
b1e86b3e 58# test retrieval of the bytea column
59{
60 my $row = $schema->resultset('BindType')->find({ id => $new->id });
10f6d2b7 61 ok($row->get_column('bytea') eq $big_long_string, "Created the blob correctly.");
b1e86b3e 62}
9fdf90df 63
ba61fa2a 64{
6ffb5be5 65 my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
b1e86b3e 66
6ffb5be5 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 }
b1e86b3e 72
6ffb5be5 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 });
10f6d2b7 79 ok( ($row ? $row->get_column('bytea') : '') eq $new_big_long_string,
6ffb5be5 80 "Updated the row correctly (searching on the bytea column)."
81 );
82 $schema->txn_rollback;
83 });
84 }
b1e86b3e 85
6ffb5be5 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 }
a780a0ff 95
96 # create with blob from $rs
97 $new = $rs->create({});
10f6d2b7 98 ok($new->bytea eq $big_long_string, 'Object has bytea value from $rs');
a780a0ff 99 $new->discard_changes;
10f6d2b7 100 ok($new->bytea eq $big_long_string, 'bytea value made it to db');
6ffb5be5 101}
9fdf90df 102
a9bac98f 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
a780a0ff 128done_testing;
129
a9bac98f 130eval { $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE bindtype_test") } ) };
a780a0ff 131