Back to Repositories

Testing Ethereum Price Validation Workflow in AutoGPT

This test suite validates Ethereum price verification functionality by comparing stored price data against real-time market values. It ensures price accuracy within defined thresholds and implements robust error handling for cryptocurrency price validation.

Test Coverage Overview

The test suite provides comprehensive coverage for Ethereum price validation functionality.

Key areas tested include:
  • File-based price data reading
  • Price format validation using regex
  • Real-time price comparison
  • Threshold-based price deviation checks
Integration points cover file I/O operations and external price fetching functionality.

Implementation Analysis

The testing approach employs Python’s built-in unit testing capabilities with precise assertion checks for price validation. The implementation uses regex pattern matching for input validation and implements floating-point comparisons for price threshold verification.

Technical patterns include:
  • File handling with context managers
  • Regular expression validation
  • Numerical comparison with tolerance ranges

Technical Details

Testing tools and components:
  • Python’s built-in unittest framework
  • Regular expression (re) module
  • File I/O operations
  • Custom price fetching utility (get_ethereum_price)
Configuration includes predefined threshold values and regex patterns for validation.

Best Practices Demonstrated

The test implementation showcases several testing best practices including robust error handling and clear assertion messages.

Notable practices:
  • Descriptive assertion messages
  • Proper exception handling
  • Clear test case organization
  • Modular function testing

significant-gravitas/autogpt

classic/benchmark/agbenchmark/challenges/library/ethereum/check_price/artifacts_in/test.py

            
import re

from .sample_code import get_ethereum_price


def test_get_ethereum_price() -> None:
    # Read the Ethereum price from the file
    with open("eth_price.txt", "r") as file:
        eth_price = file.read().strip()

    # Validate that the eth price is all digits
    pattern = r"^\d+$"
    matches = re.match(pattern, eth_price) is not None
    assert (
        matches
    ), f"AssertionError: Ethereum price should be all digits, but got {eth_price}"

    # Get the current price of Ethereum
    real_eth_price = get_ethereum_price()

    # Convert the eth price to a numerical value for comparison
    eth_price_value = float(eth_price)
    real_eth_price_value = float(real_eth_price)

    # Check if the eth price is within $50 of the actual Ethereum price
    assert abs(real_eth_price_value - eth_price_value) <= 50, (
        "AssertionError: Ethereum price is not within $50 of the actual Ethereum price "
        f"(Provided price: ${eth_price}, Real price: ${real_eth_price})"
    )

    print("Matches")


if __name__ == "__main__":
    test_get_ethereum_price()