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