104 lines
3.2 KiB
Python
104 lines
3.2 KiB
Python
"""
|
|
Unit tests for knowledge graph helpers.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
|
|
from knowledge_graph import operations as kg_ops
|
|
|
|
|
|
class _DummyFileAnalysis:
|
|
def __init__(
|
|
self,
|
|
path: str,
|
|
language: str,
|
|
lines_of_code: int,
|
|
severity_score: float,
|
|
complexity_score: float,
|
|
issues_found,
|
|
recommendations,
|
|
detailed_analysis: str,
|
|
) -> None:
|
|
self.path = path
|
|
self.language = language
|
|
self.lines_of_code = lines_of_code
|
|
self.severity_score = severity_score
|
|
self.complexity_score = complexity_score
|
|
self.issues_found = issues_found
|
|
self.recommendations = recommendations
|
|
self.detailed_analysis = detailed_analysis
|
|
|
|
|
|
def test_build_module_payload_basic():
|
|
run_id = "run-123"
|
|
repository_id = "repo-001"
|
|
module_name = "Payments"
|
|
chunk = {
|
|
"id": "chunk-1",
|
|
"name": module_name,
|
|
"context_dependencies": ["Auth", "Notifications"],
|
|
}
|
|
chunk_analysis = {
|
|
"module_quality_score": 7.4,
|
|
"module_overview": "Handles payment orchestration.",
|
|
"module_architecture": "Microservice communicating via REST APIs.",
|
|
"module_security_assessment": "Uses token-based authentication.",
|
|
"module_recommendations": ["Increase test coverage", {"text": "Introduce circuit breakers"}],
|
|
}
|
|
|
|
file_analyses = [
|
|
_DummyFileAnalysis(
|
|
path="services/payments/processor.py",
|
|
language="Python",
|
|
lines_of_code=215,
|
|
severity_score=4.3,
|
|
complexity_score=6.1,
|
|
issues_found=[
|
|
{
|
|
"title": "Missing retry logic",
|
|
"severity": "high",
|
|
"category": "reliability",
|
|
"line_number": 58,
|
|
"recommendation": "Add exponential backoff retry",
|
|
}
|
|
],
|
|
recommendations=["Refactor long function"],
|
|
detailed_analysis="Processor heavily relies on synchronous calls.",
|
|
)
|
|
]
|
|
|
|
metadata = {
|
|
"type": "module_analysis",
|
|
"chunk_metrics": {"total_issues": 1},
|
|
"dependencies": {"depends_on_chunks": ["Auth", "Notifications"]},
|
|
"timestamp": datetime.utcnow().isoformat(),
|
|
}
|
|
ai_response = "Detailed module analysis"
|
|
|
|
payload = kg_ops.build_module_payload(
|
|
run_id=run_id,
|
|
repository_id=repository_id,
|
|
module_name=module_name,
|
|
chunk=chunk,
|
|
chunk_analysis=chunk_analysis,
|
|
file_analyses=file_analyses,
|
|
metadata=metadata,
|
|
ai_response=ai_response,
|
|
)
|
|
|
|
module_props = payload["module_props"]
|
|
files = payload["files"]
|
|
findings = payload["findings"]
|
|
dependencies = payload["dependencies"]
|
|
|
|
assert module_props["name"] == module_name
|
|
assert module_props["total_files"] == len(file_analyses)
|
|
assert "analysis_payload" in module_props
|
|
assert files[0]["path"] == "services/payments/processor.py"
|
|
assert files[0]["props"]["language"] == "Python"
|
|
assert len(findings) == 1
|
|
assert findings[0]["props"]["severity"] == "high"
|
|
assert dependencies[0]["target"] == "Auth"
|
|
assert dependencies[1]["target"] == "Notifications"
|
|
|