Files
sure/test/models/plaid_account/investments/transactions_processor_test.rb
soky srm 4fb0a3856e Providers factory (#250)
* Implement providers factory

* Multiple providers sync support

- Proper Multi-Provider Syncing: When you click sync on an account with multiple providers (e.g., both Plaid and SimpleFin), all provider items are synced
- Better API: The existing account.providers method already returns all providers, and account.provider returns the first one for backward compatibility
- Correct Holdings Deletion Logic: Holdings can only be deleted if ALL providers allow it, preventing accidental deletions that would be recreated on next sync
TODO: validate this is the way we want to go? We would need to check holdings belong to which account, and then check provider allows deletion. More complex
- Database Constraints: The existing validations ensure an account can have at most one provider of each type (one PlaidAccount, one SimplefinAccount, etc.)

* Add generic provider_import_adapter

* Finish unified import strategy

* Update app/models/plaid_account.rb

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: soky srm <sokysrm@gmail.com>

* Update app/models/provider/factory.rb

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Signed-off-by: soky srm <sokysrm@gmail.com>

* Fix account linked by plaid_id instead of external_id

* Parse numerics to BigDecimal

Parse numerics to BigDecimal before computing amount; guard nils.

Avoid String * String and float drift; also normalize date.

* Fix incorrect usage of assert_raises.

* Fix linter

* Fix processor test.

* Update current_balance_manager.rb

* Test fixes

* Fix plaid linked account test

* Add support for holding per account_provider

* Fix proper account access

Also fix account deletion for simpefin too

* FIX match tests for consistency

* Some more factory updates

* Fix account schema for multipe providers

  Can do:
  - Account #1 → PlaidAccount + SimplefinAccount (multiple different providers)
  - Account #2 → PlaidAccount only
  - Account #3 → SimplefinAccount only

  Cannot do:
  - Account #1 → PlaidAccount + PlaidAccount (duplicate provider type)
  - PlaidAccount #123 → Account #1 + Account #2 (provider linked to multiple accounts)

* Fix account setup

- An account CAN have multiple providers (the schema shows account_providers with unique index on [account_id, provider_type])
  - Each provider should maintain its own separate entries
  - We should NOT update one provider's entry when another provider syncs

* Fix linter and guard migration

* FIX linter issues.

* Fixes

- Remove duplicated index
- Pass account_provider_id
- Guard holdings call to avoid NoMethodError

* Update schema and provider import fix

* Plaid doesn't allow holdings deletion

* Use ClimateControl for proper env setup

* No need for this in .git

---------

Signed-off-by: soky srm <sokysrm@gmail.com>
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Juan José Mata <juanjo.mata@gmail.com>
2025-10-28 19:32:27 +01:00

185 lines
5.5 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: [
{
"investment_transaction_id" => "123",
"security_id" => "123",
"type" => "buy",
"quantity" => 1, # Positive, so "buy 1 share"
"price" => 100,
"amount" => 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: [
{
"investment_transaction_id" => "cash_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: [
{
"investment_transaction_id" => "fee_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
test "handles bad plaid quantity signage data" do
test_investments_payload = {
transactions: [
{
"investment_transaction_id" => "123",
"security_id" => "123",
"type" => "sell", # Correct type
"subtype" => "sell", # Correct subtype
"quantity" => 1, # ***Incorrect signage***, this should be negative
"price" => 100, # Correct price
"amount" => -100, # Correct amount
"iso_currency_code" => "USD",
"date" => Date.current,
"name" => "Sell 1 share of AAPL"
}
]
}
@plaid_account.update!(raw_investments_payload: test_investments_payload)
@security_resolver.expects(: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 "Sell 1 share of AAPL", entry.name
assert_equal -1, entry.trade.qty
end
test "creates transfer transactions as cash transactions" do
test_investments_payload = {
transactions: [
{
"investment_transaction_id" => "123",
"type" => "transfer",
"amount" => -100.0,
"iso_currency_code" => "USD",
"date" => Date.current,
"name" => "Bank Transfer"
}
]
}
@plaid_account.update!(raw_investments_payload: test_investments_payload)
@security_resolver.expects(:resolve).never
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.0, entry.amount
assert_equal "USD", entry.currency
assert_equal Date.current, entry.date
assert_equal "Bank Transfer", entry.name
assert_instance_of Transaction, entry.entryable
end
end