Back to Repositories

Testing Ethereum Price Validation in AutoGPT

This test suite validates Ethereum price data retrieval and accuracy verification in Python. It ensures proper formatting of price values and validates them against real-time market data with specified tolerance levels.

Test Coverage Overview

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

  • File-based price data validation
  • Numeric format verification using regex
  • Real-time price comparison with tolerance checks
  • Error handling for price discrepancies

Implementation Analysis

The testing approach implements a multi-step verification process using Python’s built-in assertion framework. It employs regex pattern matching for input validation and implements floating-point comparison logic with defined tolerance thresholds.

Technical Details

  • Python’s re module for regex validation
  • File I/O operations for data retrieval
  • Floating-point arithmetic for price comparisons
  • Custom assertion messages for detailed error reporting

Best Practices Demonstrated

The test demonstrates several testing best practices including isolated file operations, precise data validation, and meaningful error messages. It shows proper separation of concerns between data retrieval, validation, and comparison logic.

significant-gravitas/autogpt

classic/benchmark/agbenchmark/challenges/library/ethereum/check_price/artifacts_out/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("output.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()