| 1 | use strict; |
|---|
| 2 | use Wiki::Toolkit::Plugin::Locator::Grid; # use directly to help debug |
|---|
| 3 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 4 | use OpenGuides::Config; |
|---|
| 5 | use OpenGuides::Search; |
|---|
| 6 | use OpenGuides::Test; |
|---|
| 7 | use Test::More; |
|---|
| 8 | |
|---|
| 9 | eval { require DBD::SQLite; }; |
|---|
| 10 | if ( $@ ) { |
|---|
| 11 | plan skip_all => "DBD::SQLite not installed"; |
|---|
| 12 | } |
|---|
| 13 | |
|---|
| 14 | eval { require Plucene; }; |
|---|
| 15 | if ( $@ ) { |
|---|
| 16 | plan skip_all => "Plucene not installed"; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | eval { require Geography::NationalGrid::GB; }; |
|---|
| 20 | if ( $@ ) { |
|---|
| 21 | plan skip_all => "Geography::NationalGrid::GB not installed"; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | plan tests => 8; |
|---|
| 25 | |
|---|
| 26 | # Clear out the database from any previous runs. |
|---|
| 27 | unlink "t/node.db"; |
|---|
| 28 | unlink <t/indexes/*>; |
|---|
| 29 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 30 | |
|---|
| 31 | my $config = OpenGuides::Config->new( |
|---|
| 32 | vars => { |
|---|
| 33 | dbtype => "sqlite", |
|---|
| 34 | dbname => "t/node.db", |
|---|
| 35 | indexing_directory => "t/indexes", |
|---|
| 36 | script_name => "wiki.cgi", |
|---|
| 37 | script_url => "http://example.com/", |
|---|
| 38 | site_name => "Test Site", |
|---|
| 39 | template_path => "./templates", |
|---|
| 40 | use_plucene => 1, |
|---|
| 41 | geo_handler => 1, |
|---|
| 42 | } |
|---|
| 43 | ); |
|---|
| 44 | my $search = OpenGuides::Search->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 | os_x => 523465, |
|---|
| 52 | os_y => 177490, |
|---|
| 53 | categories => "Pubs", |
|---|
| 54 | ); |
|---|
| 55 | |
|---|
| 56 | OpenGuides::Test->write_data( |
|---|
| 57 | guide => $guide, |
|---|
| 58 | node => "Blue Anchor", |
|---|
| 59 | os_x => 522909, |
|---|
| 60 | os_y => 178232, |
|---|
| 61 | categories => "Pubs", |
|---|
| 62 | ); |
|---|
| 63 | |
|---|
| 64 | OpenGuides::Test->write_data( |
|---|
| 65 | guide => $guide, |
|---|
| 66 | node => "Star Tavern", |
|---|
| 67 | os_x => 528107, |
|---|
| 68 | os_y => 179347, |
|---|
| 69 | categories => "Pubs", |
|---|
| 70 | ); |
|---|
| 71 | |
|---|
| 72 | OpenGuides::Test->write_data( |
|---|
| 73 | guide => $guide, |
|---|
| 74 | node => "Hammersmith Bridge", |
|---|
| 75 | os_x => 522983, |
|---|
| 76 | os_y => 178118, |
|---|
| 77 | ); |
|---|
| 78 | |
|---|
| 79 | # Sanity check. |
|---|
| 80 | print "# Distances should be:\n"; |
|---|
| 81 | my $locator = Wiki::Toolkit::Plugin::Locator::Grid->new(x => "os_x", y => "os_y"); |
|---|
| 82 | my $wiki = $guide->wiki; |
|---|
| 83 | $wiki->register_plugin( plugin => $locator ); |
|---|
| 84 | foreach my $node ( "Blue Anchor", "Crabtree Tavern", "Hammersmith Bridge"){ |
|---|
| 85 | print "# $node: " . $locator->distance( from_x => 523450, |
|---|
| 86 | from_y => 177650, |
|---|
| 87 | to_node => $node ) . "\n"; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | # Check that a lat/long distance search finds them. |
|---|
| 91 | my %tt_vars = $search->run( |
|---|
| 92 | return_tt_vars => 1, |
|---|
| 93 | vars => { |
|---|
| 94 | latitude => 51.484320, |
|---|
| 95 | longitude => -0.223484, |
|---|
| 96 | latlong_dist => 1000, |
|---|
| 97 | }, |
|---|
| 98 | ); |
|---|
| 99 | my @ordered = map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 100 | my @found = sort @ordered; |
|---|
| 101 | is_deeply( \@found, |
|---|
| 102 | [ "Blue Anchor", "Crabtree Tavern", "Hammersmith Bridge" ], |
|---|
| 103 | "distance search finds the right things" ); |
|---|
| 104 | is_deeply( \@ordered, |
|---|
| 105 | [ "Crabtree Tavern", "Hammersmith Bridge", "Blue Anchor" ], |
|---|
| 106 | "...and returns them in the right order" ); |
|---|
| 107 | |
|---|
| 108 | %tt_vars = $search->run( |
|---|
| 109 | return_tt_vars => 1, |
|---|
| 110 | vars => { |
|---|
| 111 | latitude => 51.484320, |
|---|
| 112 | longitude => -0.223484, |
|---|
| 113 | latlong_dist => 1000, |
|---|
| 114 | search => " ", |
|---|
| 115 | }, |
|---|
| 116 | ); |
|---|
| 117 | @ordered = map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 118 | @found = sort @ordered; |
|---|
| 119 | is_deeply( \@found, |
|---|
| 120 | [ "Blue Anchor", "Crabtree Tavern", "Hammersmith Bridge" ], |
|---|
| 121 | "...still works if whitespace-only search text supplied" ); |
|---|
| 122 | |
|---|
| 123 | %tt_vars = $search->run( |
|---|
| 124 | return_tt_vars => 1, |
|---|
| 125 | vars => { |
|---|
| 126 | os_x => 523450, |
|---|
| 127 | os_y => 177650, |
|---|
| 128 | os_dist => 1000, |
|---|
| 129 | search => " ", |
|---|
| 130 | }, |
|---|
| 131 | ); |
|---|
| 132 | @ordered = map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 133 | @found = sort @ordered; |
|---|
| 134 | is_deeply( \@found, |
|---|
| 135 | [ "Blue Anchor", "Crabtree Tavern", "Hammersmith Bridge" ], |
|---|
| 136 | "...works with OS co-ords" ); |
|---|
| 137 | |
|---|
| 138 | %tt_vars = eval { |
|---|
| 139 | $search->run( |
|---|
| 140 | return_tt_vars => 1, |
|---|
| 141 | vars => { |
|---|
| 142 | os_x => 523450, |
|---|
| 143 | os_y => 177650, |
|---|
| 144 | os_dist => 1000, |
|---|
| 145 | search => " ", |
|---|
| 146 | latitude => " ", |
|---|
| 147 | longitude => " ", |
|---|
| 148 | }, |
|---|
| 149 | ); |
|---|
| 150 | }; |
|---|
| 151 | is( $@, "", "...works with OS co-ords and whitespace-only lat/long" ); |
|---|
| 152 | @ordered = map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 153 | @found = sort @ordered; |
|---|
| 154 | is_deeply( \@found, |
|---|
| 155 | [ "Blue Anchor", "Crabtree Tavern", "Hammersmith Bridge" ], |
|---|
| 156 | "...returns the right stuff" ); |
|---|
| 157 | |
|---|
| 158 | %tt_vars = $search->run( |
|---|
| 159 | return_tt_vars => 1, |
|---|
| 160 | vars => { |
|---|
| 161 | latitude => 51.484320, |
|---|
| 162 | longitude => -0.223484, |
|---|
| 163 | latlong_dist => 1000, |
|---|
| 164 | search => "pubs", |
|---|
| 165 | }, |
|---|
| 166 | ); |
|---|
| 167 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 168 | is_deeply( \@found, [ "Blue Anchor", "Crabtree Tavern", ], |
|---|
| 169 | "distance search in combination with text search works" ); |
|---|
| 170 | |
|---|
| 171 | %tt_vars = $search->run( |
|---|
| 172 | return_tt_vars => 1, |
|---|
| 173 | vars => { |
|---|
| 174 | os_x => 523450, |
|---|
| 175 | os_y => 177650, |
|---|
| 176 | os_dist => 1000, |
|---|
| 177 | search => "pubs", |
|---|
| 178 | }, |
|---|
| 179 | ); |
|---|
| 180 | @found = sort map { $_->{name} } @{ $tt_vars{results} || [] }; |
|---|
| 181 | is_deeply( \@found, [ "Blue Anchor", "Crabtree Tavern", ], |
|---|
| 182 | "...works with OS co-ords too" ); |
|---|