121 lines
3.1 KiB
Python
121 lines
3.1 KiB
Python
import tempfile
|
|
import os
|
|
from pathlib import Path
|
|
from codemap.core import GraphBuilder
|
|
from codemap.parsers import ParsedFile, Dependency
|
|
|
|
|
|
def test_graph_builder_basic():
|
|
builder = GraphBuilder()
|
|
|
|
dep = Dependency(
|
|
module_name="utils",
|
|
file_path=Path("/app/main.py"),
|
|
line_number=1
|
|
)
|
|
parsed1 = ParsedFile(
|
|
file_path=Path("/app/main.py"),
|
|
module_name="main",
|
|
dependencies=[dep],
|
|
file_type="python"
|
|
)
|
|
parsed2 = ParsedFile(
|
|
file_path=Path("/app/utils.py"),
|
|
module_name="utils",
|
|
dependencies=[],
|
|
file_type="python"
|
|
)
|
|
|
|
builder.add_file(parsed1)
|
|
builder.add_file(parsed2)
|
|
|
|
stats = builder.get_stats()
|
|
assert stats["node_count"] == 3
|
|
assert stats["edge_count"] == 1
|
|
|
|
|
|
def test_graph_builder_build_from_files():
|
|
builder = GraphBuilder()
|
|
|
|
files = []
|
|
for i in range(3):
|
|
parsed = ParsedFile(
|
|
file_path=Path(f"/app/module{i}.py"),
|
|
module_name=f"module{i}",
|
|
dependencies=[],
|
|
file_type="python"
|
|
)
|
|
files.append(parsed)
|
|
|
|
builder.build_from_files(files)
|
|
|
|
stats = builder.get_stats()
|
|
assert stats["node_count"] == 3
|
|
assert stats["edge_count"] == 0
|
|
|
|
|
|
def test_get_dependencies():
|
|
builder = GraphBuilder()
|
|
|
|
dep_a = Dependency(module_name="a", file_path=Path("/app/main.py"), line_number=1)
|
|
main = ParsedFile(
|
|
file_path=Path("/app/main.py"),
|
|
module_name="main",
|
|
dependencies=[dep_a],
|
|
file_type="python"
|
|
)
|
|
dep_b = Dependency(module_name="b", file_path=Path("/app/utils.py"), line_number=1)
|
|
utils = ParsedFile(
|
|
file_path=Path("/app/utils.py"),
|
|
module_name="utils",
|
|
dependencies=[dep_b],
|
|
file_type="python"
|
|
)
|
|
a = ParsedFile(
|
|
file_path=Path("/app/a.py"),
|
|
module_name="a",
|
|
dependencies=[],
|
|
file_type="python"
|
|
)
|
|
|
|
builder.add_file(main)
|
|
builder.add_file(utils)
|
|
builder.add_file(a)
|
|
|
|
graph_data = builder.get_graph_data()
|
|
assert len(graph_data.nodes) >= 3
|
|
assert len(graph_data.edges) >= 2
|
|
|
|
|
|
def test_get_packages():
|
|
builder = GraphBuilder()
|
|
|
|
files = [
|
|
ParsedFile(file_path=Path("/app/src/main.py"), module_name="main", dependencies=[], file_type="python"),
|
|
ParsedFile(file_path=Path("/app/src/utils.py"), module_name="utils", dependencies=[], file_type="python"),
|
|
ParsedFile(file_path=Path("/app/lib/helper.py"), module_name="helper", dependencies=[], file_type="python"),
|
|
]
|
|
|
|
builder.build_from_files(files)
|
|
packages = builder.get_packages()
|
|
|
|
assert "src" in packages or "lib" in packages
|
|
|
|
|
|
def test_get_stats():
|
|
builder = GraphBuilder()
|
|
|
|
for i in range(5):
|
|
parsed = ParsedFile(
|
|
file_path=Path(f"/app/module{i}.py"),
|
|
module_name=f"module{i}",
|
|
dependencies=[],
|
|
file_type="python"
|
|
)
|
|
builder.add_file(parsed)
|
|
|
|
stats = builder.get_stats()
|
|
assert stats["node_count"] == 5
|
|
assert stats["file_count"] == 5
|
|
assert stats["edge_count"] == 0
|