CC: | claco [...] cpan.org |
Christopher,
I found that
my $cc = ...;
$cc->value(1.0, 'USD', 'EUR');
$cc->value(2.0, 'USD', 'EUR');
returned the same value, since just the last *value* is stored in cache
(should have been rate, not value).
Also, cache was not setup properly for lower case currencies.
I have attached a patch with tests.
Best regards,
Christian
Subject: | fccw-patch.txt |
--- lib/Finance/Currency/Convert/WebserviceX.pm.orig 2009-05-19 17:11:11.000000000 +0200
+++ lib/Finance/Currency/Convert/WebserviceX.pm 2009-05-19 17:11:19.000000000 +0200
@@ -6,7 +6,7 @@
use LWP::UserAgent ();
use Memoize::Expire ();
-$VERSION = '0.07000';
+$VERSION = '0.07001';
my $expires = tie my %cache => 'Memoize::Expire', LIFETIME => 300;
@@ -32,11 +32,12 @@
my $value = shift || '';
my $from = shift || '';
my $to = shift || '';
- my $key = "$from-$to";
$from = uc($from);
$to = uc($to);
+ my $key = "$from-$to";
+
return unless length $value && $from =~ /^[A-Z]{3}$/ && $to =~ /^[A-Z]{3}$/;
if ($from eq $to) {
@@ -44,7 +45,7 @@
}
if (exists $cache{$key}) {
- return $cache{$key};
+ return $cache{$key}*$value;
}
my $uri = sprintf('http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=%s&ToCurrency=%s', $from, $to);
@@ -60,9 +61,9 @@
return;
} else {
if (($self->{'response'}->content || '') =~ /<double.*>(.*)<\/double>/i) {
- my $rate = $value*($1 || 1);
+ my $rate = ($1 || 1);
$cache{$key} = $rate;
- return $rate;
+ return $rate*$value;
} else {
return;
};
--- t/convert.t.orig 2009-05-19 16:56:57.000000000 +0200
+++ t/convert.t 2009-05-19 17:01:10.000000000 +0200
@@ -7,7 +7,7 @@
BEGIN {
eval 'use Test::MockObject 1.07';
if (!$@) {
- plan tests => 14;
+ plan tests => 18;
my @responses = ('<double>1.23</double>', '<double>1.23</double>', undef);
Test::MockObject->fake_module('HTTP::Response' => (
@@ -67,10 +67,21 @@
is($cc->convert(2.34, 'USD', 'USD'), 2.34);
};
-## no response returns undef
+## check cache does not return the same value for different values
{
my $cc = Finance::Currency::Convert::WebserviceX->new;
isa_ok($cc, 'Finance::Currency::Convert::WebserviceX');
- is($cc->convert(2.34, 'USD', 'CAD'), undef);
-};
\ No newline at end of file
+ isnt($cc->convert(1.00, 'USD', 'JPY'), $cc->convert(2.00, 'USD', 'JPY'));
+}
+
+## check the cache is properly setup, also for non uc-values
+{
+ my $cc = Finance::Currency::Convert::WebserviceX->new;
+ isa_ok($cc, 'Finance::Currency::Convert::WebserviceX');
+
+ ok(!exists $cc->cache->{'USD-EUR'});
+ isnt($cc->convert(1.00, 'usd', 'eur'), undef);
+ ok(exists $cc->cache->{'USD-EUR'});
+};
+
--- t/convert_live.t.orig 2009-05-19 16:57:02.000000000 +0200
+++ t/convert_live.t 2009-05-19 17:01:24.000000000 +0200
@@ -6,7 +6,7 @@
BEGIN {
plan skip_all => 'set TEST_AUTHOR to enable this test' unless $ENV{TEST_AUTHOR};
- plan tests => 14;
+ plan tests => 20;
use_ok('Finance::Currency::Convert::WebserviceX');
};
@@ -23,7 +23,7 @@
};
## try a conversion. whos knows that the rate result will be
-## but at least it will let us know it's a working connection
+## and check the cache is properly setup
{
my $cc = Finance::Currency::Convert::WebserviceX->new;
isa_ok($cc, 'Finance::Currency::Convert::WebserviceX');
@@ -33,6 +33,17 @@
ok(exists $cc->cache->{'USD-JPY'});
};
+## try a conversion. whos knows that the rate result will be
+## and check the cache is properly setup, also for non uc-values
+{
+ my $cc = Finance::Currency::Convert::WebserviceX->new;
+ isa_ok($cc, 'Finance::Currency::Convert::WebserviceX');
+
+ ok(!exists $cc->cache->{'USD-EUR'});
+ isnt($cc->convert(1.00, 'usd', 'eur'), undef);
+ ok(exists $cc->cache->{'USD-EUR'});
+};
+
## make sure we uc the from/to
{
my $cc = Finance::Currency::Convert::WebserviceX->new;
@@ -49,4 +60,12 @@
isa_ok($cc, 'Finance::Currency::Convert::WebserviceX');
is($cc->convert(2.34, 'USD', 'USD'), 2.34);
-};
\ No newline at end of file
+};
+
+## check cache does not return the same value for different values
+{
+ my $cc = Finance::Currency::Convert::WebserviceX->new;
+ isa_ok($cc, 'Finance::Currency::Convert::WebserviceX');
+
+ isnt($cc->convert(1.00, 'USD', 'JPY'), $cc->convert(2.00, 'USD', 'JPY'));
+}