Toolkit/TestLib.pm

filter: Statementstmt 42.9% 64 / 149 Branchbran 30.3% 17 / 56 Conditioncond 31.2% 10 / 32 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 92.8% 13 / 14 Podpod 100% 4 / 4 totaltotal 39.2% 108 / 275 CC 13 SCAR 37.2
CC
13
Cov
38%
new_wiki
96.0
configured_databases
3.0
_test_dsn
2.0
CRAP 41.5
« Toolkit/Feed/Atom.pm ↑ Summary Toolkit.pm »
1 package Wiki::Toolkit::TestLib;
2  
3 39 ▶use 5.006; #by perlver
Subroutine BEGIN: called CC=1 SCAR=0.0
4 39 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 39 ▶use Carp "croak";
Subroutine BEGIN: called CC=1 SCAR=0.0
6 39 ▶use Wiki::Toolkit;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 39 ▶use Wiki::Toolkit::TestConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 39 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 39 ▶use vars qw( $VERSION @wiki_info );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 39 $VERSION = '0.05';
12  
13 =head1 NAME
14  
15 Wiki::Toolkit::TestLib - Utilities for writing Wiki::Toolkit tests.
16  
17 =head1 DESCRIPTION
18  
19 When 'perl Makefile.PL' is run on a Wiki::Toolkit distribution,
20 information will be gathered about test databases etc that can be used
21 for running tests. Wiki::Toolkit::TestLib gives convenient access to this
22 information.
23  
24 =head1 SYNOPSIS
25  
26 use strict;
27 use Wiki::Toolkit::TestLib;
28 use Test::More;
29  
30 my $iterator = Wiki::Toolkit::TestLib->new_wiki_maker;
31 plan tests => ( $iterator->number * 6 );
32  
33 while ( my $wiki = $iterator->new_wiki ) {
34 # put some test data in
35 # run six tests
36 }
37  
38 Each time you call C<< ->next >> on your iterator, you will get a
39 fresh blank wiki object. The iterator will iterate over all configured
40 search and storage backends.
41  
42 The Lucy search backend will be configured to index three metadata fields:
43 address, category, and locale.
44  
45 =cut
46  
47 39 my %configured = %Wiki::Toolkit::TestConfig::config;
48  
49 39 my %datastore_info;
50  
51 39 foreach my $dbtype (qw( MySQL Pg SQLite )) {
52 117 ▶ if ( $configured{$dbtype}{dbname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($configured{$dbtype}{"dbname"}) 0 117 117
53 0 my %config = %{ $configured{$dbtype} };
54 0 my $store_class = "Wiki::Toolkit::Store::$dbtype";
55 0 my $setup_class = "Wiki::Toolkit::Setup::$dbtype";
56 0 eval "require $store_class";
57 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
58 0 warn "Couldn't require $store_class: $@\n";
59 0 warn "Will skip $dbtype tests.\n";
60 0 next;
61 }
62 0 my $dsn = $store_class->_dsn( @config{ qw( dbname dbhost dbport ) } );
63 0 my $err;
64 0 ▶ if ( $err = _test_dsn( $dsn, $config{dbuser}, $config{dbpass} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err = _test_dsn($dsn, $config{"dbuser"}, $config{"dbpass"})) 0 0 0
65 0 warn "connecting to test $dbtype database failed: $err\n";
66 0 warn "will skip $dbtype tests\n";
67 0 next;
68 }
69 $datastore_info{$dbtype} = {
70 class => $store_class,
71 setup_class => $setup_class,
72 params => {
73 dbname => $config{dbname},
74 dbuser => $config{dbuser},
75 dbpass => $config{dbpass},
76 dbhost => $config{dbhost},
77 },
78 0 dsn => $dsn
79 };
80 }
81 }
82  
83 39 my %dbixfts_info;
84 # DBIxFTS only works with MySQL.
85 39 ▶if ( $configured{dbixfts} && $configured{MySQL}{dbname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($configured{"dbixfts"} and $configured{"MySQL"}{"dbname"}) 0 39 39
Condition33%1/3
$configured{"dbixfts"} and $configured{"MySQL"}{"dbname"}
!ll&&!rl&&r
3900
MC/DC0%0/2
$configured{"dbixfts"} and $configured{"MySQL"}{"dbname"}
$configured{"dbixfts"} $configured{"MySQL"}{"dbname"}
Truth table33%1/3
A and B
A= $configured{"dbixfts"}B= $configured{"MySQL"}{"dbname"}
ABresult
0X0
100
111
86 0 my %config = %{ $configured{MySQL} };
87 $dbixfts_info{MySQL} = {
88 db_params => {
89 dbname => $config{dbname},
90 dbuser => $config{dbuser},
91 dbpass => $config{dbpass},
92 dbhost => $config{dbhost},
93 },
94 0 };
95 }
96  
97 39 my %sii_info;
98 # Test the MySQL SII backend, if we can.
99 39 ▶if ( $configured{search_invertedindex} && $configured{MySQL}{dbname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($configured{"search_invertedindex"} and $configured{"MySQL"}{"dbname"}) 0 39 39
Condition33%1/3
$configured{"search_invertedindex"} and $configured{"MySQL"}{"dbname"}
!ll&&!rl&&r
3900
MC/DC0%0/2
$configured{"search_invertedindex"} and $configured{"MySQL"}{"dbname"}
$configured{"search_invertedindex"} $configured{"MySQL"}{"dbname"}
Truth table33%1/3
A and B
A= $configured{"search_invertedindex"}B= $configured{"MySQL"}{"dbname"}
ABresult
0X0
100
111
100 0 my %config = %{ $configured{MySQL} };
101 $sii_info{MySQL} = {
102 db_class => "Search::InvertedIndex::DB::Mysql",
103 db_params => {
104 -db_name => $config{dbname},
105 -username => $config{dbuser},
106 -password => $config{dbpass},
107 0 ▶ -hostname => $config{dbhost} || "",
Condition0%0/2
$config{'dbhost'} || ''
l!l
00
MC/DC0%0/1
$config{'dbhost'} || ''
$config{'dbhost'}
Truth table0%0/2
A
A= $config{'dbhost'}
Aresult
11
00
108 -table_name => 'siindex',
109 -lock_mode => 'EX',
110 },
111 };
112 }
113  
114 # Test the Pg SII backend, if we can. It's not in the main S::II package.
115 39 eval { require Search::InvertedIndex::DB::Pg; };
116 39 ▶my $sii_pg = $@ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 39 0 39
117 39 ▶if ( $configured{search_invertedindex}
Branch: 1 branch
BranchTrueFalseTotal
if ($configured{"search_invertedindex"} and $configured{"Pg"}{"dbname"} and $sii_pg) 0 39 39
Condition33%2/6
$configured{"search_invertedindex"} and $configured{"Pg"}{"dbname"} and $sii_pg
!ll&&!rl&&r
3900
$configured{"search_invertedindex"} and $configured{"Pg"}{"dbname"}
!ll&&!rl&&r
3900
MC/DC0%0/3
$configured{"search_invertedindex"} and $configured{"Pg"}{"dbname"} and $sii_pg
$configured{"search_invertedindex"} $configured{"Pg"}{"dbname"} $sii_pg
Truth table25%1/4
A and B and C
A= $configured{"search_invertedindex"}B= $configured{"Pg"}{"dbname"}C= $sii_pg
ABCresult
0XX0
10X0
1100
1111
118 && $configured{Pg}{dbname}
119 && $sii_pg
120 ) {
121 0 my %config = %{ $configured{Pg} };
122 $sii_info{Pg} = {
123 db_class => "Search::InvertedIndex::DB::Pg",
124 db_params => {
125 -db_name => $config{dbname},
126 -username => $config{dbuser},
127 -password => $config{dbpass},
128 -hostname => $config{dbhost},
129 0 -table_name => 'siindex',
130 -lock_mode => 'EX',
131 },
132 };
133 }
134  
135 # Also test the default DB_File backend, if we have S::II installed at all.
136 39 ▶if ( $configured{search_invertedindex} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($configured{"search_invertedindex"}) 0 39 39
137 $sii_info{DB_File} = {
138 0 db_class => "Search::InvertedIndex::DB::DB_File_SplitHash",
139 db_params => {
140 -map_name => 't/sii-db-file-test.db',
141 -lock_mode => 'EX',
142 },
143 };
144 }
145  
146 39 my ( $plucene_path, $lucy_path );
147 # Test with Plucene and Lucy if possible.
148 39 ▶if ( $configured{plucene} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($configured{"plucene"}) 0 39 39
149 0 $plucene_path = "t/plucene";
150 }
151 39 ▶if ( $configured{lucy} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($configured{"lucy"}) 0 39 39
152 0 $lucy_path = "t/lucy";
153 }
154  
155 # @wiki_info describes which searches work with which stores.
156  
157 # Database-specific searchers.
158 push @wiki_info, { datastore_info => $datastore_info{MySQL},
159 dbixfts_info => $dbixfts_info{MySQL} }
160 39 ▶ if ( $datastore_info{MySQL} and $dbixfts_info{MySQL} );
Branch: 1 branch
BranchTrueFalseTotal
if $datastore_info{"MySQL"} and $dbixfts_info{"MySQL"} 0 39 39
Condition33%1/3
$datastore_info{"MySQL"} and $dbixfts_info{"MySQL"}
!ll&&!rl&&r
3900
MC/DC0%0/2
$datastore_info{"MySQL"} and $dbixfts_info{"MySQL"}
$datastore_info{"MySQL"} $dbixfts_info{"MySQL"}
Truth table33%1/3
A and B
A= $datastore_info{"MySQL"}B= $dbixfts_info{"MySQL"}
ABresult
0X0
100
111
161 push @wiki_info, { datastore_info => $datastore_info{MySQL},
162 sii_info => $sii_info{MySQL} }
163 39 ▶ if ( $datastore_info{MySQL} and $sii_info{MySQL} );
Branch: 1 branch
BranchTrueFalseTotal
if $datastore_info{"MySQL"} and $sii_info{"MySQL"} 0 39 39
Condition33%1/3
$datastore_info{"MySQL"} and $sii_info{"MySQL"}
!ll&&!rl&&r
3900
MC/DC0%0/2
$datastore_info{"MySQL"} and $sii_info{"MySQL"}
$datastore_info{"MySQL"} $sii_info{"MySQL"}
Truth table33%1/3
A and B
A= $datastore_info{"MySQL"}B= $sii_info{"MySQL"}
ABresult
0X0
100
111
164 push @wiki_info, { datastore_info => $datastore_info{Pg},
165 sii_info => $sii_info{Pg} }
166 39 ▶ if ( $datastore_info{Pg} and $sii_info{Pg} );
Branch: 1 branch
BranchTrueFalseTotal
if $datastore_info{"Pg"} and $sii_info{"Pg"} 0 39 39
Condition33%1/3
$datastore_info{"Pg"} and $sii_info{"Pg"}
!ll&&!rl&&r
3900
MC/DC0%0/2
$datastore_info{"Pg"} and $sii_info{"Pg"}
$datastore_info{"Pg"} $sii_info{"Pg"}
Truth table33%1/3
A and B
A= $datastore_info{"Pg"}B= $sii_info{"Pg"}
ABresult
0X0
100
111
167  
168 # All stores are compatible with the default S::II search, and with Plucene,
169 # and with Lucy, and with no search.
170 39 foreach my $dbtype ( qw( MySQL Pg SQLite ) ) {
171 push @wiki_info, { datastore_info => $datastore_info{$dbtype},
172 sii_info => $sii_info{DB_File} }
173 117 ▶ if ( $datastore_info{$dbtype} and $sii_info{DB_File} );
Branch: 1 branch
BranchTrueFalseTotal
if $datastore_info{$dbtype} and $sii_info{"DB_File"} 0 117 117
Condition33%1/3
$datastore_info{$dbtype} and $sii_info{"DB_File"}
!ll&&!rl&&r
11700
MC/DC0%0/2
$datastore_info{$dbtype} and $sii_info{"DB_File"}
$datastore_info{$dbtype} $sii_info{"DB_File"}
Truth table33%1/3
A and B
A= $datastore_info{$dbtype}B= $sii_info{"DB_File"}
ABresult
0X0
100
111
174 push @wiki_info, { datastore_info => $datastore_info{$dbtype},
175 plucene_path => $plucene_path }
176 117 ▶ if ( $datastore_info{$dbtype} and $plucene_path );
Branch: 1 branch
BranchTrueFalseTotal
if $datastore_info{$dbtype} and $plucene_path 0 117 117
Condition33%1/3
$datastore_info{$dbtype} and $plucene_path
!ll&&!rl&&r
11700
MC/DC0%0/2
$datastore_info{$dbtype} and $plucene_path
$datastore_info{$dbtype} $plucene_path
Truth table33%1/3
A and B
A= $datastore_info{$dbtype}B= $plucene_path
ABresult
0X0
100
111
177 push @wiki_info, { datastore_info => $datastore_info{$dbtype},
178 lucy_path => $lucy_path }
179 117 ▶ if ( $datastore_info{$dbtype} and $lucy_path );
Branch: 1 branch
BranchTrueFalseTotal
if $datastore_info{$dbtype} and $lucy_path 0 117 117
Condition33%1/3
$datastore_info{$dbtype} and $lucy_path
!ll&&!rl&&r
11700
MC/DC0%0/2
$datastore_info{$dbtype} and $lucy_path
$datastore_info{$dbtype} $lucy_path
Truth table33%1/3
A and B
A= $datastore_info{$dbtype}B= $lucy_path
ABresult
0X0
100
111
180 push @wiki_info, { datastore_info => $datastore_info{$dbtype} }
181 117 ▶ if $datastore_info{$dbtype};
Branch: 1 branch
BranchTrueFalseTotal
if $datastore_info{$dbtype} 0 117 117
182 }
183  
184 =head1 METHODS
185  
186 =over 4
187  
188 =item B<new_wiki_maker>
189  
190 my $iterator = Wiki::Toolkit::TestLib->new_wiki_maker;
191  
192 =cut
193  
194 2 ▶sub new_wiki_maker {
Subroutine new_wiki_maker: called CC=1 SCAR=0.0
Pod documented
195 2 my $class = shift;
196 2 my $count = 0;
197 2 my $iterator = \$count;
198 2 bless $iterator, $class;
199 2 return $iterator;
200 }
201  
202 =item B<number>
203  
204 use Test::More;
205 my $iterator = Wiki::Toolkit::TestLib->new_wiki_maker;
206 plan tests => ( $iterator->number * 6 );
207  
208 Returns the number of new wikis that your iterator will be able to give you.
209  
210 =cut
211  
212 1 ▶sub number {
Subroutine number: called CC=1 SCAR=0.0
Pod documented
213 1 return scalar @wiki_info;
214 }
215  
216 =item B<new_wiki>
217  
218 my $wiki = $iterator->new_wiki;
219  
220 Returns a fresh blank wiki object, or false if you've used up all the
221 configured search and storage backends.
222  
223 =cut
224  
225 1 ▶sub new_wiki {
Subroutine new_wiki: called CC=11 SCAR=45.6
Pod documented
226 1 my $self = shift;
227 1 ▶ return undef if $$self > $#wiki_info;
Branch: 1 branch
BranchTrueFalseTotal
if $$self > $#Wiki::Toolkit::TestLib::wiki_info 1 0 1
228 0 my $details = $wiki_info[$$self];
229 0 my %wiki_config;
230  
231 # Set up and clear datastore.
232 0 my %datastore_info = %{ $details->{datastore_info } };
233 0 my $setup_class = $datastore_info{setup_class};
234 0 eval "require $setup_class";
235 {
236 0 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.2
237 0 &{"$setup_class\:\:cleardb"}( $datastore_info{params} );
238 0 &{"$setup_class\:\:setup"}( $datastore_info{params} );
239 }
240 0 my $class = $datastore_info{class};
241 0 eval "require $class";
242 0 $wiki_config{store} = $class->new( %{ $datastore_info{params} } );
243  
244 # Set up and clear search object (if required).
245 0 ▶ if ( $details->{dbixfts_info} ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($details->{'dbixfts_info'}) { } 0 0 0
elsif ($details->{'sii_info'}) { } 0 0 0
elsif ($details->{'plucene_path'}) { } 0 0 0
elsif ($details->{'lucy_path'}) { } 0 0 0
246 0 my %fts_info = %{ $details->{dbixfts_info} };
247 0 require Wiki::Toolkit::Store::MySQL;
248 0 my %dbconfig = %{ $fts_info{db_params} };
249 my $dsn = Wiki::Toolkit::Store::MySQL->_dsn( $dbconfig{dbname},
250 0 $dbconfig{dbhost} );
251 my $dbh = DBI->connect( $dsn, $dbconfig{dbuser}, $dbconfig{dbpass},
252 0 ▶ { PrintError => 0, RaiseError => 1, AutoCommit => 1 } )
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect($dsn, $dbconfig{"dbuser"}, $dbconfig{"dbpass"}, {"PrintError", 0, "RaiseError", 1, "AutoCommit", 1}) or croak(qq[Can't connect to $dbconfig{"dbname"} using ${dsn}: ] . "DBI"->errstr) 0 0 0
253 or croak "Can't connect to $dbconfig{dbname} using $dsn: " . DBI->errstr;
254 0 require Wiki::Toolkit::Setup::DBIxFTSMySQL;
255 Wiki::Toolkit::Setup::DBIxFTSMySQL::setup(
256 0 @dbconfig{ qw( dbname dbuser dbpass dbhost ) }
257 );
258 0 require Wiki::Toolkit::Search::DBIxFTS;
259 0 $wiki_config{search} = Wiki::Toolkit::Search::DBIxFTS->new( dbh => $dbh );
260 } elsif ( $details->{sii_info} ) {
261 0 my %sii_info = %{ $details->{sii_info} };
262 0 my $db_class = $sii_info{db_class};
263 0 eval "use $db_class";
264 0 my %db_params = %{ $sii_info{db_params} };
265 0 my $indexdb = $db_class->new( %db_params );
266 0 require Wiki::Toolkit::Setup::SII;
267 0 Wiki::Toolkit::Setup::SII::setup( indexdb => $indexdb );
268 0 $wiki_config{search} = Wiki::Toolkit::Search::SII->new(indexdb =>$indexdb);
269 } elsif ( $details->{plucene_path} ) {
270 0 require Wiki::Toolkit::Search::Plucene;
271 0 my $dir = $details->{plucene_path};
272 0 ▶ unlink <$dir/*>; # don't die if false since there may be no files
Subroutine BEGIN: called CC=1 SCAR=0.2
273 0 ▶ if ( -d $dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 0 0 0
274 0 ▶ rmdir $dir or die $!;
Branch: 1 branch
BranchTrueFalseTotal
rmdir $dir or die $! 0 0 0
275 }
276 0 ▶ mkdir $dir or die $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or die $! 0 0 0
277 0 $wiki_config{search} = Wiki::Toolkit::Search::Plucene->new( path => $dir );
278 } elsif ( $details->{lucy_path} ) {
279 0 require Wiki::Toolkit::Search::Lucy;
280 0 require File::Path;
281 0 my $dir = $details->{lucy_path};
282 0 File::Path::rmtree( $dir, 0, 1 ); # 0 = verbose, 1 = safe
283 0 ▶ mkdir $dir or die $!;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or die $! 0 0 0
284 0 $wiki_config{search} = Wiki::Toolkit::Search::Lucy->new(
285 path => $dir,
286 metadata_fields => [ "address", "category", "locale" ] );
287 }
288  
289 # Make a wiki.
290 0 my $wiki = Wiki::Toolkit->new( %wiki_config );
291 0 $$self++;
292 0 return $wiki;
293 }
294  
295 =item B<configured_databases>
296  
297 my @configured_databases = $iterator->configured_databases;
298  
299 Returns the @configured_databases array detailing configured test databases.
300 Useful for very low-level testing only.
301  
302 =cut
303  
304 1 ▶sub configured_databases {
Subroutine configured_databases: called CC=3 SCAR=11.1
Pod documented
305 1 my @configured_databases;
306 1 foreach my $dbtype (qw( MySQL Pg SQLite )) {
307 push @configured_databases, $datastore_info{$dbtype}
308 3 ▶ if $datastore_info{$dbtype};
Branch: 1 branch
BranchTrueFalseTotal
if $datastore_info{$dbtype} 0 3 3
309 }
310 1 return @configured_databases;
311 }
312  
313 0 ▶sub _test_dsn {
Subroutine _test_dsn: not called CC=1 SCAR=6.9
314 0 my ( $dsn, $dbuser, $dbpass ) = @_;
315 0 my $dbh = eval {
316 0 DBI->connect($dsn, $dbuser, $dbpass, {RaiseError => 1});
317 };
318 0 return $@;
319 }
320  
321 =back
322  
323 =head1 SEE ALSO
324  
325 L<Wiki::Toolkit>
326  
327 =head1 AUTHOR
328  
329 Kake Pugh (kake@earth.li).
330  
331 =head1 COPYRIGHT
332  
333 Copyright (C) 2003-2004 Kake Pugh. All Rights Reserved.
334 Copyright (C) 2008 the Wiki::Toolkit team. All Rights Reserved.
335  
336 This module is free software; you can redistribute it and/or modify it
337 under the same terms as Perl itself.
338  
339 =head1 CAVEATS
340  
341 If you have the L<Search::InvertedIndex> backend configured (see
342 L<Wiki::Toolkit::Search::SII>) then your tests will raise warnings like
343  
344 (in cleanup) Search::InvertedIndex::DB::Mysql::lock() -
345 testdb is not open. Can't lock.
346 at /usr/local/share/perl/5.6.1/Search/InvertedIndex.pm line 1348
347  
348 or
349  
350 (in cleanup) Can't call method "sync" on an undefined value
351 at /usr/local/share/perl/5.6.1/Tie/DB_File/SplitHash.pm line 331
352 during global destruction.
353  
354 in unexpected places. I don't know whether this is a bug in me or in
355 L<Search::InvertedIndex>.
356  
357 =cut
358  
359 39 1;
« Toolkit/Feed/Atom.pm ↑ Summary Toolkit.pm »