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