feat: add Pydantic Settings configuration
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
from src.backend.config import settings
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def test_settings_loads_api_key():
|
||||
assert settings.api_key == "test-key-12345"
|
||||
|
||||
|
||||
def test_settings_loads_db_path():
|
||||
assert "test.db" in settings.db_path
|
||||
|
||||
|
||||
def test_settings_loads_worker_url():
|
||||
assert settings.worker_url == "http://localhost:3001"
|
||||
|
||||
|
||||
def test_settings_loads_claude_model():
|
||||
assert settings.claude_model == "claude-sonnet-4-6"
|
||||
|
||||
|
||||
def test_settings_loads_rate_limits():
|
||||
assert settings.rate_limit_per_minute == 100
|
||||
assert settings.rate_limit_per_hour == 1000
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_init_db_creates_tables():
|
||||
from src.backend.db.database import init_db, get_db
|
||||
|
||||
await init_db()
|
||||
db = await get_db()
|
||||
try:
|
||||
cursor = await db.execute(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"
|
||||
)
|
||||
tables = [row[0] for row in await cursor.fetchall()]
|
||||
assert "topics" in tables
|
||||
assert "requests" in tables
|
||||
finally:
|
||||
await db.close()
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_foreign_keys_enabled():
|
||||
from src.backend.db.database import init_db, get_db
|
||||
|
||||
await init_db()
|
||||
db = await get_db()
|
||||
try:
|
||||
cursor = await db.execute("PRAGMA foreign_keys")
|
||||
row = await cursor.fetchone()
|
||||
assert row[0] == 1
|
||||
finally:
|
||||
await db.close()
|
||||
|
||||
|
||||
def test_topic_create_model_validates():
|
||||
from src.backend.models.api import TopicCreate
|
||||
|
||||
topic = TopicCreate(
|
||||
name="coding",
|
||||
system_prompt="You are an expert coder.",
|
||||
summary="Coding questions",
|
||||
model="claude-sonnet-4-6",
|
||||
)
|
||||
assert topic.name == "coding"
|
||||
assert topic.model == "claude-sonnet-4-6"
|
||||
|
||||
|
||||
def test_topic_create_model_optional_fields():
|
||||
from src.backend.models.api import TopicCreate
|
||||
|
||||
topic = TopicCreate(name="coding", system_prompt="You are an expert coder.")
|
||||
assert topic.summary == ""
|
||||
assert topic.model is None
|
||||
|
||||
|
||||
def test_request_create_model_validates():
|
||||
from src.backend.models.api import RequestCreate
|
||||
|
||||
req = RequestCreate(
|
||||
prompt="How to do X?",
|
||||
topic_id="550e8400-e29b-41d4-a716-446655440000",
|
||||
)
|
||||
assert req.session_id is None
|
||||
assert req.model is None
|
||||
assert req.summarize is False
|
||||
|
||||
|
||||
def test_model_validation_rejects_invalid():
|
||||
from src.backend.models.api import TopicCreate
|
||||
from pydantic import ValidationError
|
||||
|
||||
with pytest.raises(ValidationError):
|
||||
TopicCreate(name="test", system_prompt="test", model="gpt-4")
|
||||
|
||||
|
||||
def test_envelope_ok():
|
||||
from src.backend.models.api import ok
|
||||
|
||||
result = ok({"key": "value"})
|
||||
assert result == {"success": True, "data": {"key": "value"}, "error": None}
|
||||
|
||||
|
||||
def test_envelope_fail():
|
||||
from src.backend.models.api import fail
|
||||
|
||||
result = fail("NOT_FOUND", "Resource not found")
|
||||
assert result["success"] is False
|
||||
assert result["error"]["code"] == "NOT_FOUND"
|
||||
Reference in New Issue
Block a user