Back to Repositories

Testing Graph Root Node Detection in AutoGPT

This test suite validates the get_roots function in AutoGPT’s graph dependency analysis system. It focuses on verifying correct identification of root nodes in directed graphs, which is crucial for dependency resolution and task orchestration within the AutoGPT framework.

Test Coverage Overview

The test suite provides comprehensive coverage of root node identification in directed graphs.

Key areas tested include:
  • Basic root node detection in partially connected graphs
  • Edge case handling for fully connected cyclic graphs
  • Verification of isolated nodes as roots
The tests specifically validate both positive and negative scenarios for root node identification.

Implementation Analysis

The testing approach employs pytest’s assertion framework to validate graph traversal outcomes. The implementation uses dictionary-based graph representation with nodes and edges as key data structures.

Notable patterns include:
  • Graph structure definition using nested dictionaries
  • Set-based comparison for root node validation
  • Clear test case separation for different graph scenarios

Technical Details

Testing infrastructure includes:
  • Python’s pytest framework
  • Custom graph utility module (agbenchmark.utils.dependencies.graphs)
  • Dictionary-based graph representation
  • Set operations for result validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Python.

Notable practices include:
  • Clear test case isolation
  • Descriptive assertion messages
  • Comprehensive edge case coverage
  • Clean and maintainable test structure
  • Effective use of data structures for test scenarios

significant-gravitas/autogpt

classic/benchmark/tests/test_get_roots.py

            
from agbenchmark.utils.dependencies.graphs import get_roots


def test_get_roots():
    graph = {
        "nodes": [
            {"id": "A", "data": {"category": []}},
            {"id": "B", "data": {"category": []}},
            {"id": "C", "data": {"category": []}},
            {"id": "D", "data": {"category": []}},
        ],
        "edges": [
            {"from": "A", "to": "B"},
            {"from": "B", "to": "C"},
        ],
    }

    result = get_roots(graph)
    assert set(result) == {
        "A",
        "D",
    }, f"Expected roots to be 'A' and 'D', but got {result}"


def test_no_roots():
    fully_connected_graph = {
        "nodes": [
            {"id": "A", "data": {"category": []}},
            {"id": "B", "data": {"category": []}},
            {"id": "C", "data": {"category": []}},
        ],
        "edges": [
            {"from": "A", "to": "B"},
            {"from": "B", "to": "C"},
            {"from": "C", "to": "A"},
        ],
    }

    result = get_roots(fully_connected_graph)
    assert not result, "Expected no roots, but found some"


# def test_no_rcoots():
#     fully_connected_graph = {
#         "nodes": [
#             {"id": "A", "data": {"category": []}},
#             {"id": "B", "data": {"category": []}},
#             {"id": "C", "data": {"category": []}},
#         ],
#         "edges": [
#             {"from": "A", "to": "B"},
#             {"from": "D", "to": "C"},
#         ],
#     }
#
#     result = get_roots(fully_connected_graph)
#     assert set(result) == {"A"}, f"Expected roots to be 'A', but got {result}"