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
|
#####################################################################
#
# Test suite for 'Weather::Com::Cached'
#
# Testing network connection (without proxy).
# Functional tests with 'Test::MockObject'. These could only be run
# if Test::MockObject is installed.
#
# Before `make install' is performed this script should be runnable
# with `make test'. After `make install' it should work as
# `perl t/Cached.t'
#
#####################################################################
#
# initialization
#
no warnings;
use Test::More tests => 7;
require 't/TestData.pm';
BEGIN {
use_ok('Weather::Com::Cached');
}
#####################################################################
#
# Testing object instantiation (do we use the right class)?
#
my %weatherargs = (
'debug' => 0,
'language' => 'en',
);
my $wc = Weather::Com::Cached->new(%weatherargs);
isa_ok( $wc, "Weather::Com::Cached", 'Is a Weatcher::Com::Cached object' );
isa_ok( $wc, "Weather::Com::Base", 'Is a Weatcher::Com::Base object' );
#
# Test network connection
#
SKIP: {
eval { my $locations = $wc->search('New York'); };
skip(
"Could not connect to 'weather.com'! No network connection available?",
1
)
if ($@);
is_deeply( $wc->search('New York'),
$NY_Hash, 'Locations search with network connection.' );
}
#
# Test functionality if Test::MockObject is installed.
#
SKIP: {
eval { require Test::MockObject; };
skip "Test::MockObject not installed", 3 if $@;
# remove all old cache files
unlink <*.dat>;
# define mock object
my $mock = Test::MockObject->new();
$mock->fake_module( 'LWP::UserAgent' =>
( 'request' => sub { return HTTP::Response->new() }, ) );
$mock->fake_module(
'HTTP::Response' => (
'is_success' => sub { return 1 },
'content' => sub { return $NY_HTML },
)
);
$mock->fake_module(
'Weather::Com::Cached' => ( '_cache_time' => sub { return 1110000000 } ) );
# test search method
is_deeply( $wc->search('New York'),
$NY_Hash, 'Look for locations named "New York"' );
# search for 'New York/Central Park, NY' to check locations cache
my $NYCP = { 'USNY0998' => 'New York/Central Park, NY', };
is_deeply( $wc->search('New York/Central Park, NY'),
$NYCP, 'Check locations cache' );
$mock->fake_module(
'HTTP::Response' => (
'is_success' => sub { return 1 },
'content' => sub { return $NYCP_HTML },
)
);
is_deeply( $wc->get_weather('USNY0998'),
$NYCP_HashCached,
'Look for weather data for "New York/Central Park, NY"' );
}
|