codeflash-internal/django/aiservice/testgen/models.py

76 lines
2.8 KiB
Python
Raw Normal View History

2024-12-30 03:11:20 +00:00
import enum
2025-10-14 22:55:06 +00:00
from typing import Self
from ninja import Schema
from pydantic import model_validator
from aiservice.models.functions_to_optimize import FunctionToOptimize
2024-12-30 03:11:20 +00:00
2024-12-30 03:19:19 +00:00
class TestingMode(enum.Enum):
2024-12-30 03:11:20 +00:00
BEHAVIOR = "behavior"
PERFORMANCE = "performance"
2025-10-14 22:55:06 +00:00
class TestGenSchema(Schema):
source_code_being_tested: str
function_to_optimize: FunctionToOptimize
2025-10-15 09:21:52 +00:00
helper_function_names: list[str] | None = None # This is the only one we should use
dependent_function_names: list[str] | None = None # Only for backwards compatibility
2025-10-14 22:55:06 +00:00
module_path: str
test_module_path: str
2026-01-28 20:23:54 +00:00
test_framework: str # "pytest", "jest"
2025-10-14 22:55:06 +00:00
test_timeout: int
trace_id: str
2026-01-15 06:15:27 +00:00
python_version: str | None = None # Made optional for multi-language support
language: str = "python" # NEW: language identifier (python, javascript, typescript)
language_version: str | None = None # NEW: e.g., "ES2022", "Node 20", or Python version
2025-10-14 22:55:06 +00:00
codeflash_version: str | None = None
test_index: int | None = None
is_async: bool | None = False
call_sequence: int | None = None
is_numerical_code: bool | None = None
2025-10-14 22:55:06 +00:00
@model_validator(mode="after")
def helper_function_names_validator(self) -> Self:
# To maintain backwards compatibility
if self.dependent_function_names is None and self.helper_function_names is None:
raise ValueError("either field 'helper_function_names' or 'dependent_function_names' is required")
if self.helper_function_names is not None:
return self
self.helper_function_names = self.dependent_function_names
self.dependent_function_names = None
return self
class TestGenResponseSchema(Schema):
generated_tests: str
instrumented_behavior_tests: str
instrumented_perf_tests: str
class TestGenDebugInfo(Schema):
"""Debug information for failed test generation."""
stage: str # "llm_generation", "code_validation", "instrumentation", "postprocessing"
raw_llm_output: str | None = None # The raw LLM response before parsing
initial_code: str | None = None # Code extracted from LLM response
fixed_code: str | None = None # Code after isort/quote fixes
final_code: str | None = None # Final code that failed validation
lines_removed: int | None = None # Number of lines truncated during validation
validation_error: str | None = None # Specific validation error message
2025-10-14 22:55:06 +00:00
class TestGenErrorResponseSchema(Schema):
error: str
trace_id: str | None = None
debug_info: TestGenDebugInfo | None = None
2025-10-14 22:55:06 +00:00
2025-10-15 09:24:34 +00:00
2025-10-14 22:55:06 +00:00
class TestGenerationFailedError(Exception):
"""Exception for test generation failures with debug context."""
def __init__(self, message: str, debug_info: dict[str, str | int | None] | None = None) -> None:
super().__init__(message)
self.debug_info = debug_info or {}