source: trunk/t/36_supersearch_order.t @ 552

Last change on this file since 552 was 552, checked in by Dominic Hargreaves, 17 years ago

Disable some tests unless plucene is enabled. Default to plucene on for new installs.

  • 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 CGI::Wiki::Setup::SQLite;
3use Config::Tiny;
4use OpenGuides::SuperSearch;
5use Test::More;
6
7eval { require DBD::SQLite; };
8if ( $@ ) {
9    plan skip_all => "DBD::SQLite not installed";
10    exit 0;
11}
12
13eval { require CGI::Wiki::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
25CGI::Wiki::Setup::SQLite::setup( { dbname => "t/node.db" } );
26my $config = Config::Tiny->new;
27$config->{_} = {
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
38my $search = OpenGuides::SuperSearch->new( config => $config );
39isa_ok( $search, "OpenGuides::SuperSearch" );
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 repository browser.