| 1 | use strict; |
|---|
| 2 | use CGI::Wiki::Setup::SQLite; |
|---|
| 3 | use OpenGuides::Config; |
|---|
| 4 | use OpenGuides::SuperSearch; |
|---|
| 5 | use OpenGuides::Test; |
|---|
| 6 | use Test::More; |
|---|
| 7 | |
|---|
| 8 | eval { require DBD::SQLite; }; |
|---|
| 9 | if ( $@ ) { |
|---|
| 10 | plan skip_all => "DBD::SQLite not installed"; |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | eval { require Plucene; }; |
|---|
| 14 | if ( $@ ) { |
|---|
| 15 | plan skip_all => "Plucene not installed"; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | eval { require Geo::Coordinates::UTM; }; |
|---|
| 19 | if ( $@ ) { |
|---|
| 20 | plan skip_all => "Geo::Coordinates::UTM not installed"; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | plan tests => 4; |
|---|
| 24 | |
|---|
| 25 | # Clear out the database from any previous runs. |
|---|
| 26 | unlink "t/node.db"; |
|---|
| 27 | unlink <t/indexes/*>; |
|---|
| 28 | CGI::Wiki::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 29 | |
|---|
| 30 | my $config = OpenGuides::Config->new( |
|---|
| 31 | vars => { |
|---|
| 32 | dbtype => "sqlite", |
|---|
| 33 | dbname => "t/node.db", |
|---|
| 34 | indexing_directory => "t/indexes", |
|---|
| 35 | script_name => "wiki.cgi", |
|---|
| 36 | script_url => "http://example.com/", |
|---|
| 37 | site_name => "Test Site", |
|---|
| 38 | template_path => "./templates", |
|---|
| 39 | use_plucene => 1, |
|---|
| 40 | geo_handler => 3, |
|---|
| 41 | ellipsoid => "Airy", |
|---|
| 42 | } |
|---|
| 43 | ); |
|---|
| 44 | my $search = OpenGuides::SuperSearch->new( config => $config ); |
|---|
| 45 | my $guide = OpenGuides->new( config => $config ); |
|---|
| 46 | |
|---|
| 47 | # Write some data. |
|---|
| 48 | OpenGuides::Test->write_data( |
|---|
| 49 | guide => $guide, |
|---|
| 50 | node => "Crabtree Tavern", |
|---|
| 51 | latitude => 51.482385, |
|---|
| 52 | longitude => -0.221743, |
|---|
| 53 | categories => "Pubs", |
|---|
| 54 | ); |
|---|
| 55 | |
|---|
| 56 | OpenGuides::Test->write_data( |
|---|
| 57 | guide => $guide, |
|---|
| 58 | node => "Blue Anchor", |
|---|
| 59 | latitude => 51.489176, |
|---|
| 60 | longitude => -0.229488, |
|---|
| 61 | categories => "Pubs", |
|---|
| 62 | ); |
|---|
| 63 | |
|---|
| 64 | OpenGuides::Test->write_data( |
|---|
| 65 | guide => $guide, |
|---|
| 66 | node => "Star Tavern", |
|---|
| 67 | latitude => 51.498043, |
|---|
| 68 | longitude => -0.154247, |
|---|
| 69 | categories => "Pubs", |
|---|
| 70 | ); |
|---|
| 71 | |
|---|
| 72 | OpenGuides::Test->write_data( |
|---|
| 73 | guide => $guide, |
|---|
| 74 | node => "Hammersmith Bridge", |
|---|
| 75 | latitude => 51.488135, |
|---|
| 76 | longitude => -0.228463, |
|---|
| 77 | ); |
|---|
| 78 | |
|---|
| 79 | # Sanity check. |
|---|
| 80 | print "# Distances should be round about:\n"; |
|---|
| 81 | my $locator = $guide->locator; |
|---|
| 82 | foreach my $node ( "Blue Anchor", "Crabtree Tavern", "Hammersmith Bridge"){ |
|---|
| 83 | print "# $node: " . $locator->distance( from_x => 692756, |
|---|
| 84 | from_y => 5706917, |
|---|
| 85 | to_node => $node ) . "\n"; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | # Check that a lat/long distance search finds them. |
|---|
| 89 | my %tt_vars = $search->run( |
|---|
| 90 | return_tt_vars => 1, |
|---|
| 91 | vars => { |
|---|
| 92 | latitude => 51.484320, |
|---|
| 93 | longitude => -0.223484, |
|---|
| 94 | latlong_dist => 1000, |
|---|
| 95 | }, |
|---|
| 96 | ); |
|---|
| 97 | my @ordered = map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 98 | my @found = sort @ordered; |
|---|
| 99 | is_deeply( \@found, |
|---|
| 100 | [ "Blue Anchor", "Crabtree Tavern", "Hammersmith Bridge" ], |
|---|
| 101 | "distance search finds the right things" ); |
|---|
| 102 | is_deeply( \@ordered, |
|---|
| 103 | [ "Crabtree Tavern", "Hammersmith Bridge", "Blue Anchor" ], |
|---|
| 104 | "...and returns them in the right order" ); |
|---|
| 105 | |
|---|
| 106 | %tt_vars = $search->run( |
|---|
| 107 | return_tt_vars => 1, |
|---|
| 108 | vars => { |
|---|
| 109 | latitude => 51.484320, |
|---|
| 110 | longitude => -0.223484, |
|---|
| 111 | latlong_dist => 1000, |
|---|
| 112 | search => " ", |
|---|
| 113 | }, |
|---|
| 114 | ); |
|---|
| 115 | @ordered = map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 116 | @found = sort @ordered; |
|---|
| 117 | is_deeply( \@found, |
|---|
| 118 | [ "Blue Anchor", "Crabtree Tavern", "Hammersmith Bridge" ], |
|---|
| 119 | "...still works if whitespace-only search text supplied" ); |
|---|
| 120 | |
|---|
| 121 | %tt_vars = $search->run( |
|---|
| 122 | return_tt_vars => 1, |
|---|
| 123 | vars => { |
|---|
| 124 | latitude => 51.484320, |
|---|
| 125 | longitude => -0.223484, |
|---|
| 126 | latlong_dist => 1000, |
|---|
| 127 | search => "pubs", |
|---|
| 128 | }, |
|---|
| 129 | ); |
|---|
| 130 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 131 | is_deeply( \@found, [ "Blue Anchor", "Crabtree Tavern", ], |
|---|
| 132 | "distance search in combination with text search works" ); |
|---|