| 1 | use strict; |
|---|
| 2 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 3 | use OpenGuides::Search; |
|---|
| 4 | use OpenGuides::Test; |
|---|
| 5 | use Test::More; |
|---|
| 6 | |
|---|
| 7 | eval { require DBD::SQLite; }; |
|---|
| 8 | if ( $@ ) { |
|---|
| 9 | my ($error) = $@ =~ /^(.*?)\n/; |
|---|
| 10 | plan skip_all => "DBD::SQLite could not be used - no database to test with. ($error)"; |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | plan tests => 16; |
|---|
| 14 | |
|---|
| 15 | # Clear out the database from any previous runs. |
|---|
| 16 | unlink "t/node.db"; |
|---|
| 17 | unlink <t/indexes/*>; |
|---|
| 18 | |
|---|
| 19 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 20 | my $config = OpenGuides::Test->make_basic_config; |
|---|
| 21 | my $guide = OpenGuides->new( config => $config ); |
|---|
| 22 | my $search = OpenGuides::Search->new( config => $config ); |
|---|
| 23 | |
|---|
| 24 | my %results; |
|---|
| 25 | |
|---|
| 26 | %results = $search->run( vars => { format => "raw" } ); |
|---|
| 27 | is_deeply( \%results, { }, |
|---|
| 28 | "raw search returns empty hash if no criteria supplied" ); |
|---|
| 29 | %results = $search->run( vars => { search => "banananana", format => "raw" } ); |
|---|
| 30 | is_deeply( \%results, { }, |
|---|
| 31 | "raw search returns empty hash if no hits on search string" ); |
|---|
| 32 | |
|---|
| 33 | # Pop some data in and search again. |
|---|
| 34 | OpenGuides::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 | ); |
|---|
| 41 | OpenGuides::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 | ); |
|---|
| 48 | OpenGuides::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" } ); |
|---|
| 57 | is_deeply( [ sort keys %results ], [ "Blacksmiths Arms", "Carpenters Arms" ], |
|---|
| 58 | "raw search on single word finds the right nodes" ); |
|---|
| 59 | my %ba = %{$results{"Blacksmiths Arms"}}; |
|---|
| 60 | is( $ba{name}, "Blacksmiths Arms", "result hash has correct name" ); |
|---|
| 61 | is( $ba{summary}, "Rubbish pub.", "...and correct summary" ); |
|---|
| 62 | ok( $ba{wgs84_long}, "...WGS-84 latitude returned" ); |
|---|
| 63 | ok( $ba{wgs84_lat}, "...WGS-84 longitude returned" ); |
|---|
| 64 | ok( $ba{score}, "...score returned" ); |
|---|
| 65 | ok( !$ba{distance}, "...no distance returned" ); |
|---|
| 66 | |
|---|
| 67 | # Now try a distance search. |
|---|
| 68 | %results = $search->run( |
|---|
| 69 | vars => { |
|---|
| 70 | os_dist => 1000, |
|---|
| 71 | os_x => 500200, |
|---|
| 72 | os_y => 150000, |
|---|
| 73 | format => "raw", |
|---|
| 74 | } ); |
|---|
| 75 | is_deeply( [ sort keys %results ], [ "Blacksmiths Arms", "Red Lion" ], |
|---|
| 76 | "raw distance search finds the right nodes" ); |
|---|
| 77 | my %rl = %{$results{"Red Lion"}}; |
|---|
| 78 | is( $rl{name}, "Red Lion", "result hash has correct name" ); |
|---|
| 79 | is( $rl{summary}, "Nice pub.", "...and correct summary" ); |
|---|
| 80 | ok( $rl{wgs84_lat}, "...WGS-84 latitude returned" ); |
|---|
| 81 | ok( $rl{wgs84_long}, "...WGS-84 longitude returned" ); |
|---|
| 82 | ok( !$rl{score}, "...no score returned" ); |
|---|
| 83 | is( $rl{distance}, 200, "...correct distance returned" ); |
|---|