|
import os
|
|
import tempfile
|
|
|
|
import pytest
|
|
|
|
from rp.core.exceptions import ValidationError
|
|
from rp.core.validation import (
|
|
validate_api_url,
|
|
validate_directory_path,
|
|
validate_file_path,
|
|
validate_max_tokens,
|
|
validate_model_name,
|
|
validate_session_name,
|
|
validate_temperature,
|
|
)
|
|
|
|
|
|
def test_validate_file_path_empty():
|
|
with pytest.raises(ValidationError, match="File path cannot be empty"):
|
|
validate_file_path("")
|
|
|
|
|
|
def test_validate_file_path_not_exist():
|
|
with pytest.raises(ValidationError, match="File does not exist"):
|
|
validate_file_path("/nonexistent/file.txt", must_exist=True)
|
|
|
|
|
|
def test_validate_file_path_is_dir():
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
with pytest.raises(ValidationError, match="Path is a directory"):
|
|
validate_file_path(tmpdir, must_exist=True)
|
|
|
|
|
|
def test_validate_file_path_valid():
|
|
with tempfile.NamedTemporaryFile() as tmpfile:
|
|
result = validate_file_path(tmpfile.name, must_exist=True)
|
|
assert os.path.isabs(result)
|
|
assert result == os.path.abspath(tmpfile.name)
|
|
|
|
|
|
def test_validate_directory_path_empty():
|
|
with pytest.raises(ValidationError, match="Directory path cannot be empty"):
|
|
validate_directory_path("")
|
|
|
|
|
|
def test_validate_directory_path_not_exist():
|
|
with pytest.raises(ValidationError, match="Directory does not exist"):
|
|
validate_directory_path("/nonexistent/dir", must_exist=True)
|
|
|
|
|
|
def test_validate_directory_path_not_dir():
|
|
with tempfile.NamedTemporaryFile() as tmpfile:
|
|
with pytest.raises(ValidationError, match="Path is not a directory"):
|
|
validate_directory_path(tmpfile.name, must_exist=True)
|
|
|
|
|
|
def test_validate_directory_path_create():
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
new_dir = os.path.join(tmpdir, "new_dir")
|
|
result = validate_directory_path(new_dir, must_exist=True, create=True)
|
|
assert os.path.isdir(new_dir)
|
|
assert result == os.path.abspath(new_dir)
|
|
|
|
|
|
def test_validate_directory_path_valid():
|
|
with tempfile.TemporaryDirectory() as tmpdir:
|
|
result = validate_directory_path(tmpdir, must_exist=True)
|
|
assert result == os.path.abspath(tmpdir)
|
|
|
|
|
|
def test_validate_model_name_empty():
|
|
with pytest.raises(ValidationError, match="Model name cannot be empty"):
|
|
validate_model_name("")
|
|
|
|
|
|
def test_validate_model_name_too_short():
|
|
with pytest.raises(ValidationError, match="Model name too short"):
|
|
validate_model_name("a")
|
|
|
|
|
|
def test_validate_model_name_valid():
|
|
result = validate_model_name("gpt-3.5-turbo")
|
|
assert result == "gpt-3.5-turbo"
|
|
|
|
|
|
def test_validate_api_url_empty():
|
|
with pytest.raises(ValidationError, match="API URL cannot be empty"):
|
|
validate_api_url("")
|
|
|
|
|
|
def test_validate_api_url_invalid():
|
|
with pytest.raises(ValidationError, match="API URL must start with"):
|
|
validate_api_url("invalid-url")
|
|
|
|
|
|
def test_validate_api_url_valid():
|
|
result = validate_api_url("https://api.example.com")
|
|
assert result == "https://api.example.com"
|
|
|
|
|
|
def test_validate_session_name_empty():
|
|
with pytest.raises(ValidationError, match="Session name cannot be empty"):
|
|
validate_session_name("")
|
|
|
|
|
|
def test_validate_session_name_invalid_char():
|
|
with pytest.raises(ValidationError, match="contains invalid character"):
|
|
validate_session_name("test/session")
|
|
|
|
|
|
def test_validate_session_name_too_long():
|
|
long_name = "a" * 256
|
|
with pytest.raises(ValidationError, match="Session name too long"):
|
|
validate_session_name(long_name)
|
|
|
|
|
|
def test_validate_session_name_valid():
|
|
result = validate_session_name("valid_session_123")
|
|
assert result == "valid_session_123"
|
|
|
|
|
|
def test_validate_temperature_too_low():
|
|
with pytest.raises(ValidationError, match="Temperature must be between"):
|
|
validate_temperature(-0.1)
|
|
|
|
|
|
def test_validate_temperature_too_high():
|
|
with pytest.raises(ValidationError, match="Temperature must be between"):
|
|
validate_temperature(2.1)
|
|
|
|
|
|
def test_validate_temperature_valid():
|
|
result = validate_temperature(0.7)
|
|
assert result == 0.7
|
|
|
|
|
|
def test_validate_max_tokens_too_low():
|
|
with pytest.raises(ValidationError, match="Max tokens must be at least 1"):
|
|
validate_max_tokens(0)
|
|
|
|
|
|
def test_validate_max_tokens_too_high():
|
|
with pytest.raises(ValidationError, match="Max tokens too high"):
|
|
validate_max_tokens(100001)
|
|
|
|
|
|
def test_validate_max_tokens_valid():
|
|
result = validate_max_tokens(1000)
|
|
assert result == 1000
|