mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-31 02:59:39 +02:00
Plaid sync domain improvements (#2267)
Breaks our Plaid sync process out into more manageable classes. Notably, this moves the sync process to a distinct, 2-step flow: 1. Import stage - we first make API calls and import Plaid data to "mirror" tables 2. Processing stage - read the raw data, apply business rules, build internal domain models and sync balances This provides several benefits: - Plaid syncs can now be "replayed" without fetching API data again - Mirror tables provide better audit and debugging capabilities - Eliminates the "all or nothing" sync behavior that is currently in place, which is brittle
This commit is contained in:
parent
5c82af0e8c
commit
03a146222d
72 changed files with 3763 additions and 706 deletions
35
test/models/plaid_account/importer_test.rb
Normal file
35
test/models/plaid_account/importer_test.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::ImporterTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@mock_provider = PlaidMock.new
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
@plaid_item = @plaid_account.plaid_item
|
||||
|
||||
@accounts_snapshot = PlaidItem::AccountsSnapshot.new(@plaid_item, plaid_provider: @mock_provider)
|
||||
@account_snapshot = @accounts_snapshot.get_account_data(@plaid_account.plaid_id)
|
||||
end
|
||||
|
||||
test "imports account data" do
|
||||
PlaidAccount::Importer.new(@plaid_account, account_snapshot: @account_snapshot).import
|
||||
|
||||
assert_equal @account_snapshot.account_data.account_id, @plaid_account.plaid_id
|
||||
assert_equal @account_snapshot.account_data.name, @plaid_account.name
|
||||
assert_equal @account_snapshot.account_data.mask, @plaid_account.mask
|
||||
assert_equal @account_snapshot.account_data.type, @plaid_account.plaid_type
|
||||
assert_equal @account_snapshot.account_data.subtype, @plaid_account.plaid_subtype
|
||||
|
||||
# This account has transactions data
|
||||
assert_equal PlaidMock::TRANSACTIONS.count, @plaid_account.raw_transactions_payload["added"].count
|
||||
|
||||
# This account does not have investment data
|
||||
assert_equal 0, @plaid_account.raw_investments_payload["holdings"].count
|
||||
assert_equal 0, @plaid_account.raw_investments_payload["securities"].count
|
||||
assert_equal 0, @plaid_account.raw_investments_payload["transactions"].count
|
||||
|
||||
# This account is a credit card, so it should have liability data
|
||||
assert_equal @plaid_account.plaid_id, @plaid_account.raw_liabilities_payload["credit"]["account_id"]
|
||||
assert_nil @plaid_account.raw_liabilities_payload["mortgage"]
|
||||
assert_nil @plaid_account.raw_liabilities_payload["student"]
|
||||
end
|
||||
end
|
|
@ -0,0 +1,83 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::Investments::BalanceCalculatorTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
|
||||
@plaid_account.update!(
|
||||
plaid_type: "investment",
|
||||
current_balance: 4000,
|
||||
available_balance: 2000 # We ignore this since we have current_balance + holdings
|
||||
)
|
||||
end
|
||||
|
||||
test "calculates total balance from cash and positions" do
|
||||
brokerage_cash_security_id = "plaid_brokerage_cash" # Plaid's brokerage cash security
|
||||
cash_equivalent_security_id = "plaid_cash_equivalent" # Cash equivalent security (i.e. money market fund)
|
||||
aapl_security_id = "plaid_aapl_security" # Regular stock security
|
||||
|
||||
test_investments = {
|
||||
transactions: [], # Irrelevant for balance calcs, leave empty
|
||||
holdings: [
|
||||
# $1,000 in brokerage cash
|
||||
{
|
||||
security_id: brokerage_cash_security_id,
|
||||
cost_basis: 1000,
|
||||
institution_price: 1,
|
||||
institution_value: 1000,
|
||||
quantity: 1000
|
||||
},
|
||||
# $1,000 in money market funds
|
||||
{
|
||||
security_id: cash_equivalent_security_id,
|
||||
cost_basis: 1000,
|
||||
institution_price: 1,
|
||||
institution_value: 1000,
|
||||
quantity: 1000
|
||||
},
|
||||
# $2,000 worth of AAPL stock
|
||||
{
|
||||
security_id: aapl_security_id,
|
||||
cost_basis: 2000,
|
||||
institution_price: 200,
|
||||
institution_value: 2000,
|
||||
quantity: 10
|
||||
}
|
||||
],
|
||||
securities: [
|
||||
{
|
||||
security_id: brokerage_cash_security_id,
|
||||
ticker_symbol: "CUR:USD",
|
||||
is_cash_equivalent: true,
|
||||
type: "cash"
|
||||
},
|
||||
{
|
||||
security_id: cash_equivalent_security_id,
|
||||
ticker_symbol: "VMFXX", # Vanguard Money Market Reserves
|
||||
is_cash_equivalent: true,
|
||||
type: "mutual fund"
|
||||
},
|
||||
{
|
||||
security_id: aapl_security_id,
|
||||
ticker_symbol: "AAPL",
|
||||
is_cash_equivalent: false,
|
||||
type: "equity",
|
||||
market_identifier_code: "XNAS"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@plaid_account.update!(raw_investments_payload: test_investments)
|
||||
|
||||
security_resolver = PlaidAccount::Investments::SecurityResolver.new(@plaid_account)
|
||||
balance_calculator = PlaidAccount::Investments::BalanceCalculator.new(@plaid_account, security_resolver: security_resolver)
|
||||
|
||||
# We set this equal to `current_balance`
|
||||
assert_equal 4000, balance_calculator.balance
|
||||
|
||||
# This is the sum of "non-brokerage-cash-holdings". In the above test case, this means
|
||||
# we're summing up $2,000 of AAPL + $1,000 Vanguard MM for $3,000 in holdings value.
|
||||
# We back this $3,000 from the $4,000 total to get $1,000 in cash balance.
|
||||
assert_equal 1000, balance_calculator.cash_balance
|
||||
end
|
||||
end
|
|
@ -0,0 +1,49 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::Investments::HoldingsProcessorTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
@security_resolver = PlaidAccount::Investments::SecurityResolver.new(@plaid_account)
|
||||
end
|
||||
|
||||
test "creates holding records from Plaid holdings snapshot" do
|
||||
test_investments_payload = {
|
||||
securities: [], # mocked
|
||||
holdings: [
|
||||
{
|
||||
"security_id" => "123",
|
||||
"quantity" => 100,
|
||||
"institution_price" => 100,
|
||||
"iso_currency_code" => "USD"
|
||||
}
|
||||
],
|
||||
transactions: [] # not relevant for test
|
||||
}
|
||||
|
||||
@plaid_account.update!(raw_investments_payload: test_investments_payload)
|
||||
|
||||
@security_resolver.expects(:resolve)
|
||||
.with(plaid_security_id: "123")
|
||||
.returns(
|
||||
OpenStruct.new(
|
||||
security: securities(:aapl),
|
||||
cash_equivalent?: false,
|
||||
brokerage_cash?: false
|
||||
)
|
||||
)
|
||||
|
||||
processor = PlaidAccount::Investments::HoldingsProcessor.new(@plaid_account, security_resolver: @security_resolver)
|
||||
|
||||
assert_difference "Holding.count" do
|
||||
processor.process
|
||||
end
|
||||
|
||||
holding = Holding.order(created_at: :desc).first
|
||||
|
||||
assert_equal 100, holding.qty
|
||||
assert_equal 100, holding.price
|
||||
assert_equal "USD", holding.currency
|
||||
assert_equal securities(:aapl), holding.security
|
||||
assert_equal Date.current, holding.date
|
||||
end
|
||||
end
|
115
test/models/plaid_account/investments/security_resolver_test.rb
Normal file
115
test/models/plaid_account/investments/security_resolver_test.rb
Normal file
|
@ -0,0 +1,115 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::Investments::SecurityResolverTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@upstream_resolver = mock("Security::Resolver")
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
@resolver = PlaidAccount::Investments::SecurityResolver.new(@plaid_account)
|
||||
end
|
||||
|
||||
test "handles missing plaid security" do
|
||||
missing_id = "missing_security_id"
|
||||
|
||||
# Ensure there are *no* securities that reference the missing ID
|
||||
@plaid_account.update!(raw_investments_payload: {
|
||||
securities: [
|
||||
{
|
||||
"security_id" => "some_other_id",
|
||||
"ticker_symbol" => "FOO",
|
||||
"type" => "equity",
|
||||
"market_identifier_code" => "XNAS"
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
Security::Resolver.expects(:new).never
|
||||
Sentry.stubs(:capture_exception)
|
||||
|
||||
response = @resolver.resolve(plaid_security_id: missing_id)
|
||||
|
||||
assert_nil response.security
|
||||
refute response.cash_equivalent?
|
||||
refute response.brokerage_cash?
|
||||
end
|
||||
|
||||
test "identifies brokerage cash plaid securities" do
|
||||
brokerage_cash_id = "brokerage_cash_security_id"
|
||||
|
||||
@plaid_account.update!(raw_investments_payload: {
|
||||
securities: [
|
||||
{
|
||||
"security_id" => brokerage_cash_id,
|
||||
"ticker_symbol" => "CUR:USD", # Plaid brokerage cash ticker
|
||||
"type" => "cash",
|
||||
"is_cash_equivalent" => true
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
Security::Resolver.expects(:new).never
|
||||
|
||||
response = @resolver.resolve(plaid_security_id: brokerage_cash_id)
|
||||
|
||||
assert_nil response.security
|
||||
assert response.cash_equivalent?
|
||||
assert response.brokerage_cash?
|
||||
end
|
||||
|
||||
test "identifies cash equivalent plaid securities" do
|
||||
mmf_security_id = "money_market_security_id"
|
||||
|
||||
@plaid_account.update!(raw_investments_payload: {
|
||||
securities: [
|
||||
{
|
||||
"security_id" => mmf_security_id,
|
||||
"ticker_symbol" => "VMFXX", # Vanguard Federal Money Market Fund
|
||||
"type" => "mutual fund",
|
||||
"is_cash_equivalent" => true,
|
||||
"market_identifier_code" => "XNAS"
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
resolved_security = Security.create!(ticker: "VMFXX", exchange_operating_mic: "XNAS")
|
||||
|
||||
Security::Resolver.expects(:new)
|
||||
.with("VMFXX", exchange_operating_mic: "XNAS")
|
||||
.returns(@upstream_resolver)
|
||||
@upstream_resolver.expects(:resolve).returns(resolved_security)
|
||||
|
||||
response = @resolver.resolve(plaid_security_id: mmf_security_id)
|
||||
|
||||
assert_equal resolved_security, response.security
|
||||
assert response.cash_equivalent?
|
||||
refute response.brokerage_cash?
|
||||
end
|
||||
|
||||
test "resolves normal plaid securities" do
|
||||
security_id = "regular_security_id"
|
||||
|
||||
@plaid_account.update!(raw_investments_payload: {
|
||||
securities: [
|
||||
{
|
||||
"security_id" => security_id,
|
||||
"ticker_symbol" => "IVV",
|
||||
"type" => "etf",
|
||||
"is_cash_equivalent" => false,
|
||||
"market_identifier_code" => "XNAS"
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
resolved_security = Security.create!(ticker: "IVV", exchange_operating_mic: "XNAS")
|
||||
|
||||
Security::Resolver.expects(:new)
|
||||
.with("IVV", exchange_operating_mic: "XNAS")
|
||||
.returns(@upstream_resolver)
|
||||
@upstream_resolver.expects(:resolve).returns(resolved_security)
|
||||
|
||||
response = @resolver.resolve(plaid_security_id: security_id)
|
||||
|
||||
assert_equal resolved_security, response.security
|
||||
refute response.cash_equivalent? # Normal securities are not cash equivalent
|
||||
refute response.brokerage_cash?
|
||||
end
|
||||
end
|
|
@ -0,0 +1,111 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::Investments::TransactionsProcessorTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
@security_resolver = PlaidAccount::Investments::SecurityResolver.new(@plaid_account)
|
||||
end
|
||||
|
||||
|
||||
test "creates regular trade entries" do
|
||||
test_investments_payload = {
|
||||
transactions: [
|
||||
{
|
||||
"transaction_id" => "123",
|
||||
"security_id" => "123",
|
||||
"type" => "buy",
|
||||
"quantity" => 1, # Positive, so "buy 1 share"
|
||||
"price" => 100,
|
||||
"iso_currency_code" => "USD",
|
||||
"date" => Date.current,
|
||||
"name" => "Buy 1 share of AAPL"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@plaid_account.update!(raw_investments_payload: test_investments_payload)
|
||||
|
||||
@security_resolver.stubs(:resolve).returns(OpenStruct.new(
|
||||
security: securities(:aapl)
|
||||
))
|
||||
|
||||
processor = PlaidAccount::Investments::TransactionsProcessor.new(@plaid_account, security_resolver: @security_resolver)
|
||||
|
||||
assert_difference [ "Entry.count", "Trade.count" ], 1 do
|
||||
processor.process
|
||||
end
|
||||
|
||||
entry = Entry.order(created_at: :desc).first
|
||||
|
||||
assert_equal 100, entry.amount
|
||||
assert_equal "USD", entry.currency
|
||||
assert_equal Date.current, entry.date
|
||||
assert_equal "Buy 1 share of AAPL", entry.name
|
||||
end
|
||||
|
||||
test "creates cash transactions" do
|
||||
test_investments_payload = {
|
||||
transactions: [
|
||||
{
|
||||
"transaction_id" => "123",
|
||||
"type" => "cash",
|
||||
"subtype" => "withdrawal",
|
||||
"amount" => 100, # Positive, so moving money OUT of the account
|
||||
"iso_currency_code" => "USD",
|
||||
"date" => Date.current,
|
||||
"name" => "Withdrawal"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@plaid_account.update!(raw_investments_payload: test_investments_payload)
|
||||
|
||||
@security_resolver.expects(:resolve).never # Cash transactions don't have a security
|
||||
|
||||
processor = PlaidAccount::Investments::TransactionsProcessor.new(@plaid_account, security_resolver: @security_resolver)
|
||||
|
||||
assert_difference [ "Entry.count", "Transaction.count" ], 1 do
|
||||
processor.process
|
||||
end
|
||||
|
||||
entry = Entry.order(created_at: :desc).first
|
||||
|
||||
assert_equal 100, entry.amount
|
||||
assert_equal "USD", entry.currency
|
||||
assert_equal Date.current, entry.date
|
||||
assert_equal "Withdrawal", entry.name
|
||||
end
|
||||
|
||||
test "creates fee transactions" do
|
||||
test_investments_payload = {
|
||||
transactions: [
|
||||
{
|
||||
"transaction_id" => "123",
|
||||
"type" => "fee",
|
||||
"subtype" => "miscellaneous fee",
|
||||
"amount" => 10.25,
|
||||
"iso_currency_code" => "USD",
|
||||
"date" => Date.current,
|
||||
"name" => "Miscellaneous fee"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@plaid_account.update!(raw_investments_payload: test_investments_payload)
|
||||
|
||||
@security_resolver.expects(:resolve).never # Cash transactions don't have a security
|
||||
|
||||
processor = PlaidAccount::Investments::TransactionsProcessor.new(@plaid_account, security_resolver: @security_resolver)
|
||||
|
||||
assert_difference [ "Entry.count", "Transaction.count" ], 1 do
|
||||
processor.process
|
||||
end
|
||||
|
||||
entry = Entry.order(created_at: :desc).first
|
||||
|
||||
assert_equal 10.25, entry.amount
|
||||
assert_equal "USD", entry.currency
|
||||
assert_equal Date.current, entry.date
|
||||
assert_equal "Miscellaneous fee", entry.name
|
||||
end
|
||||
end
|
|
@ -0,0 +1,39 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::Liabilities::CreditProcessorTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
@plaid_account.update!(
|
||||
plaid_type: "credit",
|
||||
plaid_subtype: "credit_card"
|
||||
)
|
||||
|
||||
@plaid_account.account.update!(
|
||||
accountable: CreditCard.new,
|
||||
)
|
||||
end
|
||||
|
||||
test "updates credit card minimum payment and APR from Plaid data" do
|
||||
@plaid_account.update!(raw_liabilities_payload: {
|
||||
credit: {
|
||||
minimum_payment_amount: 100,
|
||||
aprs: [ { apr_percentage: 15.0 } ]
|
||||
}
|
||||
})
|
||||
|
||||
processor = PlaidAccount::Liabilities::CreditProcessor.new(@plaid_account)
|
||||
processor.process
|
||||
|
||||
assert_equal 100, @plaid_account.account.credit_card.minimum_payment
|
||||
assert_equal 15.0, @plaid_account.account.credit_card.apr
|
||||
end
|
||||
|
||||
test "does nothing when liability data absent" do
|
||||
@plaid_account.update!(raw_liabilities_payload: {})
|
||||
processor = PlaidAccount::Liabilities::CreditProcessor.new(@plaid_account)
|
||||
processor.process
|
||||
|
||||
assert_nil @plaid_account.account.credit_card.minimum_payment
|
||||
assert_nil @plaid_account.account.credit_card.apr
|
||||
end
|
||||
end
|
|
@ -0,0 +1,44 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::Liabilities::MortgageProcessorTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
@plaid_account.update!(
|
||||
plaid_type: "loan",
|
||||
plaid_subtype: "mortgage"
|
||||
)
|
||||
|
||||
@plaid_account.account.update!(accountable: Loan.new)
|
||||
end
|
||||
|
||||
test "updates loan interest rate and type from Plaid data" do
|
||||
@plaid_account.update!(raw_liabilities_payload: {
|
||||
mortgage: {
|
||||
interest_rate: {
|
||||
type: "fixed",
|
||||
percentage: 4.25
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
processor = PlaidAccount::Liabilities::MortgageProcessor.new(@plaid_account)
|
||||
processor.process
|
||||
|
||||
loan = @plaid_account.account.loan
|
||||
|
||||
assert_equal "fixed", loan.rate_type
|
||||
assert_equal 4.25, loan.interest_rate
|
||||
end
|
||||
|
||||
test "does nothing when mortgage data absent" do
|
||||
@plaid_account.update!(raw_liabilities_payload: {})
|
||||
|
||||
processor = PlaidAccount::Liabilities::MortgageProcessor.new(@plaid_account)
|
||||
processor.process
|
||||
|
||||
loan = @plaid_account.account.loan
|
||||
|
||||
assert_nil loan.rate_type
|
||||
assert_nil loan.interest_rate
|
||||
end
|
||||
end
|
|
@ -0,0 +1,68 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::Liabilities::StudentLoanProcessorTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
@plaid_account.update!(
|
||||
plaid_type: "loan",
|
||||
plaid_subtype: "student"
|
||||
)
|
||||
|
||||
# Change the underlying accountable to a Loan so the helper method `loan` is available
|
||||
@plaid_account.account.update!(accountable: Loan.new)
|
||||
end
|
||||
|
||||
test "updates loan details including term months from Plaid data" do
|
||||
@plaid_account.update!(raw_liabilities_payload: {
|
||||
student: {
|
||||
interest_rate_percentage: 5.5,
|
||||
origination_principal_amount: 20000,
|
||||
origination_date: Date.new(2020, 1, 1),
|
||||
expected_payoff_date: Date.new(2022, 1, 1)
|
||||
}
|
||||
})
|
||||
|
||||
processor = PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account)
|
||||
processor.process
|
||||
|
||||
loan = @plaid_account.account.loan
|
||||
|
||||
assert_equal "fixed", loan.rate_type
|
||||
assert_equal 5.5, loan.interest_rate
|
||||
assert_equal 20000, loan.initial_balance
|
||||
assert_equal 24, loan.term_months
|
||||
end
|
||||
|
||||
test "handles missing payoff dates gracefully" do
|
||||
@plaid_account.update!(raw_liabilities_payload: {
|
||||
student: {
|
||||
interest_rate_percentage: 4.8,
|
||||
origination_principal_amount: 15000,
|
||||
origination_date: Date.new(2021, 6, 1)
|
||||
# expected_payoff_date omitted
|
||||
}
|
||||
})
|
||||
|
||||
processor = PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account)
|
||||
processor.process
|
||||
|
||||
loan = @plaid_account.account.loan
|
||||
|
||||
assert_nil loan.term_months
|
||||
assert_equal 4.8, loan.interest_rate
|
||||
assert_equal 15000, loan.initial_balance
|
||||
end
|
||||
|
||||
test "does nothing when loan data absent" do
|
||||
@plaid_account.update!(raw_liabilities_payload: {})
|
||||
|
||||
processor = PlaidAccount::Liabilities::StudentLoanProcessor.new(@plaid_account)
|
||||
processor.process
|
||||
|
||||
loan = @plaid_account.account.loan
|
||||
|
||||
assert_nil loan.interest_rate
|
||||
assert_nil loan.initial_balance
|
||||
assert_nil loan.term_months
|
||||
end
|
||||
end
|
172
test/models/plaid_account/processor_test.rb
Normal file
172
test/models/plaid_account/processor_test.rb
Normal file
|
@ -0,0 +1,172 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::ProcessorTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
end
|
||||
|
||||
test "processes new account and assigns attributes" do
|
||||
Account.destroy_all # Clear out internal accounts so we start fresh
|
||||
|
||||
expect_default_subprocessor_calls
|
||||
|
||||
@plaid_account.update!(
|
||||
plaid_id: "test_plaid_id",
|
||||
plaid_type: "depository",
|
||||
plaid_subtype: "checking",
|
||||
current_balance: 1000,
|
||||
available_balance: 1000,
|
||||
currency: "USD",
|
||||
name: "Test Plaid Account",
|
||||
mask: "1234"
|
||||
)
|
||||
|
||||
assert_difference "Account.count" do
|
||||
PlaidAccount::Processor.new(@plaid_account).process
|
||||
end
|
||||
|
||||
@plaid_account.reload
|
||||
|
||||
account = Account.order(created_at: :desc).first
|
||||
assert_equal "Test Plaid Account", account.name
|
||||
assert_equal @plaid_account.id, account.plaid_account_id
|
||||
assert_equal "checking", account.subtype
|
||||
assert_equal 1000, account.balance
|
||||
assert_equal 1000, account.cash_balance
|
||||
assert_equal "USD", account.currency
|
||||
assert_equal "Depository", account.accountable_type
|
||||
assert_equal "checking", account.subtype
|
||||
end
|
||||
|
||||
test "processing is idempotent with updates and enrichments" do
|
||||
expect_default_subprocessor_calls
|
||||
|
||||
assert_equal "Plaid Depository Account", @plaid_account.account.name
|
||||
assert_equal "checking", @plaid_account.account.subtype
|
||||
|
||||
@plaid_account.account.update!(
|
||||
name: "User updated name",
|
||||
subtype: "savings",
|
||||
balance: 2000 # User cannot override balance. This will be overridden by the processor on next processing
|
||||
)
|
||||
|
||||
@plaid_account.account.lock_attr!(:name)
|
||||
@plaid_account.account.lock_attr!(:subtype)
|
||||
@plaid_account.account.lock_attr!(:balance) # Even if balance somehow becomes locked, Plaid ignores it and overrides it
|
||||
|
||||
assert_no_difference "Account.count" do
|
||||
PlaidAccount::Processor.new(@plaid_account).process
|
||||
end
|
||||
|
||||
@plaid_account.reload
|
||||
|
||||
assert_equal "User updated name", @plaid_account.account.name
|
||||
assert_equal "savings", @plaid_account.account.subtype
|
||||
assert_equal @plaid_account.current_balance, @plaid_account.account.balance # Overriden by processor
|
||||
end
|
||||
|
||||
test "account processing failure halts further processing" do
|
||||
Account.any_instance.stubs(:save!).raises(StandardError.new("Test error"))
|
||||
|
||||
PlaidAccount::Transactions::Processor.any_instance.expects(:process).never
|
||||
PlaidAccount::Investments::TransactionsProcessor.any_instance.expects(:process).never
|
||||
PlaidAccount::Investments::HoldingsProcessor.any_instance.expects(:process).never
|
||||
|
||||
expect_no_investment_balance_calculator_calls
|
||||
expect_no_liability_processor_calls
|
||||
|
||||
assert_raises(StandardError) do
|
||||
PlaidAccount::Processor.new(@plaid_account).process
|
||||
end
|
||||
end
|
||||
|
||||
test "product processing failure reports exception and continues processing" do
|
||||
PlaidAccount::Transactions::Processor.any_instance.stubs(:process).raises(StandardError.new("Test error"))
|
||||
|
||||
# Subsequent product processors still run
|
||||
expect_investment_product_processor_calls
|
||||
|
||||
assert_nothing_raised do
|
||||
PlaidAccount::Processor.new(@plaid_account).process
|
||||
end
|
||||
end
|
||||
|
||||
test "calculates balance using BalanceCalculator for investment accounts" do
|
||||
@plaid_account.update!(plaid_type: "investment")
|
||||
|
||||
PlaidAccount::Investments::BalanceCalculator.any_instance.expects(:balance).returns(1000).once
|
||||
PlaidAccount::Investments::BalanceCalculator.any_instance.expects(:cash_balance).returns(1000).once
|
||||
|
||||
PlaidAccount::Processor.new(@plaid_account).process
|
||||
end
|
||||
|
||||
test "processes credit liability data" do
|
||||
expect_investment_product_processor_calls
|
||||
expect_no_investment_balance_calculator_calls
|
||||
expect_depository_product_processor_calls
|
||||
|
||||
@plaid_account.update!(plaid_type: "credit", plaid_subtype: "credit card")
|
||||
|
||||
PlaidAccount::Liabilities::CreditProcessor.any_instance.expects(:process).once
|
||||
PlaidAccount::Liabilities::MortgageProcessor.any_instance.expects(:process).never
|
||||
PlaidAccount::Liabilities::StudentLoanProcessor.any_instance.expects(:process).never
|
||||
|
||||
PlaidAccount::Processor.new(@plaid_account).process
|
||||
end
|
||||
|
||||
test "processes mortgage liability data" do
|
||||
expect_investment_product_processor_calls
|
||||
expect_no_investment_balance_calculator_calls
|
||||
expect_depository_product_processor_calls
|
||||
|
||||
@plaid_account.update!(plaid_type: "loan", plaid_subtype: "mortgage")
|
||||
|
||||
PlaidAccount::Liabilities::CreditProcessor.any_instance.expects(:process).never
|
||||
PlaidAccount::Liabilities::MortgageProcessor.any_instance.expects(:process).once
|
||||
PlaidAccount::Liabilities::StudentLoanProcessor.any_instance.expects(:process).never
|
||||
|
||||
PlaidAccount::Processor.new(@plaid_account).process
|
||||
end
|
||||
|
||||
test "processes student loan liability data" do
|
||||
expect_investment_product_processor_calls
|
||||
expect_no_investment_balance_calculator_calls
|
||||
expect_depository_product_processor_calls
|
||||
|
||||
@plaid_account.update!(plaid_type: "loan", plaid_subtype: "student")
|
||||
|
||||
PlaidAccount::Liabilities::CreditProcessor.any_instance.expects(:process).never
|
||||
PlaidAccount::Liabilities::MortgageProcessor.any_instance.expects(:process).never
|
||||
PlaidAccount::Liabilities::StudentLoanProcessor.any_instance.expects(:process).once
|
||||
|
||||
PlaidAccount::Processor.new(@plaid_account).process
|
||||
end
|
||||
|
||||
private
|
||||
def expect_investment_product_processor_calls
|
||||
PlaidAccount::Investments::TransactionsProcessor.any_instance.expects(:process).once
|
||||
PlaidAccount::Investments::HoldingsProcessor.any_instance.expects(:process).once
|
||||
end
|
||||
|
||||
def expect_depository_product_processor_calls
|
||||
PlaidAccount::Transactions::Processor.any_instance.expects(:process).once
|
||||
end
|
||||
|
||||
def expect_no_investment_balance_calculator_calls
|
||||
PlaidAccount::Investments::BalanceCalculator.any_instance.expects(:balance).never
|
||||
PlaidAccount::Investments::BalanceCalculator.any_instance.expects(:cash_balance).never
|
||||
end
|
||||
|
||||
def expect_no_liability_processor_calls
|
||||
PlaidAccount::Liabilities::CreditProcessor.any_instance.expects(:process).never
|
||||
PlaidAccount::Liabilities::MortgageProcessor.any_instance.expects(:process).never
|
||||
PlaidAccount::Liabilities::StudentLoanProcessor.any_instance.expects(:process).never
|
||||
end
|
||||
|
||||
def expect_default_subprocessor_calls
|
||||
expect_depository_product_processor_calls
|
||||
expect_investment_product_processor_calls
|
||||
expect_no_investment_balance_calculator_calls
|
||||
expect_no_liability_processor_calls
|
||||
end
|
||||
end
|
|
@ -1,6 +1,6 @@
|
|||
require "test_helper"
|
||||
|
||||
class Provider::Plaid::CategoryAliasMatcherTest < ActiveSupport::TestCase
|
||||
class PlaidAccount::Transactions::CategoryMatcherTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@family = families(:empty)
|
||||
|
||||
|
@ -32,7 +32,7 @@ class Provider::Plaid::CategoryAliasMatcherTest < ActiveSupport::TestCase
|
|||
|
||||
@giving = @family.categories.create!(name: "Giving")
|
||||
|
||||
@matcher = Provider::Plaid::CategoryAliasMatcher.new(@family.categories)
|
||||
@matcher = PlaidAccount::Transactions::CategoryMatcher.new(@family.categories)
|
||||
end
|
||||
|
||||
test "matches expense categories" do
|
63
test/models/plaid_account/transactions/processor_test.rb
Normal file
63
test/models/plaid_account/transactions/processor_test.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::Transactions::ProcessorTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
end
|
||||
|
||||
test "processes added and modified plaid transactions" do
|
||||
added_transactions = [ { "transaction_id" => "123" } ]
|
||||
modified_transactions = [ { "transaction_id" => "456" } ]
|
||||
|
||||
@plaid_account.update!(raw_transactions_payload: {
|
||||
added: added_transactions,
|
||||
modified: modified_transactions,
|
||||
removed: []
|
||||
})
|
||||
|
||||
mock_processor = mock("PlaidEntry::Processor")
|
||||
category_matcher_mock = mock("PlaidAccount::Transactions::CategoryMatcher")
|
||||
|
||||
PlaidAccount::Transactions::CategoryMatcher.stubs(:new).returns(category_matcher_mock)
|
||||
PlaidEntry::Processor.expects(:new)
|
||||
.with(added_transactions.first, plaid_account: @plaid_account, category_matcher: category_matcher_mock)
|
||||
.returns(mock_processor)
|
||||
.once
|
||||
|
||||
PlaidEntry::Processor.expects(:new)
|
||||
.with(modified_transactions.first, plaid_account: @plaid_account, category_matcher: category_matcher_mock)
|
||||
.returns(mock_processor)
|
||||
.once
|
||||
|
||||
mock_processor.expects(:process).twice
|
||||
|
||||
processor = PlaidAccount::Transactions::Processor.new(@plaid_account)
|
||||
processor.process
|
||||
end
|
||||
|
||||
test "removes transactions no longer in plaid" do
|
||||
destroyable_transaction_id = "destroy_me"
|
||||
@plaid_account.account.entries.create!(
|
||||
plaid_id: destroyable_transaction_id,
|
||||
date: Date.current,
|
||||
amount: 100,
|
||||
name: "Destroy me",
|
||||
currency: "USD",
|
||||
entryable: Transaction.new
|
||||
)
|
||||
|
||||
@plaid_account.update!(raw_transactions_payload: {
|
||||
added: [],
|
||||
modified: [],
|
||||
removed: [ { "transaction_id" => destroyable_transaction_id } ]
|
||||
})
|
||||
|
||||
processor = PlaidAccount::Transactions::Processor.new(@plaid_account)
|
||||
|
||||
assert_difference [ "Entry.count", "Transaction.count" ], -1 do
|
||||
processor.process
|
||||
end
|
||||
|
||||
assert_nil Entry.find_by(plaid_id: destroyable_transaction_id)
|
||||
end
|
||||
end
|
35
test/models/plaid_account/type_mappable_test.rb
Normal file
35
test/models/plaid_account/type_mappable_test.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidAccount::TypeMappableTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
class MockProcessor
|
||||
include PlaidAccount::TypeMappable
|
||||
end
|
||||
|
||||
@mock_processor = MockProcessor.new
|
||||
end
|
||||
|
||||
test "maps types to accountables" do
|
||||
assert_instance_of Depository, @mock_processor.map_accountable("depository")
|
||||
assert_instance_of Investment, @mock_processor.map_accountable("investment")
|
||||
assert_instance_of CreditCard, @mock_processor.map_accountable("credit")
|
||||
assert_instance_of Loan, @mock_processor.map_accountable("loan")
|
||||
assert_instance_of OtherAsset, @mock_processor.map_accountable("other")
|
||||
end
|
||||
|
||||
test "maps subtypes" do
|
||||
assert_equal "checking", @mock_processor.map_subtype("depository", "checking")
|
||||
assert_equal "roth_ira", @mock_processor.map_subtype("investment", "roth")
|
||||
end
|
||||
|
||||
test "raises on invalid types" do
|
||||
assert_raises PlaidAccount::TypeMappable::UnknownAccountTypeError do
|
||||
@mock_processor.map_accountable("unknown")
|
||||
end
|
||||
end
|
||||
|
||||
test "handles nil subtypes" do
|
||||
assert_equal "other", @mock_processor.map_subtype("depository", nil)
|
||||
assert_equal "other", @mock_processor.map_subtype("depository", "unknown")
|
||||
end
|
||||
end
|
91
test/models/plaid_entry/processor_test.rb
Normal file
91
test/models/plaid_entry/processor_test.rb
Normal file
|
@ -0,0 +1,91 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidEntry::ProcessorTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
@category_matcher = mock("PlaidAccount::Transactions::CategoryMatcher")
|
||||
end
|
||||
|
||||
test "creates new entry transaction" do
|
||||
plaid_transaction = {
|
||||
"transaction_id" => "123",
|
||||
"merchant_name" => "Amazon", # this is used for merchant and entry name
|
||||
"amount" => 100,
|
||||
"date" => Date.current,
|
||||
"iso_currency_code" => "USD",
|
||||
"personal_finance_category" => {
|
||||
"detailed" => "Food"
|
||||
},
|
||||
"merchant_entity_id" => "123"
|
||||
}
|
||||
|
||||
@category_matcher.expects(:match).with("Food").returns(categories(:food_and_drink))
|
||||
|
||||
processor = PlaidEntry::Processor.new(
|
||||
plaid_transaction,
|
||||
plaid_account: @plaid_account,
|
||||
category_matcher: @category_matcher
|
||||
)
|
||||
|
||||
assert_difference [ "Entry.count", "Transaction.count", "ProviderMerchant.count" ], 1 do
|
||||
processor.process
|
||||
end
|
||||
|
||||
entry = Entry.order(created_at: :desc).first
|
||||
|
||||
assert_equal 100, entry.amount
|
||||
assert_equal "USD", entry.currency
|
||||
assert_equal Date.current, entry.date
|
||||
assert_equal "Amazon", entry.name
|
||||
assert_equal categories(:food_and_drink).id, entry.transaction.category_id
|
||||
|
||||
provider_merchant = ProviderMerchant.order(created_at: :desc).first
|
||||
|
||||
assert_equal "Amazon", provider_merchant.name
|
||||
end
|
||||
|
||||
test "updates existing entry transaction" do
|
||||
existing_plaid_id = "existing_plaid_id"
|
||||
|
||||
plaid_transaction = {
|
||||
"transaction_id" => existing_plaid_id,
|
||||
"merchant_name" => "Amazon", # this is used for merchant and entry name
|
||||
"amount" => 200, # Changed amount will be updated
|
||||
"date" => 1.day.ago.to_date, # Changed date will be updated
|
||||
"iso_currency_code" => "USD",
|
||||
"personal_finance_category" => {
|
||||
"detailed" => "Food"
|
||||
}
|
||||
}
|
||||
|
||||
@category_matcher.expects(:match).with("Food").returns(categories(:food_and_drink))
|
||||
|
||||
# Create an existing entry
|
||||
@plaid_account.account.entries.create!(
|
||||
plaid_id: existing_plaid_id,
|
||||
amount: 100,
|
||||
currency: "USD",
|
||||
date: Date.current,
|
||||
name: "Amazon",
|
||||
entryable: Transaction.new
|
||||
)
|
||||
|
||||
processor = PlaidEntry::Processor.new(
|
||||
plaid_transaction,
|
||||
plaid_account: @plaid_account,
|
||||
category_matcher: @category_matcher
|
||||
)
|
||||
|
||||
assert_no_difference [ "Entry.count", "Transaction.count", "ProviderMerchant.count" ] do
|
||||
processor.process
|
||||
end
|
||||
|
||||
entry = Entry.order(created_at: :desc).first
|
||||
|
||||
assert_equal 200, entry.amount
|
||||
assert_equal "USD", entry.currency
|
||||
assert_equal 1.day.ago.to_date, entry.date
|
||||
assert_equal "Amazon", entry.name
|
||||
assert_equal categories(:food_and_drink).id, entry.transaction.category_id
|
||||
end
|
||||
end
|
|
@ -1,82 +0,0 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidInvestmentSyncTest < ActiveSupport::TestCase
|
||||
include PlaidTestHelper
|
||||
|
||||
setup do
|
||||
@plaid_account = plaid_accounts(:one)
|
||||
end
|
||||
|
||||
test "syncs basic investments and handles cash holding" do
|
||||
assert_equal 0, @plaid_account.account.entries.count
|
||||
assert_equal 0, @plaid_account.account.holdings.count
|
||||
|
||||
plaid_aapl_id = "aapl_id"
|
||||
|
||||
transactions = [
|
||||
create_plaid_investment_transaction({
|
||||
investment_transaction_id: "inv_txn_1",
|
||||
security_id: plaid_aapl_id,
|
||||
quantity: 10,
|
||||
price: 200,
|
||||
date: 5.days.ago.to_date,
|
||||
type: "buy"
|
||||
})
|
||||
]
|
||||
|
||||
holdings = [
|
||||
create_plaid_cash_holding,
|
||||
create_plaid_holding({
|
||||
security_id: plaid_aapl_id,
|
||||
quantity: 10,
|
||||
institution_price: 200,
|
||||
cost_basis: 2000
|
||||
})
|
||||
]
|
||||
|
||||
securities = [
|
||||
create_plaid_security({
|
||||
security_id: plaid_aapl_id,
|
||||
close_price: 200,
|
||||
ticker_symbol: "AAPL"
|
||||
})
|
||||
]
|
||||
|
||||
# Cash holding should be ignored, resulting in 1, NOT 2 total holdings after sync
|
||||
assert_difference -> { Trade.count } => 1,
|
||||
-> { Transaction.count } => 0,
|
||||
-> { Holding.count } => 1,
|
||||
-> { Security.count } => 0 do
|
||||
PlaidInvestmentSync.new(@plaid_account).sync!(
|
||||
transactions: transactions,
|
||||
holdings: holdings,
|
||||
securities: securities
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
# Some cash transactions from Plaid are labeled as type: "cash" while others are linked to a "cash" security
|
||||
# In both cases, we should treat them as cash-only transactions (not trades)
|
||||
test "handles cash investment transactions" do
|
||||
transactions = [
|
||||
create_plaid_investment_transaction({
|
||||
price: 1,
|
||||
quantity: 5,
|
||||
amount: 5,
|
||||
type: "fee",
|
||||
subtype: "miscellaneous fee",
|
||||
security_id: PLAID_TEST_CASH_SECURITY_ID
|
||||
})
|
||||
]
|
||||
|
||||
assert_difference -> { Trade.count } => 0,
|
||||
-> { Transaction.count } => 1,
|
||||
-> { Security.count } => 0 do
|
||||
PlaidInvestmentSync.new(@plaid_account).sync!(
|
||||
transactions: transactions,
|
||||
holdings: [ create_plaid_cash_holding ],
|
||||
securities: [ create_plaid_cash_security ]
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
23
test/models/plaid_item/importer_test.rb
Normal file
23
test/models/plaid_item/importer_test.rb
Normal file
|
@ -0,0 +1,23 @@
|
|||
require "test_helper"
|
||||
require "ostruct"
|
||||
|
||||
class PlaidItem::ImporterTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@mock_provider = PlaidMock.new
|
||||
@plaid_item = plaid_items(:one)
|
||||
@importer = PlaidItem::Importer.new(@plaid_item, plaid_provider: @mock_provider)
|
||||
end
|
||||
|
||||
test "imports item metadata" do
|
||||
PlaidAccount::Importer.any_instance.expects(:import).times(PlaidMock::ACCOUNTS.count)
|
||||
|
||||
PlaidItem::Importer.new(@plaid_item, plaid_provider: @mock_provider).import
|
||||
|
||||
assert_equal PlaidMock::ITEM.institution_id, @plaid_item.institution_id
|
||||
assert_equal PlaidMock::ITEM.available_products, @plaid_item.available_products
|
||||
assert_equal PlaidMock::ITEM.billed_products, @plaid_item.billed_products
|
||||
|
||||
assert_equal PlaidMock::ITEM.item_id, @plaid_item.raw_payload["item_id"]
|
||||
assert_equal PlaidMock::INSTITUTION.institution_id, @plaid_item.raw_institution_payload["institution_id"]
|
||||
end
|
||||
end
|
|
@ -5,11 +5,11 @@ class PlaidItemTest < ActiveSupport::TestCase
|
|||
|
||||
setup do
|
||||
@plaid_item = @syncable = plaid_items(:one)
|
||||
@plaid_provider = mock
|
||||
Provider::Registry.stubs(:plaid_provider_for_region).returns(@plaid_provider)
|
||||
end
|
||||
|
||||
test "removes plaid item when destroyed" do
|
||||
@plaid_provider = mock
|
||||
@plaid_item.stubs(:plaid_provider).returns(@plaid_provider)
|
||||
@plaid_provider.expects(:remove_item).with(@plaid_item.access_token).once
|
||||
|
||||
assert_difference "PlaidItem.count", -1 do
|
||||
|
@ -18,8 +18,6 @@ class PlaidItemTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
test "if plaid item not found, silently continues with deletion" do
|
||||
@plaid_provider = mock
|
||||
@plaid_item.stubs(:plaid_provider).returns(@plaid_provider)
|
||||
@plaid_provider.expects(:remove_item).with(@plaid_item.access_token).raises(Plaid::ApiError.new("Item not found"))
|
||||
|
||||
assert_difference "PlaidItem.count", -1 do
|
||||
|
|
80
test/models/provider/plaid_test.rb
Normal file
80
test/models/provider/plaid_test.rb
Normal file
|
@ -0,0 +1,80 @@
|
|||
require "test_helper"
|
||||
|
||||
class Provider::PlaidTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
# Do not change, this is whitelisted in the Plaid Dashboard for local dev
|
||||
@redirect_url = "http://localhost:3000/accounts"
|
||||
|
||||
# A specialization of Plaid client with sandbox-only extensions
|
||||
@plaid = Provider::PlaidSandbox.new
|
||||
end
|
||||
|
||||
test "gets link token" do
|
||||
VCR.use_cassette("plaid/link_token") do
|
||||
link_token = @plaid.get_link_token(
|
||||
user_id: "test-user-id",
|
||||
webhooks_url: "https://example.com/webhooks",
|
||||
redirect_url: @redirect_url
|
||||
)
|
||||
|
||||
assert_match /link-sandbox-.*/, link_token.link_token
|
||||
end
|
||||
end
|
||||
|
||||
test "exchanges public token" do
|
||||
VCR.use_cassette("plaid/exchange_public_token") do
|
||||
public_token = @plaid.create_public_token
|
||||
exchange_response = @plaid.exchange_public_token(public_token)
|
||||
|
||||
assert_match /access-sandbox-.*/, exchange_response.access_token
|
||||
end
|
||||
end
|
||||
|
||||
test "gets item" do
|
||||
VCR.use_cassette("plaid/get_item") do
|
||||
access_token = get_access_token
|
||||
item = @plaid.get_item(access_token).item
|
||||
|
||||
assert_equal "ins_109508", item.institution_id
|
||||
assert_equal "First Platypus Bank", item.institution_name
|
||||
end
|
||||
end
|
||||
|
||||
test "gets item accounts" do
|
||||
VCR.use_cassette("plaid/get_item_accounts") do
|
||||
access_token = get_access_token
|
||||
accounts_response = @plaid.get_item_accounts(access_token)
|
||||
|
||||
assert_equal 4, accounts_response.accounts.size
|
||||
end
|
||||
end
|
||||
|
||||
test "gets item investments" do
|
||||
VCR.use_cassette("plaid/get_item_investments") do
|
||||
access_token = get_access_token
|
||||
investments_response = @plaid.get_item_investments(access_token)
|
||||
|
||||
assert_equal 3, investments_response.holdings.size
|
||||
assert_equal 4, investments_response.transactions.size
|
||||
end
|
||||
end
|
||||
|
||||
test "gets item liabilities" do
|
||||
VCR.use_cassette("plaid/get_item_liabilities") do
|
||||
access_token = get_access_token
|
||||
liabilities_response = @plaid.get_item_liabilities(access_token)
|
||||
|
||||
assert liabilities_response.credit.count > 0
|
||||
assert liabilities_response.student.count > 0
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def get_access_token
|
||||
VCR.use_cassette("plaid/access_token") do
|
||||
public_token = @plaid.create_public_token
|
||||
exchange_response = @plaid.exchange_public_token(public_token)
|
||||
exchange_response.access_token
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue