File: geocode.t

package info (click to toggle)
libgeo-coder-googlev3-perl 0.17-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 136 kB
  • sloc: perl: 126; makefile: 4
file content (261 lines) | stat: -rw-r--r-- 10,119 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# -*- coding:iso-8859-1; -*-

use strict;
use FindBin;
use lib "$FindBin::RealBin";

use Test::More 'no_plan';

sub within ($$$$$$;$);
sub safe_geocode (&;$);

use_ok 'Geo::Coder::Googlev3';

my $geocoder = Geo::Coder::Googlev3->new;
isa_ok $geocoder, 'Geo::Coder::Googlev3';

SKIP: {

last SKIP if $ENV{NO_NETWORK};

{ # key
    my $geocoder = Geo::Coder::Googlev3->new(key => "INVALID_KEY");
    my %info;
    safe_geocode { $geocoder->geocode(location => 'Berlin') } \%info;
    like $info{err}, qr{REQUEST_DENIED}, 'invalid api key';
}

{ # list context
    ## There are eight hits in Berlin. Google uses to know seven of them.
    ## But beginning from approx. 2010-05, only one location is returned.
    #my @locations = $geocoder->geocode(location => 'Berliner Strae, Berlin, Germany');
    #cmp_ok scalar(@locations), ">=", 1, "One or more results found";
    #like $locations[0]->{formatted_address}, qr{Berliner Strae}, 'First result looks OK';

    my @locations = safe_geocode { $geocoder->geocode(location => 'Waterloo, UK') };
    # Since approx. 2011-12 there's only one result, previously it was more
    cmp_ok scalar(@locations), ">=", 1, "One or more results found";
    like $locations[0]->{formatted_address}, qr{Waterloo}, 'First result looks OK';
}

{
    my $location = safe_geocode { $geocoder->geocode(location => 'Brandenburger Tor, Berlin, Germany') };
    # Since approx. 2011-12 "brandenburg gate" instead of "brandenburger tor" is returned
    # Since approx. 2017-01 "pariser platz" is returned
    like $location->{formatted_address}, qr{(brandenburger tor.*berlin|brandenburg gate|pariser platz.*berlin.*germany)}i;
    my($lat, $lng) = @{$location->{geometry}->{location}}{qw(lat lng)};
    within $lat, $lng, 52.5, 52.6, 13.3, 13.4;
}

{
    # ... but if language=>"de" is forced, then the German name is returned
    my $geocoder_de = Geo::Coder::Googlev3->new(language => 'de');
    my $location = safe_geocode { $geocoder_de->geocode(location => 'Brandenburger Tor, Berlin, Germany') };
    # Since approx. 2017-01 "pariser platz" is returned
    like $location->{formatted_address}, qr{(brandenburger tor.*berlin|pariser platz.*berlin.*deutschland)}i;
    my($lat, $lng) = @{$location->{geometry}->{location}}{qw(lat lng)};
    within $lat, $lng, 52.5, 52.6, 13.3, 13.4;
}

# Since approx. 2014-10 "Oeschelbronner Path" instead of "schelbronner Weg" is returned (!)
{ # encoding checks - bytes
    my $location = safe_geocode { $geocoder->geocode(location => 'schelbronner Weg, Berlin, Germany') };
    like $location->{formatted_address}, qr{schelbronner (weg|path).*berlin}i;
    my($lat, $lng) = @{$location->{geometry}->{location}}{qw(lat lng)};
    within $lat, $lng, 52.6, 52.7, 13.3, 13.4;
}

{ # encoding checks - utf8
    my $street = 'schelbronner Weg';
    utf8::upgrade($street);
    my $location = safe_geocode { $geocoder->geocode(location => "$street, Berlin, Germany") };
    like $location->{formatted_address}, qr{schelbronner (weg|path).*berlin}i;
    my($lat, $lng) = @{$location->{geometry}->{location}}{qw(lat lng)};
    within $lat, $lng, 52.6, 52.7, 13.3, 13.4;
}

{ # encoding checks - more utf8
    my $street = "Trg bana Josipa Jela\x{10d}i\x{107}a";
    my $alternative = "Ban Jela\x{10d}i\x{107} Square"; # outcome as of 2011-02-02
    my $alternative2 = 'City of Zagreb, Croatia'; # happened once in February 2011, see http://www.cpantesters.org/cpan/report/447c31b8-6cb5-1014-b648-c13506c0976e
    my $location = safe_geocode { $geocoder->geocode(location => "$street, Zagreb, Croatia") };
    like $location->{formatted_address}, qr{($street|$alternative|$alternative2)}i;
    my($lat, $lng) = @{$location->{geometry}->{location}}{qw(lat lng)};
    within $lat, $lng, 45.8, 45.9, 15.9, 16.0;
}

{
    my $postal_code = 'E1A 7G1';
    my $location = safe_geocode { $geocoder->geocode(location => "$postal_code, Canada") };
    my $postal_code_component;
    for my $address_component (@{ $location->{address_components} }) {
	if (grep { $_ eq 'postal_code' } @{ $address_component->{types} }) {
	    $postal_code_component = $address_component;
	    last;
	}
    }
    is $postal_code_component->{long_name}, $postal_code;
}

{ # region
    my $geocoder_es = Geo::Coder::Googlev3->new(gl => 'es', language => 'de');
    is $geocoder_es->language, 'de', 'language accessor';
    is $geocoder_es->region, 'es', 'region accessor';
    my $location_es = safe_geocode { $geocoder_es->geocode(location => 'Toledo') };
    within $location_es->{geometry}->{location}->{lat}, $location_es->{geometry}->{location}->{lng},
	39.852434, 39.881947, -4.04314, -4.012585;
    my $geocoder_us = Geo::Coder::Googlev3->new();
    my $location_us = safe_geocode { $geocoder_us->geocode(location => 'Toledo') };
    within $location_us->{geometry}->{location}->{lat}, $location_us->{geometry}->{location}->{lng},
	41.652805, 41.663939, -83.55522, -83.537867;
}

{ # bounds
    my $location_chicago = safe_geocode { $geocoder->geocode(location => 'Winnetka') };
    within $location_chicago->{geometry}->{location}->{lat}, $location_chicago->{geometry}->{location}->{lng},
	42.1080830, 42.1080840, -87.735900, -87.735890, 'Winnetka without bounds';

    my $bounds = [{lat=>34.172684,lng=>-118.604794},{lat=>34.236144,lng=>-118.500938}];
    my $geocoder_la = Geo::Coder::Googlev3->new(bounds => $bounds);
    is_deeply $geocoder_la->bounds, $bounds, 'bounds accessor';
    my $location_la = safe_geocode { $geocoder_la->geocode(location => 'Winnetka') };
    within $location_la->{geometry}->{location}->{lat}, $location_la->{geometry}->{location}->{lng},
	34.172684, 34.236144, -118.604794, -118.500938, 'Winnetka with bounds';
}

{ # invalid bounds
    eval { $geocoder->bounds('scalar') };
    like $@, qr{array reference}, 'bounds is not an array';
    eval { $geocoder->bounds([]) };
    like $@, qr{two array elements}, 'bounds has not enough elements';
    eval { $geocoder->bounds([1,2]) };
    like $@, qr{lat/lng hashes}, 'bound elements are not hashes';
    eval { $geocoder->bounds([{lng=>1},{lat=>2}]) };
    like $@, qr{lat/lng hashes}, 'bound elements are missing keys';
    is $geocoder->bounds, undef, 'bounds is still unchanged';
}

{ # zero results
    my @locations = safe_geocode { $geocoder->geocode(location => 'This query should not find anything but return ZERO_RESULTS, Foobartown') };
    cmp_ok scalar(@locations), "==", 0, "No result found";

    my $location = safe_geocode { $geocoder->geocode(location => 'This query should not find anything but return ZERO_RESULTS, Foobartown') };
    is $location, undef, "No result found";
}

{ # raw
    my $raw_result = $geocoder->geocode(location => 'Brandenburger Tor, Berlin, Germany', raw => 1);
    # This is the 11th query here, so it's very likely that the API
    # limits are hit.
    like $raw_result->{status}, qr{^(OK|OVER_QUERY_LIMIT)$}, 'raw query';
    if ($raw_result->{status} eq 'OVER_QUERY_LIMIT') {
	diag 'over query limit hit, sleep a little bit';
	sleep 1; # in case a smoker tries this module with another perl...
    }
}

{ # sensor
    {
	my $geocoder = Geo::Coder::Googlev3->new(sensor => "false");
	ok $geocoder;
	is $geocoder->sensor, 'false';
	my $url = $geocoder->geocode_url(location => 'Hauptstr., Berlin');
	like $url, qr{sensor=false}, 'sensor=false detected in URL';

	my $geocoder_default = Geo::Coder::Googlev3->new();
	ok $geocoder_default;
	is $geocoder_default->sensor, undef, "There's no default";
	my $url_default = $geocoder_default->geocode_url(location => 'Hauptstr., Berlin');
	unlike $url_default, qr{sensor=false}, 'no sensor=false required anymore in URL without explicit sensor setting';
    }

    {
	my $geocoder = Geo::Coder::Googlev3->new(sensor => "true");
	ok $geocoder;
	is $geocoder->sensor, 'true';
	my $url = $geocoder->geocode_url(location => 'Hauptstr., Berlin');
	like $url, qr{sensor=true}, 'sensor=false detected in URL';
    }

    eval {
	Geo::Coder::Googlev3->new(sensor => "nonsense");
    };
    like $@, qr{sensor argument has to be either 'false' or 'true'}, 'expected error message for unsupported sensor argument';
}

{ # https
    my $geocoder = Geo::Coder::Googlev3->new(use_https => 1);

    # Probably should not use nested SKIP blocks here
    if ($geocoder->ua->is_protocol_supported('https')) {
	my $location = safe_geocode {
	    $geocoder->geocode(location => 'Berlin')
	};
	like $location->{formatted_address}, qr{berlin}i, 'https query';
    } else {
	# but here is OK
    SKIP: {
	    skip "UA does not support https (maybe you have to install LWP::Protocol::https or so", 1;
	}
    }
}

} # SKIP

