File Coverage

File:t/pg-farm/sanity.t
Coverage:92.9%

linestmtbrancondsubpodtimecode
1
1
1
1
27
5
6
use strict;
2
1
1
1
9
3
8
use warnings;
3
4
1
1
1
19
5
9
use lib 'lib';
5
1
1
1
10
4
13
use OpenAPI::Config;
6
7my $reason;
8BEGIN {
9
1
8
    OpenAPI::Config->init('.');
10
1
19
    if ($OpenAPI::Config{'backend.type'} ne 'PgFarm') {
11
1
8
        $reason = 'backend.type in the config files is not PgFarm.';
12    }
13}
14
1
1
1
15
3
21
use Test::More $reason ? (skip_all => $reason) : 'no_plan';
15
16use OpenAPI::Backend::PgFarm;
17use Data::Dumper;
18use subs 'dump';
19
20my $backend = OpenAPI::Backend::PgFarm->new({ RaiseError => 0 });
21ok $backend, "database handle okay";
22if ($backend->has_user("agentz")) {
23    # $backend->do("drop table test cascade");
24    $backend->drop_user("agentz");
25}
26
27my $res = $backend->add_user("agentz", 'blahblahblah');
28cmp_ok $res, '>', -1, "user added okay";
29
30$backend->set_user("agentz");
31
32$res = $backend->has_user("agentz");
33ok $res, "user has registered!";
34
35$res = $backend->set_user("agentz");
36#ok $res, "user switched";
37
38$res = $backend->do("create table test (id serial, body text)");
39#ok $res, "table created";
40cmp_ok $res, '>', -1;
41
42$res = $backend->do("insert into test (body) values ('hello world')");
43#ok $res, "insert a record";
44is $res, '1', 'rows affected';
45
46$res = $backend->last_insert_id("test");
47ok $res, "get last insert id";
48is $res, 1, "last id okay";
49
50$Data::Dumper::Sortkeys = 1;
51$Data::Dumper::Indent = 0;
52$res = $backend->select('select * from test');
53is dump($res), "[['1','hello world']];";
54
55$res = $backend->select('select * from test', { use_hash => 1 });
56is dump($res), "[{'body' => 'hello world','id' => '1'}];";
57
58$res = $backend->do("insert into test (body) values ('hello world');\ninsert into test (body) values ('blah');");
59ok $res, "insert 2 records";
60is $res, '1', 'rows affected';
61
62$res = $backend->do("update test set body=body||'aaa';");
63ok $res, "insert 2 records";
64is $res, '3', 'rows affected';
65
66$res = $backend->select('select * from test');
67is dump($res), "[['1','hello worldaaa'],['2','hello worldaaa'],['3','blahaaa']];";
68
69$res = $backend->select('select * from test', {use_hash => 1});
70is dump($res), "[{'body' => 'hello worldaaa','id' => '1'},{'body' => 'hello worldaaa','id' => '2'},{'body' => 'blahaaa','id' => '3'}];";
71
72$res = $backend->do("insert into test (body) values (null);");
73ok $res;
74
75$res = $backend->select('select * from test', {use_hash => 1});
76is dump($res), "[{'body' => 'hello worldaaa','id' => '1'},{'body' => 'hello worldaaa','id' => '2'},{'body' => 'blahaaa','id' => '3'},{'body' => undef,'id' => '4'}];";
77
78$res = $backend->do("drop table test cascade");
79is $res+0, '0', "table dropped";
80
81sub dump {
82    my $var = shift;
83    my $s = Dumper($var);
84    $s =~ s/^\$VAR1\s*=\s*//;
85    $s
86}
87