Only load DBICTest::Schema when needed in tests
[dbsrgits/DBIx-Class.git] / t / 72pg_bytea.t
CommitLineData
6e399b4f 1use strict;
68de9438 2use warnings;
6e399b4f 3
4use Test::More;
199fbc45 5use DBIx::Class::Optional::Dependencies ();
9aec3ec6 6use Try::Tiny;
6e399b4f 7use lib qw(t/lib);
8use DBICTest;
9
199fbc45 10plan 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
9fdf90df 13my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
6e399b4f 14
9fdf90df 15plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
16 unless ($dsn && $dbuser);
d7f20fdf 17
2c2bc4e5 18my $schema = DBICTest->connect_schema($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
9fdf90df 19
9aec3ec6 20if ($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}
26elsif (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
eda28767 31my $dbh = $schema->storage->dbh;
9fdf90df 32
bb452615 33{
34 local $SIG{__WARN__} = sub {};
a65c1894 35 $dbh->do('DROP TABLE IF EXISTS bindtype_test');
6ec7d1bb 36
37 # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
bb452615 38 $dbh->do(qq[
f3a9ea3d 39 CREATE TABLE bindtype_test
bb452615 40 (
6ec7d1bb 41 id serial NOT NULL PRIMARY KEY,
42 bytea bytea NULL,
43 blob bytea NULL,
f3a9ea3d 44 clob text NULL,
45 a_memo text NULL
bb452615 46 );
47 ],{ RaiseError => 1, PrintError => 1 });
48}
9fdf90df 49
0007aedf 50$schema->storage->debug(0); # these tests spew up way too much stuff, disable trace
51
d7f20fdf 52my $big_long_string = "\x00\x01\x02 abcd" x 125000;
6ffb5be5 53
54my $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");
10f6d2b7 60 ok($new->bytea eq $big_long_string, "Set the blob correctly.");
6ffb5be5 61}
62
b1e86b3e 63# test retrieval of the bytea column
64{
65 my $row = $schema->resultset('BindType')->find({ id => $new->id });
10f6d2b7 66 ok($row->get_column('bytea') eq $big_long_string, "Created the blob correctly.");
b1e86b3e 67}
9fdf90df 68
ba61fa2a 69{
6ffb5be5 70 my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
b1e86b3e 71
6ffb5be5 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 }
b1e86b3e 77
6ffb5be5 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 });
10f6d2b7 84 ok( ($row ? $row->get_column('bytea') : '') eq $new_big_long_string,
6ffb5be5 85 "Updated the row correctly (searching on the bytea column)."
86 );
87 $schema->txn_rollback;
88 });
89 }
b1e86b3e 90
6ffb5be5 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 }
a780a0ff 100
101 # create with blob from $rs
102 $new = $rs->create({});
10f6d2b7 103 ok($new->bytea eq $big_long_string, 'Object has bytea value from $rs');
a780a0ff 104 $new->discard_changes;
10f6d2b7 105 ok($new->bytea eq $big_long_string, 'bytea value made it to db');
6ffb5be5 106}
9fdf90df 107
a9bac98f 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
a780a0ff 133done_testing;
134
a9bac98f 135eval { $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE bindtype_test") } ) };
a780a0ff 136