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