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