1use strict;
2use warnings;
3use xt::Utils::postgresql;
4use Test::More;
5
6use Teng;
7use Teng::Schema::Loader;
8
9eval q{
10    require DBD::Pg;
11    DBD::Pg->import(':pg_types');
12};
13if ( $@ ) {
14    plan skip_all => $@;
15}
16
17my $dbh = t::Utils::setup_dbh();
18
19$dbh->do(q{
20    CREATE TABLE foo (
21        id      serial,
22        bar     text,
23        baz     bytea,
24        PRIMARY KEY (id)
25    );
26});
27
28my $db = Teng::Schema::Loader->load(
29    dbh => $dbh,
30    namespace => 'Mock::DB',
31);
32
33isa_ok( $db, 'Teng' );
34
35my $binary = "\x21\x00\x21";
36
37# normal
38my $row = $db->insert('foo', { bar => 'あいうえお', baz => $binary } );
39
40is( $row->bar, 'あいうえお', 'text' );
41is( $row->baz, $binary, 'bytea' );
42
43$row = $db->single('foo', { id => 1 });
44
45is( $row->bar, 'あいうえお', 'selected text' );
46is( $row->baz, $binary, 'selected bytea' );
47
48# row object
49$db->suppress_row_objects(1);
50
51$row = $db->insert('foo', { bar => 'あいうえお', baz => $binary } );
52
53is( $row->{bar}, 'あいうえお', 'row object text' );
54is( $row->{baz}, $binary, 'row object bytea' );
55
56$row = $db->single('foo', { id => 2 });
57
58is( $row->{bar}, 'あいうえお', 'selected row object text' );
59is( $row->{baz}, $binary, 'selected row object bytea' );
60
61# update
62$db->suppress_row_objects(0);
63
64is( $db->update('foo', { baz => $binary . $binary  }, { id => 1 }), 1, 'update' );
65
66$row = $db->single('foo', { id => 1 });
67is( $row->baz, $binary . $binary, 'updated bytea' );
68
69# row update
70$row = $db->single('foo', { id => 2 });
71$row->update( { baz => $binary . $binary } );
72
73$row = $db->single('foo', { id => 2 });
74is( $row->baz, $binary . $binary, 'row updated bytea' );
75
76# explicitly type specified
77$row = $db->insert('foo', { baz => [ $binary, { pg_type => DBD::Pg::PG_BYTEA } ] } );
78is( $row->baz, $binary, 'explicitly type specified bytea' );
79$row = $db->single('foo', { id => 3 });
80is( $row->baz, $binary, 'selected explicitly type specified' );
81
82done_testing;
83
84
85