Back to Repositories

Testing Circular Dependency Detection in AutoGPT

This test suite validates the circular dependency detection functionality in graph structures for AutoGPT. It includes comprehensive tests for both cyclic and acyclic graph scenarios, ensuring reliable detection of circular dependencies in dependency graphs.

Test Coverage Overview

The test suite provides complete coverage for circular dependency detection in directed graphs.

Key areas tested include:
  • Cyclic graph detection with a four-node circular path
  • Acyclic graph validation with linear path structure
  • Edge case handling for complex graph structures
  • Path verification for detected cycles

Implementation Analysis

The testing approach employs pytest’s assertion framework to validate graph cycle detection logic. The implementation uses dictionary-based graph representation with nodes and edges collections, following a clear pattern for graph structure validation.

Technical patterns include:
  • Graph structure validation using node-edge relationships
  • Path verification through edge traversal
  • Boolean assertion checks for cycle presence

Technical Details

Testing components include:
  • Python’s pytest framework for test execution
  • Custom graph structure using dictionary format
  • Node-edge relationship validation
  • Path verification algorithms
  • Assertion-based validation checks

Best Practices Demonstrated

The test suite exemplifies strong testing practices through clear test case separation and comprehensive validation.

Notable practices include:
  • Distinct test cases for positive and negative scenarios
  • Detailed assertion messages for debugging
  • Comprehensive path validation
  • Clear test data structure definition

significant-gravitas/autogpt

classic/benchmark/tests/test_is_circular.py

            
from agbenchmark.utils.dependencies.graphs import is_circular


def test_is_circular():
    cyclic_graph = {
        "nodes": [
            {"id": "A", "data": {"category": []}},
            {"id": "B", "data": {"category": []}},
            {"id": "C", "data": {"category": []}},
            {"id": "D", "data": {"category": []}},  # New node
        ],
        "edges": [
            {"from": "A", "to": "B"},
            {"from": "B", "to": "C"},
            {"from": "C", "to": "D"},
            {"from": "D", "to": "A"},  # This edge creates a cycle
        ],
    }

    result = is_circular(cyclic_graph)
    assert result is not None, "Expected a cycle, but none was detected"
    assert all(
        (
            (result[i], result[i + 1])
            in [(x["from"], x["to"]) for x in cyclic_graph["edges"]]
        )
        for i in range(len(result) - 1)
    ), "The detected cycle path is not part of the graph's edges"


def test_is_not_circular():
    acyclic_graph = {
        "nodes": [
            {"id": "A", "data": {"category": []}},
            {"id": "B", "data": {"category": []}},
            {"id": "C", "data": {"category": []}},
            {"id": "D", "data": {"category": []}},  # New node
        ],
        "edges": [
            {"from": "A", "to": "B"},
            {"from": "B", "to": "C"},
            {"from": "C", "to": "D"},
            # No back edge from D to any node, so it remains acyclic
        ],
    }

    assert is_circular(acyclic_graph) is None, "Detected a cycle in an acyclic graph"