Back to Repositories

Validating Account Transfer Operations in Maybe Finance

This test suite validates the Account::Transfer model functionality in the Maybe Finance application, focusing on financial transfer validations between accounts. The tests ensure proper handling of inflow and outflow transactions across different currencies and account types.

Test Coverage Overview

The test suite provides comprehensive coverage of account transfer validation scenarios, including:

  • Basic transfer validation between different accounts
  • Transaction count validation
  • Same-account transfer restrictions
  • Transfer marking requirements
  • Currency balance validation for single and multi-currency transfers

Implementation Analysis

The testing approach utilizes Minitest’s ActiveSupport::TestCase framework with fixture-based test data setup. The implementation follows Ruby on Rails testing patterns, employing setup blocks for common test data and explicit assertion methods for validation checks.

Tests are structured to verify both positive and negative scenarios, with proper exception handling for invalid cases.

Technical Details

Key technical components include:

  • Minitest testing framework
  • ActiveSupport::TestCase for Rails integration
  • Fixture-based test data
  • ActiveRecord validation testing
  • Exception handling validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test case naming and organization
  • Proper setup and teardown management
  • Comprehensive edge case coverage
  • Explicit assertion usage
  • Isolated test scenarios

maybe-finance/maybe

test/models/account/transfer_test.rb

            
require "test_helper"

class Account::TransferTest < ActiveSupport::TestCase
  setup do
    @outflow = account_entries(:transfer_out)
    @inflow = account_entries(:transfer_in)
  end

  test "transfer valid if it has inflow and outflow from different accounts for the same amount" do
    transfer = Account::Transfer.create! entries: [ @inflow, @outflow ]

    assert transfer.valid?
  end

  test "transfer must have 2 transactions" do
    invalid_transfer_1 = Account::Transfer.new entries: [ @outflow ]
    invalid_transfer_2 = Account::Transfer.new entries: [ @inflow, @outflow, account_entries(:transaction) ]

    assert invalid_transfer_1.invalid?
    assert invalid_transfer_2.invalid?
  end

  test "transfer cannot have 2 transactions from the same account" do
    account = accounts(:depository)

    inflow = account.entries.create! \
      date: Date.current,
      name: "Inflow",
      amount: -100,
      currency: "USD",
      marked_as_transfer: true,
      entryable: Account::Transaction.new

    outflow = account.entries.create! \
      date: Date.current,
      name: "Outflow",
      amount: 100,
      currency: "USD",
      marked_as_transfer: true,
      entryable: Account::Transaction.new

    assert_raise ActiveRecord::RecordInvalid do
      Account::Transfer.create! entries: [ inflow, outflow ]
    end
  end

  test "all transfer transactions must be marked as transfers" do
    @inflow.update! marked_as_transfer: false

    assert_raise ActiveRecord::RecordInvalid do
      Account::Transfer.create! entries: [ @inflow, @outflow ]
    end
  end

  test "single-currency transfer transactions must net to zero" do
    @outflow.update! amount: 105

    assert_raises ActiveRecord::RecordInvalid do
      Account::Transfer.create! entries: [ @inflow, @outflow ]
    end
  end

  test "multi-currency transfer transactions do not have to net to zero" do
    @outflow.update! amount: 105, currency: "EUR"
    transfer = Account::Transfer.create! entries: [ @inflow, @outflow ]

    assert transfer.valid?
  end
end