test_graph_export.py•2.94 kB
"""
Test for graph export functionality
"""
import sys
import os
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))
from src.tools.graph_export import GraphExporter
from src.core.dependency_analyzer import DependencyAnalyzer
def test_graph_exporter_creation():
"""Test that we can create a GraphExporter"""
exporter = GraphExporter()
assert exporter is not None
def test_mermaid_export():
"""Test Mermaid format export"""
analyzer = DependencyAnalyzer(Path.cwd())
# Add some sample data
analyzer.import_graph["module_a"] = {"module_b", "module_c"}
analyzer.import_graph["module_b"] = {"module_c"}
exporter = GraphExporter(dependency_analyzer=analyzer)
mermaid_output = exporter.export_to_mermaid("imports", "Test Graph")
assert "```mermaid" in mermaid_output
assert "graph TD" in mermaid_output
assert "module_a" in mermaid_output
assert "module_b" in mermaid_output
assert "module_c" in mermaid_output
def test_dot_export():
"""Test DOT format export"""
analyzer = DependencyAnalyzer(Path.cwd())
# Add some sample data
analyzer.call_graph["func_a"] = {"func_b", "func_c"}
exporter = GraphExporter(dependency_analyzer=analyzer)
dot_output = exporter.export_to_dot("calls", "Call Graph")
assert 'digraph "Call Graph"' in dot_output
assert "func_a" in dot_output
assert "func_b" in dot_output
assert "->" in dot_output
def test_json_export():
"""Test JSON format export"""
analyzer = DependencyAnalyzer(Path.cwd())
# Add some sample data
analyzer.inheritance_tree["ChildClass"] = {"ParentClass"}
exporter = GraphExporter(dependency_analyzer=analyzer)
json_output = exporter.export_to_json("inheritance", include_metadata=True)
import json
data = json.loads(json_output)
assert data["type"] == "inheritance"
assert "nodes" in data
assert "edges" in data
assert "metadata" in data
assert len(data["nodes"]) == 2 # ChildClass and ParentClass
def test_circular_dependency_detection():
"""Test detection of circular dependencies"""
analyzer = DependencyAnalyzer(Path.cwd())
# Create a circular dependency
analyzer.import_graph["module_a"] = {"module_b"}
analyzer.import_graph["module_b"] = {"module_c"}
analyzer.import_graph["module_c"] = {"module_a"}
exporter = GraphExporter(dependency_analyzer=analyzer)
cycles = exporter.detect_circular_dependencies("imports")
assert len(cycles) > 0
assert any("module_a" in cycle for cycle in cycles)
if __name__ == "__main__":
# Allow running directly
test_graph_exporter_creation()
test_mermaid_export()
test_dot_export()
test_json_export()
test_circular_dependency_detection()
print("✅ All graph export tests passed!")