|
|
"""Tests for utility functions and configurations.""" |
|
|
|
|
|
import os |
|
|
import tempfile |
|
|
from pathlib import Path |
|
|
|
|
|
from create_env_example import env_content |
|
|
|
|
|
|
|
|
class TestEnvironmentConfiguration: |
|
|
"""Test environment configuration utilities.""" |
|
|
|
|
|
def test_env_example_content_structure(self): |
|
|
"""Test .env.example content has required sections.""" |
|
|
|
|
|
assert "# API Keys" in env_content |
|
|
assert "# Azure OpenAI" in env_content |
|
|
assert "# Knowledge Graph Configuration" in env_content |
|
|
assert "# Application Settings" in env_content |
|
|
assert "# Gradio Configuration" in env_content |
|
|
assert "# Security" in env_content |
|
|
|
|
|
def test_env_example_required_variables(self): |
|
|
"""Test .env.example contains required environment variables.""" |
|
|
required_vars = [ |
|
|
"OPENAI_API_KEY", |
|
|
"APP_ENV", |
|
|
"APP_PORT", |
|
|
"GRADIO_SERVER_PORT", |
|
|
"LOG_LEVEL", |
|
|
] |
|
|
|
|
|
for var in required_vars: |
|
|
assert var in env_content |
|
|
|
|
|
def test_env_example_no_real_secrets(self): |
|
|
"""Test .env.example doesn't contain real secrets.""" |
|
|
|
|
|
assert "sk-" not in env_content |
|
|
assert "your_" in env_content |
|
|
assert "localhost" in env_content |
|
|
|
|
|
def test_env_example_creates_file(self): |
|
|
"""Test create_env_example.py creates the file correctly.""" |
|
|
with tempfile.TemporaryDirectory() as temp_dir: |
|
|
|
|
|
original_cwd = Path.cwd() |
|
|
try: |
|
|
os.chdir(temp_dir) |
|
|
|
|
|
|
|
|
with Path(".env.example").open("w") as f: |
|
|
f.write(env_content) |
|
|
|
|
|
|
|
|
env_file = Path(".env.example") |
|
|
assert env_file.exists() |
|
|
|
|
|
|
|
|
content = env_file.read_text() |
|
|
assert "OPENAI_API_KEY" in content |
|
|
assert "APP_ENV" in content |
|
|
|
|
|
finally: |
|
|
os.chdir(original_cwd) |
|
|
|
|
|
|
|
|
class TestProjectStructure: |
|
|
"""Test project structure and configuration.""" |
|
|
|
|
|
def test_required_directories_exist(self): |
|
|
"""Test required project directories exist.""" |
|
|
required_dirs = ["agents", "kg_services", "tests", "data", "docs", "scripts"] |
|
|
|
|
|
for dir_name in required_dirs: |
|
|
assert Path(dir_name).exists(), f"Required directory {dir_name} not found" |
|
|
|
|
|
def test_required_files_exist(self): |
|
|
"""Test required project files exist.""" |
|
|
required_files = [ |
|
|
"app.py", |
|
|
"simple_app.py", |
|
|
"pyproject.toml", |
|
|
"requirements.txt", |
|
|
"README.md", |
|
|
] |
|
|
|
|
|
for file_name in required_files: |
|
|
assert Path(file_name).exists(), f"Required file {file_name} not found" |
|
|
|
|
|
def test_data_files_exist(self): |
|
|
"""Test required data files exist.""" |
|
|
data_files = ["data/initial_tools.json", "data/initial_prompts.json"] |
|
|
|
|
|
for file_name in data_files: |
|
|
assert Path(file_name).exists(), f"Required data file {file_name} not found" |
|
|
|
|
|
def test_init_files_exist(self): |
|
|
"""Test __init__.py files exist in Python packages.""" |
|
|
init_files = [ |
|
|
"agents/__init__.py", |
|
|
"kg_services/__init__.py", |
|
|
"tests/__init__.py", |
|
|
] |
|
|
|
|
|
for file_name in init_files: |
|
|
assert Path( |
|
|
file_name |
|
|
).exists(), f"Required __init__.py file {file_name} not found" |
|
|
|
|
|
|
|
|
class TestConfigurationFiles: |
|
|
"""Test configuration files are properly structured.""" |
|
|
|
|
|
def test_pyproject_toml_exists_and_valid(self): |
|
|
"""Test pyproject.toml exists and has required sections.""" |
|
|
pyproject_path = Path("pyproject.toml") |
|
|
assert pyproject_path.exists() |
|
|
|
|
|
content = pyproject_path.read_text() |
|
|
|
|
|
|
|
|
assert "[build-system]" in content |
|
|
assert "[project]" in content |
|
|
assert 'name = "kgraph-mcp"' in content |
|
|
|
|
|
def test_requirements_files_exist(self): |
|
|
"""Test requirements files exist.""" |
|
|
req_files = ["requirements.txt", "requirements-dev.txt"] |
|
|
|
|
|
for file_name in req_files: |
|
|
req_path = Path(file_name) |
|
|
assert req_path.exists(), f"Requirements file {file_name} not found" |
|
|
|
|
|
|
|
|
content = req_path.read_text().strip() |
|
|
assert len(content) > 0, f"Requirements file {file_name} is empty" |
|
|
|
|
|
def test_justfile_exists(self): |
|
|
"""Test justfile exists for task automation.""" |
|
|
justfile_path = Path("justfile") |
|
|
assert justfile_path.exists() |
|
|
|
|
|
content = justfile_path.read_text() |
|
|
|
|
|
|
|
|
assert "test" in content |
|
|
assert "lint" in content |
|
|
assert "format" in content |
|
|
|
|
|
|
|
|
class TestImportStructure: |
|
|
"""Test that all modules can be imported without errors.""" |
|
|
|
|
|
def test_import_agents_module(self): |
|
|
"""Test agents module can be imported.""" |
|
|
import agents |
|
|
import agents.planner |
|
|
|
|
|
|
|
|
assert hasattr(agents.planner, "SimplePlannerAgent") |
|
|
|
|
|
def test_import_kg_services_module(self): |
|
|
"""Test kg_services module can be imported.""" |
|
|
import kg_services |
|
|
import kg_services.embedder |
|
|
import kg_services.knowledge_graph |
|
|
import kg_services.ontology |
|
|
|
|
|
|
|
|
assert hasattr(kg_services.knowledge_graph, "InMemoryKG") |
|
|
assert hasattr(kg_services.ontology, "MCPTool") |
|
|
assert hasattr(kg_services.embedder, "EmbeddingService") |
|
|
|
|
|
def test_import_main_apps(self): |
|
|
"""Test main application files can be imported.""" |
|
|
import app |
|
|
import simple_app |
|
|
|
|
|
|
|
|
assert hasattr(app, "app") |
|
|
assert hasattr(simple_app, "app") |
|
|
|