source: trunk/t/36_search_order.t @ 878

Last change on this file since 878 was 785, checked in by Dominic Hargreaves, 16 years ago

Use Wiki::Toolkit. Note that CGI::Wiki::Plugin::Diff hasn't been converted yet,
so tests will be broken for now.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.4 KB
Line 
1use strict;
2use Wiki::Toolkit::Setup::SQLite;
3use OpenGuides::Config;
4use OpenGuides::Search;
5use Test::More;
6
7eval { require DBD::SQLite; };
8if ( $@ ) {
9    plan skip_all => "DBD::SQLite not installed";
10    exit 0;
11}
12
13eval { require Wiki::Toolkit::Search::Plucene; };
14if ( $@ ) {
15    plan skip_all => "Plucene not installed";
16    exit 0;
17}
18
19plan tests => 9;
20
21# Clear out the database from any previous runs.
22unlink "t/node.db";
23unlink <t/indexes/*>;
24
25Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } );
26my $config = OpenGuides::Config->new(
27       vars => {
28                 dbtype             => "sqlite",
29                 dbname             => "t/node.db",
30                 indexing_directory => "t/indexes",
31                 script_name        => "wiki.cgi",
32                 script_url         => "http://example.com/",
33                 site_name          => "Test Site",
34                 template_path      => "./templates",
35                 use_plucene        => 1
36               }
37);
38
39my $search = OpenGuides::Search->new( config => $config );
40isa_ok( $search, "OpenGuides::Search" );
41
42# Write some data.
43my $wiki = $search->{wiki};
44$wiki->write_node( "Parks", "A page about parks." )
45    or die "Can't write node";
46$wiki->write_node( "Wandsworth Common", "A common.", undef,
47                   { category => "Parks" } )
48    or die "Can't write node";
49$wiki->write_node( "Kake", "I like walking in parks." )
50    or die "Can't write node";
51
52my %tt_vars = $search->run(
53                            return_tt_vars => 1,
54                            vars           => { search => "parks" },
55                          );
56foreach my $result ( @{ $tt_vars{results} || [] } ) {
57    print "# $result->{name} scores $result->{score}\n";
58}
59my %scores = map { $_->{name} => $_->{score} } @{$tt_vars{results} || []};
60ok( $scores{Kake} < $scores{'Wandsworth Common'},
61    "content match scores less than category match" );
62ok( $scores{'Wandsworth Common'} < $scores{Parks},
63    "title match scores more than category match" );
64
65# Now test locales.
66$wiki->write_node( "Hammersmith", "A page about Hammersmith." )
67    or die "Can't write node";
68$wiki->write_node( "The Gate", "A restaurant.", undef,
69                   { locale => "Hammersmith" } )
70    or die "Can't write node";
71$wiki->write_node( "Kake Pugh", "I live in Hammersmith." )
72    or die "Can't write node";
73
74%tt_vars = $search->run(
75                         return_tt_vars => 1,
76                         vars           => { search => "hammersmith" },
77                       );
78foreach my $result ( @{ $tt_vars{results} || [] } ) {
79    print "# $result->{name} scores $result->{score}\n";
80}
81%scores = map { $_->{name} => $_->{score} } @{$tt_vars{results} || []};
82ok( $scores{'Kake Pugh'} < $scores{'The Gate'},
83    "content match scores less than locale match" );
84ok( $scores{'The Gate'} < $scores{Hammersmith},
85    "locale match scores less than title match" );
86
87# Check that two words in the title beats one in the title and
88# one in the content.
89$wiki->write_node( "Putney Tandoori", "Indian food" )
90  or die "Couldn't write node";
91$wiki->write_node( "Putney", "There is a tandoori restaurant here" )
92  or die "Couldn't write node";
93
94%tt_vars = $search->run(
95                         return_tt_vars => 1,
96                         vars           => { search => "putney tandoori" },
97                       );
98foreach my $result ( @{ $tt_vars{results} || [] } ) {
99    print "# $result->{name} scores $result->{score}\n";
100}
101%scores = map { $_->{name} => $_->{score} } @{$tt_vars{results} || []};
102ok( $scores{Putney} < $scores{'Putney Tandoori'},
103   "two words in title beats one in title and one in content" );
104
105SKIP: {
106   skip "Word proximity not yet taken into account", 1;
107# Check that in an AND match words closer together get higher priority.
108$wiki->write_node( "Spitalfields Market",
109                   "Mango juice from the Indian stall" )
110  or die "Can't write node";
111$wiki->write_node( "Borough Market", "dried mango and real apple juice" )
112  or die "Can't write node";
113
114%tt_vars = $search->run(
115                         return_tt_vars => 1,
116                         vars           => { search => "mango juice" },
117                       );
118foreach my $result ( @{ $tt_vars{results} || [] } ) {
119    print "# $result->{name} scores $result->{score}\n";
120}
121%scores = map { $_->{name} => $_->{score} } @{$tt_vars{results} || []};
122ok( $scores{'Borough Market'} < $scores{'Spitalfields Market'},
123    "words closer together gives higher score" );
124} # end of SKIP
125
126# Check that the number of occurrences of the search term is significant.
127
128$wiki->write_node( "Pub Crawls", "The basic premise of the pub crawl is to visit a succession of pubs, rather than spending the entire evening or day in a single establishment. London offers an excellent choice of themes for your pub crawl.", undef, { category => "Pubs" } ) or die "Can't write node";
129$wiki->write_node( "The Pub", "A pub.", undef, { category => "Pubs" } ) or die "Can't write node";
130
131%tt_vars = $search->run(
132                         return_tt_vars => 1,
133                         vars           => { search => "pub" }
134                       );
135foreach my $result ( @{ $tt_vars{results} || [] } ) {
136    print "# $result->{name} scores $result->{score}\n";
137}
138is( $tt_vars{results}[0]{name}, "Pub Crawls",
139    "node with two mentions of search term comes top" );
140ok( $tt_vars{results}[0]{score} > $tt_vars{results}[1]{score},
141    "...with a score strictly greater than node with one mention" );
Note: See TracBrowser for help on using the repository browser.