Back to Repositories

Testing Import Configuration Controller Workflow in Maybe Finance

This test suite validates the functionality of import configurations in the Maybe Finance application, focusing on transaction data import settings and CSV processing. The tests ensure proper handling of import configurations and row regeneration based on user-defined parameters.

Test Coverage Overview

The test suite covers essential import configuration operations including:

  • Basic configuration display functionality
  • CSV data processing with column mapping
  • Transaction import row generation
  • Response handling and redirects

Implementation Analysis

The testing approach utilizes Minitest within a Rails integration test context, employing user authentication and fixture data. The implementation leverages mocking with the ‘expects’ method to verify CSV processing behavior without executing actual file operations.

Technical Details

Testing tools and configuration include:

  • ActionDispatch::IntegrationTest framework
  • Fixture-based test data setup
  • Mock expectations for TransactionImport
  • User authentication simulation
  • HTTP request assertion methods

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test setup and user authentication
  • Isolated test cases with clear purposes
  • Effective use of fixtures for test data
  • Verification of both successful and error paths
  • Clear assertion messages and response validation

maybe-finance/maybe

test/controllers/import/configurations_controller_test.rb

            
require "test_helper"

class Import::ConfigurationsControllerTest < ActionDispatch::IntegrationTest
  setup do
    sign_in @user = users(:family_admin)
    @import = imports(:transaction)
  end

  test "show" do
    get import_configuration_url(@import)
    assert_response :success
  end

  test "updating a valid configuration regenerates rows" do
    TransactionImport.any_instance.expects(:generate_rows_from_csv).once

    patch import_configuration_url(@import), params: {
      import: {
        date_col_label: "Date",
        date_format: "%Y-%m-%d",
        name_col_label: "Name",
        category_col_label: "Category",
        tags_col_label: "Tags",
        amount_col_label: "Amount",
        signage_convention: "inflows_positive",
        account_col_label: "Account"
      }
    }

    assert_redirected_to import_clean_url(@import)
    assert_equal "Import configured successfully.", flash[:notice]
  end
end