mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-25 08:09:38 +02:00
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
111 lines
3.3 KiB
Ruby
111 lines
3.3 KiB
Ruby
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
|