Back to Repositories

Testing CSV Upload Controller Functionality in Maybe Finance

This test suite evaluates the functionality of CSV file uploads in the Maybe Finance application, focusing on both manual input and file upload methods. The tests verify proper handling of valid and invalid CSV data, ensuring robust import functionality for financial transactions.

Test Coverage Overview

The test suite provides comprehensive coverage of the CSV upload functionality through the UploadsController.

Key areas tested include:
  • Basic route accessibility for the upload interface
  • CSV data upload via copy-paste functionality
  • Direct file upload handling
  • Validation of CSV file contents and structure
Integration points cover the connection between file upload processing and subsequent import configuration.

Implementation Analysis

The testing approach utilizes Minitest’s integration testing capabilities within the Rails framework. The implementation follows RESTful patterns for handling file uploads, with specific attention to different input methods.

Key patterns include:
  • Controller-level integration testing
  • File fixture usage for test data
  • Flash message verification
  • Response status validation

Technical Details

Testing tools and configuration:
  • Minitest framework with ActionDispatch::IntegrationTest
  • Rails file_fixture for test data management
  • User authentication setup via sign_in helper
  • CSV file handling with different column separators
  • Flash message system for user feedback

Best Practices Demonstrated

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

Notable practices include:
  • Proper test setup and user authentication
  • Comprehensive error case handling
  • Clear test case organization
  • Effective use of fixture data
  • Validation of both success and failure scenarios

maybe-finance/maybe

test/controllers/import/uploads_controller_test.rb

            
require "test_helper"

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

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

  test "uploads valid csv by copy and pasting" do
    patch import_upload_url(@import), params: {
      import: {
        raw_file_str: file_fixture("imports/valid.csv").read,
        col_sep: ","
      }
    }

    assert_redirected_to import_configuration_url(@import)
    assert_equal "CSV uploaded successfully.", flash[:notice]
  end

  test "uploads valid csv by file" do
    patch import_upload_url(@import), params: {
      import: {
        csv_file: file_fixture_upload("imports/valid.csv"),
        col_sep: ","
      }
    }

    assert_redirected_to import_configuration_url(@import)
    assert_equal "CSV uploaded successfully.", flash[:notice]
  end

  test "invalid csv cannot be uploaded" do
    patch import_upload_url(@import), params: {
      import: {
        csv_file: file_fixture_upload("imports/invalid.csv"),
        col_sep: ","
      }
    }

    assert_response :unprocessable_entity
    assert_equal "Must be valid CSV with headers and at least one row of data", flash[:alert]
  end
end