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
|
use strict;
use warnings;
use Test::More;
use Test::Fatal;
use GeoIP2::Database::Reader;
use MaxMind::DB::Metadata;
use Path::Class qw( file );
my @locales = qw( en de );
{
for my $type (qw( Country City Precision-Enterprise )) {
subtest "GeoIP2-$type" => sub {
my $reader = GeoIP2::Database::Reader->new(
file => file(
'maxmind-db', 'test-data', "GeoIP2-$type-Test.mmdb"
)->stringify,
locales => \@locales
);
( my $model = lc $type ) =~ s/^precision-//;
like(
exception { $reader->$model() },
qr/Required param/,
"dies on missing ip - $model method"
);
like(
exception { $reader->$model( ip => 'me' ) },
qr/me is not a valid IP/,
qq{dies on "me" - $model method}
);
like(
exception { $reader->$model( ip => '10.0.0.0' ) },
qr/not a public IP/,
qq{dies on private IP - $model method}
);
like(
exception { $reader->$model( ip => '9.10.11.12' ) },
qr/\QNo record found for IP address 9.10.11.12/,
"dies if IP is not in database - $model method"
);
my $e = exception { $reader->$model( ip => '9.10.11.12' ) };
isa_ok(
$e,
'GeoIP2::Error::IPAddressNotFound',
'error thrown when IP address cannot be found'
);
is(
$e->ip_address,
'9.10.11.12',
'exception ip_address() method returns the IP address'
);
like(
exception { $reader->$model( ip => 'x' ) },
qr/\QThe IP address you provided (x) is not a valid IPv4 or IPv6 address/,
"dies on invalid ip - $model method"
);
my $ip = '81.2.69.160';
my $model_obj = $reader->$model( ip => $ip );
is(
$model_obj->country->is_in_european_union,
1,
"country is_in_european_union - $model method"
);
is(
$model_obj->traits->ip_address,
'81.2.69.160',
"ip address is filled in - $model method"
);
my @record_methods = qw(
city
continent
country
location
maxmind
postal
registered_country
represented_country
traits
);
for my $method ( grep { $model_obj->can($_) } @record_methods ) {
is(
exception { $model_obj->$method() },
undef,
"calling \$model_obj->$method() does not throw an error - $model model"
);
}
return if $model eq 'country';
is(
$model_obj->city->name, 'London',
"city name - $model method"
);
is(
$model_obj->country->name,
'United Kingdom',
"country name - $model method"
);
is(
$model_obj->location->accuracy_radius, 100,
'accuracy_radius'
);
};
}
}
{
# We want to test the type checking in _model_for_address without having
# to actually having test files for all the different database types.
my $force_type;
{
no warnings 'redefine';
*MaxMind::DB::Metadata::database_type = sub { $force_type };
}
my $reader
= GeoIP2::Database::Reader->new(
file => file( 'maxmind-db', 'test-data', 'GeoIP2-City-Test.mmdb' )
->stringify );
my @model_methods = qw(
city
country
connection_type
domain
enterprise
isp
anonymous_ip
);
my %type_to_model = (
'GeoIP2-City' => { city => 1 },
'GeoIP2-Precision-City' => { city => 1 },
'GeoIP2-City-Europe' => { city => 1 },
'GeoIP2-City-South-America' => { city => 1 },
'GeoLite2-City' => { city => 1 },
'GeoIP2-Country' => { country => 1 },
'GeoIP2-Precision-Country' => { country => 1 },
'GeoLite2-Country' => { country => 1 },
'GeoIP2-Connection-Type' => { connection_type => 1 },
'GeoIP2-Precision-Connection-Type' => { connection_type => 1 },
'GeoIP2-Domain' => { domain => 1 },
'GeoIP2-Precision-Domain' => { domain => 1 },
'GeoIP2-Enterprise' => { enterprise => 1 },
'GeoIP2-Precision-Enterprise' => { enterprise => 1 },
'GeoIP2-ISP' => { isp => 1 },
'GeoIP2-Precision-ISP' => { isp => 1 },
'GeoIP2-Anonymous-IP' => { anonymous_ip => 1 },
'GeoIP2-Precision-Anonymous-IP' => { anonymous_ip => 1 },
);
for my $type ( sort keys %type_to_model ) {
$force_type = $type;
for my $method (@model_methods) {
if ( $type_to_model{$type}{$method} ) {
like(
exception { $reader->$method( ip => '9.10.11.12' ) },
qr/\QNo record found for IP address 9.10.11.12/,
"the $method method accepts $type database"
);
}
else {
like(
exception { $reader->$method( ip => '9.10.11.12' ) },
qr/\QThe GeoIP2::Database::Reader->$method() method cannot be called with a $type database/,
"the $method method rejected $type database"
);
}
}
}
}
done_testing();
|