| 1 | 23 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 23 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | package KinoSearch1::Test::TestUtils; | ||||||||||
| 5 | 23 | ▶ | use base qw( Exporter ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 23 | our @EXPORT_OK = qw( | |||||||||
| 8 | working_dir | ||||||||||
| 9 | create_working_dir | ||||||||||
| 10 | remove_working_dir | ||||||||||
| 11 | create_index | ||||||||||
| 12 | create_persistent_test_index | ||||||||||
| 13 | test_index_loc | ||||||||||
| 14 | persistent_test_index_loc | ||||||||||
| 15 | init_test_index_loc | ||||||||||
| 16 | get_uscon_docs | ||||||||||
| 17 | utf8_test_strings | ||||||||||
| 18 | test_analyzer | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | 23 | ▶ | use KinoSearch1::InvIndexer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 23 | ▶ | use KinoSearch1::Store::RAMInvIndex; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | 23 | ▶ | use KinoSearch1::Store::FSInvIndex; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 23 | ▶ | use KinoSearch1::Analysis::Tokenizer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 23 | ▶ | use KinoSearch1::Analysis::TokenBatch; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 23 | ▶ | use KinoSearch1::Analysis::PolyAnalyzer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | |||||||||||
| 28 | 23 | ▶ | use File::Spec::Functions qw( catdir catfile curdir ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | 23 | ▶ | use Encode qw( _utf8_off ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 23 | ▶ | use File::Path qw( rmtree ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 31 | 23 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 23 | my $working_dir = catfile( curdir(), 'kinosearch_test' ); | |||||||||
| 34 | |||||||||||
| 35 | # Return a directory within the system's temp directory where we will put all | ||||||||||
| 36 | # testing scratch files. | ||||||||||
| 37 | 3 | ▶ | sub working_dir {$working_dir} | ||||||||
|
Subroutine
working_dir: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | sub create_working_dir { | ||||||||
|
Subroutine
create_working_dir: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||
| 40 | 1 | ▶ | mkdir( $working_dir, 0700 ) or die "Can't mkdir '$working_dir': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | # Verify that this user owns the working dir, then zap it. Returns true upon | ||||||||||
| 44 | # success. | ||||||||||
| 45 | 2 | ▶ | sub remove_working_dir { | ||||||||
|
Subroutine
remove_working_dir: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 46 | 2 | ▶ | return unless -d $working_dir; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 1 | rmtree $working_dir; | |||||||||
| 48 | 1 | return 1; | |||||||||
| 49 | } | ||||||||||
| 50 | |||||||||||
| 51 | # Return a location for a test index to be used by a single test file. If | ||||||||||
| 52 | # the test file crashes it cannot clean up after itself, so we put the cleanup | ||||||||||
| 53 | # routine in a single test file to be run at or near the end of the test | ||||||||||
| 54 | # suite. | ||||||||||
| 55 | 2 | ▶ | sub test_index_loc { | ||||||||
|
Subroutine
test_index_loc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 56 | 2 | return catdir( $working_dir, 'test_index' ); | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | # Return a location for a test index intended to be shared by multiple test | ||||||||||
| 60 | # files. It will be cleaned as above. | ||||||||||
| 61 | 5 | ▶ | sub persistent_test_index_loc { | ||||||||
|
Subroutine
persistent_test_index_loc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 62 | 5 | return catdir( $working_dir, 'persistent_test_index' ); | |||||||||
| 63 | } | ||||||||||
| 64 | |||||||||||
| 65 | # Destroy anything left over in the test_index location, then create the | ||||||||||
| 66 | # directory. Finally, return the path. | ||||||||||
| 67 | 1 | ▶ | sub init_test_index_loc { | ||||||||
|
Subroutine
init_test_index_loc: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||
| 68 | 1 | my $dir = test_index_loc(); | |||||||||
| 69 | 1 | rmtree $dir; | |||||||||
| 70 | 1 | ▶ | die "Can't clean up '$dir'" if -e $dir; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 71 | 1 | ▶ | mkdir $dir or die "Can't mkdir '$dir': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 1 | return $dir; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | # Build a RAM index, using the supplied array of strings as source material. | ||||||||||
| 76 | # The index will have a single field: "content". | ||||||||||
| 77 | 20 | ▶ | sub create_index { | ||||||||
|
Subroutine
create_index: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 78 | 20 | my @docs = @_; | |||||||||
| 79 | |||||||||||
| 80 | 20 | my $tokenizer = KinoSearch1::Analysis::Tokenizer->new; | |||||||||
| 81 | 20 | my $invindex = KinoSearch1::Store::RAMInvIndex->new; | |||||||||
| 82 | 20 | my $invindexer = KinoSearch1::InvIndexer->new( | |||||||||
| 83 | invindex => $invindex, | ||||||||||
| 84 | analyzer => $tokenizer, | ||||||||||
| 85 | create => 1, | ||||||||||
| 86 | ); | ||||||||||
| 87 | |||||||||||
| 88 | 20 | $invindexer->spec_field( name => 'content' ); | |||||||||
| 89 | |||||||||||
| 90 | 20 | for (@docs) { | |||||||||
| 91 | 2530 | my $doc = $invindexer->new_doc; | |||||||||
| 92 | 2530 | $doc->set_value( content => $_ ); | |||||||||
| 93 | 2530 | $invindexer->add_doc($doc); | |||||||||
| 94 | } | ||||||||||
| 95 | |||||||||||
| 96 | 20 | $invindexer->finish; | |||||||||
| 97 | |||||||||||
| 98 | 20 | return $invindex; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | # Slurp us constitition docs and build hashrefs. | ||||||||||
| 102 | 1 | ▶ | sub get_uscon_docs { | ||||||||
|
Subroutine
get_uscon_docs: called CC=8 SCAR=21.1
Pod
undocumented
| |||||||||||
| 103 | |||||||||||
| 104 | 1 | my $uscon_dir = catdir( 't', 'us_constitution' ); | |||||||||
| 105 | 1 | ▶ | opendir( my $uscon_dh, $uscon_dir ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 106 | or die "couldn't opendir '$uscon_dir': $!"; | ||||||||||
| 107 | 1 | my @filenames = grep {/\.html$/} sort readdir $uscon_dh; | |||||||||
| 108 | 1 | ▶ | closedir $uscon_dh or die "couldn't closedir '$uscon_dir': $!"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | |||||||||||
| 110 | 1 | my %docs; | |||||||||
| 111 | |||||||||||
| 112 | 1 | for my $filename (@filenames) { | |||||||||
| 113 | 53 | ▶ | next if $filename eq 'index.html'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 52 | my $filepath = catfile( $uscon_dir, $filename ); | |||||||||
| 115 | 52 | ▶ | open( my $fh, '<', $filepath ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | or die "couldn't open file '$filepath': $!"; | ||||||||||
| 117 | 52 | my $content = do { local $/; <$fh> }; | |||||||||
| 118 | 52 | ▶ | $content =~ m#<title>(.*?)</title>#s | ||||||||
|
Branch: 1 branch
| |||||||||||
| 119 | or die "couldn't isolate title in '$filepath'"; | ||||||||||
| 120 | 52 | my $title = $1; | |||||||||
| 121 | 52 | ▶ | $content =~ m#<div id="bodytext">(.*?)</div><!--bodytext-->#s | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | or die "couldn't isolate bodytext in '$filepath'"; | ||||||||||
| 123 | 52 | my $bodytext = $1; | |||||||||
| 124 | 52 | $bodytext =~ s/<.*?>//sg; | |||||||||
| 125 | 52 | $bodytext =~ s/\s+/ /sg; | |||||||||
| 126 | |||||||||||
| 127 | 52 | $docs{$filename} = { | |||||||||
| 128 | title => $title, | ||||||||||
| 129 | bodytext => $bodytext, | ||||||||||
| 130 | url => "/us_constitution/$filename", | ||||||||||
| 131 | }; | ||||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | 1 | return \%docs; | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 1 | ▶ | sub create_persistent_test_index { | ||||||||
|
Subroutine
create_persistent_test_index: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||
| 138 | 1 | my $invindexer; | |||||||||
| 139 | 1 | my $polyanalyzer | |||||||||
| 140 | = KinoSearch1::Analysis::PolyAnalyzer->new( language => 'en' ); | ||||||||||
| 141 | |||||||||||
| 142 | 1 | $invindexer = KinoSearch1::InvIndexer->new( | |||||||||
| 143 | invindex => persistent_test_index_loc(), | ||||||||||
| 144 | create => 1, | ||||||||||
| 145 | analyzer => $polyanalyzer, | ||||||||||
| 146 | ); | ||||||||||
| 147 | 1 | $invindexer->spec_field( name => 'content' ); | |||||||||
| 148 | 1 | for ( 0 .. 10000 ) { | |||||||||
| 149 | 10001 | my $doc = $invindexer->new_doc; | |||||||||
| 150 | 10001 | $doc->set_value( content => "zz$_" ); | |||||||||
| 151 | 10001 | $invindexer->add_doc($doc); | |||||||||
| 152 | } | ||||||||||
| 153 | 1 | $invindexer->finish; | |||||||||
| 154 | 1 | undef $invindexer; | |||||||||
| 155 | |||||||||||
| 156 | 1 | $invindexer = KinoSearch1::InvIndexer->new( | |||||||||
| 157 | invindex => persistent_test_index_loc(), | ||||||||||
| 158 | analyzer => $polyanalyzer, | ||||||||||
| 159 | ); | ||||||||||
| 160 | 1 | $invindexer->spec_field( name => 'content' ); | |||||||||
| 161 | 1 | my $source_docs = get_uscon_docs(); | |||||||||
| 162 | 1 | for ( values %$source_docs ) { | |||||||||
| 163 | 52 | my $doc = $invindexer->new_doc; | |||||||||
| 164 | 52 | $doc->set_value( content => $_->{bodytext} ); | |||||||||
| 165 | 52 | $invindexer->add_doc($doc); | |||||||||
| 166 | } | ||||||||||
| 167 | 1 | $invindexer->finish; | |||||||||
| 168 | 1 | undef $invindexer; | |||||||||
| 169 | |||||||||||
| 170 | 1 | $invindexer = KinoSearch1::InvIndexer->new( | |||||||||
| 171 | invindex => persistent_test_index_loc(), | ||||||||||
| 172 | analyzer => $polyanalyzer, | ||||||||||
| 173 | ); | ||||||||||
| 174 | 1 | $invindexer->spec_field( name => 'content' ); | |||||||||
| 175 | 1 | my @chars = ( 'a' .. 'z' ); | |||||||||
| 176 | 1 | for ( 0 .. 1000 ) { | |||||||||
| 177 | 1001 | my $content = ''; | |||||||||
| 178 | 1001 | for my $num_words ( 1 .. int( rand(20) ) ) { | |||||||||
| 179 | 9449 | for ( 1 .. ( int( rand(10) ) + 10 ) ) { | |||||||||
| 180 | 136917 | $content .= @chars[ rand(@chars) ]; | |||||||||
| 181 | } | ||||||||||
| 182 | 9449 | $content .= ' '; | |||||||||
| 183 | } | ||||||||||
| 184 | 1001 | my $doc = $invindexer->new_doc; | |||||||||
| 185 | 1001 | $doc->set_value( content => $content ); | |||||||||
| 186 | 1001 | $invindexer->add_doc($doc); | |||||||||
| 187 | } | ||||||||||
| 188 | 1 | $invindexer->finish( optimize => 1 ); | |||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | # Return 3 strings useful for verifying UTF-8 integrity. | ||||||||||
| 192 | 1 | ▶ | sub utf8_test_strings { | ||||||||
|
Subroutine
utf8_test_strings: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 193 | 1 | my $smiley = "\x{263a}"; | |||||||||
| 194 | 1 | my $not_a_smiley = $smiley; | |||||||||
| 195 | 1 | _utf8_off($not_a_smiley); | |||||||||
| 196 | 1 | my $frowny = $not_a_smiley; | |||||||||
| 197 | 1 | utf8::upgrade($frowny); | |||||||||
| 198 | 1 | return ( $smiley, $not_a_smiley, $frowny ); | |||||||||
| 199 | } | ||||||||||
| 200 | |||||||||||
| 201 | # Verify an Analyzer's analyze() method. | ||||||||||
| 202 | 5 | ▶ | sub test_analyzer { | ||||||||
|
Subroutine
test_analyzer: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 203 | 5 | my ( $analyzer, $source, $expected, $message ) = @_; | |||||||||
| 204 | |||||||||||
| 205 | 5 | my $batch = KinoSearch1::Analysis::TokenBatch->new; | |||||||||
| 206 | 5 | $batch->append( $source, 0, length($source) ); | |||||||||
| 207 | |||||||||||
| 208 | 5 | $batch = $analyzer->analyze($batch); | |||||||||
| 209 | 5 | my @got; | |||||||||
| 210 | 5 | ▶ | while ( $batch->next ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 211 | 14 | push @got, $batch->get_text; | |||||||||
| 212 | } | ||||||||||
| 213 | 5 | Test::More::is_deeply( \@got, $expected, "analyze: $message" ); | |||||||||
| 214 | } | ||||||||||
| 215 | |||||||||||
| 216 | 23 | 1; | |||||||||
| 217 | |||||||||||
| 218 | __END__ | ||||||||||