Back to Repositories

Testing Import Row Controller Operations in Maybe Finance

This test suite validates the functionality of import row handling in a financial application, covering different types of imports including transactions, trades, accounts, and Mint data. The tests ensure proper display and update capabilities for various import row types with their specific fields.

Test Coverage Overview

The test suite provides comprehensive coverage for import row operations across multiple import types.

Key functionality tested includes:
  • Transaction row display and field validation
  • Trade import row handling with stock-specific fields
  • Account import verification with balance fields
  • Mint import data processing
  • Row update operations with field modifications

Implementation Analysis

The testing approach utilizes ActionDispatch::IntegrationTest for end-to-end validation of controller actions. The implementation leverages fixtures for test data setup and employs custom assertions for field validation.

Technical patterns include:
  • Setup hooks for user authentication
  • Fixture-based test data initialization
  • Custom helper methods for field verification
  • Turbo frame validation for dynamic content

Technical Details

Testing tools and configuration:
  • Minitest framework for test execution
  • ActionDispatch for integration testing
  • Fixtures for test data management
  • Custom assert_row_fields helper method
  • Turbo frames for dynamic content validation
  • DOM ID helpers for element identification

Best Practices Demonstrated

The test suite exemplifies several testing best practices for Rails applications.

Notable practices include:
  • Proper test isolation and setup
  • Consistent assertion patterns
  • DRY principle through helper methods
  • Comprehensive edge case coverage
  • Clear test organization by import type
  • Explicit field validation checks

maybe-finance/maybe

test/controllers/import/rows_controller_test.rb

            
require "test_helper"

class Import::RowsControllerTest < ActionDispatch::IntegrationTest
  setup do
    sign_in @user = users(:family_admin)

    @import = imports(:transaction)
    @row = import_rows(:one)
  end

  test "show transaction row" do
    get import_row_path(@import, @row)

    assert_row_fields(@row, [ :date, :name, :amount, :currency, :category, :tags, :account, :notes ])

    assert_response :success
  end

  test "show trade row" do
    import = @user.family.imports.create!(type: "TradeImport")
    row = import.rows.create!(date: "01/01/2024", currency: "USD", qty: 10, price: 100, ticker: "AAPL")

    get import_row_path(import, row)

    assert_row_fields(row, [ :date, :ticker, :qty, :price, :currency, :account, :name ])

    assert_response :success
  end

  test "show account row" do
    import = @user.family.imports.create!(type: "AccountImport")
    row = import.rows.create!(name: "Test Account", amount: 10000, currency: "USD")

    get import_row_path(import, row)

    assert_row_fields(row, [ :entity_type, :name, :amount, :currency ])

    assert_response :success
  end

  test "show mint row" do
    import = @user.family.imports.create!(type: "MintImport")
    row = import.rows.create!(date: "01/01/2024", amount: 100, currency: "USD")

    get import_row_path(import, row)

    assert_row_fields(row, [ :date, :name, :amount, :currency, :category, :tags, :account, :notes ])

    assert_response :success
  end

  test "update" do
    patch import_row_path(@import, @row), params: {
      import_row: {
        account: "Checking Account",
        date: "2024-01-01",
        qty: nil,
        ticker: nil,
        price: nil,
        amount: 100,
        currency: "USD",
        name: "Test",
        category: "Food",
        tags: "grocery, dinner",
        entity_type: nil,
        notes: "Weekly shopping"
      }
    }

    assert_redirected_to import_row_path(@import, @row)
  end

  private
    def assert_row_fields(row, fields)
      fields.each do |field|
        assert_select "turbo-frame##{dom_id(row, field)}"
      end
    end
end