Weather-GHCN-Fetch
view release on metacpan or search on metacpan
t/91_ghcn_fetch.t view on Meta::CPAN
# Note that it may take some time to fetch the pages.
# Expect the cache to be about 45 Mb
$Refresh = 'always';
ok $TRUE, "creating cache: $Cachedir\n";
}
BAIL_OUT "\n*E* test profile not found or unreadable: " . $PROFILE
if not -r $PROFILE;
my $ghcn_fetch = path($Bin, '../bin', 'ghcn_fetch')->stringify;
my @cmd = (
'perl',
'-I' . $Lib,
$ghcn_fetch,
);
local $, = ' ';
my $stdout;
my $stderr;
my @args;
subtest 'deabbrev' => sub {
my $ro = Weather::GHCN::Options->deabbrev_refresh_option('y');
is $ro, 'yearly', 'deabbrev_refresh_option';
my $rt = Weather::GHCN::Options->deabbrev_report_type('da');
is $rt, 'daily', 'deabbrev_report_type';
};
subtest 'get_user_options (_no_tk and _tk)' => sub {
local @ARGV = qw(-report detail);
my $opt_href = Weather::GHCN::App::Fetch::get_user_options_no_tk;
is $opt_href->{'report'}, 'detail', 'get_user_options_no_tk';
local @ARGV = qw(-report detail);
if ( check_install(module=>'Tk') and check_install(module=>'Tk::Getopt')) {
$opt_href = Weather::GHCN::App::Fetch::get_user_options_tk;
is $opt_href->{'report'}, 'detail', 'get_user_options_tk';
} else {
ok 1, 'Tk or Tk::Getopt not installed';
}
};
subtest 'option validation' => sub {
my @opttable = ( Weather::GHCN::Options->get_tk_options_table() );
@opttable = ( Weather::GHCN::Options->get_tk_options_table() );
my $valid_rt = Weather::GHCN::Options->valid_report_type('detail',\@opttable);
ok $valid_rt, 'valid_report_type - detail valid';
ok !Weather::GHCN::Options->valid_report_type('xxx',\@opttable), 'valid_report_type - xxx invalid';
my $ro = Weather::GHCN::Options->valid_refresh_option('never',\@opttable);
ok $ro,'valid_refresh_option - never valid';
ok !Weather::GHCN::Options->valid_refresh_option('xxx',\@opttable), 'valid_refresh_option - xxx invalid';
};
subtest 'output to clipboard' => sub {
if ( not check_install( module => 'Win32::Clipboard') ) {
plan skip_all => 'no Win32::Clipboard';
}
my $clip = Win32::Clipboard();
$clip->Empty();
@args = (
'-country', 'US',
'-state', 'NY',
'-location', 'New York',
'-active', '1900-1910',
'-report', 'yearly',
'-refresh', 'never',
'-profile', $PROFILE,
'-cachedir', $Cachedir,
'-outclip',
);
my $got;
($stdout, $stderr) = capture {
Weather::GHCN::App::Fetch->run( \@args );
$got = $clip->Get();
};
like $got, qr/Year\s+Decade\s+TMAX\s+TMIN.*?\d{4}/ms, 'clipboard output';
};
subtest 'kml and color options' => sub {
@args = (
'-report', 'kml',
'-kmlcolor', 'azure',
'-location', 'CA006105976,CA006105978',
'-refresh', 'never',
'-profile', $PROFILE,
'-cachedir', $Cachedir,
);
($stdout, $stderr) = capture {
Weather::GHCN::App::Fetch->run( \@args );
};
like $stdout, qr/xml.*kml/ms, 'kml "" to stdout';
};
subtest 'station ids from file' => sub {
my $tempfile1 = Path::Tiny->tempfile( TEMPLATE => '91_ghcn_fetch_t_1_XXXXXX', SUFFIX => '.tmp' );
$tempfile1->touch; # create an empty file
my $tempfile2 = Path::Tiny->tempfile( TEMPLATE => '91_ghcn_fetch_t_2_XXXXXX', SUFFIX => '.tmp' );
my @stnids = qw( CA006105976 CA006105978 );
$tempfile2->spew( join "\n", @stnids );
@args = (
'-report', '',
'-refresh', 'never',
'-profile', $PROFILE,
'-cachedir', $Cachedir,
);
*STDIN_SAVED = *STDIN;
open *STDIN, '<', $tempfile1
or die '*E* cannot open tempfile: ' . $tempfile1;
throws_ok {
Weather::GHCN::App::Fetch->run( \@args, stdin => $TRUE );
} qr/no station ids found/, 'no station ids found in stdin';
close *STDIN or warn $!;
*STDIN = *STDIN_SAVED;
*STDIN_SAVED = *STDIN;
open *STDIN, '<', $tempfile2
or die '*E* cannot open tempfile: ' . $tempfile2;
($stdout, $stderr) = capture {
Weather::GHCN::App::Fetch->run( \@args, stdin => $TRUE );
};
like $stderr, qr/2\s+stations/, 'found 2 stations';
close *STDIN or die $!;
*STDIN = *STDIN_SAVED;
( run in 1.052 second using v1.01-cache-2.11-cpan-84e82930d8c )