Nexus / tests /test_models.py
abc1181's picture
REBUILD
aca03d2
Raw
History Blame Contribute Delete
3.29 kB
import pytest
from sqlalchemy import select
from app.models import (
Setting, User, ProviderKey, HubKey, ModelAlias, FallbackChain,
RoutingRule, CustomProvider, ModelPreset, ABTest, RequestTransformer,
ResponseFilter, Webhook, InjectionLog, ProviderUptime, RequestLog,
ConversationHistory, RagDocument, ExactCache, SemanticCache,
ModelCatalog, BatchJob, HubKeyModelLimit, RateLimitLog,
RealtimeSession, AgentConversation, Base,
)
from tests._helpers import db_session
class TestModels:
@pytest.mark.asyncio
async def test_create_setting(self, db_session):
s = Setting(key="test_key", value="test_value")
db_session.add(s)
await db_session.commit()
result = await db_session.execute(select(Setting).where(Setting.key == "test_key"))
row = result.scalar_one()
assert row.value == "test_value"
@pytest.mark.asyncio
async def test_create_user(self, db_session):
u = User(username="testuser", password_hash="hash123", role="admin")
db_session.add(u)
await db_session.commit()
result = await db_session.execute(select(User).where(User.username == "testuser"))
row = result.scalar_one()
assert row.role == "admin"
@pytest.mark.asyncio
async def test_create_provider_key(self, db_session):
pk = ProviderKey(provider="openai", name="test-key", api_key="sk-xxx")
db_session.add(pk)
await db_session.commit()
result = await db_session.execute(select(ProviderKey).where(ProviderKey.name == "test-key"))
row = result.scalar_one()
assert row.provider == "openai"
assert row.is_active == 1
@pytest.mark.asyncio
async def test_create_hub_key(self, db_session):
hk = HubKey(name="test-hub", key_hash="abc123", key_prefix="nxs-test")
db_session.add(hk)
await db_session.commit()
result = await db_session.execute(select(HubKey).where(HubKey.name == "test-hub"))
row = result.scalar_one()
assert row.key_prefix == "nxs-test"
@pytest.mark.asyncio
async def test_all_tables_registered(self):
tables = Base.metadata.tables
assert "settings" in tables
assert "users" in tables
assert "provider_keys" in tables
assert "hub_keys" in tables
assert "model_aliases" in tables
assert "fallback_chains" in tables
assert "routing_rules" in tables
assert "custom_providers" in tables
assert "model_presets" in tables
assert "ab_tests" in tables
assert "request_transformers" in tables
assert "response_filters" in tables
assert "webhooks" in tables
assert "injection_logs" in tables
assert "provider_uptime" in tables
assert "request_logs" in tables
assert "conversation_history" in tables
assert "rag_documents" in tables
assert "exact_cache" in tables
assert "semantic_cache" in tables
assert "model_catalog" in tables
assert "batch_jobs" in tables
assert "hub_key_model_limits" in tables
assert "rate_limit_log" in tables
assert "realtime_sessions" in tables
assert "agent_conversations" in tables
assert len(tables) == 27