diff --git a/libgnucash/app-utils/gnc-quotes.cpp b/libgnucash/app-utils/gnc-quotes.cpp index e86c76e6905..435806c3e4b 100644 --- a/libgnucash/app-utils/gnc-quotes.cpp +++ b/libgnucash/app-utils/gnc-quotes.cpp @@ -659,24 +659,33 @@ GNCPrice* GncQuotesImpl::parse_one_quote(const bpt::ptree& pt, gnc_commodity* comm) { PriceParams p; + bpt::ptree comm_pt; + p.ns = gnc_commodity_get_namespace (comm); p.mnemonic = gnc_commodity_get_mnemonic (comm); if (gnc_commodity_equiv(comm, m_dflt_curr) || (!p.mnemonic || (strcmp (p.mnemonic, "XXX") == 0))) return nullptr; - auto comm_pt_ai{pt.find(p.mnemonic)}; - if (comm_pt_ai == pt.not_found()) + auto source{gnc_quote_source_get_internal_name(gnc_commodity_get_quote_source(comm))}; + auto source_pt_ai{pt.find(source)}; + auto ok{source_pt_ai != pt.not_found()}; + if (ok) + { + auto comm_pt_ai{source_pt_ai->second.find(p.mnemonic)}; + ok = (comm_pt_ai != pt.not_found()); + if (ok) + comm_pt = comm_pt_ai->second; + } + if (!ok) { m_failures.emplace_back(p.ns, p.mnemonic, GncQuoteError::NO_RESULT, empty_string); - PINFO("Skipped %s:%s - Finance::Quote didn't return any data.", - p.ns, p.mnemonic); + PINFO("Skipped %s:%s - Finance::Quote didn't return any data from %s.", + p.ns, p.mnemonic, source); return nullptr; } - auto comm_pt{comm_pt_ai->second}; parse_quote_json(p, comm_pt); - if (!p.success) { m_failures.emplace_back(p.ns, p.mnemonic, GncQuoteError::QUOTE_FAILED, diff --git a/libgnucash/app-utils/test/gtest-gnc-quotes.cpp b/libgnucash/app-utils/test/gtest-gnc-quotes.cpp index fc719bfad3b..87f23fc3a58 100644 --- a/libgnucash/app-utils/test/gtest-gnc-quotes.cpp +++ b/libgnucash/app-utils/test/gtest-gnc-quotes.cpp @@ -181,17 +181,19 @@ TEST_F(GncQuotesTest, offline_wiggle) { StrVec quote_vec{ "{" - "\"EUR\":{\"symbol\":\"EUR\",\"currency\":\"USD\",\"success\":\"1\",\"inverted\":0,\"last\":1.0004}," + "\"currency\":{" + "\"EUR\":{\"symbol\":\"EUR\",\"currency\":\"USD\",\"success\":\"1\",\"inverted\":0,\"last\":1.0004}}," + "\"alphavantage\":{" "\"AAPL\":{\"eps\":6.05,\"success\":1,\"year_range\":\" 129.04 - 182.94\",\"currency\":\"USD\",\"exchange\":\"Sourced from Alphavantage\",\"volume\":73539475,\"close\":157.22,\"high\":158.39,\"open\":156.64,\"div_yield\":0.5660857,\"last\":157.96,\"isodate\":\"2022-09-01\",\"method\":\"alphavantage\",\"name\":\"AAPL (Apple Inc.)\",\"pe\":26.10909,\"low\":154.67,\"type\":\"EQUITY\",\"symbol\":\"AAPL\",\"date\":\"09/01/2022\"}," "\"HPE\":{\"symbol\":\"HPE\",\"date\":\"09/01/2022\",\"low\":13.13,\"type\":\"EQUITY\",\"method\":\"alphavantage\",\"name\":\"HPE (Hewlett Packard Enterprise Comp)\",\"isodate\":\"2022-09-01\",\"pe\":4.7921147,\"last\":13.37,\"high\":13.535,\"close\":13.6,\"open\":13.5,\"div_yield\":3.5294116,\"volume\":16370483,\"exchange\":\"Sourced from Alphavantage\",\"currency\":\"USD\",\"year_range\":\" 12.4 - 17.76\",\"eps\":2.79,\"success\":1}," "\"FKCM\":{\"success\":0,\"symbol\":\"FKCM\",\"errormsg\":\"Error retrieving quote for FKCM - no listing for this name found. Please check symbol and the two letter extension (if any)\"}" - "}" + "}}" }; StrVec err_vec; GncQuotesImpl quotes(m_book, std::make_unique(std::move(quote_vec), std::move(err_vec))); quotes.fetch(m_book); auto failures{quotes.failures()}; - ASSERT_EQ(1u, failures.size()); + EXPECT_EQ(1u, failures.size()); EXPECT_EQ(GncQuoteError::QUOTE_FAILED, std::get<2>(failures[0])); auto pricedb{gnc_pricedb_get_db(m_book)}; EXPECT_EQ(3u, gnc_pricedb_get_num_prices(pricedb)); @@ -201,10 +203,11 @@ TEST_F(GncQuotesTest, offline_report) { StrVec quote_vec{ "{" + "\"alphavantage\":{" "\"AAPL\":{\"eps\":6.05,\"success\":1,\"year_range\":\" 129.04 - 182.94\",\"currency\":\"USD\",\"exchange\":\"Sourced from Alphavantage\",\"volume\":73539475,\"close\":157.22,\"high\":158.39,\"open\":156.64,\"div_yield\":0.5660857,\"last\":157.96,\"isodate\":\"2022-09-01\",\"method\":\"alphavantage\",\"name\":\"AAPL (Apple Inc.)\",\"pe\":26.10909,\"low\":154.67,\"type\":\"EQUITY\",\"symbol\":\"AAPL\",\"date\":\"09/01/2022\"}," "\"HPE\":{\"symbol\":\"HPE\",\"date\":\"09/01/2022\",\"low\":13.13,\"type\":\"EQUITY\",\"method\":\"alphavantage\",\"name\":\"HPE (Hewlett Packard Enterprise Comp)\",\"isodate\":\"2022-09-01\",\"pe\":4.7921147,\"last\":13.37,\"high\":13.535,\"close\":13.6,\"open\":13.5,\"div_yield\":3.5294116,\"volume\":16370483,\"exchange\":\"Sourced from Alphavantage\",\"currency\":\"USD\",\"year_range\":\" 12.4 - 17.76\",\"eps\":2.79,\"success\":1}," "\"FKCM\":{\"success\":0,\"symbol\":\"FKCM\",\"errormsg\":\"Error retrieving quote for FKCM - no listing for this name found. Please check symbol and the two letter extension (if any)\"}" - "}" + "}}" }; StrVec commodities{"AAPL", "HPE", "FKCM"}; StrVec err_vec; @@ -217,8 +220,9 @@ TEST_F(GncQuotesTest, offline_currency_report) { StrVec quote_vec{ "{" + "\"currency\":{" "\"EUR\":{\"symbol\":\"EUR\",\"currency\":\"USD\",\"success\":\"1\",\"inverted\":0,\"last\":1.0004}" - "}" + "}}" }; StrVec commodities{"USD", "EUR"}; StrVec err_vec; @@ -231,9 +235,10 @@ TEST_F(GncQuotesTest, comvec_fetch) { StrVec quote_vec{ "{" + "\"alphavantage\":{" "\"AAPL\":{\"eps\":6.05,\"success\":1,\"year_range\":\" 129.04 - 182.94\",\"currency\":\"USD\",\"exchange\":\"Sourced from Alphavantage\",\"volume\":73539475,\"close\":157.22,\"high\":158.39,\"open\":156.64,\"div_yield\":0.5660857,\"last\":157.96,\"isodate\":\"2022-09-01\",\"method\":\"alphavantage\",\"name\":\"AAPL (Apple Inc.)\",\"pe\":26.10909,\"low\":154.67,\"type\":\"EQUITY\",\"symbol\":\"AAPL\",\"date\":\"09/01/2022\"}," "\"HPE\":{\"symbol\":\"HPE\",\"date\":\"09/01/2022\",\"low\":13.13,\"type\":\"EQUITY\",\"method\":\"alphavantage\",\"name\":\"HPE (Hewlett Packard Enterprise Comp)\",\"isodate\":\"2022-09-01\",\"pe\":4.7921147,\"last\":13.37,\"high\":13.535,\"close\":13.6,\"open\":13.5,\"div_yield\":3.5294116,\"volume\":16370483,\"exchange\":\"Sourced from Alphavantage\",\"currency\":\"USD\",\"year_range\":\" 12.4 - 17.76\",\"eps\":2.79,\"success\":1}" - "}" + "}}" }; StrVec err_vec; auto commtable{gnc_commodity_table_get_table(m_book)}; @@ -252,8 +257,9 @@ TEST_F(GncQuotesTest, fetch_one_commodity) { StrVec quote_vec{ "{" + "\"alphavantage\":{" "\"HPE\":{\"date\":\"09/01/2022\",\"last\":13.37,\"currency\":\"USD\",\"success\":1}" - "}" + "}}" }; StrVec err_vec; auto commtable{gnc_commodity_table_get_table(m_book)}; @@ -281,8 +287,9 @@ TEST_F(GncQuotesTest, fetch_one_currency) { StrVec quote_vec{ "{" + "\"currency\":{" "\"EUR\":{\"symbol\":\"EUR\",\"currency\":\"USD\",\"success\":\"1\",\"inverted\":0,\"last\":1.0004}" - "}" + "}}" }; StrVec err_vec; auto commtable{gnc_commodity_table_get_table(m_book)}; @@ -312,8 +319,9 @@ TEST_F(GncQuotesTest, no_currency) { StrVec quote_vec{ "{" + "\"alphavantage\":{" "\"HPE\":{\"date\":\"09/01/2022\",\"last\":13.37,\"success\":1}" - "}" + "}}" }; StrVec err_vec; auto commtable{gnc_commodity_table_get_table(m_book)}; @@ -331,8 +339,9 @@ TEST_F(GncQuotesTest, bad_currency) { StrVec quote_vec{ "{" - "\"HPE\":{\"date\":\"09/01/2022\",\"last\":13.37,\"currency\":\"BTC\",\"success\":1}" - "}" + "\"alphavantage\":{" + "\"HPE\":{\"date\":\"09/01/2022\",\"last\":13.37,\"currency\":\"BTC\",\"success\":1}" + "}}" }; StrVec err_vec; auto commtable{gnc_commodity_table_get_table(m_book)}; @@ -350,8 +359,9 @@ TEST_F(GncQuotesTest, no_date) { StrVec quote_vec{ "{" + "\"alphavantage\":{" "\"HPE\":{\"last\":13.37,\"currency\":\"USD\",\"success\":1}" - "}" + "}}" }; StrVec err_vec; auto commtable{gnc_commodity_table_get_table(m_book)}; diff --git a/libgnucash/quotes/finance-quote-wrapper.in b/libgnucash/quotes/finance-quote-wrapper.in index cbb0e8976e0..d9ab879bbce 100755 --- a/libgnucash/quotes/finance-quote-wrapper.in +++ b/libgnucash/quotes/finance-quote-wrapper.in @@ -176,7 +176,7 @@ sub parse_currencies { $results{$from_currency}{"currency"} = $to_currency; $results{$from_currency}{"last"} = $price; } - return %results; + return ("currency" => \%results); } sub parse_commodities { @@ -184,7 +184,7 @@ sub parse_commodities { my %quote_data = $quoter->fetch($quote_method_name, keys %$commodities); my %normalized_quote_data = sanitize_hash(%quote_data); - return %normalized_quote_data; + return ($quote_method_name => \%normalized_quote_data); } #---------------------------------------------------------------------------