The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
use strict;
use warnings;

use Test::More;
use WebService::Nestoria::Search Warnings => 1;

##########################################################################
## require internet connection
##
if (! WebService::Nestoria::Search->test_connection) {
    plan 'skip_all' => 'test requires internet connection';
    exit 0;
}

##########################################################################
## plan
##
plan tests => 15;

##########################################################################
## search_listings
##
{
    my %countries = (
        'uk' => 'oxford',
        'es' => 'bilbao',
        'it' => 'roma',
        'de' => 'koeln',
        'fr' => 'lyon',
        'br' => 'sao-paulo',
        'in' => 'mumbai',
    );

    foreach my $country (sort keys %countries) {
        my $ns = WebService::Nestoria::Search->new(
            'country'           => $country,
            'encoding'          => 'json',
            'number_of_results' => 10,
        );

        my $place_name = $countries{$country};
        my @results = $ns->results('place_name' => $place_name);

        is @results, 10, "search_listings - got 10 results for $place_name, $country";
    }
}

##########################################################################
## metadata
##
{
    my %countries = (
        'uk' => 'oxford',
        'es' => 'bilbao',
        'it' => 'roma',
        'de' => 'koeln',
        'fr' => 'lyon',
        'au' => 'newcastle',
        'br' => 'sao-paulo',
        'in' => 'mumbai',
    );

    foreach my $country (sort keys %countries) {
        my $ns = WebService::Nestoria::Search->new(
            'country'           => $country,
            'encoding'          => 'json',
        );

        my $place_name = $countries{$country};
        my $metadata = $ns->metadata(
            'place_name' => $place_name,
        );

        my ($prev_month, $prev_months_year) = do {
            my (undef, undef, undef, undef, $month, $year, undef, undef, undef) = localtime();
            $month += 1;
            $year += 1900;

            my $prev_month = $month - 1;
            my $prev_months_year = $year;
            if ($prev_month == 0) {
                $prev_month = 12;
                $prev_months_year = $prev_months_year - 1;
            }

            ($prev_month, $prev_months_year);
        };
        ok(
            $metadata->get_average_price(
                'range'        => 'monthly',
                'year'         => $prev_months_year,
                'month'        => $prev_month,
                'listing_type' => 'buy',
            ),
            "metadata - got average price for $place_name, $country, $prev_months_year-$prev_month"
        );
    }
}