source: trunk/t/30_search_raw.t @ 1023

Last change on this file since 1023 was 1023, checked in by kake, 15 years ago

Added format => "raw" option to OpenGuides::Search->run to let you get your results back as a hash.

File size: 3.2 KB
Line 
1use strict;
2use Wiki::Toolkit::Setup::SQLite;
3use OpenGuides::Search;
4use OpenGuides::Test;
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
13plan tests => 16;
14
15# Clear out the database from any previous runs.
16unlink "t/node.db";
17unlink <t/indexes/*>;
18
19Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } );
20my $config = OpenGuides::Test->make_basic_config;
21my $guide = OpenGuides->new( config => $config );
22my $search = OpenGuides::Search->new( config => $config );
23
24my %results;
25
26%results = $search->run( format => "raw" );
27is_deeply( \%results, { },
28           "raw search returns empty hash if no criteria supplied" );
29%results = $search->run( vars => { search => "banananana" }, format => "raw" );
30is_deeply( \%results, { },
31           "raw search returns empty hash if no hits on search string" );
32
33# Pop some data in and search again.
34OpenGuides::Test->write_data( guide => $guide,
35                              node  => "Red Lion",
36                              content => "A nice pub in Anyville.",
37                              summary => "Nice pub.",
38                              os_x => 500000,
39                              os_y => 150000,
40                            );
41OpenGuides::Test->write_data( guide => $guide,
42                              node  => "Blacksmiths Arms",
43                              content => "Not a very nice pub.",
44                              summary => "Rubbish pub.",
45                              os_x => 500100,
46                              os_y => 150000,
47                            );
48OpenGuides::Test->write_data( guide => $guide,
49                              node  => "Carpenters Arms",
50                              content => "Not a bad pub.",
51                              summary => "Average pub.",
52                              os_x => 450000,
53                              os_y => 140000,
54                            );
55
56%results = $search->run( vars => { search => "arms" }, format => "raw" );
57is_deeply( [ sort keys %results ], [ "Blacksmiths Arms", "Carpenters Arms" ],
58           "raw search on single word finds the right nodes" );
59my %ba = %{$results{"Blacksmiths Arms"}};
60is( $ba{name}, "Blacksmiths Arms", "result hash has correct name" );
61is( $ba{summary}, "Rubbish pub.", "...and correct summary" );
62ok( $ba{wgs84_long}, "...WGS-84 latitude returned" );
63ok( $ba{wgs84_lat}, "...WGS-84 longitude returned" );
64ok( $ba{score}, "...score returned" );
65ok( !$ba{distance}, "...no distance returned" );
66
67# Now try a distance search.
68%results = $search->run(
69    vars => { os_dist => 1000, os_x => 500200, os_y => 150000 },
70    format => "raw" );
71is_deeply( [ sort keys %results ], [ "Blacksmiths Arms", "Red Lion" ],
72           "raw distance search finds the right nodes" );
73my %rl = %{$results{"Red Lion"}};
74is( $rl{name}, "Red Lion", "result hash has correct name" );
75is( $rl{summary}, "Nice pub.", "...and correct summary" );
76ok( $rl{wgs84_lat}, "...WGS-84 latitude returned" );
77ok( $rl{wgs84_long}, "...WGS-84 longitude returned" );
78ok( !$rl{score}, "...no score returned" );
79is( $rl{distance}, 200, "...correct distance returned" );
Note: See TracBrowser for help on using the repository browser.