Add tests for SimpleFin integration

- Add SimplefinItem model tests with fixtures
- Add SimplefinAccount model tests
- Add SimplefinItemsController tests
- Include test coverage for sync and account creation
This commit is contained in:
Sholom Ber
2025-08-07 12:40:41 -04:00
parent 71d720b25b
commit 2caba0a00b
3 changed files with 186 additions and 0 deletions

View File

@@ -0,0 +1,45 @@
require "test_helper"
class SimplefinItemsControllerTest < ActionDispatch::IntegrationTest
setup do
sign_in users(:dylan)
@family = families(:dylan_family)
@simplefin_item = SimplefinItem.create!(
family: @family,
name: "Test Connection",
access_url: "https://example.com/test_access"
)
end
test "should get index" do
get simplefin_items_url
assert_response :success
assert_includes response.body, @simplefin_item.name
end
test "should get new" do
get new_simplefin_item_url
assert_response :success
end
test "should show simplefin item" do
get simplefin_item_url(@simplefin_item)
assert_response :success
end
test "should destroy simplefin item" do
assert_difference("SimplefinItem.count", 0) do # doesn't actually delete immediately
delete simplefin_item_url(@simplefin_item)
end
assert_redirected_to simplefin_items_path
@simplefin_item.reload
assert @simplefin_item.scheduled_for_deletion?
end
test "should sync simplefin item" do
post sync_simplefin_item_url(@simplefin_item)
assert_redirected_to simplefin_item_path(@simplefin_item)
assert_equal "Sync started", flash[:notice]
end
end

View File

@@ -0,0 +1,77 @@
require "test_helper"
class SimplefinAccountTest < ActiveSupport::TestCase
setup do
@family = families(:dylan_family)
@simplefin_item = SimplefinItem.create!(
family: @family,
name: "Test SimpleFin Connection",
access_url: "https://example.com/access_token"
)
@simplefin_account = SimplefinAccount.create!(
simplefin_item: @simplefin_item,
name: "Test Checking Account",
account_id: "test_checking_123",
currency: "USD",
account_type: "checking",
current_balance: 1500.50
)
end
test "belongs to simplefin_item" do
assert_equal @simplefin_item, @simplefin_account.simplefin_item
end
test "validates presence of required fields" do
account = SimplefinAccount.new
refute account.valid?
assert_includes account.errors[:name], "can't be blank"
assert_includes account.errors[:account_type], "can't be blank"
assert_includes account.errors[:currency], "can't be blank"
end
test "validates balance presence" do
account = SimplefinAccount.new(
simplefin_item: @simplefin_item,
name: "No Balance Account",
account_id: "no_balance_123",
currency: "USD",
account_type: "checking"
)
refute account.valid?
assert_includes account.errors[:base], "SimpleFin account must have either current or available balance"
end
test "can upsert snapshot data" do
snapshot = {
balance: 2000.00,
available_balance: 1800.00,
currency: "USD",
type: "savings",
subtype: "savings",
name: "Updated Savings Account",
id: "updated_123"
}
@simplefin_account.upsert_simplefin_snapshot!(snapshot)
assert_equal 2000.00, @simplefin_account.current_balance
assert_equal 1800.00, @simplefin_account.available_balance
assert_equal "savings", @simplefin_account.account_type
assert_equal "Updated Savings Account", @simplefin_account.name
assert_equal snapshot, @simplefin_account.raw_payload
end
test "can upsert transactions" do
transactions = [
{ id: "txn_1", amount: -50.00, description: "Coffee Shop", posted: "2024-01-01" },
{ id: "txn_2", amount: 1000.00, description: "Paycheck", posted: "2024-01-02" }
]
@simplefin_account.upsert_simplefin_transactions_snapshot!(transactions)
assert_equal transactions, @simplefin_account.raw_transactions_payload
end
end

View File

@@ -0,0 +1,64 @@
require "test_helper"
class SimplefinItemTest < ActiveSupport::TestCase
setup do
@family = families(:dylan_family)
@simplefin_item = SimplefinItem.create!(
family: @family,
name: "Test SimpleFin Connection",
access_url: "https://example.com/access_token"
)
end
test "belongs to family" do
assert_equal @family, @simplefin_item.family
end
test "has many simplefin_accounts" do
account = @simplefin_item.simplefin_accounts.create!(
name: "Test Account",
account_id: "test_123",
currency: "USD",
account_type: "checking",
current_balance: 1000.00
)
assert_includes @simplefin_item.simplefin_accounts, account
end
test "has good status by default" do
assert_equal "good", @simplefin_item.status
end
test "can be marked for deletion" do
refute @simplefin_item.scheduled_for_deletion?
@simplefin_item.destroy_later
assert @simplefin_item.scheduled_for_deletion?
end
test "is syncable" do
assert_respond_to @simplefin_item, :sync_later
assert_respond_to @simplefin_item, :syncing?
end
test "scopes work correctly" do
# Create one for deletion
item_for_deletion = SimplefinItem.create!(
family: @family,
name: "Delete Me",
access_url: "https://example.com/delete_token",
scheduled_for_deletion: true
)
active_items = SimplefinItem.active
ordered_items = SimplefinItem.ordered
assert_includes active_items, @simplefin_item
refute_includes active_items, item_for_deletion
assert_equal [ @simplefin_item, item_for_deletion ].sort_by(&:created_at).reverse,
ordered_items.to_a
end
end