7049b319cf8e3c8895f22df497e7e6ce285ba1c1
[dbsrgits/DBIx-Class.git] / t / 72pg_bytea.t
1 use DBIx::Class::Optional::Dependencies -skip_all_without => qw(test_rdbms_pg binary_data);
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use DBIx::Class::_Util 'modver_gt_or_eq';
8
9 use lib qw(t/lib);
10 use DBICTest;
11
12 my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
13
14 my $schema = DBICTest::Schema->connect($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
15
16 plan 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 );
21
22 my $dbh = $schema->storage->dbh;
23
24 {
25     local $SIG{__WARN__} = sub {};
26     $dbh->do('DROP TABLE IF EXISTS bindtype_test');
27
28     # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
29     $dbh->do(qq[
30         CREATE TABLE bindtype_test
31         (
32             id              serial       NOT NULL   PRIMARY KEY,
33             bytea           bytea        NULL,
34             blob            bytea        NULL,
35             clob            text         NULL,
36             a_memo          text         NULL
37         );
38     ],{ RaiseError => 1, PrintError => 1 });
39 }
40
41 $schema->storage->debug(0); # these tests spew up way too much stuff, disable trace
42
43 my $big_long_string = "\x00\x01\x02 abcd" x 125000;
44
45 my $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");
51   ok($new->bytea eq $big_long_string, "Set the blob correctly.");
52 }
53
54 # test retrieval of the bytea column
55 {
56   my $row = $schema->resultset('BindType')->find({ id => $new->id });
57   ok($row->get_column('bytea') eq $big_long_string, "Created the blob correctly.");
58 }
59
60 {
61   my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
62
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   }
68
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 });
75       ok( ($row ? $row->get_column('bytea') : '') eq $new_big_long_string,
76         "Updated the row correctly (searching on the bytea column)."
77       );
78       $schema->txn_rollback;
79     });
80   }
81
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   }
91
92   # create with blob from $rs
93   $new = $rs->create({});
94   ok($new->bytea eq $big_long_string, 'Object has bytea value from $rs');
95   $new->discard_changes;
96   ok($new->bytea eq $big_long_string, 'bytea value made it to db');
97 }
98
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
124 done_testing;
125
126 eval { $schema->storage->dbh_do(sub { $_[1]->do("DROP TABLE bindtype_test") } ) };
127