root/trunk/t/36_search_order.t

Revision 956, 5.4 kB (checked in by earle, 22 months ago)

Complete transition to using skip_all (remove old SKIP blocks).
More verbose reporting for error "require"ing DBD::SQLite.

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