Privatize _sth
[dbsrgits/DBIx-Class.git] / t / 72pg_bytea.t
CommitLineData
6e399b4f 1use strict;
68de9438 2use warnings;
6e399b4f 3
4use Test::More;
5use lib qw(t/lib);
6use DBICTest;
7
9fdf90df 8my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
6e399b4f 9
9fdf90df 10plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
11 unless ($dsn && $dbuser);
d7f20fdf 12
77d76d0f 13my $schema = DBICTest::Schema->connection($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
9fdf90df 14
eda28767 15my $dbh = $schema->storage->dbh;
9fdf90df 16
bb452615 17{
18 local $SIG{__WARN__} = sub {};
a65c1894 19 $dbh->do('DROP TABLE IF EXISTS bindtype_test');
6ec7d1bb 20
21 # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
bb452615 22 $dbh->do(qq[
f3a9ea3d 23 CREATE TABLE bindtype_test
bb452615 24 (
6ec7d1bb 25 id serial NOT NULL PRIMARY KEY,
26 bytea bytea NULL,
27 blob bytea NULL,
f3a9ea3d 28 clob text NULL,
29 a_memo text NULL
bb452615 30 );
31 ],{ RaiseError => 1, PrintError => 1 });
32}
9fdf90df 33
0007aedf 34$schema->storage->debug(0); # these tests spew up way too much stuff, disable trace
35
d7f20fdf 36my $big_long_string = "\x00\x01\x02 abcd" x 125000;
6ffb5be5 37
38my $new;
39# test inserting a row
40{
41 $new = $schema->resultset('BindType')->create({ bytea => $big_long_string });
42
43 ok($new->id, "Created a bytea row");
10f6d2b7 44 ok($new->bytea eq $big_long_string, "Set the blob correctly.");
6ffb5be5 45}
46
b1e86b3e 47# test retrieval of the bytea column
48{
49 my $row = $schema->resultset('BindType')->find({ id => $new->id });
10f6d2b7 50 ok($row->get_column('bytea') eq $big_long_string, "Created the blob correctly.");
b1e86b3e 51}
9fdf90df 52
ba61fa2a 53{
6ffb5be5 54 my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
b1e86b3e 55
6ffb5be5 56 # search on the bytea column (select)
57 {
58 my $row = $rs->first;
59 is($row ? $row->id : undef, $new->id, "Found the row searching on the bytea column.");
60 }
b1e86b3e 61
6ffb5be5 62 # search on the bytea column (update)
63 {
64 my $new_big_long_string = $big_long_string . "2";
65 $schema->txn_do(sub {
66 $rs->update({ bytea => $new_big_long_string });
67 my $row = $schema->resultset('BindType')->find({ id => $new->id });
10f6d2b7 68 ok( ($row ? $row->get_column('bytea') : '') eq $new_big_long_string,
6ffb5be5 69 "Updated the row correctly (searching on the bytea column)."
70 );
71 $schema->txn_rollback;
72 });
73 }
b1e86b3e 74
6ffb5be5 75 # search on the bytea column (delete)
76 {
77 $schema->txn_do(sub {
78 $rs->delete;
79 my $row = $schema->resultset('BindType')->find({ id => $new->id });
80 is($row, undef, "Deleted the row correctly (searching on the bytea column).");
81 $schema->txn_rollback;
82 });
83 }
a780a0ff 84
85 # create with blob from $rs
86 $new = $rs->create({});
10f6d2b7 87 ok($new->bytea eq $big_long_string, 'Object has bytea value from $rs');
a780a0ff 88 $new->discard_changes;
10f6d2b7 89 ok($new->bytea eq $big_long_string, 'bytea value made it to db');
6ffb5be5 90}
9fdf90df 91
a780a0ff 92done_testing;
93
94eval { $dbh->do("DROP TABLE bindtype_test") };
95