From 4d560defe36eb01217806aba3e12d840e06330fd Mon Sep 17 00:00:00 2001 From: Stephane Janel Date: Wed, 11 Oct 2023 14:11:54 +0200 Subject: [PATCH] Avoids live queries to fiats in most tests (except main one) --- data/cache/fiatcache.json | 184 ++++++++++++++++++ src/api/common/include/commonapi.hpp | 4 +- src/api/common/src/commonapi.cpp | 4 +- .../common/test/exchangeprivateapi_test.cpp | 2 +- .../common/test/exchangepublicapi_test.cpp | 2 +- .../test/bithumb_place_order_test.cpp | 2 +- src/api/exchanges/test/commonapi_test.hpp | 2 +- src/engine/test/exchangedata_test.hpp | 2 +- 8 files changed, 194 insertions(+), 8 deletions(-) create mode 100644 data/cache/fiatcache.json diff --git a/data/cache/fiatcache.json b/data/cache/fiatcache.json new file mode 100644 index 00000000..a707a73b --- /dev/null +++ b/data/cache/fiatcache.json @@ -0,0 +1,184 @@ +{ + "fiats": [ + "AED", + "AFN", + "ALL", + "AMD", + "ANG", + "AOA", + "ARS", + "AUD", + "AWG", + "AZN", + "BAM", + "BBD", + "BDT", + "BGN", + "BHD", + "BIF", + "BMD", + "BND", + "BOB", + "BOV", + "BRL", + "BSD", + "BTN", + "BWP", + "BYN", + "BZD", + "CAD", + "CDF", + "CHE", + "CHF", + "CHW", + "CLF", + "CLP", + "CNY", + "COP", + "COU", + "CRC", + "CUC", + "CUP", + "CVE", + "CZK", + "DJF", + "DKK", + "DOP", + "DZD", + "EGP", + "ERN", + "ETB", + "EUR", + "FJD", + "FKP", + "GBP", + "GEL", + "GHS", + "GIP", + "GMD", + "GNF", + "GTQ", + "GYD", + "HKD", + "HNL", + "HRK", + "HTG", + "HUF", + "IDR", + "ILS", + "INR", + "IQD", + "IRR", + "ISK", + "JMD", + "JOD", + "JPY", + "KES", + "KGS", + "KHR", + "KMF", + "KPW", + "KRW", + "KWD", + "KYD", + "KZT", + "LAK", + "LBP", + "LKR", + "LRD", + "LSL", + "LYD", + "MAD", + "MDL", + "MGA", + "MKD", + "MMK", + "MNT", + "MOP", + "MRU", + "MUR", + "MVR", + "MWK", + "MXN", + "MXV", + "MYR", + "MZN", + "NAD", + "NGN", + "NIO", + "NOK", + "NPR", + "NZD", + "OMR", + "PAB", + "PEN", + "PGK", + "PHP", + "PKR", + "PLN", + "PYG", + "QAR", + "RON", + "RSD", + "RUB", + "RWF", + "SAR", + "SBD", + "SCR", + "SDG", + "SEK", + "SGD", + "SHP", + "SLL", + "SOS", + "SRD", + "SSP", + "STN", + "SVC", + "SYP", + "SZL", + "THB", + "TJS", + "TMT", + "TND", + "TOP", + "TRY", + "TTD", + "TWD", + "TZS", + "UAH", + "UGX", + "USD", + "USN", + "UYI", + "UYU", + "UYW", + "UZS", + "VES", + "VND", + "VUV", + "WST", + "XAF", + "XAG", + "XAU", + "XBA", + "XBB", + "XBC", + "XBD", + "XCD", + "XDR", + "XOF", + "XPD", + "XPF", + "XPT", + "XSU", + "XTS", + "XUA", + "XXX", + "YER", + "ZAR", + "ZMW", + "ZWL" + ], + "timeepoch": 1696955013 +} diff --git a/src/api/common/include/commonapi.hpp b/src/api/common/include/commonapi.hpp index 4d33333b..13858bfb 100644 --- a/src/api/common/include/commonapi.hpp +++ b/src/api/common/include/commonapi.hpp @@ -18,8 +18,10 @@ class CommonAPI : public ExchangeBase { public: using Fiats = FlatSet; + enum class AtInit : int8_t { kLoadFromFileCache, kNoLoadFromFileCache }; + CommonAPI(const CoincenterInfo &config, Duration fiatsUpdateFrequency = std::chrono::hours(96), - bool loadFromFileCacheAtInit = true); + AtInit atInit = AtInit::kLoadFromFileCache); /// Returns a new set of fiat currencies. Fiats queryFiats() { diff --git a/src/api/common/src/commonapi.cpp b/src/api/common/src/commonapi.cpp index 6d031e45..488cbfc5 100644 --- a/src/api/common/src/commonapi.cpp +++ b/src/api/common/src/commonapi.cpp @@ -21,9 +21,9 @@ File GetFiatCacheFile(std::string_view dataDir) { } // namespace -CommonAPI::CommonAPI(const CoincenterInfo& config, Duration fiatsUpdateFrequency, bool loadFromFileCacheAtInit) +CommonAPI::CommonAPI(const CoincenterInfo& config, Duration fiatsUpdateFrequency, AtInit atInit) : _coincenterInfo(config), _fiatsCache(CachedResultOptions(fiatsUpdateFrequency, _cachedResultVault)) { - if (loadFromFileCacheAtInit) { + if (atInit == AtInit::kLoadFromFileCache) { json data = GetFiatCacheFile(_coincenterInfo.dataDir()).readAllJson(); if (!data.empty()) { int64_t timeEpoch = data["timeepoch"].get(); diff --git a/src/api/common/test/exchangeprivateapi_test.cpp b/src/api/common/test/exchangeprivateapi_test.cpp index 9151bc23..f9951ae2 100644 --- a/src/api/common/test/exchangeprivateapi_test.cpp +++ b/src/api/common/test/exchangeprivateapi_test.cpp @@ -49,7 +49,7 @@ class ExchangePrivateTest : public ::testing::Test { LoadConfiguration loadConfiguration{kDefaultDataDir, LoadConfiguration::ExchangeConfigFileType::kTest}; CoincenterInfo coincenterInfo{settings::RunMode::kTestKeys, loadConfiguration}; - CommonAPI commonAPI{coincenterInfo, Duration::max(), true}; + CommonAPI commonAPI{coincenterInfo, Duration::max()}; FiatConverter fiatConverter{coincenterInfo, Duration::max()}; // max to avoid real Fiat converter queries MockExchangePublic exchangePublic{kSupportedExchanges[0], fiatConverter, commonAPI, coincenterInfo}; diff --git a/src/api/common/test/exchangepublicapi_test.cpp b/src/api/common/test/exchangepublicapi_test.cpp index 1b394466..0a6676bc 100644 --- a/src/api/common/test/exchangepublicapi_test.cpp +++ b/src/api/common/test/exchangepublicapi_test.cpp @@ -17,7 +17,7 @@ class ExchangePublicTest : public ::testing::Test { settings::RunMode runMode = settings::RunMode::kTestKeys; LoadConfiguration loadConfiguration{kDefaultDataDir, LoadConfiguration::ExchangeConfigFileType::kTest}; CoincenterInfo coincenterInfo{runMode, loadConfiguration}; - CommonAPI commonAPI{coincenterInfo}; + CommonAPI commonAPI{coincenterInfo, Duration::max()}; FiatConverter fiatConverter{coincenterInfo, Duration::max()}; // max to avoid real Fiat converter queries MockExchangePublic exchangePublic{kSupportedExchanges[0], fiatConverter, commonAPI, coincenterInfo}; }; diff --git a/src/api/exchanges/test/bithumb_place_order_test.cpp b/src/api/exchanges/test/bithumb_place_order_test.cpp index ed97a8b6..96c84742 100644 --- a/src/api/exchanges/test/bithumb_place_order_test.cpp +++ b/src/api/exchanges/test/bithumb_place_order_test.cpp @@ -26,7 +26,7 @@ class BithumbPrivateAPIPlaceOrderTest : public ::testing::Test { LoadConfiguration loadConfig{kDefaultDataDir, LoadConfiguration::ExchangeConfigFileType::kTest}; CoincenterInfo coincenterInfo{runMode, loadConfig}; FiatConverter fiatConverter{coincenterInfo, Duration::max()}; // max to avoid real Fiat converter queries - CommonAPI commonAPI{coincenterInfo}; + CommonAPI commonAPI{coincenterInfo, Duration::max()}; BithumbPublic exchangePublic{coincenterInfo, fiatConverter, commonAPI}; APIKeysProvider apiKeysProvider{coincenterInfo.dataDir(), coincenterInfo.getRunMode()}; ExchangeName exchangeName{exchangePublic.name(), apiKeysProvider.getKeyNames(exchangePublic.name()).front()}; diff --git a/src/api/exchanges/test/commonapi_test.hpp b/src/api/exchanges/test/commonapi_test.hpp index 89e40489..302e9ac5 100644 --- a/src/api/exchanges/test/commonapi_test.hpp +++ b/src/api/exchanges/test/commonapi_test.hpp @@ -53,7 +53,7 @@ class TestAPI { CoincenterInfo coincenterInfo{runMode, loadConfig}; APIKeysProvider apiKeysProvider{coincenterInfo.dataDir(), coincenterInfo.getRunMode()}; FiatConverter fiatConverter{coincenterInfo, Duration::max()}; // max to avoid real Fiat converter queries - CommonAPI commonAPI{coincenterInfo}; + CommonAPI commonAPI{coincenterInfo, Duration::max()}; PublicExchangeT exchangePublic{coincenterInfo, fiatConverter, commonAPI}; std::optional exchangePrivateOpt; diff --git a/src/engine/test/exchangedata_test.hpp b/src/engine/test/exchangedata_test.hpp index d8a712de..7b782115 100644 --- a/src/engine/test/exchangedata_test.hpp +++ b/src/engine/test/exchangedata_test.hpp @@ -23,7 +23,7 @@ class ExchangesBaseTest : public ::testing::Test { LoadConfiguration loadConfiguration{kDefaultDataDir, LoadConfiguration::ExchangeConfigFileType::kTest}; settings::RunMode runMode = settings::RunMode::kTestKeys; CoincenterInfo coincenterInfo{runMode, loadConfiguration}; - api::CommonAPI commonAPI{coincenterInfo, Duration::max(), true}; + api::CommonAPI commonAPI{coincenterInfo, Duration::max()}; FiatConverter fiatConverter{coincenterInfo, Duration::max()}; // max to avoid real Fiat converter queries api::MockExchangePublic exchangePublic1{kSupportedExchanges[0], fiatConverter, commonAPI, coincenterInfo}; api::MockExchangePublic exchangePublic2{kSupportedExchanges[1], fiatConverter, commonAPI, coincenterInfo};