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