The personal finance app for everyone (by everyone)
Ruby
Read the Holdings tab's rows in get_holdings (#5034)
Krish Wanchoo7fc54c22eb07
diff --git a/app/models/assistant/function/get_holdings.rb b/app/models/assistant/function/get_holdings.rbindex ff5fbb74a5..b360231437 100644--- a/app/models/assistant/function/get_holdings.rb+++ b/app/models/assistant/function/get_holdings.rb@@ -89,15 +89,24 @@ class Assistant::Function::GetHoldings < Assistant::Functionenddef call(params = {})- holdings_query = build_holdings_query(params)+ holdings = build_holdings_query(params).includes(:security, :account).to_a- ordered_holdings = holdings_query.order(amount: :desc)- pagy = Pagy.new(count: ordered_holdings.count, page: resolved_page(params), limit: default_page_size)- paginated_holdings = ordered_holdings.includes(:security, :account).offset(pagy.offset).limit(pagy.limit)+ # A provider snapshot can be in another currency than the account, and the+ # family's accounts can differ too, so amounts are converted to the family+ # currency before they are ordered and totaled. They convert at today's+ # rate, as InvestmentStatement and the balance sheet do for family totals,+ # even when a provider snapshot is dated earlier. (Account#current_holdings_total+ # uses each holding's own date, but converts into the account currency for+ # the weights.) A currency with no rate counts as 0 rather than 1:1.+ rates = ExchangeRate.rates_for(holdings.map(&:currency), to: family.currency, date: Date.current)+ family_amounts = holdings.index_with { |holding| holding.amount * (rates[holding.currency] || 0) }- total_value = holdings_query.sum(:amount)+ ordered_holdings = holdings.sort_by { |holding| [ -family_amounts[holding], holding.id ] }+ pagy = Pagy.new(count: ordered_holdings.size, page: resolved_page(params), limit: default_page_size)+ paginated_holdings = ordered_holdings[pagy.offset, pagy.limit] || []++ total_value = family_amounts.values.sum- paginated_holdings = paginated_holdings.to_aopen_lots = Holding.open_lots_for(paginated_holdings)normalized_holdings = paginated_holdings.map do |holding|@@ -154,13 +163,11 @@ class Assistant::Function::GetHoldings < Assistant::Functionaccounts = accounts.where(name: params["accounts"])end- holdings = Holding.where(account: accounts)- .where(- id: Holding.where(account: accounts)- .select("DISTINCT ON (account_id, security_id) id")- .where.not(qty: 0)- .order(:account_id, :security_id, date: :desc)- )+ # The rows each account's Holdings tab shows, so the quantity and average+ # cost match it. On a provider-linked account these are the provider rows+ # on the latest snapshot date. The newest row there can be a calculated+ # one dated today, whose cost basis covers only the imported trades.+ holdings = Holding.where(id: accounts.flat_map { |account| account.current_holdings.ids })if params["securities"].present?holdings = holdings.joins(:security).where(securities: { ticker: params["securities"] })diff --git a/test/models/assistant/function/get_holdings_test.rb b/test/models/assistant/function/get_holdings_test.rbindex cd3e429b37..f225020cd3 100644--- a/test/models/assistant/function/get_holdings_test.rb+++ b/test/models/assistant/function/get_holdings_test.rb@@ -36,6 +36,139 @@ class Assistant::Function::GetHoldingsTest < ActiveSupport::TestCaseassert_equal [ "AAPL", "MSFT" ], tickers({})end+ test "get_holdings leaves out a position whose newest row is zero" do+ msft = securities(:msft)+ @account.holdings.create!(security: msft, date: 1.day.ago.to_date, qty: 5, price: 400, amount: 2000, currency: "USD")+ @account.holdings.find_by!(security: msft, date: Date.current).update!(qty: 0, amount: 0)++ assert_equal [ "AAPL" ], tickers({})+ end++ test "get_holdings reports the same average cost as the holdings page on a provider-linked account" do+ account, account_provider = linked_account++ security = Security.create!(ticker: "REPRO", name: "Repro Security")+ (5.days.ago.to_date..Date.current).each do |date|+ Security::Price.create!(security: security, date: date, price: 400)+ end++ # The provider dates its snapshot yesterday: 40 shares at an average cost of 200.+ account.holdings.create!(+ security: security, date: 1.day.ago.to_date, qty: 40, price: 400, amount: 16000, currency: "USD",+ cost_basis: 200, cost_basis_source: "provider", account_provider: account_provider+ )+ # The imported trades explain only 10 of the 40 shares, so the calculated+ # row the materializer writes for today averages 350.+ create_trade(security, account: account, qty: 10, price: 350, date: 3.days.ago.to_date)+ Holding::Materializer.new(account, strategy: :reverse).materialize_holdings++ # The row the old query picked. Without it this test would pass without+ # testing anything.+ calculated_holding = account.holdings.find_by!(security: security, date: Date.current, account_provider_id: nil)+ assert_equal 350.0, calculated_holding.avg_cost.amount.to_f++ page_holding = account.reload.current_holdings.find_by!(security: security)+ tool_holding = @fn.call("page" => 1)[:holdings].find { |holding| holding[:ticker] == "REPRO" }++ assert_equal 200.0, page_holding.avg_cost.amount.to_f+ assert_equal page_holding.avg_cost.amount.to_f, tool_holding[:average_cost]+ assert_equal page_holding.date, tool_holding[:date]+ end++ test "get_holdings converts other currencies to the family currency before ordering and totaling" do+ account, account_provider = linked_account+ ExchangeRate.create!(from_currency: "EUR", to_currency: "USD", date: Date.current, rate: 1.5)+ euro_security = Security.create!(ticker: "EURX", name: "Euro Security")+ dollar_security = Security.create!(ticker: "USDX", name: "Dollar Security")++ # A USD account whose provider snapshot holds one position in EUR.+ account.holdings.create!(+ security: euro_security, date: 1.day.ago.to_date, qty: 10, price: 100, amount: 1000, currency: "EUR",+ account_provider: account_provider+ )+ account.holdings.create!(+ security: dollar_security, date: 1.day.ago.to_date, qty: 10, price: 120, amount: 1200, currency: "USD",+ account_provider: account_provider+ )++ result = @fn.call("page" => 1, "accounts" => [ account.name ])++ assert_equal [ "EURX", "USDX" ], result[:holdings].map { |holding| holding[:ticker] }+ assert_equal Money.new(2700, "USD").format, result[:total_value]+ end++ test "get_holdings counts a holding whose currency has no rate as zero and lists it last" do+ account, account_provider = linked_account+ unrated_security = Security.create!(ticker: "JPYX", name: "Yen Security")+ dollar_security = Security.create!(ticker: "USDX", name: "Dollar Security")++ # No JPY rate is stored, so the yen row can't be converted.+ account.holdings.create!(+ security: unrated_security, date: 1.day.ago.to_date, qty: 10, price: 500, amount: 5000, currency: "JPY",+ account_provider: account_provider+ )+ account.holdings.create!(+ security: dollar_security, date: 1.day.ago.to_date, qty: 10, price: 120, amount: 1200, currency: "USD",+ account_provider: account_provider+ )++ result = @fn.call("page" => 1, "accounts" => [ account.name ])++ assert_equal [ "USDX", "JPYX" ], result[:holdings].map { |holding| holding[:ticker] }+ assert_equal [ 5000.0, "JPY" ], result[:holdings].last.values_at(:amount, :currency)+ assert_equal Money.new(1200, "USD").format, result[:total_value]+ end++ test "get_holdings converts accounts in different currencies to the family currency" do+ ExchangeRate.create!(from_currency: "EUR", to_currency: "USD", date: Date.current, rate: 1.5)+ euro_account = investment_account("Euro Brokerage", currency: "EUR")+ dollar_account = investment_account("Dollar Brokerage", currency: "USD")+ euro_account.holdings.create!(security: securities(:aapl), date: Date.current, qty: 5, price: 200, amount: 1000, currency: "EUR")+ dollar_account.holdings.create!(security: securities(:msft), date: Date.current, qty: 3, price: 400, amount: 1200, currency: "USD")++ result = @fn.call("page" => 1, "accounts" => [ euro_account.name, dollar_account.name ])++ assert_equal [ euro_account.name, dollar_account.name ], result[:holdings].map { |holding| holding[:account] }+ assert_equal Money.new(2700, "USD").format, result[:total_value]+ end++ test "get_holdings keeps the same order across pages" do+ Assistant::Function::GetHoldings.stubs(:default_page_size).returns(2)+ account = investment_account("Paged Brokerage", currency: "USD")+ largest, *tied = [ 300, 200, 200 ].each_with_index.map do |amount, index|+ security = Security.create!(ticker: "PAGE#{index}", name: "Paged Security #{index}")+ account.holdings.create!(security: security, date: Date.current, qty: 1, price: amount, amount: amount, currency: "USD")+ end+ # Equal amounts are ordered by id, so a tie can't land on both pages or on neither.+ expected = [ largest, *tied.sort_by(&:id) ].map(&:ticker)++ first_page = @fn.call("page" => 1, "accounts" => [ account.name ])+ second_page = @fn.call("page" => 2, "accounts" => [ account.name ])++ assert_equal expected.first(2), first_page[:holdings].map { |holding| holding[:ticker] }+ assert_equal expected.last(1), second_page[:holdings].map { |holding| holding[:ticker] }+ assert_equal [ 3, 2, 2 ], second_page.values_at(:total_results, :total_pages, :page)+ assert_equal [ Money.new(700, "USD").format ] * 2, [ first_page[:total_value], second_page[:total_value] ]+ end++ test "get_holdings reads linked and manual accounts in one family each by their own rule" do+ account, account_provider = linked_account+ security = Security.create!(ticker: "LINKED", name: "Linked Security")+ account.holdings.create!(+ security: security, date: 1.day.ago.to_date, qty: 40, price: 400, amount: 16000, currency: "USD",+ account_provider: account_provider+ )+ # A calculated row dated today. The linked account reports its provider row instead.+ account.holdings.create!(security: security, date: Date.current, qty: 10, price: 410, amount: 4100, currency: "USD")++ rows = @fn.call("page" => 1)[:holdings].index_by { |holding| holding[:ticker] }++ assert_equal [ "AAPL", "LINKED", "MSFT" ], rows.keys.sort+ assert_equal [ 40.0, 1.day.ago.to_date ], rows["LINKED"].values_at(:quantity, :date)+ # The manual account reports each security's newest row: today's AAPL, not yesterday's.+ assert_equal [ 2150.0, Date.current ], rows["AAPL"].values_at(:amount, :date)+ end+# With no stored cost the figure is the fallback, which on a FIFO account# reads the open lots; the assistant must answer what the drawer shows.test "get_holdings average_cost equals the drawer figure" do@@ -72,6 +205,22 @@ class Assistant::Function::GetHoldingsTest < ActiveSupport::TestCaseprivate+ # An investment account linked to a provider; any provider works, it+ # only makes the holdings provider rows.+ def linked_account+ account = @user.family.accounts.create!(+ name: "Linked Brokerage", balance: 20000, cash_balance: 4000, currency: "USD",+ accountable: Investment.new, owner: @user+ )+ provider_item = @user.family.coinstats_items.create!(name: "CoinStats", api_key: "test-key")+ provider_account = provider_item.coinstats_accounts.create!(name: "Provider", currency: "USD")+ [ account, AccountProvider.create!(account: account, provider: provider_account) ]+ end++ def investment_account(name, currency:)+ @user.family.accounts.create!(name: name, balance: 0, currency: currency, accountable: Investment.new, owner: @user)+ end+def tickers(params)@fn.call({ "page" => 1 }.merge(params))[:holdings].map { |row| row[:ticker] }.uniq.sortend