TEST/TestCase.pm

filter: Statementstmt 86.6% 13 / 15 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 90.0% 18 / 20 CC 1 SCAR 0.6
CC
1
Cov
87%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Search/Base.pm ↑ Summary Search.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=2.6
9 use Data::Dumper;
10  
11 sub set_up
12 {
13 my $self = shift;
14 $self->init_db();
15 $self->init_test_object();
16 }
17  
18 sub tear_down
19 {
20 my $self = shift;
21 $self->{_test_obj}->{-dbh}->disconnect();
22 }
23  
24 sub init_db {
25 my $self = shift;
26 # create test database schema and insert data ...
27 $self->{-dbh} = DBI->connect('DBI:mysql:database=test;host=localhost', 'test', undef);
28 map { $self->{-dbh}->do($_); } $self->_db_schemas();
29  
30 $self->_insert_test_data();
31 }
32  
33 sub init_test_object {
34 my $self = shift;
35 $self->{_test_obj} = $self->{ws} =
36 CGI::Widget::DBI::Search->new(q => CGI->new, -dbh => $self->{-dbh});
37 }
38  
39 sub _db_schemas {
40 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 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 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 return @schemas;
62 }
63  
64 sub _insert_test_data {
65 my ($self) = @_;
66 my $sth1 = $self->{-dbh}->prepare_cached('insert into widgets (widget_no, name, description, size) values (?, ?, ?, ?)');
67 my $sth2 = $self->{-dbh}->prepare_cached('insert into tools (tool_no, name, type) values (?, ?, ?)');
68 my $sth3 = $self->{-dbh}->prepare_cached('insert into widget_tools (widget_no, tool_no) values (?, ?)');
69 $sth1->execute(1, 'clock_widget', "A time keeper widget", 'small');
70 $sth1->execute(2, 'calendar_widget', "A date tracker widget", 'medium');
71 $sth1->execute(3, 'silly_widget', "A goofball widget", 'unknown');
72 $sth1->execute(4, 'gps_widget', "A GPS widget", 'medium');
73 $sth2->execute(1, 'hammer', 'hand');
74 $sth2->execute(2, 'wrench', 'hand');
75 $sth2->execute(3, 'ls', 'unix');
76 $sth2->execute(4, 'rm', 'unix');
77 $sth2->execute(5, 'emacs', 'software');
78 $sth2->execute(6, 'apache', 'software');
79 $sth3->execute(1, 2);
80 $sth3->execute(1, 1);
81 $sth3->execute(2, 5);
82 $sth3->execute(2, 6);
83 $sth3->execute(3, 4);
84  
85 $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 $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 $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 sub assert_table_contents_equal {
119 my ($self, $table, $columns, $row_contents, $verbose) = @_;
120 die "no DBI handle set: set -dbh variable in your test object"
121 unless ref $self->{-dbh} && $self->{-dbh}->isa('DBI::db');
122 my $sth = $self->{-dbh}->prepare_cached("SELECT ".join(',', @$columns)." FROM $table");
123 $sth->execute();
124 my $table_contents = $sth->fetchall_arrayref();
125  
126 if ($verbose) {
127 print "==== contents of table in database ====\n" . (Dumper [$table_contents])
128 . "====\n";;
129 }
130  
131 local $Error::Depth = 1;
132 $self->assert_deep_equals(
133 $row_contents,
134 $table_contents,
135 );
136 }
137  
138 sub assert_display_contains {
139 my ($self, @rows) = @_;
140 my $ws = $self->{_test_obj};
141 local $Error::Depth = 1;
142 $ws->{_test_cached_output} ||= $ws->display_results;
143 $self->assert_matches($self->word_sequence_regex_for_rows(@rows), $ws->{_test_cached_output});
144 }
145  
146 sub assert_display_does_not_contain {
147 my ($self, @rows) = @_;
148 my $ws = $self->{_test_obj};
149 local $Error::Depth = 1;
150 $ws->{_test_cached_output} ||= $ws->display_results;
151 $self->assert_does_not_match($self->word_sequence_regex_for_rows(@rows), $ws->{_test_cached_output});
152 }
153  
154 sub word_sequence_regex_for_rows {
155 my ($self, @rows) = @_;
156 my $regex = join('.*', map {defined $_ && $_ ne '' ? '\b'.$_.'\b' : ()} map {@$_} @rows);
157 return qr|$regex|s;
158 }
159  
160  
161 1;
« Search/Base.pm ↑ Summary Search.pm »