[gnome-calculator] Added support for offline tests (fixes #208)
- From: Robert Roth <robertroth src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator] Added support for offline tests (fixes #208)
- Date: Tue, 23 Feb 2021 22:11:12 +0000 (UTC)
commit 8d0fafe67c8a0954e43dee871b4fad7678c39a3f
Author: Robert Roth <robert roth off gmail com>
Date: Wed Feb 24 00:10:00 2021 +0200
Added support for offline tests (fixes #208)
lib/currency-provider.vala | 72 ++++++++++++++++++++++++++---
lib/currency.vala | 25 ++++++----
meson.build | 2 +-
tests/meson.build | 4 +-
tests/rms_five.xls | 94 ++++++++++++++++++++++++++++++++++++++
tests/test-currency-providers.vala | 9 ++--
6 files changed, 185 insertions(+), 21 deletions(-)
---
diff --git a/lib/currency-provider.vala b/lib/currency-provider.vala
index 518b12f0..9c9fd5c8 100644
--- a/lib/currency-provider.vala
+++ b/lib/currency-provider.vala
@@ -25,9 +25,9 @@ public abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
update_rates (asyncLoad);
}
- private bool loading;
- private bool loaded;
- private List<Currency> currencies;
+ protected bool loading;
+ protected bool loaded;
+ protected List<Currency> currencies;
public CurrencyManager currency_manager {get; construct;}
public void clear () {
@@ -58,10 +58,10 @@ public abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
if (asyncLoad) {
debug ("Downloading %s rates async".printf(source_name));
- download_file_async.begin (rate_source_url, rate_filepath, source_name);
+ this.download_file_async.begin (rate_source_url, rate_filepath, source_name);
} else {
debug ("Downloading %s rates sync".printf(source_name));
- download_file_sync (rate_source_url, rate_filepath, source_name);
+ this.download_file_sync (rate_source_url, rate_filepath, source_name);
do_load_rates ();
}
@@ -102,7 +102,7 @@ public abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
return false;
}
- private async void download_file_sync (string uri, string filename, string source)
+ protected virtual void download_file_sync (string uri, string filename, string source)
{
var directory = Path.get_dirname (filename);
@@ -126,7 +126,7 @@ public abstract class AbstractCurrencyProvider : Object, CurrencyProvider {
}
}
- private async void download_file_async (string uri, string filename, string source)
+ protected virtual async void download_file_async (string uri, string filename, string source)
{
var directory = Path.get_dirname (filename);
@@ -300,6 +300,64 @@ public class ImfCurrencyProvider : AbstractCurrencyProvider {
}
+public class OfflineImfCurrencyProvider : ImfCurrencyProvider {
+ private string source_file;
+
+ public OfflineImfCurrencyProvider (CurrencyManager _currency_manager, string source_file)
+ {
+ base(_currency_manager);
+ this.source_file = source_file;
+ }
+
+ protected override void download_file_sync (string uri, string filename, string source)
+ {
+
+ var directory = Path.get_dirname (filename);
+ DirUtils.create_with_parents (directory, 0755);
+
+ var dest = File.new_for_path (filename);
+ var source_file = File.new_for_path (source_file);
+ try
+ {
+ var bodyinput = source_file.read ();
+ var output = dest.replace (null, false, FileCreateFlags.REPLACE_DESTINATION);
+ output.splice (bodyinput, OutputStreamSpliceFlags.CLOSE_SOURCE |
OutputStreamSpliceFlags.CLOSE_TARGET);
+ loading = false;
+ do_load_rates ();
+ debug ("%s rates updated", source);
+ }
+ catch (Error e)
+ {
+ warning ("Couldn't download %s currency rate file: %s", source, e.message);
+ }
+ }
+
+ protected override async void download_file_async (string uri, string filename, string source)
+ {
+
+ var directory = Path.get_dirname (filename);
+ DirUtils.create_with_parents (directory, 0755);
+
+ var dest = File.new_for_path (filename);
+ var source_file = File.new_for_path (source_file);
+ try
+ {
+ var bodyinput = yield source_file.read_async ();
+ var output = yield dest.replace_async (null, false, FileCreateFlags.REPLACE_DESTINATION,
Priority.DEFAULT);
+ yield output.splice_async (bodyinput,
+ OutputStreamSpliceFlags.CLOSE_SOURCE |
OutputStreamSpliceFlags.CLOSE_TARGET,
+ Priority.DEFAULT);
+ loading = false;
+ do_load_rates ();
+ debug ("%s rates updated", source);
+ }
+ catch (Error e)
+ {
+ warning ("Couldn't download %s currency rate file: %s", source, e.message);
+ }
+ }
+}
+
public class EcbCurrencyProvider : AbstractCurrencyProvider {
public override string rate_filepath { owned get {
return Path.build_filename (Environment.get_user_cache_dir (), "gnome-calculator",
"eurofxref-daily.xml"); } }
diff --git a/lib/currency.vala b/lib/currency.vala
index 9663868f..e0d4f108 100644
--- a/lib/currency.vala
+++ b/lib/currency.vala
@@ -31,18 +31,18 @@ public class CurrencyManager : Object
}
public void refresh_sync () {
- foreach (var p in default_currency_manager.providers) {
+ foreach (var p in providers) {
p.set_refresh_interval(_refresh_interval, false);
}
}
public void refresh_async () {
- foreach (var p in default_currency_manager.providers) {
+ foreach (var p in providers) {
p.set_refresh_interval(_refresh_interval, true);
}
}
- public static CurrencyManager get_default (bool asyncLoad = true)
+ public static CurrencyManager get_default (bool asyncLoad = true, bool defaultProviders = true)
{
if (default_currency_manager != null)
return default_currency_manager;
@@ -109,17 +109,24 @@ public class CurrencyManager : Object
default_currency_manager.currencies.append (new Currency ("VEF", _("Venezuelan BolĂvar"), "Bs F"));
default_currency_manager.currencies.append (new Currency ("ZAR", _("South African Rand"), "R"));
- new ImfCurrencyProvider (default_currency_manager);
- new EcbCurrencyProvider (default_currency_manager);
- /* Start downloading the rates if they are outdated. */
- foreach (var p in default_currency_manager.providers) {
- p.updated.connect ( () => { default_currency_manager.updated (); });
- p.update_rates (asyncLoad);
+ if (defaultProviders) {
+ new ImfCurrencyProvider (default_currency_manager);
+ new EcbCurrencyProvider (default_currency_manager);
+ default_currency_manager.initialize_providers (asyncLoad);
}
return default_currency_manager;
}
+ public void initialize_providers (bool asyncLoad = true)
+ {
+ /* Start downloading the rates if they are outdated. */
+ foreach (var p in providers) {
+ p.updated.connect ( () => { updated (); });
+ p.update_rates (asyncLoad);
+ }
+ }
+
public List<Currency> get_currencies ()
{
var r = new List<Currency> ();
diff --git a/meson.build b/meson.build
index 97c9684c..9fead912 100644
--- a/meson.build
+++ b/meson.build
@@ -1,6 +1,6 @@
project('gnome-calculator', ['c', 'vala'],
version: '40.rc',
- meson_version: '>= 0.50.0',
+ meson_version: '>= 0.52.0',
license: 'GPLv3+',
)
diff --git a/tests/meson.build b/tests/meson.build
index f9c4f1a7..82c0a7af 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -33,12 +33,14 @@ test('Equation test', test_equation)
test_currency_sources = [
'test-currency-providers.vala',
]
+
test_currency = executable('test-currency-providers', test_currency_sources,
dependencies: gnome_calculator_tests_deps,
link_with: [libcalculator, lib_mpfrg],
include_directories: gnome_calculator_tests_includes,
)
-test('Currency test', test_currency)
+tests_root_env = environment({'TESTS_ROOT': meson.current_source_dir()})
+test('Currency test', test_currency, env: tests_root_env)
test_number_sources = [
diff --git a/tests/rms_five.xls b/tests/rms_five.xls
new file mode 100644
index 00000000..c5b53846
--- /dev/null
+++ b/tests/rms_five.xls
@@ -0,0 +1,94 @@
+SDRs per Currency unit and Currency units per SDR (1)
+last five days
+SDRs per Currency unit (2)
+
+Currency February 23, 2021 February 22, 2021 February 19, 2021 February 18, 2021
February 17, 2021
+Chinese yuan 0.1071850000 0.1071770000 0.1072520000 0.1074290000
+Euro 0.8409440000 0.8411200000 0.8412810000 0.8394770000 0.8385140000
+Japanese yen 0.0065723400 0.0065560500 0.0065711400 0.0065500200
+U.K. pound 0.9757110000 0.9720750000 0.9701870000 0.9686220000 0.9637350000
+U.S. dollar 0.6925340000 0.6932500000 0.6930400000 0.6947010000 0.6952850000
+Algerian dinar 0.0052211600 0.0052189300 0.0052167600 0.0052266200
+Australian dollar 0.5486950000 0.5460040000 0.5384920000 0.5386710000 0.5390540000
+Botswana pula 0.0638516000 0.0635710000 0.0639676000 0.0638430000 0.0638272000
+Brazilian real 0.1259580000 0.1285360000 0.1280980000 0.1284430000
+Brunei dollar 0.5237210000 0.5217500000 0.5230390000 0.5234000000
+Canadian dollar 0.5496310000 0.5494210000 0.5471810000 0.5469520000
+Chilean peso 0.0009758810 0.0009803850 0.0009731790 0.0009651570 0.0009692000
+Colombian peso 0.0001922420 0.0001949850 0.0001958920 0.0001959200 0.0001976260
+Czech koruna 0.0324615000 0.0324039000 0.0325508000 0.0324627000 0.0323916000
+Danish krone 0.1130820000 0.1131060000 0.1131240000 0.1128900000 0.1127520000
+Indian rupee 0.0095692600 0.0095797200 0.0095514300 0.0095459100
+Israeli New Shekel 0.2121730000 0.2121330000 0.2118740000 0.2127720000 0.2134740000
+Korean won 0.0006258780 0.0006261860 0.0006267880 0.0006271560 0.0006322500
+Kuwaiti dinar 2.2943000000 2.2943900000 2.2972900000 2.2992200000
+Malaysian ringgit 0.1713340000 0.1715750000 0.1713960000 0.1720410000 0.1721640000
+Mauritian rupee 0.0173363000 0.0173245000 0.0173049000 0.0173677000 0.0173954000
+Mexican peso 0.0335255000 0.0339356000 0.0340264000 0.0343763000
+New Zealand dollar 0.5076270000 0.5066960000 0.5004440000 0.4998370000 0.4995620000
+Norwegian krone 0.0814947000 0.0815157000 0.0824018000 0.0821587000 0.0821161000
+Omani rial 1.8011300000 1.8029900000 1.8067600000 1.8082800000
+Peruvian sol 0.1898740000 0.1904330000 0.1904370000
+Philippine peso 0.0143115000 0.0143175000 0.0143581000 0.0144688000
+Polish zloty 0.1865810000 0.1866840000 0.1873230000 0.1869890000 0.1864680000
+Qatari riyal 0.1902570000 0.1904530000 0.1908520000 0.1910120000
+Russian ruble 0.0093689900 0.0094164200 0.0094254300
+Saudi Arabian riyal 0.1846760000 0.1848670000 0.1852540000 0.1854090000
+Singapore dollar 0.5244480000 0.5237210000 0.5217500000 0.5230390000 0.5234000000
+South African rand 0.0472043000 0.0466489000 0.0473587000 0.0470543000 0.0470939000
+Swedish krona 0.0835445000 0.0834939000 0.0835018000 0.0835298000 0.0836202000
+Swiss franc 0.7692260000 0.7718640000 0.7750390000 0.7744280000 0.7764640000
+Thai baht 0.0230791000 0.0230983000 0.0230906000 0.0231505000 0.0231909000
+Trinidadian dollar 0.1025170000 0.1025560000 0.1029890000 0.1033400000
+U.A.E. dirham 0.1885730000 0.1887680000 0.1891630000 0.1893220000
+Uruguayan peso 0.0161075000 0.0161740000 0.0162150000 0.0162435000
+
+Currency units per SDR(3)
+
+Currency February 23, 2021 February 22, 2021 February 19, 2021 February 18, 2021
February 17, 2021
+Chinese yuan 9.329660 9.330360 9.323840 9.308470
+Euro 1.189140 1.188890 1.188660 1.191220 1.192590
+Japanese yen 152.153000 152.531000 152.181000 152.671000
+U.K. pound 1.024890 1.028730 1.030730 1.032390 1.037630
+U.S. dollar 1.443970 1.442480 1.442920 1.439470 1.438260
+Algerian dinar 191.528000 191.610000 191.690000 191.328000
+Australian dollar 1.822510 1.831490 1.857040 1.856420 1.855100
+Botswana pula 15.661300 15.730400 15.632900 15.663400 15.667300
+Brazilian real 7.939150 7.779920 7.806520 7.785550
+Brunei dollar 1.909410 1.916630 1.911900 1.910580
+Canadian dollar 1.819400 1.820100 1.827550 1.828310
+Chilean peso 1,024.720000 1,020.010000 1,027.560000 1,036.100000 1,031.780000
+Colombian peso 5,201.780000 5,128.600000 5,104.850000 5,104.120000 5,060.060000
+Czech koruna 30.805700 30.860500 30.721200 30.804600 30.872200
+Danish krone 8.843140 8.841260 8.839860 8.858180 8.869020
+Indian rupee 104.501000 104.387000 104.696000 104.757000
+Israeli New Shekel 4.713140 4.714020 4.719790 4.699870 4.684410
+Korean won 1,597.760000 1,596.970000 1,595.440000 1,594.500000 1,581.650000
+Kuwaiti dinar 0.435863 0.435846 0.435296 0.434930
+Malaysian ringgit 5.836550 5.828350 5.834440 5.812570 5.808420
+Mauritian rupee 57.682400 57.721700 57.787100 57.578100 57.486500
+Mexican peso 29.828000 29.467600 29.388900 29.089800
+New Zealand dollar 1.969950 1.973570 1.998230 2.000650 2.001750
+Norwegian krone 12.270700 12.267600 12.135700 12.171600 12.177900
+Omani rial 0.555207 0.554634 0.553477 0.553012
+Peruvian sol 5.266650 5.251190 5.251080
+Philippine peso 69.873900 69.844600 69.647100 69.114200
+Polish zloty 5.359600 5.356650 5.338370 5.347910 5.362850
+Qatari riyal 5.256050 5.250640 5.239660 5.235270
+Russian ruble 106.735000 106.197000 106.096000
+Saudi Arabian riyal 5.414890 5.409290 5.397990 5.393480
+Singapore dollar 1.906770 1.909410 1.916630 1.911900 1.910580
+South African rand 21.184500 21.436700 21.115400 21.252000 21.234200
+Swedish krona 11.969700 11.976900 11.975800 11.971800 11.958800
+Swiss franc 1.300010 1.295570 1.290260 1.291280 1.287890
+Thai baht 43.329200 43.293200 43.307700 43.195600 43.120400
+Trinidadian dollar 9.754480 9.750770 9.709770 9.676800
+U.A.E. dirham 5.302990 5.297510 5.286450 5.282010
+Uruguayan peso 62.082900 61.827600 61.671300 61.563100
+
+
+(1) Exchange rates are published daily except on IMF holidays or whenever the IMF is closed for business.
+
+(2) The value of the U.S. dollar in terms of the SDR is the reciprocal of the sum of the dollar values,
based on market exchange rates, of specified quantities of the SDR basket currencies. See SDR Valuation.The
value in terms of the SDR of each of the other currencies shown above is derived from that currency's
representative exchange rate against the U.S. dollar as reported by the issuing central bank and the SDR
value of the U.S. dollar, except for the Iranian rial and the Libyan dinar, the values of which are
officially expressed directly in terms of domestic currency units per SDR. All figures are rounded to six
significant digits. See Representative Exchange Rates for Selected Currencies".
+
+(3) The value in terms of each national currency of the SDR is the reciprocal of the value in terms of the
SDR of each national currency, rounded to six significant digits.
\ No newline at end of file
diff --git a/tests/test-currency-providers.vala b/tests/test-currency-providers.vala
index 7bde4b37..85060177 100644
--- a/tests/test-currency-providers.vala
+++ b/tests/test-currency-providers.vala
@@ -136,7 +136,7 @@ private void test (string expression, string expected, ErrorCode expected_error)
private void test_imf_provider ()
{
var currency_manager = new CurrencyManager();
- var imf_provider = new ImfCurrencyProvider (currency_manager);
+ var imf_provider = new OfflineImfCurrencyProvider (currency_manager, Path.build_filename
(Environment.get_variable ("TESTS_ROOT"), "rms_five.xls"));
imf_provider.clear ();
test_currency (currency_manager, "EUR", null, 0);
@@ -151,8 +151,11 @@ private void test_ecb_provider ()
private void test_currency_conversions ()
{
- CurrencyManager.get_default (false).refresh_interval = 3600;
- CurrencyManager.get_default ().refresh_sync ();
+ var currency_manager = CurrencyManager.get_default (false, false);
+ new OfflineImfCurrencyProvider (currency_manager, Path.build_filename (Environment.get_variable
("TESTS_ROOT"), "rms_five.xls"));
+ currency_manager.refresh_interval = 3600;
+ currency_manager.initialize_providers ();
+ currency_manager.refresh_sync ();
test ("1 EUR in EUR", "1", 0);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]