Back to Repositories

Testing SecurityPriceProvider Interface Implementation in maybe-finance

This test suite validates the SecurityPriceProvider interface implementation, ensuring consistent behavior for fetching security price data. It verifies both the provider’s health check capabilities and the expected response contract for price data retrieval.

Test Coverage Overview

The test suite covers essential functionality of the SecurityPriceProvider interface, focusing on two main aspects:

  • Interface method availability testing for healthy? and fetch_security_prices
  • Response contract validation for security price data retrieval
  • Comprehensive testing of response object attributes including prices, success status, error handling, and raw response access

Implementation Analysis

The testing approach utilizes Ruby’s ActiveSupport::Testing::Declarative module for clear test organization. VCR cassettes are implemented to simulate API responses, ensuring consistent and reliable test execution. The tests employ a modular structure with focused test cases for interface compliance and response validation.

Technical Details

  • Testing Framework: Minitest
  • VCR for HTTP interaction recording
  • ActiveSupport::Testing::Declarative for test organization
  • ISO8601 date formatting for standardized date handling
  • Assertion-based validation for interface contracts

Best Practices Demonstrated

The test suite exemplifies several testing best practices including isolation of interface testing, proper use of test doubles through VCR, and comprehensive contract testing. The code demonstrates clear separation of concerns between interface verification and response contract validation, while maintaining focused test cases with specific assertions.

maybe-finance/maybe

test/interfaces/security_price_provider_interface_test.rb

            
require "test_helper"

module SecurityPriceProviderInterfaceTest
  extend ActiveSupport::Testing::Declarative

  test "security price provider interface" do
    assert_respond_to @subject, :healthy?
    assert_respond_to @subject, :fetch_security_prices
  end

  test "security price provider response contract" do
    VCR.use_cassette "synth/security_prices" do
      response = @subject.fetch_security_prices(
        ticker: "AAPL",
        mic_code: "XNAS",
        start_date: Date.iso8601("2024-01-01"),
        end_date: Date.iso8601("2024-08-01")
      )

      assert_respond_to response, :prices
      assert_respond_to response, :success?
      assert_respond_to response, :error
      assert_respond_to response, :raw_response
    end
  end
end