Re-upload: CI infrastructure issue resolved, all tests verified passing
This commit is contained in:
0
tests/unit/__init__.py
Normal file
0
tests/unit/__init__.py
Normal file
111
tests/unit/test_commit_service.py
Normal file
111
tests/unit/test_commit_service.py
Normal file
@@ -0,0 +1,111 @@
|
||||
"""Unit tests for CommitService."""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from memory_manager.core.services import CommitService
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_repository():
|
||||
return AsyncMock()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def commit_service(mock_repository):
|
||||
return CommitService(mock_repository)
|
||||
|
||||
|
||||
class TestCommitService:
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_commit(self, commit_service, mock_repository):
|
||||
mock_commit = MagicMock()
|
||||
mock_commit.to_dict.return_value = {
|
||||
"id": 1,
|
||||
"hash": "abc123",
|
||||
"message": "Test commit",
|
||||
"agent_id": "test-agent",
|
||||
"project_path": "/test",
|
||||
"snapshot": [],
|
||||
"created_at": "2024-01-01T00:00:00",
|
||||
}
|
||||
mock_repository.get_all_entries_snapshot = AsyncMock(return_value=[])
|
||||
mock_repository.create_commit = AsyncMock(return_value=mock_commit)
|
||||
|
||||
result = await commit_service.create_commit(
|
||||
message="Test commit",
|
||||
agent_id="test-agent",
|
||||
project_path="/test",
|
||||
)
|
||||
|
||||
assert result["hash"] == "abc123"
|
||||
assert result["message"] == "Test commit"
|
||||
mock_repository.create_commit.assert_called_once()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_commit(self, commit_service, mock_repository):
|
||||
mock_commit = MagicMock()
|
||||
mock_commit.to_dict.return_value = {"id": 1, "hash": "abc123"}
|
||||
mock_repository.get_commit = AsyncMock(return_value=mock_commit)
|
||||
|
||||
result = await commit_service.get_commit("abc123")
|
||||
|
||||
assert result["hash"] == "abc123"
|
||||
mock_repository.get_commit.assert_called_once_with("abc123")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_commit_not_found(self, commit_service, mock_repository):
|
||||
mock_repository.get_commit = AsyncMock(return_value=None)
|
||||
|
||||
result = await commit_service.get_commit("nonexistent")
|
||||
|
||||
assert result is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_commits(self, commit_service, mock_repository):
|
||||
mock_commits = [
|
||||
MagicMock(to_dict=lambda: {"id": 1, "hash": "abc123"}),
|
||||
MagicMock(to_dict=lambda: {"id": 2, "hash": "def456"}),
|
||||
]
|
||||
mock_repository.list_commits = AsyncMock(return_value=mock_commits)
|
||||
|
||||
result = await commit_service.list_commits()
|
||||
|
||||
assert len(result) == 2
|
||||
assert result[0]["hash"] == "abc123"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_diff(self, commit_service, mock_repository):
|
||||
mock_commit1 = MagicMock()
|
||||
mock_commit1.to_dict.return_value = {"id": 1, "hash": "abc123"}
|
||||
mock_commit1.snapshot = [{"id": 1, "title": "Entry 1"}]
|
||||
|
||||
mock_commit2 = MagicMock()
|
||||
mock_commit2.to_dict.return_value = {"id": 2, "hash": "def456"}
|
||||
mock_commit2.snapshot = [{"id": 1, "title": "Entry 1 Updated"}, {"id": 2, "title": "Entry 2"}]
|
||||
|
||||
mock_repository.get_commit = AsyncMock(
|
||||
side_effect=[mock_commit1, mock_commit2]
|
||||
)
|
||||
|
||||
result = await commit_service.diff("abc123", "def456")
|
||||
|
||||
assert result is not None
|
||||
assert len(result["modified"]) == 1
|
||||
assert len(result["added"]) == 1
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_diff_commit_not_found(self, commit_service, mock_repository):
|
||||
mock_repository.get_commit = AsyncMock(return_value=None)
|
||||
|
||||
result = await commit_service.diff("nonexistent1", "nonexistent2")
|
||||
|
||||
assert result is None
|
||||
|
||||
def test_generate_hash(self, commit_service):
|
||||
hash1 = commit_service._generate_hash("test data")
|
||||
hash2 = commit_service._generate_hash("test data")
|
||||
hash3 = commit_service._generate_hash("different data")
|
||||
|
||||
assert hash1 == hash2
|
||||
assert hash1 != hash3
|
||||
assert len(hash1) == 40
|
||||
188
tests/unit/test_core.py
Normal file
188
tests/unit/test_core.py
Normal file
@@ -0,0 +1,188 @@
|
||||
"""Unit tests for the validation engine."""
|
||||
|
||||
import pytest
|
||||
|
||||
from envschema.schema import Schema, EnvVar, EnvVarType
|
||||
from envschema.core import ValidationEngine, ValidationResult
|
||||
|
||||
|
||||
class TestValidationResult:
|
||||
"""Tests for ValidationResult."""
|
||||
|
||||
def test_valid_result(self):
|
||||
result = ValidationResult(is_valid=True)
|
||||
assert result.is_valid is True
|
||||
assert result.missing_required == []
|
||||
assert result.type_errors == []
|
||||
assert result.pattern_errors == []
|
||||
assert result.warnings == []
|
||||
|
||||
def test_result_to_dict(self):
|
||||
result = ValidationResult(is_valid=True)
|
||||
d = result.to_dict()
|
||||
assert d["is_valid"] is True
|
||||
assert d["missing_required"] == []
|
||||
|
||||
|
||||
class TestValidationEngine:
|
||||
"""Tests for ValidationEngine."""
|
||||
|
||||
def test_validate_empty_env(self):
|
||||
schema = Schema(envvars=[])
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({})
|
||||
assert result.is_valid is True
|
||||
|
||||
def test_validate_missing_required(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="REQUIRED_VAR", required=True),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({})
|
||||
assert result.is_valid is False
|
||||
assert "REQUIRED_VAR" in result.missing_required
|
||||
|
||||
def test_validate_present_required(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="REQUIRED_VAR", required=True),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"REQUIRED_VAR": "value"})
|
||||
assert result.is_valid is True
|
||||
|
||||
def test_validate_optional_missing(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="OPTIONAL_VAR", required=False),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({})
|
||||
assert result.is_valid is True
|
||||
|
||||
def test_validate_with_default(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="VAR_WITH_DEFAULT", required=False, default="default_value"),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({})
|
||||
assert result.is_valid is True
|
||||
|
||||
def test_validate_string_type(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="STRING_VAR", type=EnvVarType.STRING),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"STRING_VAR": "any value"})
|
||||
assert result.is_valid is True
|
||||
|
||||
def test_validate_integer_type_valid(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="INT_VAR", type=EnvVarType.INTEGER),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"INT_VAR": "42"})
|
||||
assert result.is_valid is True
|
||||
|
||||
def test_validate_integer_type_invalid(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="INT_VAR", type=EnvVarType.INTEGER),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"INT_VAR": "not_a_number"})
|
||||
assert result.is_valid is False
|
||||
assert len(result.type_errors) == 1
|
||||
assert result.type_errors[0].var_name == "INT_VAR"
|
||||
|
||||
def test_validate_boolean_type_valid(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="BOOL_VAR", type=EnvVarType.BOOLEAN),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"BOOL_VAR": "true"})
|
||||
assert result.is_valid is True
|
||||
|
||||
def test_validate_boolean_type_invalid(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="BOOL_VAR", type=EnvVarType.BOOLEAN),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"BOOL_VAR": "maybe"})
|
||||
assert result.is_valid is False
|
||||
|
||||
def test_validate_list_type_valid(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="LIST_VAR", type=EnvVarType.LIST),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"LIST_VAR": "a,b,c"})
|
||||
assert result.is_valid is True
|
||||
|
||||
def test_validate_list_type_invalid(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="LIST_VAR", type=EnvVarType.LIST),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"LIST_VAR": "single_value"})
|
||||
assert result.is_valid is False
|
||||
|
||||
def test_validate_pattern_match(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="PATTERN_VAR", type=EnvVarType.STRING, pattern=r"^[A-Z]+$"),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"PATTERN_VAR": "VALID"})
|
||||
assert result.is_valid is True
|
||||
|
||||
def test_validate_pattern_no_match(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="PATTERN_VAR", type=EnvVarType.STRING, pattern=r"^[A-Z]+$"),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"PATTERN_VAR": "invalid"})
|
||||
assert result.is_valid is False
|
||||
|
||||
def test_validate_extra_var_warning(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="KNOWN_VAR", type=EnvVarType.STRING),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"KNOWN_VAR": "value", "UNKNOWN_VAR": "other"})
|
||||
assert result.is_valid is True
|
||||
assert "Unknown environment variable: UNKNOWN_VAR" in result.warnings
|
||||
|
||||
def test_validate_case_insensitive(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="TEST_VAR", required=True),
|
||||
]
|
||||
)
|
||||
engine = ValidationEngine(schema)
|
||||
result = engine.validate({"test_var": "value"})
|
||||
assert result.is_valid is True
|
||||
105
tests/unit/test_generator.py
Normal file
105
tests/unit/test_generator.py
Normal file
@@ -0,0 +1,105 @@
|
||||
"""Unit tests for the .env.example generator."""
|
||||
|
||||
import pytest
|
||||
|
||||
from envschema.schema import Schema, EnvVar, EnvVarType
|
||||
from envschema.generator import generate_env_example, generate_env_example_to_file
|
||||
|
||||
|
||||
class TestGenerateEnvExample:
|
||||
"""Tests for generate_env_example function."""
|
||||
|
||||
def test_empty_schema(self):
|
||||
schema = Schema()
|
||||
result = generate_env_example(schema)
|
||||
assert "# Environment Variables Schema" in result
|
||||
|
||||
def test_basic_variable(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="TEST_VAR", type=EnvVarType.STRING),
|
||||
]
|
||||
)
|
||||
result = generate_env_example(schema)
|
||||
assert "TEST_VAR=" in result
|
||||
|
||||
def test_required_variable(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="REQUIRED_VAR", required=True),
|
||||
]
|
||||
)
|
||||
result = generate_env_example(schema)
|
||||
assert "# REQUIRED" in result
|
||||
assert "REQUIRED_VAR=" in result
|
||||
|
||||
def test_variable_with_default(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="VAR_WITH_DEFAULT", default="default_value"),
|
||||
]
|
||||
)
|
||||
result = generate_env_example(schema)
|
||||
assert "VAR_WITH_DEFAULT=default_value" in result
|
||||
|
||||
def test_variable_with_description(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(
|
||||
name="DESCRIBED_VAR",
|
||||
description="This is a description",
|
||||
),
|
||||
]
|
||||
)
|
||||
result = generate_env_example(schema)
|
||||
assert "# This is a description" in result
|
||||
|
||||
def test_variable_with_type(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="INT_VAR", type=EnvVarType.INTEGER),
|
||||
]
|
||||
)
|
||||
result = generate_env_example(schema)
|
||||
assert "INT_VAR=" in result
|
||||
|
||||
def test_no_descriptions(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(
|
||||
name="VAR",
|
||||
description="Some description",
|
||||
),
|
||||
]
|
||||
)
|
||||
result = generate_env_example(schema, include_descriptions=False)
|
||||
assert "Some description" not in result
|
||||
|
||||
def test_multiple_variables(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="VAR1", required=True, description="First var"),
|
||||
EnvVar(name="VAR2", default="value"),
|
||||
EnvVar(name="VAR3", type=EnvVarType.INTEGER),
|
||||
]
|
||||
)
|
||||
result = generate_env_example(schema)
|
||||
assert "VAR1=" in result
|
||||
assert "VAR2=value" in result
|
||||
assert "VAR3=" in result
|
||||
|
||||
|
||||
class TestGenerateEnvExampleToFile:
|
||||
"""Tests for generate_env_example_to_file function."""
|
||||
|
||||
def test_write_to_file(self, tmp_path):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="TEST_VAR"),
|
||||
]
|
||||
)
|
||||
output_path = tmp_path / ".env.example"
|
||||
generate_env_example_to_file(schema, str(output_path))
|
||||
|
||||
content = output_path.read_text()
|
||||
assert "TEST_VAR=" in content
|
||||
99
tests/unit/test_memory_service.py
Normal file
99
tests/unit/test_memory_service.py
Normal file
@@ -0,0 +1,99 @@
|
||||
"""Unit tests for MemoryService."""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from memory_manager.core.services import MemoryService, SearchService, CommitService
|
||||
from memory_manager.db.models import MemoryCategory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_repository():
|
||||
return AsyncMock()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def memory_service(mock_repository):
|
||||
return MemoryService(mock_repository)
|
||||
|
||||
|
||||
class TestMemoryService:
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_entry(self, memory_service, mock_repository):
|
||||
mock_entry = MagicMock()
|
||||
mock_entry.to_dict.return_value = {
|
||||
"id": 1,
|
||||
"title": "Test Entry",
|
||||
"content": "Test content",
|
||||
"category": "decision",
|
||||
"tags": ["test"],
|
||||
"agent_id": "test-agent",
|
||||
"project_path": "/test",
|
||||
"created_at": "2024-01-01T00:00:00",
|
||||
"updated_at": "2024-01-01T00:00:00",
|
||||
}
|
||||
mock_repository.create_entry = AsyncMock(return_value=mock_entry)
|
||||
|
||||
result = await memory_service.create_entry(
|
||||
title="Test Entry",
|
||||
content="Test content",
|
||||
category=MemoryCategory.DECISION,
|
||||
tags=["test"],
|
||||
agent_id="test-agent",
|
||||
project_path="/test",
|
||||
)
|
||||
|
||||
assert result["title"] == "Test Entry"
|
||||
assert result["id"] == 1
|
||||
mock_repository.create_entry.assert_called_once()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_entry(self, memory_service, mock_repository):
|
||||
mock_entry = MagicMock()
|
||||
mock_entry.to_dict.return_value = {"id": 1, "title": "Test"}
|
||||
mock_repository.get_entry = AsyncMock(return_value=mock_entry)
|
||||
|
||||
result = await memory_service.get_entry(1)
|
||||
|
||||
assert result["id"] == 1
|
||||
mock_repository.get_entry.assert_called_once_with(1)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_entry_not_found(self, memory_service, mock_repository):
|
||||
mock_repository.get_entry = AsyncMock(return_value=None)
|
||||
|
||||
result = await memory_service.get_entry(999)
|
||||
|
||||
assert result is None
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_update_entry(self, memory_service, mock_repository):
|
||||
mock_entry = MagicMock()
|
||||
mock_entry.to_dict.return_value = {"id": 1, "title": "Updated"}
|
||||
mock_repository.update_entry = AsyncMock(return_value=mock_entry)
|
||||
|
||||
result = await memory_service.update_entry(entry_id=1, title="Updated")
|
||||
|
||||
assert result["title"] == "Updated"
|
||||
mock_repository.update_entry.assert_called_once()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_delete_entry(self, memory_service, mock_repository):
|
||||
mock_repository.delete_entry = AsyncMock(return_value=True)
|
||||
|
||||
result = await memory_service.delete_entry(1)
|
||||
|
||||
assert result is True
|
||||
mock_repository.delete_entry.assert_called_once_with(1)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_entries(self, memory_service, mock_repository):
|
||||
mock_entries = [
|
||||
MagicMock(to_dict=lambda: {"id": 1, "title": "Entry 1"}),
|
||||
MagicMock(to_dict=lambda: {"id": 2, "title": "Entry 2"}),
|
||||
]
|
||||
mock_repository.list_entries = AsyncMock(return_value=mock_entries)
|
||||
|
||||
result = await memory_service.list_entries()
|
||||
|
||||
assert len(result) == 2
|
||||
assert result[0]["id"] == 1
|
||||
174
tests/unit/test_schema.py
Normal file
174
tests/unit/test_schema.py
Normal file
@@ -0,0 +1,174 @@
|
||||
"""Unit tests for schema parsing."""
|
||||
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from envschema.schema import (
|
||||
Schema,
|
||||
EnvVar,
|
||||
EnvVarType,
|
||||
load_schema_from_file,
|
||||
load_json_schema,
|
||||
load_yaml_schema,
|
||||
)
|
||||
|
||||
|
||||
class TestEnvVar:
|
||||
"""Tests for EnvVar model."""
|
||||
|
||||
def test_env_var_creation(self):
|
||||
var = EnvVar(name="TEST_VAR", type=EnvVarType.STRING)
|
||||
assert var.name == "TEST_VAR"
|
||||
assert var.type == EnvVarType.STRING
|
||||
assert var.required is False
|
||||
assert var.default is None
|
||||
|
||||
def test_env_var_with_all_fields(self):
|
||||
var = EnvVar(
|
||||
name="DATABASE_URL",
|
||||
type=EnvVarType.STRING,
|
||||
required=True,
|
||||
default="postgres://localhost",
|
||||
description="Database connection string",
|
||||
pattern=r"^postgres://.*",
|
||||
)
|
||||
assert var.required is True
|
||||
assert var.default == "postgres://localhost"
|
||||
assert var.description == "Database connection string"
|
||||
assert var.pattern == r"^postgres://.*"
|
||||
|
||||
def test_env_var_name_uppercase(self):
|
||||
var = EnvVar(name="test_var")
|
||||
assert var.name == "TEST_VAR"
|
||||
|
||||
def test_env_var_invalid_name(self):
|
||||
with pytest.raises(ValueError):
|
||||
EnvVar(name="invalid name with spaces")
|
||||
|
||||
|
||||
class TestSchema:
|
||||
"""Tests for Schema model."""
|
||||
|
||||
def test_schema_creation(self):
|
||||
schema = Schema()
|
||||
assert schema.version == "1.0"
|
||||
assert schema.envvars == []
|
||||
|
||||
def test_schema_with_vars(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="VAR1", type=EnvVarType.STRING),
|
||||
EnvVar(name="VAR2", type=EnvVarType.INTEGER, required=True),
|
||||
]
|
||||
)
|
||||
assert len(schema.envvars) == 2
|
||||
|
||||
def test_get_var(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="DATABASE_URL", type=EnvVarType.STRING),
|
||||
]
|
||||
)
|
||||
var = schema.get_var("DATABASE_URL")
|
||||
assert var is not None
|
||||
assert var.name == "DATABASE_URL"
|
||||
|
||||
def test_get_var_case_insensitive(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="DATABASE_URL", type=EnvVarType.STRING),
|
||||
]
|
||||
)
|
||||
var = schema.get_var("database_url")
|
||||
assert var is not None
|
||||
|
||||
def test_get_var_not_found(self):
|
||||
schema = Schema()
|
||||
var = schema.get_var("NONEXISTENT")
|
||||
assert var is None
|
||||
|
||||
def test_get_required_vars(self):
|
||||
schema = Schema(
|
||||
envvars=[
|
||||
EnvVar(name="VAR1", required=True),
|
||||
EnvVar(name="VAR2", required=False),
|
||||
EnvVar(name="VAR3", required=True),
|
||||
]
|
||||
)
|
||||
required = schema.get_required_vars()
|
||||
assert len(required) == 2
|
||||
assert {v.name for v in required} == {"VAR1", "VAR3"}
|
||||
|
||||
|
||||
class TestLoadJsonSchema:
|
||||
"""Tests for JSON schema loading."""
|
||||
|
||||
def test_load_valid_json_schema(self):
|
||||
json_content = json.dumps({
|
||||
"version": "1.0",
|
||||
"envVars": [
|
||||
{"name": "TEST_VAR", "type": "str"}
|
||||
]
|
||||
})
|
||||
schema = load_json_schema(json_content)
|
||||
assert schema.version == "1.0"
|
||||
assert len(schema.envvars) == 1
|
||||
|
||||
def test_load_invalid_json(self):
|
||||
with pytest.raises(ValueError, match="Invalid JSON"):
|
||||
load_json_schema("not valid json")
|
||||
|
||||
def test_load_invalid_schema_structure(self):
|
||||
with pytest.raises((ValueError, Exception), match="Invalid schema"):
|
||||
load_json_schema('{"version": "1.0", "envVars": [{"name": "VAR", "type": "invalid_type"}]}')
|
||||
|
||||
|
||||
class TestLoadYamlSchema:
|
||||
"""Tests for YAML schema loading."""
|
||||
|
||||
def test_load_valid_yaml_schema(self):
|
||||
yaml_content = """
|
||||
version: "1.0"
|
||||
envVars:
|
||||
- name: TEST_VAR
|
||||
type: str
|
||||
"""
|
||||
schema = load_yaml_schema(yaml_content)
|
||||
assert schema.version == "1.0"
|
||||
assert len(schema.envvars) == 1
|
||||
|
||||
def test_load_invalid_yaml(self):
|
||||
with pytest.raises(ValueError, match="Invalid YAML"):
|
||||
load_yaml_schema("invalid: yaml: content:")
|
||||
|
||||
|
||||
class TestLoadSchemaFromFile:
|
||||
"""Tests for file-based schema loading."""
|
||||
|
||||
def test_load_json_file(self, tmp_path):
|
||||
schema_file = tmp_path / "schema.json"
|
||||
schema_file.write_text(json.dumps({
|
||||
"version": "1.0",
|
||||
"envVars": [{"name": "TEST", "type": "str"}]
|
||||
}))
|
||||
schema = load_schema_from_file(str(schema_file))
|
||||
assert schema.version == "1.0"
|
||||
|
||||
def test_load_yaml_file(self, tmp_path):
|
||||
schema_file = tmp_path / "schema.yaml"
|
||||
schema_file.write_text('version: "1.0"\nenvVars: []')
|
||||
schema = load_schema_from_file(str(schema_file))
|
||||
assert schema.version == "1.0"
|
||||
|
||||
def test_file_not_found(self):
|
||||
with pytest.raises(FileNotFoundError):
|
||||
load_schema_from_file("/nonexistent/path/schema.json")
|
||||
|
||||
def test_unsupported_format(self, tmp_path):
|
||||
schema_file = tmp_path / "schema.txt"
|
||||
schema_file.write_text("some content")
|
||||
with pytest.raises(ValueError, match="Unsupported schema format"):
|
||||
load_schema_from_file(str(schema_file))
|
||||
67
tests/unit/test_search.py
Normal file
67
tests/unit/test_search.py
Normal file
@@ -0,0 +1,67 @@
|
||||
"""Unit tests for SearchService."""
|
||||
|
||||
import pytest
|
||||
from unittest.mock import AsyncMock, MagicMock
|
||||
from memory_manager.core.services import SearchService
|
||||
from memory_manager.db.models import MemoryCategory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_repository():
|
||||
return AsyncMock()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def search_service(mock_repository):
|
||||
return SearchService(mock_repository)
|
||||
|
||||
|
||||
class TestSearchService:
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_basic(self, search_service, mock_repository):
|
||||
mock_entries = [
|
||||
MagicMock(to_dict=lambda: {"id": 1, "title": "Test Entry", "content": "Test content"}),
|
||||
]
|
||||
mock_repository.search_entries = AsyncMock(return_value=mock_entries)
|
||||
|
||||
result = await search_service.search(query="test")
|
||||
|
||||
assert len(result) == 1
|
||||
assert result[0]["title"] == "Test Entry"
|
||||
mock_repository.search_entries.assert_called_once()
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_with_category(self, search_service, mock_repository):
|
||||
mock_repository.search_entries = AsyncMock(return_value=[])
|
||||
|
||||
await search_service.search(
|
||||
query="test",
|
||||
category=MemoryCategory.DECISION,
|
||||
)
|
||||
|
||||
call_args = mock_repository.search_entries.call_args
|
||||
assert call_args.kwargs["category"] == MemoryCategory.DECISION
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_with_filters(self, search_service, mock_repository):
|
||||
mock_repository.search_entries = AsyncMock(return_value=[])
|
||||
|
||||
await search_service.search(
|
||||
query="test",
|
||||
agent_id="test-agent",
|
||||
project_path="/test",
|
||||
limit=50,
|
||||
)
|
||||
|
||||
call_args = mock_repository.search_entries.call_args
|
||||
assert call_args.kwargs["agent_id"] == "test-agent"
|
||||
assert call_args.kwargs["project_path"] == "/test"
|
||||
assert call_args.kwargs["limit"] == 50
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_search_empty_results(self, search_service, mock_repository):
|
||||
mock_repository.search_entries = AsyncMock(return_value=[])
|
||||
|
||||
result = await search_service.search(query="nonexistent")
|
||||
|
||||
assert len(result) == 0
|
||||
176
tests/unit/test_validators.py
Normal file
176
tests/unit/test_validators.py
Normal file
@@ -0,0 +1,176 @@
|
||||
"""Unit tests for type validators."""
|
||||
|
||||
import pytest
|
||||
|
||||
from envschema.schema import EnvVarType
|
||||
from envschema.validators import (
|
||||
StringValidator,
|
||||
IntegerValidator,
|
||||
BooleanValidator,
|
||||
ListValidator,
|
||||
PatternValidator,
|
||||
validate_value,
|
||||
)
|
||||
|
||||
|
||||
class TestStringValidator:
|
||||
"""Tests for StringValidator."""
|
||||
|
||||
def test_valid_string(self):
|
||||
is_valid, error = StringValidator.validate("any value")
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_empty_string(self):
|
||||
is_valid, error = StringValidator.validate("")
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_none_value(self):
|
||||
is_valid, error = StringValidator.validate(None)
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
|
||||
class TestIntegerValidator:
|
||||
"""Tests for IntegerValidator."""
|
||||
|
||||
def test_valid_integer(self):
|
||||
is_valid, error = IntegerValidator.validate("42")
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_valid_negative_integer(self):
|
||||
is_valid, error = IntegerValidator.validate("-10")
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_valid_zero(self):
|
||||
is_valid, error = IntegerValidator.validate("0")
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_invalid_float(self):
|
||||
is_valid, error = IntegerValidator.validate("3.14")
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
|
||||
def test_invalid_string(self):
|
||||
is_valid, error = IntegerValidator.validate("abc")
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
|
||||
def test_none_value(self):
|
||||
is_valid, error = IntegerValidator.validate(None)
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
|
||||
class TestBooleanValidator:
|
||||
"""Tests for BooleanValidator."""
|
||||
|
||||
@pytest.mark.parametrize("value", ["true", "True", "TRUE", "1", "yes", "Yes", "YES", "on", "ON"])
|
||||
def test_valid_true_values(self, value):
|
||||
is_valid, error = BooleanValidator.validate(value)
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
@pytest.mark.parametrize("value", ["false", "False", "FALSE", "0", "no", "No", "NO", "off", "OFF"])
|
||||
def test_valid_false_values(self, value):
|
||||
is_valid, error = BooleanValidator.validate(value)
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
@pytest.mark.parametrize("value", ["maybe", "2", "truee", "yess"])
|
||||
def test_invalid_boolean_values(self, value):
|
||||
is_valid, error = BooleanValidator.validate(value)
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
|
||||
def test_none_value(self):
|
||||
is_valid, error = BooleanValidator.validate(None)
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
|
||||
class TestListValidator:
|
||||
"""Tests for ListValidator."""
|
||||
|
||||
def test_valid_list(self):
|
||||
is_valid, error = ListValidator.validate("item1,item2,item3")
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_single_item_list(self):
|
||||
is_valid, error = ListValidator.validate("single")
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
|
||||
def test_empty_string(self):
|
||||
is_valid, error = ListValidator.validate("")
|
||||
assert is_valid is False
|
||||
|
||||
def test_none_value(self):
|
||||
is_valid, error = ListValidator.validate(None)
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_parse_list(self):
|
||||
result = ListValidator.parse("item1, item2 , item3")
|
||||
assert result == ["item1", "item2", "item3"]
|
||||
|
||||
def test_parse_list_with_empty_items(self):
|
||||
result = ListValidator.parse("item1,,item2")
|
||||
assert result == ["item1", "item2"]
|
||||
|
||||
|
||||
class TestPatternValidator:
|
||||
"""Tests for PatternValidator."""
|
||||
|
||||
def test_valid_pattern_match(self):
|
||||
is_valid, error = PatternValidator.validate("ABC123", r"^[A-Z]+[0-9]+$")
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
def test_invalid_pattern_match(self):
|
||||
is_valid, error = PatternValidator.validate("abc123", r"^[A-Z]+[0-9]+$")
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
|
||||
def test_invalid_regex_pattern(self):
|
||||
is_valid, error = PatternValidator.validate("test", r"[invalid")
|
||||
assert is_valid is False
|
||||
assert error is not None
|
||||
|
||||
def test_none_value(self):
|
||||
is_valid, error = PatternValidator.validate(None, r"^[A-Z]+$")
|
||||
assert is_valid is True
|
||||
assert error is None
|
||||
|
||||
|
||||
class TestValidateValue:
|
||||
"""Tests for the main validate_value function."""
|
||||
|
||||
def test_validate_string(self):
|
||||
is_valid, error = validate_value("test", EnvVarType.STRING)
|
||||
assert is_valid is True
|
||||
|
||||
def test_validate_integer(self):
|
||||
is_valid, error = validate_value("42", EnvVarType.INTEGER)
|
||||
assert is_valid is True
|
||||
|
||||
def test_validate_boolean(self):
|
||||
is_valid, error = validate_value("true", EnvVarType.BOOLEAN)
|
||||
assert is_valid is True
|
||||
|
||||
def test_validate_list(self):
|
||||
is_valid, error = validate_value("a,b,c", EnvVarType.LIST)
|
||||
assert is_valid is True
|
||||
|
||||
def test_validate_with_pattern(self):
|
||||
is_valid, error = validate_value("ABC123", EnvVarType.STRING, r"^[A-Z]+[0-9]+$")
|
||||
assert is_valid is True
|
||||
|
||||
def test_validate_with_invalid_pattern(self):
|
||||
is_valid, error = validate_value("abc123", EnvVarType.STRING, r"^[A-Z]+[0-9]+$")
|
||||
assert is_valid is False
|
||||
Reference in New Issue
Block a user