Shuffle tests around (no changes)
[dbsrgits/DBIx-Class.git] / t / 72pg_bytea.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use lib qw(t/lib);
6 use DBICTest;
7
8 my ($dsn, $dbuser, $dbpass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
9
10 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
11   unless ($dsn && $dbuser);
12
13 my $schema = DBICTest::Schema->connection($dsn, $dbuser, $dbpass, { AutoCommit => 1 });
14
15 my $dbh = $schema->storage->dbh;
16
17 {
18     local $SIG{__WARN__} = sub {};
19     $dbh->do('DROP TABLE IF EXISTS bindtype_test');
20
21     # the blob/clob are for reference only, will be useful when we switch to SQLT and can test Oracle along the way
22     $dbh->do(qq[
23         CREATE TABLE bindtype_test
24         (
25             id              serial       NOT NULL   PRIMARY KEY,
26             bytea           bytea        NULL,
27             blob            bytea        NULL,
28             clob            text         NULL,
29             a_memo          text         NULL
30         );
31     ],{ RaiseError => 1, PrintError => 1 });
32 }
33
34 $schema->storage->debug(0); # these tests spew up way too much stuff, disable trace
35
36 my $big_long_string = "\x00\x01\x02 abcd" x 125000;
37
38 my $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");
44   ok($new->bytea eq $big_long_string, "Set the blob correctly.");
45 }
46
47 # test retrieval of the bytea column
48 {
49   my $row = $schema->resultset('BindType')->find({ id => $new->id });
50   ok($row->get_column('bytea') eq $big_long_string, "Created the blob correctly.");
51 }
52
53 {
54   my $rs = $schema->resultset('BindType')->search({ bytea => $big_long_string });
55
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   }
61
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 });
68       ok( ($row ? $row->get_column('bytea') : '') eq $new_big_long_string,
69         "Updated the row correctly (searching on the bytea column)."
70       );
71       $schema->txn_rollback;
72     });
73   }
74
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   }
84
85   # create with blob from $rs
86   $new = $rs->create({});
87   ok($new->bytea eq $big_long_string, 'Object has bytea value from $rs');
88   $new->discard_changes;
89   ok($new->bytea eq $big_long_string, 'bytea value made it to db');
90 }
91
92 done_testing;
93
94 eval { $dbh->do("DROP TABLE bindtype_test") };
95