sub within ($$$$$$;$) {
    my($lat,$lng,$lat_min,$lat_max,$lng_min,$lng_max,$testname_prefix) = @_;
    my $testname = sub ($) {
	(defined $testname_prefix ? "$testname_prefix (" : "") . $_[0] . (defined $testname_prefix ? ")" : "");
    };
    local $Test::Builder::Level = $Test::Builder::Level + 1;
    cmp_ok $lat, ">=", $lat_min, $testname->("southern latitude");
    cmp_ok $lat, "<=", $lat_max, $testname->("northern latitude");
    cmp_ok $lng, ">=", $lng_min, $testname->("western longitude");
    cmp_ok $lng, "<=", $lng_max, $testname->("eastern longitude");
}

sub safe_geocode (&;$) {
    my($code0, $inforef) = @_;
    my @locations;
    my $code;
    if (wantarray) {
	$code = sub { @locations = eval { &$code0 } };
    } else {
	$code = sub { $locations[0] = eval { &$code0 } };
    }

    $code->();
    if ($@ =~ m{OVER_QUERY_LIMIT}) {
	diag $@;
	diag "Hit OVER_QUERY_LIMIT, sleep some seconds before retrying...";
	sleep 3;
	$code->();
	if ($@ =~ m{OVER_QUERY_LIMIT}) {
	    diag $@;
	    diag "Hit OVER_QUERY_LIMIT, skipping remaining tests...";
	    no warnings 'exiting';
	    last SKIP;
	}
    } elsif ($@ =~ m{Fetching.*failed: 500}) {
	diag $@;
	diag "Fetch failed, probably network connection problems, skipping remaining tests";
	no warnings 'exiting';
	last SKIP;
    }

    if ($inforef) {
	$inforef->{err} = $@;
    }

    if (wantarray) {
	@locations;
    } else {
	$locations[0];
    }
}

# Local Variables:
# mode: cperl
# cperl-indent-level: 4
# End:
# vim:ft=perl:et:sw=4