mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-08-05 05:25:24 +02:00
Only fetch needed Plaid products, improve Plaid tests and mocks
This commit is contained in:
parent
03a146222d
commit
6935ffa3d1
5 changed files with 188 additions and 246 deletions
105
test/models/plaid_item/accounts_snapshot_test.rb
Normal file
105
test/models/plaid_item/accounts_snapshot_test.rb
Normal file
|
@ -0,0 +1,105 @@
|
|||
require "test_helper"
|
||||
|
||||
class PlaidItem::AccountsSnapshotTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@plaid_item = plaid_items(:one)
|
||||
@plaid_item.plaid_accounts.destroy_all # Clean slate
|
||||
|
||||
@plaid_provider = mock
|
||||
@snapshot = PlaidItem::AccountsSnapshot.new(@plaid_item, plaid_provider: @plaid_provider)
|
||||
end
|
||||
|
||||
test "fetches accounts" do
|
||||
@plaid_provider.expects(:get_item_accounts).with(@plaid_item.access_token).returns(
|
||||
OpenStruct.new(accounts: [])
|
||||
)
|
||||
@snapshot.accounts
|
||||
end
|
||||
|
||||
test "fetches transactions data if item supports transactions and any accounts present" do
|
||||
@plaid_item.update!(available_products: [ "transactions" ], billed_products: [])
|
||||
|
||||
@snapshot.expects(:accounts).returns([
|
||||
OpenStruct.new(
|
||||
account_id: "123",
|
||||
type: "depository"
|
||||
)
|
||||
]).at_least_once
|
||||
|
||||
@plaid_provider.expects(:get_transactions).with(@plaid_item.access_token).once
|
||||
@plaid_provider.expects(:get_item_investments).never
|
||||
@plaid_provider.expects(:get_item_liabilities).never
|
||||
|
||||
@snapshot.get_account_data("123")
|
||||
end
|
||||
|
||||
test "does not fetch transactions if no accounts" do
|
||||
@plaid_item.update!(available_products: [ "transactions" ], billed_products: [])
|
||||
|
||||
@snapshot.expects(:accounts).returns([]).at_least_once
|
||||
|
||||
@plaid_provider.expects(:get_transactions).never
|
||||
@plaid_provider.expects(:get_item_investments).never
|
||||
@plaid_provider.expects(:get_item_liabilities).never
|
||||
|
||||
@snapshot.get_account_data("123")
|
||||
end
|
||||
|
||||
test "fetches investments data if item supports investments and investment accounts present" do
|
||||
@plaid_item.update!(available_products: [ "investments" ], billed_products: [])
|
||||
|
||||
@snapshot.expects(:accounts).returns([
|
||||
OpenStruct.new(
|
||||
account_id: "123",
|
||||
type: "investment"
|
||||
)
|
||||
]).at_least_once
|
||||
|
||||
@plaid_provider.expects(:get_transactions).never
|
||||
@plaid_provider.expects(:get_item_investments).with(@plaid_item.access_token).once
|
||||
@plaid_provider.expects(:get_item_liabilities).never
|
||||
|
||||
@snapshot.get_account_data("123")
|
||||
end
|
||||
|
||||
test "does not fetch investments if no investment accounts" do
|
||||
@plaid_item.update!(available_products: [ "investments" ], billed_products: [])
|
||||
|
||||
@snapshot.expects(:accounts).returns([]).at_least_once
|
||||
|
||||
@plaid_provider.expects(:get_transactions).never
|
||||
@plaid_provider.expects(:get_item_investments).never
|
||||
@plaid_provider.expects(:get_item_liabilities).never
|
||||
|
||||
@snapshot.get_account_data("123")
|
||||
end
|
||||
|
||||
test "fetches liabilities data if item supports liabilities and liabilities accounts present" do
|
||||
@plaid_item.update!(available_products: [ "liabilities" ], billed_products: [])
|
||||
|
||||
@snapshot.expects(:accounts).returns([
|
||||
OpenStruct.new(
|
||||
account_id: "123",
|
||||
type: "credit"
|
||||
)
|
||||
]).at_least_once
|
||||
|
||||
@plaid_provider.expects(:get_transactions).never
|
||||
@plaid_provider.expects(:get_item_investments).never
|
||||
@plaid_provider.expects(:get_item_liabilities).with(@plaid_item.access_token).once
|
||||
|
||||
@snapshot.get_account_data("123")
|
||||
end
|
||||
|
||||
test "does not fetch liabilities if no liabilities accounts" do
|
||||
@plaid_item.update!(available_products: [ "liabilities" ], billed_products: [])
|
||||
|
||||
@snapshot.expects(:accounts).returns([]).at_least_once
|
||||
|
||||
@plaid_provider.expects(:get_transactions).never
|
||||
@plaid_provider.expects(:get_item_investments).never
|
||||
@plaid_provider.expects(:get_item_liabilities).never
|
||||
|
||||
@snapshot.get_account_data("123")
|
||||
end
|
||||
end
|
|
@ -3,21 +3,47 @@ require "ostruct"
|
|||
|
||||
class PlaidItem::ImporterTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@mock_provider = PlaidMock.new
|
||||
@mock_provider = mock("Provider::Plaid")
|
||||
@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)
|
||||
item_data = OpenStruct.new(
|
||||
item_id: "item_1",
|
||||
available_products: [ "transactions", "investments", "liabilities" ],
|
||||
billed_products: [],
|
||||
institution_id: "ins_1",
|
||||
institution_name: "First Platypus Bank",
|
||||
)
|
||||
|
||||
PlaidItem::Importer.new(@plaid_item, plaid_provider: @mock_provider).import
|
||||
@mock_provider.expects(:get_item).with(@plaid_item.access_token).returns(
|
||||
OpenStruct.new(item: item_data)
|
||||
)
|
||||
|
||||
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
|
||||
institution_data = OpenStruct.new(
|
||||
institution_id: "ins_1",
|
||||
institution_name: "First Platypus Bank",
|
||||
)
|
||||
|
||||
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"]
|
||||
@mock_provider.expects(:get_institution).with("ins_1").returns(
|
||||
OpenStruct.new(institution: institution_data)
|
||||
)
|
||||
|
||||
PlaidItem::AccountsSnapshot.any_instance.expects(:accounts).returns([
|
||||
OpenStruct.new(
|
||||
account_id: "acc_1",
|
||||
type: "depository"
|
||||
)
|
||||
]).at_least_once
|
||||
|
||||
PlaidItem::AccountsSnapshot.any_instance.expects(:get_account_data).with("acc_1").once
|
||||
|
||||
PlaidAccount::Importer.any_instance.expects(:import).once
|
||||
|
||||
@plaid_item.expects(:upsert_plaid_snapshot!).with(item_data)
|
||||
@plaid_item.expects(:upsert_plaid_institution_snapshot!).with(institution_data)
|
||||
|
||||
@importer.import
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue