blib/lib/CellBIS/SQL/Abstract/Test.pm

filter: Statementstmt 78.9% 105 / 133 Branchbran 28.2% 13 / 46 Conditioncond 14.2% 1 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 88.2% 15 / 17 Podpod 25.0% 2 / 8 totaltotal 63.2% 136 / 215 CC 27 SCAR 33.8
CC
27
Cov
64%
change_dbms
110.0
create
12.0
new
9.5
CRAP 29.4
↑ Summary lib/CellBIS/SQL/Abstract/Table.pm »
1 package CellBIS::SQL::Abstract::Test;
2 4 $CellBIS::SQL::Abstract::Test::VERSION = '1.5';
3 4 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Mojo::Loader 'load_class';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Mojo::Util qw(dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Mojo::Home;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use CellBIS::SQL::Abstract;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use CellBIS::SQL::Abstract::Test::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 has 'dsn';
13 4 has 'via';
14 4 has 'backend';
15 4 has 'table';
16  
17 # internal purpose
18 4 has 'abstract';
19 4 has 'dir';
20 1 ▶has home => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
21 1 state $home = Mojo::Home->new;
22 4 };
23 1 ▶has table_info => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
24 1 state $table = CellBIS::SQL::Abstract::Test::Table->new;
25 4 };
26  
27 3 ▶sub create_table {
Subroutine create_table: called CC=2 SCAR=6.9
Pod undocumented
28 3 my $self = shift;
29  
30 3 my $result = {result => 0, code => 400};
31 3 my $dbtype = $self->via;
32 3 my $table = $self->table;
33  
34 3 my @table_info = $self->table_info->$table->$dbtype;
35 3 my $q = $self->abstract->create_table(@table_info);
36 3 ▶ if (my $dbh = $self->backend->db->query($q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = $self->backend->db->query($q)) 3 0 3
37 3 $result->{result} = $dbh->rows;
38 3 $result->{code} = 200;
39 }
40 3 return $result;
41 }
42  
43 1 ▶sub create_table_with_fk {
Subroutine create_table_with_fk: called CC=2 SCAR=6.9
Pod undocumented
44 1 my $self = shift;
45  
46 1 my ($result, $dbtype, $table_users, $table_roles, @table_info, $table, $q);
47 1 $result = {result => 0, code => 400};
48 1 $dbtype = $self->via;
49 1 $table = $self->table;
50 1 $table_users = $self->table_info->users;
51 1 $table_roles = $self->table_info->roles;
52  
53 # table construction
54 1 @table_info = $self->table_info->$table->$dbtype;
55 1 push @table_info,
56 {
57 fk => {
58 name => 'users_roles_id',
59 col_name => $table_users->id_roles,
60 table_target => $table_roles->table_name,
61 col_target => $table_roles->id,
62 attr => {onupdate => 'cascade', ondelete => 'cascade'}
63 }
64 };
65 1 $q = $self->abstract->create_table(@table_info);
66 1 ▶ if (my $dbh = $self->backend->db->query($q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = $self->backend->db->query($q)) 1 0 1
67 1 $result->{result} = $dbh->rows;
68 1 $result->{code} = 200;
69 }
70 1 return $result;
71 }
72  
73 4 ▶sub check_table {
Subroutine check_table: called CC=5 SCAR=16.4
Pod undocumented
74 4 my $self = shift;
75  
76 4 my ($result, $dbtype, $table_info, $table, @pre_q, $q);
77 4 $result = {result => 0, code => 400};
78 4 $dbtype = $self->via;
79 4 $table = $self->table;
80 4 $table_info = $self->table_info->$table;
81  
82 4 ▶ @pre_q = (
Branch: 1 branch
BranchTrueFalseTotal
if $dbtype eq "sqlite" 4 0 4
83 'sqlite_master',
84 ['name'],
85 {
86 where => 'type=\'table\' AND tbl_name=\''
87 . $table_info->table_name . '\''
88 }
89 ) if $dbtype eq 'sqlite';
90 4 ▶ @pre_q = (
Branch: 1 branch
BranchTrueFalseTotal
if $dbtype eq "mariadb" 0 4 4
91 'information_schema.tables', ['table_name'],
92 {where => 'table_name=\'' . $table_info->table_name . '\''}
93 ) if $dbtype eq 'mariadb';
94 4 ▶ @pre_q = (
Branch: 1 branch
BranchTrueFalseTotal
if $dbtype eq "pg" 0 4 4
95 'information_schema.tables',
96 ['table_name'],
97 {
98 where =>
99 'table_type=\'BASE TABLE\' AND table_schema=\'public\' AND table_name=\''
100 . $table_info->table_name . '\''
101 }
102 ) if $dbtype eq 'pg';
103  
104 4 $q = $self->abstract->select(@pre_q);
105 4 ▶ if (my $dbh = $self->backend->db->query($q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = $self->backend->db->query($q)) 4 0 4
106 4 $result->{result} = $dbh->hash;
107 4 $result->{code} = 200;
108 }
109 4 return $result;
110 }
111  
112 2 ▶sub empty_table {
Subroutine empty_table: called CC=2 SCAR=6.9
Pod undocumented
113 2 my $self = shift;
114  
115 2 my ($result, $dbtype, $table, $table_info);
116 2 $dbtype = $self->via;
117 2 $table = $self->table;
118 2 $table_info = $self->table_info->$table;
119 2 $result = {result => 0, code => 500};
120  
121 2 ▶ if (my $dbh
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = $self->backend->db->query('DELETE FROM ' . $table_info->table_name)) 2 0 2
122 = $self->backend->db->query('DELETE FROM ' . $table_info->table_name))
123 {
124 2 $result->{result} = $dbh->rows;
125 2 $result->{code} = 200;
126 }
127 2 return $result;
128 }
129  
130 2 ▶sub drop_table {
Subroutine drop_table: called CC=2 SCAR=6.9
Pod undocumented
131 2 my $self = shift;
132  
133 2 my ($result, $dbtype, $table, $table_info);
134 2 $dbtype = $self->via;
135 2 $table = $self->table;
136 2 $table_info = $self->table_info->$table;
137 2 $result = {result => 0, code => 500};
138  
139 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = $self->backend->db->query('DROP TABLE IF EXISTS ' . $table_info->table_name)) 2 0 2
140 my $dbh = $self->backend->db->query(
141 'DROP TABLE IF EXISTS ' . $table_info->table_name
142 )
143 )
144 {
145 2 $result->{result} = $dbh->rows;
146 2 $result->{code} = 200;
147 }
148 2 return $result;
149 }
150  
151 0 ▶sub create {
Subroutine create: not called CC=3 SCAR=24.8
Pod undocumented
152 0 my $self = shift;
153  
154 0 my ($result, $dbtype, $table, $table_info);
155 0 $dbtype = $self->via;
156 0 $table = $self->table;
157 0 $table_info = $self->table_info->$table;
158  
159 # return if table is not exist.
160 0 $result = {result => 0, code => 500};
161 0 ▶ return $result unless $self->check_table->{result};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->check_table->{"result"} 0 0 0
162  
163 0 $result = {result => 0, code => 400};
164 0 my $q = $self->abstract->insert();
165 0 ▶ if (my $dbh = $self->backend->db->query($q)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $dbh = $self->backend->db->query($q)) 0 0 0
166 0 $result->{result} = $dbh->rows;
167 0 $result->{code} = 200;
168 }
169 0 return $result;
170 }
171  
172 0 ▶sub change_dbms {
Subroutine change_dbms: not called CC=10 SCAR=47.0
Pod documented
173 0 my ($self, $dbms) = @_;
174  
175 0 ▶ $dbms //= 'sqlite';
Condition0%0/2
$dbms //= "sqlite"
l!l
00
MC/DC0%0/1
$dbms //= "sqlite"
$dbms
Truth table0%0/2
A
A= $dbms
Aresult
11
00
176 0 $self->{via} = $dbms;
177 0 $self->abstract(CellBIS::SQL::Abstract->new(db_type => $self->via));
178 0 ▶ my $backend = 'Mojo::mysql' if $self->via eq 'mariadb';
Branch: 1 branch
BranchTrueFalseTotal
if $self->via eq "mariadb" 0 0 0
179 0 ▶ $backend = 'Mojo::Pg' if $self->via eq 'pg';
Branch: 1 branch
BranchTrueFalseTotal
if $self->via eq "pg" 0 0 0
180 0 ▶ $backend = 'Mojo::SQLite' if $self->via eq 'sqlite';
Branch: 1 branch
BranchTrueFalseTotal
if $self->via eq "sqlite" 0 0 0
181  
182 # dsn attribute alert
183 0 ▶ croak 'dsn attribute must be defined'
Branch: 1 branch
BranchTrueFalseTotal
if $self->via ne "sqlite" and $self->dsn =~ /^sqlite\:/u 0 0 0
Condition0%0/3
$self->via ne "sqlite" and $self->dsn =~ /^sqlite\:/u
!ll&&!rl&&r
000
MC/DC0%0/2
$self->via ne "sqlite" and $self->dsn =~ /^sqlite\:/u
$self->via ne "sqlite" $self->dsn =~ /^sqlite\:/u
Truth table0%0/3
A and B
A= $self->via ne "sqlite"B= $self->dsn =~ /^sqlite\:/u
ABresult
0X0
100
111
184 if ($self->via ne 'sqlite' && $self->dsn =~ /^sqlite\:/);
185  
186 # Only for SQLite
187 0 ▶ if ($self->via eq 'sqlite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->via eq "sqlite") 0 0 0
188 0 $self->dir($self->home->child(qw(t db)));
189 0 $self->dsn('sqlite:' . $self->dir . '/csa_test.db');
190 }
191  
192 0 my $load = load_class $backend;
193 0 ▶ croak ref $load ? $load : qq{Backend "$backend" missing} if $load;
Branch: 2 branches
BranchTrueFalseTotal
if $load 0 0 0
ref $load ? : 0 0 0
194 0 $self->backend($backend->new($self->dsn));
195  
196 0 return $self;
197 }
198  
199 1 ▶sub new {
Subroutine new: called CC=8 SCAR=22.5
Pod documented
200 1 my $self = shift->SUPER::new(@_);
201  
202 1 my ($backend);
203 1 ▶ $self->{via} //= 'sqlite';
Condition50%1/2
$self->{"via"} //= "sqlite"
l!l
01
MC/DC0%0/1
$self->{"via"} //= "sqlite"
$self->{"via"}
Truth table50%1/2
A
A= $self->{"via"}
Aresult
11
00
204  
205 1 $self->abstract(CellBIS::SQL::Abstract->new(db_type => $self->via));
206 1 ▶ $backend = 'Mojo::mysql' if $self->via eq 'mariadb';
Branch: 1 branch
BranchTrueFalseTotal
if $self->via eq "mariadb" 0 1 1
207 1 ▶ $backend = 'Mojo::Pg' if $self->via eq 'pg';
Branch: 1 branch
BranchTrueFalseTotal
if $self->via eq "pg" 0 1 1
208 1 ▶ $backend = 'Mojo::SQLite' if $self->via eq 'sqlite';
Branch: 1 branch
BranchTrueFalseTotal
if $self->via eq "sqlite" 1 0 1
209  
210 # Only for SQLite
211 1 ▶ if ($self->via eq 'sqlite') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->via eq "sqlite") 1 0 1
212 1 $self->dir($self->home->child(qw(t db)));
213 1 $self->dsn('sqlite:' . $self->dir . '/csa_test.db');
214 }
215  
216 # Load Class for backend database type
217 1 my $load = load_class $backend;
218 1 ▶ croak ref $load ? $load : qq{Backend "$backend" missing} if $load;
Branch: 2 branches
BranchTrueFalseTotal
if $load 0 1 1
ref $load ? : 0 0 0
219 1 $self->backend($backend->new($self->dsn));
220  
221 1 return $self;
222 }
223 4 1;
224  
225 =encoding utf8
226  
227 =head1 NAME
228  
229 CellBIS::SQL::Abstract::Test - A part of Unit Testing
230  
231 =head1 SYNOPSIS
232  
233 use CellBIS::SQL::Abstract::Test;
234
235 # Initialization for SQLite
236 my $test = CellBIS::SQL::Abstract::Test->new(table => 'users');
237 unless (-d $test->dir) { mkdir $test->dir }
238
239 $backend = $test->backend;
240 $db = $backend->db;
241 ok $db->ping, 'connected';
242
243 # Initialization for MariaDB
244 my $test = CellBIS::SQL::Abstract::Test->new(
245 table => 'users',
246 via => 'mariadb',
247 dsn => 'mariadb://myuser:mypass@localhost:3306/mydbtest'
248 );
249 $backend = $test->backend;
250 $db = $backend->db;
251 ok $db->ping, 'connected';
252
253 # Initialization for PostgreSQL
254 my $test = CellBIS::SQL::Abstract::Test->new(
255 table => 'users',
256 via => 'pg',
257 dsn => 'posgresql://myuser:mypass@localhost:5432/mydbtest'
258 );
259 $backend = $test->backend;
260 $db = $backend->db;
261 ok $db->ping, 'connected';
262  
263 =head1 DESCRIPTION
264  
265 This module is only a test instrument in SQLite, Mariadb, and PostgreSQL
266  
267 =head1 ATTRIBUTES
268  
269 L<CellBIS::SQL::Abstract::Test> implements the following attributes.
270  
271 =head2 table
272  
273 my $test = CellBIS::SQL::Abstract::Test->new(
274 ...
275 table => 'users',
276 ...
277 );
278
279 $test->table('users'); # to defined table
280 $test->table; # to use get table
281  
282 Information of table form L<CellBIS::SQL::Abstract::Test::Table>
283  
284 =head2 via and dsn
285  
286 # initialization for mariadb
287 my $test = CellBIS::SQL::Abstract::Test->new(
288 ...
289 via => 'mariadb',
290 dsn => 'mariadb://myuser:mypass@localhost:3306/mydbtest',
291 ...
292 );
293
294 # initialization for postgresql
295 my $test = CellBIS::SQL::Abstract::Test->new(
296 ...
297 via => 'pg',
298 dsn => 'posgresql://myuser:mypass@localhost:3306/mydbtest',
299 ...
300 );
301
302 # switch to mariadb
303 $test->dsn('mariadb://myuser:mypass@localhost:3306/mydbtest');
304
305 # switch to postgresql
306 $test->dsn('posgresql://myuser:mypass@localhost:5432/mydbtest');
307
308 C<dsn> attribute must be defined together with C<via> attribute when
309 initializing mariadb or postgresql. However, when initializing using
310 sqlite, you don't need to use the C<via> and C<dsn> (Data Source Name)
311 attributes.
312  
313 =head2 backend
314  
315 $test->backend;
316 $test->backend(Mojo::SQLite->new);
317 $test->backend(Mojo::mysq->new);
318 $test->backend(Mojo::Pg->new);
319  
320 C<backend> attribute only for initializing L<Mojo::SQLite>, L<Mojo::mysql>,
321 and L<Mojo::Pg>.
322  
323 =head1 METHODS
324  
325 L<CellBIS::SQL::Abstract::Test> implements the following new ones
326  
327 =head2 change_dbms
328  
329 This method for change dbms from one to the other. For example from
330 sqlite to mariadb or from mariadb to sqlite and vice versa.
331  
332 # switch to mariadb
333 $test->dsn('mariadb://myuser:mypass@localhost:3306/mydbtest');
334 $test->change_dbms('mariadb');
335
336 # switch to postgresql
337 $test->dsn('postgresql://myuser:mypass@localhost:5432/mydbtest');
338 $test->change_dbms('pg');
339
340 # switch back to sqlite
341 $test->change_dbms('sqlite');
342 unless (-d $test->dir) { mkdir $test->dir }
343  
344 =head2 methods for tables query
345  
346 The method here is to query tables, such as check, create, empty, and drop tables.
347  
348 $test->check_table;
349 $test->create_table;
350 $test->create_table_with_fk;
351 $test->empty_table;
352 $test->drop_table;
353
354 # if use key hashref 'result'
355 $test->check_table->{result};
356 $test->create_table->{result};
357 $test->create_table_with_fk->{result};
358 $test->empty_table->{result};
359 $test->drop_table->{result};
360
361 # if use key hashref 'code'
362 $test->check_table->{code};
363 $test->create_table->{code};
364 $test->create_table_with_fk->{code};
365 $test->empty_table->{code};
366 $test->drop_table->{code};
367  
368 The output of this method is a hashref and contains key C<result> and C<code>.
369  
370 =head1 AUTHOR
371  
372 Achmad Yusri Afandi, C<yusrideb@cpan.org>
373  
374 =head1 COPYRIGHT AND LICENSE
375  
376 Copyright (C) 2021 by Achmad Yusri Afandi
377  
378 This program is free software, you can redistribute it and/or modify
379 it under the terms of the Artistic License version 2.0.
380  
381 =cut
↑ Summary lib/CellBIS/SQL/Abstract/Table.pm »