blib/lib/Odoo/Database/Manager.pm

filter: Statementstmt 71.6% 53 / 74 Branchbran 10.0% 2 / 20 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 82.3% 14 / 17 Podpod 60.0% 3 / 5 totaltotal 61.3% 73 / 119 CC 12 SCAR 22.2
CC
12
Cov
58%
createdb
30.0
dropdb
12.0
_execute
9.3
CRAP 9.2
↑ Summary
1 package Odoo::Database::Manager;
2 2 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our $VERSION = '0.03';
7  
8 2 ▶use JSON::RPC2::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 2 ▶use failures qw/odoo::rpc::http odoo::rpc::http::connection odoo::rpc::response odoo::rpc::method odoo::nopassword/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 has url => (is => 'ro', isa => 'Str', default => 'http://localhost:8069');
20 2 has password => (is => 'ro', required => 0, isa => 'Str', default => 'admin');
21  
22 2 has ua => (is => 'ro', lazy => 1, builder => 'build_ua');
23 2 has json_rpc_client => (is => 'ro', lazy => 1, builder => 'build_json_rpc_client');
24  
25 1 ▶sub build_ua {
Subroutine build_ua: called CC=1 SCAR=0.0
Pod undocumented
26 1 return LWP::UserAgent->new();
27 }
28  
29 1 ▶sub build_json_rpc_client {
Subroutine build_json_rpc_client: called CC=1 SCAR=0.0
Pod undocumented
30 1 return JSON::RPC2::Client->new();
31 }
32  
33 1 ▶sub list_databases {
Subroutine list_databases: called CC=1 SCAR=0.4
Pod documented
34 1 my ($self) = @_;
35 1 my $dbs = $self->_execute('/web/database/get_list', 'call');
36 0 return @$dbs;
37 }
38  
39 0 ▶sub createdb {
Subroutine createdb: not called CC=5 SCAR=34.0
Pod documented
40 0 my ($self, %params) = @_;
41 0 ▶ my $dbname = $params{dbname} or die 'must specify dbname';
Branch: 1 branch
BranchTrueFalseTotal
my $dbname = $params{"dbname"} or die "must specify dbname" 0 0 0
42 0 ▶ my $lang = $params{lang} or die 'must specify lang';
Branch: 1 branch
BranchTrueFalseTotal
my $lang = $params{"lang"} or die "must specify lang" 0 0 0
43 0 ▶ my $admin_password = $params{admin_password} or die 'must specify admin_password';
Branch: 1 branch
BranchTrueFalseTotal
my $admin_password = $params{"admin_password"} or die "must specify admin_password" 0 0 0
44 0 ▶ failure::odoo::nopassword->throw('password not specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->password 0 0 0
45 unless defined $self->password;
46 # TODO support demo option
47 0 $self->_execute('/web/database/create', 'call', [$self->_cvt_params(
48 super_admin_pwd => $self->password,
49 db_name => $dbname,
50 db_lang => $lang,
51 create_admin_pwd => $admin_password,
52 create_confirm_pwd => $admin_password,
53 )]);
54 # {"jsonrpc":"2.0","method":"call","params":{"fields":[{"name":"super_admin_pwd","value":"admin"},{"name":"db_name","value":"another"},{"name":"db_lang","value":"en_GB"},{"name":"create_admin_pwd","value":"password"},{"name":"create_confirm_pwd","value":"password"}]},"id":776199543}
55 }
56  
57 0 ▶sub _cvt_params {
Subroutine _cvt_params: not called CC=1 SCAR=6.9
58 0 my ($self, %params) = @_;
59 0 return (fields => [ map { { name => $_, value => $params{$_} } } keys(%params) ]);
60 }
61  
62 0 ▶sub dropdb {
Subroutine dropdb: not called CC=3 SCAR=24.8
Pod documented
63 0 my ($self, $dbname) = @_;
64 0 ▶ die 'must specify $dbname' unless $dbname;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbname 0 0 0
65 0 ▶ failure::odoo::nopassword->throw('password not specified')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->password 0 0 0
66 unless defined $self->password;
67 0 $self->_execute('/web/database/drop', 'call', [
68 $self->_cvt_params( drop_db => $dbname, drop_pwd => $self->password ),
69 ]);
70 #{"jsonrpc":"2.0","method":"call","params":{"fields":[{"name":"drop_db","value":"another"},{"name":"drop_pwd","value":"wrongpass"}]},"id":692816518}
71 }
72  
73 1 ▶sub _execute {
Subroutine _execute: called CC=6 SCAR=22.3
74 1 my ($self, $urlpart, $method, @args) = @_;
75  
76 1 my $url = $self->url . $urlpart; # TODO Use a URI library
77 1 my $jsonreq = $self->json_rpc_client->call($method, @args);
78 1 my $json = decode_json($jsonreq);
79 1 ▶ $json->{params} //= {};
Condition50%1/2
$json->{"params"} //= {}
l!l
01
MC/DC0%0/1
$json->{"params"} //= {}
$json->{"params"}
Truth table50%1/2
A
A= $json->{"params"}
Aresult
11
00
80 1 $jsonreq = encode_json($json);
81 1 my $request = HTTP::Request->new(POST => $url);
82 1 $request->content_type('application/json');
83 1 $request->header(Accept => 'application/json, text/javascript, */*; q=0.01');
84 1 $request->content($jsonreq);
85  
86 1 my $res = $self->ua->request($request);
87  
88 1 ▶ unless ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 1 0 1
89 1 ▶ my $subclass = ($res->status_line =~ /^500 Can't connect to/) ? "::connection" : "";
Branch: 1 branch
BranchTrueFalseTotal
$res->status_line =~ /^500 Can't connect to/u ? : 1 0 1
90 1 "failure::odoo::rpc::http$subclass"->throw({
91 msg => $res->status_line,
92 payload => { result => $res }
93 });
94 }
95  
96 0 my ($failed, $result, $error) = $self->json_rpc_client->response($res->content);
97 0 ▶ failure::odoo::rpc::response->throw($failed)
Branch: 1 branch
BranchTrueFalseTotal
if $failed 0 0 0
98 if $failed;
99  
100 0 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 0 0
101 0 my $code = $error->{code};
102 0 my $message = $error->{message};
103 0 failure::odoo::rpc::method->throw({
104 msg => qq/method $method failed with code=$code: $message/,
105 payload => $error,
106 });
107 }
108 0 return $result;
109 }
110  
111 2 1;
112  
113 __END__
↑ Summary