mirror of
https://github.com/codeflash-ai/codeflash.git
synced 2026-05-04 18:25:17 +00:00
66 lines
2.4 KiB
Python
66 lines
2.4 KiB
Python
import pytest
|
|
from pydantic import ValidationError
|
|
|
|
from codeflash.api.aiservice import AiServiceClient
|
|
from codeflash.models.models import CodeString, OptimizedCandidateSource
|
|
|
|
|
|
def test_python_string():
|
|
code = CodeString(code="print('Hello, World!')")
|
|
assert code.code == "print('Hello, World!')"
|
|
|
|
|
|
def test_valid_python_code():
|
|
# This should pass without errors
|
|
valid_code = "x = 1\ny = x + 2\nprint(y)"
|
|
cs = CodeString(code=valid_code)
|
|
assert cs.code == valid_code
|
|
|
|
|
|
def test_invalid_python_code_syntax():
|
|
# Missing a parenthesis should cause a syntax error
|
|
invalid_code = "x = 1\nprint(x"
|
|
with pytest.raises(ValidationError) as exc_info:
|
|
CodeString(code=invalid_code)
|
|
# Check that the error message mentions "Invalid Python code"
|
|
assert "Invalid Python code:" in str(exc_info.value)
|
|
|
|
|
|
def test_invalid_python_code_name_error():
|
|
# Note that compile won't catch NameError because it's a runtime error, not a syntax error.
|
|
# This code is syntactically valid but would fail at runtime. However, compile won't fail.
|
|
invalid_runtime_code = "print(undefined_variable)"
|
|
cs = CodeString(code=invalid_runtime_code)
|
|
assert cs.code == invalid_runtime_code
|
|
|
|
|
|
def test_empty_code_string():
|
|
# Empty code is still syntactically valid (no-op)
|
|
empty_code = ""
|
|
cs = CodeString(code=empty_code)
|
|
assert cs.code == empty_code
|
|
|
|
|
|
def test_whitespace_only():
|
|
# Whitespace is still syntactically valid (no-op)
|
|
whitespace_code = " "
|
|
cs = CodeString(code=whitespace_code)
|
|
assert cs.code == whitespace_code
|
|
|
|
|
|
def test_generated_candidates_validation():
|
|
ai_service = AiServiceClient()
|
|
code = """```python:file.py
|
|
print name
|
|
```"""
|
|
mock_generate_candidates = [{"source_code": code, "explanation": "", "optimization_id": ""}]
|
|
candidates = ai_service._get_valid_candidates(mock_generate_candidates, OptimizedCandidateSource.OPTIMIZE)
|
|
assert len(candidates) == 0
|
|
code = """```python:file.py
|
|
print('Hello, World!')
|
|
```"""
|
|
mock_generate_candidates = [{"source_code": code, "explanation": "", "optimization_id": ""}]
|
|
candidates = ai_service._get_valid_candidates(mock_generate_candidates, OptimizedCandidateSource.OPTIMIZE)
|
|
assert len(candidates) == 1
|
|
assert candidates[0].source_code.code_strings[0].code == "print('Hello, World!')"
|
|
assert candidates[0].source_code.code_strings[0].file_path.as_posix() == "file.py"
|