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

# File Coverage

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

line | stmt | bran | cond | sub | pod | time | code
--- | --- | --- | --- | --- | --- | --- | ---
1 | 1 1 1 |  |  | 1 |  | 27 5 6 | use strict;
2 | 1 1 1 |  |  | 1 |  | 9 3 8 | use warnings;
3 |
4 | 1 1 1 |  |  | 1 |  | 19 5 9 | use lib 'lib';
5 | 1 1 1 |  |  | 1 |  | 10 4 13 | use OpenAPI::Config;
6 |
7 |  |  |  |  |  |  | my $reason;
8 |  |  |  |  |  |  | BEGIN {
9 | 1 |  |  | 1 |  | 8 | OpenAPI::Config->init('.');
10 | 1 | 50 |  |  |  | 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 | 50 |  | 1 |  | 15 3 21 | use Test::More $reason ? (skip_all => $reason) : 'no_plan';
15 |
16 |  |  |  |  |  |  | use OpenAPI::Backend::PgFarm;
17 |  |  |  |  |  |  | use Data::Dumper;
18 |  |  |  |  |  |  | use subs 'dump';
19 |
20 |  |  |  |  |  |  | my $backend = OpenAPI::Backend::PgFarm->new({ RaiseError => 0 });
21 |  |  |  |  |  |  | ok $backend, "database handle okay";
22 |  |  |  |  |  |  | if ($backend->has_user("agentz")) {
23 |  |  |  |  |  |  | # $backend->do("drop table test cascade");
24 |  |  |  |  |  |  | $backend->drop_user("agentz");
25 |  |  |  |  |  |  | }
26 |
27 |  |  |  |  |  |  | my $res = $backend->add_user("agentz", 'blahblahblah');
28 |  |  |  |  |  |  | cmp_ok $res, '>', -1, "user added okay";
29 |
30 |  |  |  |  |  |  | $backend->set_user("agentz");
31 |
32 |  |  |  |  |  |  | $res = $backend->has_user("agentz");
33 |  |  |  |  |  |  | ok $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";
40 |  |  |  |  |  |  | cmp_ok $res, '>', -1;
41 |
42 |  |  |  |  |  |  | $res = $backend->do("insert into test (body) values ('hello world')");
43 |  |  |  |  |  |  | #ok $res, "insert a record";
44 |  |  |  |  |  |  | is $res, '1', 'rows affected';
45 |
46 |  |  |  |  |  |  | $res = $backend->last_insert_id("test");
47 |  |  |  |  |  |  | ok $res, "get last insert id";
48 |  |  |  |  |  |  | is $res, 1, "last id okay";
49 |
50 |  |  |  |  |  |  | $Data::Dumper::Sortkeys = 1;
51 |  |  |  |  |  |  | $Data::Dumper::Indent = 0;
52 |  |  |  |  |  |  | $res = $backend->select('select * from test');
53 |  |  |  |  |  |  | is dump($res), "[['1','hello world']];";
54 |
55 |  |  |  |  |  |  | $res = $backend->select('select * from test', { use_hash => 1 });
56 |  |  |  |  |  |  | is 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');");
59 |  |  |  |  |  |  | ok $res, "insert 2 records";
60 |  |  |  |  |  |  | is $res, '1', 'rows affected';
61 |
62 |  |  |  |  |  |  | $res = $backend->do("update test set body=body||'aaa';");
63 |  |  |  |  |  |  | ok $res, "insert 2 records";
64 |  |  |  |  |  |  | is $res, '3', 'rows affected';
65 |
66 |  |  |  |  |  |  | $res = $backend->select('select * from test');
67 |  |  |  |  |  |  | is dump($res), "[['1','hello worldaaa'],['2','hello worldaaa'],['3','blahaaa']];";
68 |
69 |  |  |  |  |  |  | $res = $backend->select('select * from test', {use_hash => 1});
70 |  |  |  |  |  |  | is 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);");
73 |  |  |  |  |  |  | ok $res;
74 |
75 |  |  |  |  |  |  | $res = $backend->select('select * from test', {use_hash => 1});
76 |  |  |  |  |  |  | is 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");
79 |  |  |  |  |  |  | is $res+0, '0', "table dropped";
80 |
81 |  |  |  |  |  |  | sub dump {
82 |  |  |  |  |  |  | my $var = shift;
83 |  |  |  |  |  |  | my $s = Dumper($var);
84 |  |  |  |  |  |  | $s =~ s/^\$VAR1\s*=\s*//;
85 |  |  |  |  |  |  | $s
86 |  |  |  |  |  |  | }
87 |