Back to Repositories

Testing Import Mapping Updates in maybe-finance

This test suite validates the functionality of import mappings in a financial application, focusing on the ability to update transaction category mappings. The tests ensure proper handling of mapping updates and redirects within the import process.

Test Coverage Overview

The test suite covers the core functionality of updating import mappings for financial transactions.

Key areas tested include:
  • Mapping updates for transaction categories
  • Proper persistence of mapping changes
  • Validation of mapping attributes
  • Redirect flow after successful updates

Implementation Analysis

The testing approach utilizes ActionDispatch::IntegrationTest for comprehensive endpoint testing. The implementation follows Rails testing patterns with fixtures for test data setup and explicit assertions for both data changes and response handling.

Notable patterns include:
  • User authentication setup
  • Fixture-based test data
  • PATCH request validation
  • Model reload verification

Technical Details

Testing infrastructure includes:
  • Minitest framework
  • Rails integration testing tools
  • Fixture data management
  • Authentication helpers
  • HTTP request simulation

Best Practices Demonstrated

The test implementation showcases several testing best practices including proper test setup isolation, explicit assertions, and comprehensive state verification. The code demonstrates clean organization with setup blocks, clear test naming, and thorough validation of both data changes and application flow.

Notable practices:
  • Isolated test setup
  • Multiple assertion points
  • Clear test naming
  • Proper state verification

maybe-finance/maybe

test/controllers/import/mappings_controller_test.rb

            
require "test_helper"

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

    @import = imports(:transaction)
  end

  test "updates mapping" do
    mapping = import_mappings(:one)
    new_category = categories(:income)

    patch import_mapping_path(@import, mapping), params: {
      import_mapping: {
        mappable_type: "Category",
        mappable_id: new_category.id,
        key: "Food"
      }
    }

    mapping.reload

    assert_equal new_category, mapping.mappable
    assert_equal "Food", mapping.key

    assert_redirected_to import_confirm_path(@import)
  end
end