TEST/TestCase.pm

filter: Statementstmt 28.4% 23 / 81 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 50.0% 8 / 16 Podpod 25.0% 2 / 8 totaltotal 25.1% 33 / 131 CC 8 SCAR 19.2
CC
8
Cov
23%
assert_table_contents_equal
20.0
word_sequence_regex_for_rows
12.0
assert_display_contains
6.0
CRAP 6.8
« Search/Base.pm ↑ Summary Search/Display/TEST/Grid.pm »
1 package CGI::Widget::DBI::TEST::TestCase;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use base qw/ Test::Unit::TestCase /;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use CGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use CGI::Widget::DBI::Search;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub set_up
12 46 ▶{
Subroutine set_up: called CC=1 SCAR=0.4
Pod documented
13 46 my $self = shift;
14 46 $self->init_db();
15 0 $self->init_test_object();
16 }
17  
18 sub tear_down
19 0 ▶{
Subroutine tear_down: not called CC=1 SCAR=6.9
Pod documented
20 0 my $self = shift;
21 0 $self->{_test_obj}->{-dbh}->disconnect();
22 }
23  
24 46 ▶sub init_db {
Subroutine init_db: called CC=1 SCAR=2.0
Pod undocumented
25 46 my $self = shift;
26 # create test database schema and insert data ...
27 46 $self->{-dbh} = DBI->connect('DBI:mysql:database=test;host=localhost', 'test', undef);
28 0 map { $self->{-dbh}->do($_); } $self->_db_schemas();
29  
30 0 $self->_insert_test_data();
31 }
32  
33 0 ▶sub init_test_object {
Subroutine init_test_object: not called CC=1 SCAR=6.9
Pod undocumented
34 0 my $self = shift;
35 $self->{_test_obj} = $self->{ws} =
36 0 CGI::Widget::DBI::Search->new(q => CGI->new, -dbh => $self->{-dbh});
37 }
38  
39 0 ▶sub _db_schemas {
Subroutine _db_schemas: not called CC=1 SCAR=6.9
40 0 my @schemas = (<<'DDL1');
41 create temporary table widgets (
42 widget_no integer not null primary key auto_increment,
43 name varchar(32),
44 description text,
45 size varchar(16)
46 )
47 DDL1
48 0 push(@schemas, <<'DDL2');
49 create temporary table tools (
50 tool_no integer not null primary key auto_increment,
51 name varchar(32),
52 type varchar(16)
53 )
54 DDL2
55 0 push(@schemas, <<'DDL3');
56 create temporary table widget_tools (
57 widget_no integer not null,
58 tool_no integer not null
59 )
60 DDL3
61 0 return @schemas;
62 }
63  
64 0 ▶sub _insert_test_data {
Subroutine _insert_test_data: not called CC=1 SCAR=6.9
65 0 my ($self) = @_;
66 0 my $sth1 = $self->{-dbh}->prepare_cached('insert into widgets (widget_no, name, description, size) values (?, ?, ?, ?)');
67 0 my $sth2 = $self->{-dbh}->prepare_cached('insert into tools (tool_no, name, type) values (?, ?, ?)');
68 0 my $sth3 = $self->{-dbh}->prepare_cached('insert into widget_tools (widget_no, tool_no) values (?, ?)');
69 0 $sth1->execute(1, 'clock_widget', "A time keeper widget", 'small');
70 0 $sth1->execute(2, 'calendar_widget', "A date tracker widget", 'medium');
71 0 $sth1->execute(3, 'silly_widget', "A goofball widget", 'unknown');
72 0 $sth1->execute(4, 'gps_widget', "A GPS widget", 'medium');
73 0 $sth2->execute(1, 'hammer', 'hand');
74 0 $sth2->execute(2, 'wrench', 'hand');
75 0 $sth2->execute(3, 'ls', 'unix');
76 0 $sth2->execute(4, 'rm', 'unix');
77 0 $sth2->execute(5, 'emacs', 'software');
78 0 $sth2->execute(6, 'apache', 'software');
79 0 $sth3->execute(1, 2);
80 0 $sth3->execute(1, 1);
81 0 $sth3->execute(2, 5);
82 0 $sth3->execute(2, 6);
83 0 $sth3->execute(3, 4);
84  
85 0 $self->assert_table_contents_equal(
86 'widgets', [qw/widget_no name description size/],
87 [
88 [ 1, 'clock_widget', "A time keeper widget", 'small', ],
89 [ 2, 'calendar_widget', "A date tracker widget", 'medium', ],
90 [ 3, 'silly_widget', "A goofball widget", 'unknown', ],
91 [ 4, 'gps_widget', "A GPS widget", 'medium', ],
92 ],
93 );
94 0 $self->assert_table_contents_equal(
95 'tools', [qw/tool_no name type/],
96 [
97 [ 1, 'hammer', 'hand', ],
98 [ 2, 'wrench', 'hand', ],
99 [ 3, 'ls', 'unix', ],
100 [ 4, 'rm', 'unix', ],
101 [ 5, 'emacs', 'software', ],
102 [ 6, 'apache', 'software', ],
103 ],
104 );
105 0 $self->assert_table_contents_equal(
106 'widget_tools', [qw/widget_no tool_no/],
107 [
108 [ 1, 2, ],
109 [ 1, 1, ],
110 [ 2, 5, ],
111 [ 2, 6, ],
112 [ 3, 4, ],
113 ],
114 );
115 }
116  
117 # TODO: release as separate open source module, e.g. Test::Unit::MoreAsserts
118 0 ▶sub assert_table_contents_equal {
Subroutine assert_table_contents_equal: not called CC=4 SCAR=30.0
Pod undocumented
119 0 my ($self, $table, $columns, $row_contents, $verbose) = @_;
120 die "no DBI handle set: set -dbh variable in your test object"
121 0 ▶ unless ref $self->{-dbh} && $self->{-dbh}->isa('DBI::db');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"-dbh"} and $self->{"-dbh"}->isa("DBI::db") 0 0 0
Condition0%0/3
ref $self->{"-dbh"} and $self->{"-dbh"}->isa("DBI::db")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self->{"-dbh"} and $self->{"-dbh"}->isa("DBI::db")
ref $self->{"-dbh"} $self->{"-dbh"}->isa("DBI::db")
Truth table0%0/3
A and B
A= ref $self->{"-dbh"}B= $self->{"-dbh"}->isa("DBI::db")
ABresult
0X0
100
111
122 0 my $sth = $self->{-dbh}->prepare_cached("SELECT ".join(',', @$columns)." FROM $table");
123 0 $sth->execute();
124 0 my $table_contents = $sth->fetchall_arrayref();
125  
126 0 ▶ if ($verbose) {
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 0 0
127 0 print "==== contents of table in database ====\n" . (Dumper [$table_contents])
128 . "====\n";;
129 }
130  
131 0 local $Error::Depth = 1;
132 0 $self->assert_deep_equals(
133 $row_contents,
134 $table_contents,
135 );
136 }
137  
138 0 ▶sub assert_display_contains {
Subroutine assert_display_contains: not called CC=2 SCAR=17.9
Pod undocumented
139 0 my ($self, @rows) = @_;
140 0 my $ws = $self->{_test_obj};
141 0 local $Error::Depth = 1;
142 0 ▶ $ws->{_test_cached_output} ||= $ws->display_results;
Condition0%0/3
$ws->{"_test_cached_output"} ||= $ws->display_results
l!l&&r!l&&!r
000
MC/DC0%0/2
$ws->{"_test_cached_output"} ||= $ws->display_results
$ws->{"_test_cached_output"} $ws->display_results
Truth table0%0/3
A ||= B
A= $ws->{"_test_cached_output"}B= $ws->display_results
ABresult
1X1
011
000
143 0 $self->assert_matches($self->word_sequence_regex_for_rows(@rows), $ws->{_test_cached_output});
144 }
145  
146 0 ▶sub assert_display_does_not_contain {
Subroutine assert_display_does_not_contain: not called CC=2 SCAR=17.9
Pod undocumented
147 0 my ($self, @rows) = @_;
148 0 my $ws = $self->{_test_obj};
149 0 local $Error::Depth = 1;
150 0 ▶ $ws->{_test_cached_output} ||= $ws->display_results;
Condition0%0/3
$ws->{"_test_cached_output"} ||= $ws->display_results
l!l&&r!l&&!r
000
MC/DC0%0/2
$ws->{"_test_cached_output"} ||= $ws->display_results
$ws->{"_test_cached_output"} $ws->display_results
Truth table0%0/3
A ||= B
A= $ws->{"_test_cached_output"}B= $ws->display_results
ABresult
1X1
011
000
151 0 $self->assert_does_not_match($self->word_sequence_regex_for_rows(@rows), $ws->{_test_cached_output});
152 }
153  
154 0 ▶sub word_sequence_regex_for_rows {
Subroutine word_sequence_regex_for_rows: not called CC=3 SCAR=24.8
Pod undocumented
155 0 my ($self, @rows) = @_;
156 0 ▶ my $regex = join('.*', map {defined $_ && $_ ne '' ? '\b'.$_.'\b' : ()} map {@$_} @rows);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && $_ ne '' ? : 0 0 0
Condition0%0/3
defined $_ and $_ ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ and $_ ne ""
defined $_ $_ ne ""
Truth table0%0/3
A and B
A= defined $_B= $_ ne ""
ABresult
0X0
100
111
157 0 return qr|$regex|s;
158 }
159  
160  
161 3 1;
« Search/Base.pm ↑ Summary Search/Display/TEST/Grid.pm »