Back to Repositories

Testing Import Interface Implementation in Maybe Finance

This test suite validates the import interface functionality in the Maybe Finance application, focusing on CSV data processing and import state management. The tests ensure proper implementation of core import methods and error handling mechanisms.

Test Coverage Overview

The test suite provides comprehensive coverage of the import interface’s key methods and states.

  • Validates presence of essential interface methods like publish, publish_later, and CSV-related operations
  • Tests import state tracking through methods like uploaded?, configured?, and publishable?
  • Covers asynchronous publishing functionality and error handling scenarios

Implementation Analysis

The testing approach utilizes Minitest with ActiveSupport::Testing::Declarative for structured test organization.

  • Employs stub methods for isolated testing of publishing logic
  • Implements job queue testing for asynchronous operations
  • Uses declarative test syntax for improved readability and maintenance

Technical Details

  • Framework: Minitest with ActiveSupport extensions
  • Mocking: Uses stub methods for dependency isolation
  • Job Testing: Leverages assert_enqueued_with for background job verification
  • Error Handling: Tests exception handling and status management

Best Practices Demonstrated

The test suite exemplifies robust testing practices for interface validation.

  • Comprehensive method availability testing
  • Isolated unit testing through proper stubbing
  • Error case coverage with explicit assertions
  • Clear test organization and naming conventions

maybe-finance/maybe

test/interfaces/import_interface_test.rb

            
require "test_helper"

module ImportInterfaceTest
  extend ActiveSupport::Testing::Declarative

  test "import interface" do
    assert_respond_to @subject, :publish
    assert_respond_to @subject, :publish_later
    assert_respond_to @subject, :generate_rows_from_csv
    assert_respond_to @subject, :csv_rows
    assert_respond_to @subject, :csv_headers
    assert_respond_to @subject, :csv_sample
    assert_respond_to @subject, :uploaded?
    assert_respond_to @subject, :configured?
    assert_respond_to @subject, :cleaned?
    assert_respond_to @subject, :publishable?
    assert_respond_to @subject, :importing?
    assert_respond_to @subject, :complete?
    assert_respond_to @subject, :failed?
  end

  test "publishes later" do
    import = imports(:transaction)

    import.stubs(:publishable?).returns(true)

    assert_enqueued_with job: ImportJob, args: [ import ] do
      import.publish_later
    end

    assert_equal "importing", import.reload.status
  end

  test "raises if not publishable" do
    import = imports(:transaction)

    import.stubs(:publishable?).returns(false)

    assert_raises(RuntimeError, "Import is not publishable") do
      import.publish_later
    end
  end

  test "handles publish errors" do
    import = imports(:transaction)

    import.stubs(:publishable?).returns(true)
    import.stubs(:import!).raises(StandardError, "Failed to publish")

    assert_nil import.error

    import.publish

    assert_equal "Failed to publish", import.error
    assert_equal "failed", import.status
  end
end