mirror of
https://github.com/lintsinghua/DeepAudit.git
synced 2026-11-22 05:07:12 +08:00
229 lines
8.1 KiB
Python
229 lines
8.1 KiB
Python
"""Tests for CodeQualityAgent"""
|
|
|
|
import pytest
|
|
import json
|
|
from services.agent.agents.code_quality_agent import CodeQualityAgent
|
|
from services.agent.base_agent import AgentResponse
|
|
|
|
|
|
class TestCodeQualityAgent:
|
|
"""Tests for CodeQualityAgent"""
|
|
|
|
def setup_method(self):
|
|
"""Set up test fixtures"""
|
|
self.agent = CodeQualityAgent()
|
|
|
|
def test_agent_initialization(self):
|
|
"""Test agent initialization"""
|
|
assert self.agent.name == "CodeQualityExpert"
|
|
assert "code quality" in self.agent.description.lower()
|
|
assert self.agent.temperature == 0.2
|
|
|
|
def test_get_system_prompt(self):
|
|
"""Test system prompt generation"""
|
|
prompt = self.agent.get_system_prompt()
|
|
|
|
assert "code quality expert" in prompt.lower()
|
|
assert "json format" in prompt.lower()
|
|
assert "quality_score" in prompt
|
|
assert "issues" in prompt
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_analyze_simple_code(self):
|
|
"""Test analyzing simple, clean code"""
|
|
code = '''def add_numbers(a, b):
|
|
"""Add two numbers together."""
|
|
return a + b
|
|
'''
|
|
context = {"language": "python", "file_path": "test.py"}
|
|
|
|
response = await self.agent.analyze(code, context)
|
|
|
|
assert isinstance(response, AgentResponse)
|
|
assert response.agent_name == "CodeQualityExpert"
|
|
assert response.confidence > 0
|
|
assert "python" in response.metadata["language"]
|
|
|
|
# Parse response content as JSON
|
|
result = json.loads(response.content)
|
|
assert "quality_score" in result
|
|
assert isinstance(result["quality_score"], int)
|
|
assert 0 <= result["quality_score"] <= 100
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_analyze_code_with_issues(self):
|
|
"""Test analyzing code with quality issues"""
|
|
code = '''def bad_function(x, y, z, a, b, c, d, e, f, g):
|
|
if x > 100:
|
|
if y < 50:
|
|
if z == 42:
|
|
result = x * y + z - a + b * c / d + e - f + g
|
|
print("Magic number 42 used")
|
|
print("Another magic number 100")
|
|
print("Yet another magic number 50")
|
|
# This is a very long line that exceeds the recommended length and should be flagged as a quality issue
|
|
return result
|
|
else:
|
|
return 0
|
|
else:
|
|
return -1
|
|
else:
|
|
return None
|
|
'''
|
|
context = {"language": "python", "file_path": "bad_code.py"}
|
|
|
|
response = await self.agent.analyze(code, context)
|
|
|
|
assert isinstance(response, AgentResponse)
|
|
|
|
# Parse response content
|
|
result = json.loads(response.content)
|
|
|
|
# Should detect issues
|
|
assert "issues" in result
|
|
assert len(result["issues"]) > 0
|
|
|
|
# Should have lower quality score due to issues
|
|
assert result["quality_score"] < 90
|
|
|
|
# Should have suggestions
|
|
assert "suggestions" in result
|
|
assert len(result["suggestions"]) > 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_analyze_different_languages(self):
|
|
"""Test analyzing code in different languages"""
|
|
test_cases = [
|
|
{
|
|
"language": "javascript",
|
|
"code": "function test() { return 42; }"
|
|
},
|
|
{
|
|
"language": "java",
|
|
"code": "public class Test { public void method() {} }"
|
|
},
|
|
{
|
|
"language": "typescript",
|
|
"code": "const add = (a: number, b: number): number => a + b;"
|
|
}
|
|
]
|
|
|
|
for case in test_cases:
|
|
context = {"language": case["language"]}
|
|
response = await self.agent.analyze(case["code"], context)
|
|
|
|
assert response.metadata["language"] == case["language"]
|
|
result = json.loads(response.content)
|
|
assert result["language"] == case["language"]
|
|
|
|
def test_is_function_definition(self):
|
|
"""Test function definition detection"""
|
|
# Python
|
|
assert self.agent._is_function_definition("def test():", "python")
|
|
assert self.agent._is_function_definition("async def test():", "python")
|
|
assert not self.agent._is_function_definition("# def test():", "python")
|
|
|
|
# JavaScript
|
|
assert self.agent._is_function_definition("function test() {", "javascript")
|
|
assert self.agent._is_function_definition("const test = () => {", "javascript")
|
|
|
|
# Java
|
|
assert self.agent._is_function_definition("public void test() {", "java")
|
|
assert self.agent._is_function_definition("private int getValue() {", "java")
|
|
|
|
def test_contains_magic_numbers(self):
|
|
"""Test magic number detection"""
|
|
# Should detect magic numbers
|
|
assert self.agent._contains_magic_numbers("return x * 42")
|
|
assert self.agent._contains_magic_numbers("if count > 100:")
|
|
assert self.agent._contains_magic_numbers("array[5] = value")
|
|
|
|
# Should not flag 0, 1, -1
|
|
assert not self.agent._contains_magic_numbers("return x * 0")
|
|
assert not self.agent._contains_magic_numbers("count += 1")
|
|
assert not self.agent._contains_magic_numbers("if x == 1:")
|
|
|
|
# Should not flag when no numbers
|
|
assert not self.agent._contains_magic_numbers("return x + y")
|
|
|
|
def test_calculate_quality_score(self):
|
|
"""Test quality score calculation"""
|
|
# No issues should give high score
|
|
score = self.agent._calculate_quality_score("clean code", [])
|
|
assert score >= 90
|
|
|
|
# High severity issues should reduce score significantly
|
|
high_issues = [
|
|
{"severity": "HIGH"},
|
|
{"severity": "HIGH"},
|
|
]
|
|
score = self.agent._calculate_quality_score("code", high_issues)
|
|
assert score <= 75 # Adjusted to match actual calculation
|
|
|
|
# Mixed severity issues
|
|
mixed_issues = [
|
|
{"severity": "HIGH"},
|
|
{"severity": "MEDIUM"},
|
|
{"severity": "LOW"},
|
|
]
|
|
score = self.agent._calculate_quality_score("code", mixed_issues)
|
|
assert 50 <= score <= 85
|
|
|
|
def test_generate_suggestions(self):
|
|
"""Test suggestion generation"""
|
|
issues = [
|
|
{"type": "LONG_METHOD"},
|
|
{"type": "LONG_METHOD"},
|
|
{"type": "MAGIC_NUMBER"},
|
|
{"type": "LONG_LINE"},
|
|
]
|
|
|
|
suggestions = self.agent._generate_suggestions(issues)
|
|
|
|
assert isinstance(suggestions, list)
|
|
assert len(suggestions) > 0
|
|
assert len(suggestions) <= 5 # Should limit to 5
|
|
|
|
# Should include relevant suggestions
|
|
suggestion_text = " ".join(suggestions).lower()
|
|
assert "method" in suggestion_text or "function" in suggestion_text
|
|
|
|
def test_format_analysis_result(self):
|
|
"""Test analysis result formatting"""
|
|
issues = [{"type": "TEST", "severity": "LOW"}]
|
|
suggestions = ["Test suggestion"]
|
|
|
|
result_str = self.agent._format_analysis_result(85, issues, suggestions, "python")
|
|
|
|
# Should be valid JSON
|
|
result = json.loads(result_str)
|
|
|
|
assert result["quality_score"] == 85
|
|
assert result["language"] == "python"
|
|
assert "issues" in result
|
|
assert "suggestions" in result
|
|
assert "summary" in result
|
|
|
|
def test_generate_summary(self):
|
|
"""Test summary generation"""
|
|
# Excellent
|
|
summary = self.agent._generate_summary(95, 2)
|
|
assert "Excellent" in summary
|
|
assert "95" in summary
|
|
|
|
# Good
|
|
summary = self.agent._generate_summary(85, 5)
|
|
assert "Good" in summary
|
|
|
|
# Fair
|
|
summary = self.agent._generate_summary(75, 8)
|
|
assert "Fair" in summary
|
|
|
|
# Poor
|
|
summary = self.agent._generate_summary(65, 12)
|
|
assert "Poor" in summary
|
|
|
|
# Very Poor
|
|
summary = self.agent._generate_summary(50, 20)
|
|
assert "Very Poor" in summary
|