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