chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动

演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
wuzilongandCursor committed 2026-11-18 13:59:25 +08:00
1 parent 60158935f4
commit 6519682885
15831 files changed
+3063428

No files matched your search

Whitespace-only changes.
@@ -0,0 +1,760 @@
"""
Comprehensive unit tests for Account model.
This test suite covers:
- Account model validation
- Password hashing/verification
- Account status transitions
- Tenant relationship integrity
- Email uniqueness constraints
"""
import base64
import secrets
from collections.abc import Callable
from datetime import UTC, datetime
from uuid import uuid4
import pytest
from sqlalchemy.orm import Session
from libs.password import compare_password, hash_password, valid_password
from models.account import Account, AccountStatus, Tenant, TenantAccountJoin, TenantAccountRole
class TestAccountModelValidation:
"""Test suite for Account model validation and basic operations."""
def test_account_creation_with_required_fields(self):
"""Test creating an account with all required fields."""
# Arrange & Act
account = Account(
name="Test User",
email="test@example.com",
password="hashed_password",
password_salt="salt_value",
)
# Assert
assert account.name == "Test User"
assert account.email == "test@example.com"
assert account.password == "hashed_password"
assert account.password_salt == "salt_value"
assert account.status == "active" # Default value
def test_account_creation_with_optional_fields(self):
"""Test creating an account with optional fields."""
# Arrange & Act
account = Account(
name="Test User",
email="test@example.com",
avatar="https://example.com/avatar.png",
interface_language="en-US",
interface_theme="dark",
timezone="America/New_York",
)
# Assert
assert account.avatar == "https://example.com/avatar.png"
assert account.interface_language == "en-US"
assert account.interface_theme == "dark"
assert account.timezone == "America/New_York"
def test_account_creation_without_password(self):
"""Test creating an account without password (for invite-based registration)."""
# Arrange & Act
account = Account(
name="Invited User",
email="invited@example.com",
)
# Assert
assert account.password is None
assert account.password_salt is None
assert not account.is_password_set
def test_account_is_password_set_property(self):
"""Test the is_password_set property."""
# Arrange
account_with_password = Account(
name="User With Password",
email="withpass@example.com",
password="hashed_password",
)
account_without_password = Account(
name="User Without Password",
email="nopass@example.com",
)
# Assert
assert account_with_password.is_password_set
assert not account_without_password.is_password_set
def test_account_default_status(self):
"""Test that account has default status of 'active'."""
# Arrange & Act
account = Account(
name="Test User",
email="test@example.com",
)
# Assert
assert account.status == AccountStatus.ACTIVE
def test_account_get_status_method(self):
"""Test the get_status method returns AccountStatus enum."""
# Arrange
account = Account(
name="Test User",
email="test@example.com",
status=AccountStatus.PENDING,
)
# Act
status = account.get_status()
# Assert
assert status == AccountStatus.PENDING
assert isinstance(status, AccountStatus)
class TestPasswordHashingAndVerification:
"""Test suite for password hashing and verification functionality."""
def test_password_hashing_produces_consistent_result(self):
"""Test that hashing the same password with the same salt produces the same result."""
# Arrange
password = "TestPassword123"
salt = secrets.token_bytes(16)
# Act
hash1 = hash_password(password, salt)
hash2 = hash_password(password, salt)
# Assert
assert hash1 == hash2
def test_password_hashing_different_salts_produce_different_hashes(self):
"""Test that different salts produce different hashes for the same password."""
# Arrange
password = "TestPassword123"
salt1 = secrets.token_bytes(16)
salt2 = secrets.token_bytes(16)
# Act
hash1 = hash_password(password, salt1)
hash2 = hash_password(password, salt2)
# Assert
assert hash1 != hash2
def test_password_comparison_success(self):
"""Test successful password comparison."""
# Arrange
password = "TestPassword123"
salt = secrets.token_bytes(16)
password_hashed = hash_password(password, salt)
# Encode to base64 as done in the application
base64_salt = base64.b64encode(salt).decode()
base64_password_hashed = base64.b64encode(password_hashed).decode()
# Act
result = compare_password(password, base64_password_hashed, base64_salt)
# Assert
assert result is True
def test_password_comparison_failure(self):
"""Test password comparison with wrong password."""
# Arrange
correct_password = "TestPassword123"
wrong_password = "WrongPassword456"
salt = secrets.token_bytes(16)
password_hashed = hash_password(correct_password, salt)
# Encode to base64
base64_salt = base64.b64encode(salt).decode()
base64_password_hashed = base64.b64encode(password_hashed).decode()
# Act
result = compare_password(wrong_password, base64_password_hashed, base64_salt)
# Assert
assert result is False
def test_valid_password_with_correct_format(self):
"""Test password validation with correct format."""
# Arrange
valid_passwords = [
"Password123",
"Test1234",
"MySecure1Pass",
"abcdefgh1",
]
# Act & Assert
for password in valid_passwords:
result = valid_password(password)
assert result == password
def test_valid_password_with_incorrect_format(self):
"""Test password validation with incorrect format."""
# Arrange
invalid_passwords = [
"short1", # Too short
"NoNumbers", # No numbers
"12345678", # No letters
"Pass1", # Too short
]
# Act & Assert
for password in invalid_passwords:
with pytest.raises(ValueError, match="Password must contain letters and numbers"):
valid_password(password)
def test_password_hashing_integration_with_account(self):
"""Test password hashing integration with Account model."""
# Arrange
password = "SecurePass123"
salt = secrets.token_bytes(16)
base64_salt = base64.b64encode(salt).decode()
password_hashed = hash_password(password, salt)
base64_password_hashed = base64.b64encode(password_hashed).decode()
# Act
account = Account(
name="Test User",
email="test@example.com",
password=base64_password_hashed,
password_salt=base64_salt,
)
# Assert
assert account.is_password_set
assert compare_password(password, account.password, account.password_salt)
class TestAccountStatusTransitions:
"""Test suite for account status transitions."""
def test_account_status_enum_values(self):
"""Test that AccountStatus enum has all expected values."""
# Assert
assert AccountStatus.PENDING == "pending"
assert AccountStatus.UNINITIALIZED == "uninitialized"
assert AccountStatus.ACTIVE == "active"
assert AccountStatus.BANNED == "banned"
assert AccountStatus.CLOSED == "closed"
def test_account_status_transition_pending_to_active(self):
"""Test transitioning account status from pending to active."""
# Arrange
account = Account(
name="Test User",
email="test@example.com",
status=AccountStatus.PENDING,
)
# Act
account.status = AccountStatus.ACTIVE
account.initialized_at = datetime.now(UTC)
# Assert
assert account.get_status() == AccountStatus.ACTIVE
assert account.initialized_at is not None
def test_account_status_transition_active_to_banned(self):
"""Test transitioning account status from active to banned."""
# Arrange
account = Account(
name="Test User",
email="test@example.com",
status=AccountStatus.ACTIVE,
)
# Act
account.status = AccountStatus.BANNED
# Assert
assert account.get_status() == AccountStatus.BANNED
def test_account_status_transition_active_to_closed(self):
"""Test transitioning account status from active to closed."""
# Arrange
account = Account(
name="Test User",
email="test@example.com",
status=AccountStatus.ACTIVE,
)
# Act
account.status = AccountStatus.CLOSED
# Assert
assert account.get_status() == AccountStatus.CLOSED
def test_account_status_uninitialized(self):
"""Test account with uninitialized status."""
# Arrange & Act
account = Account(
name="Test User",
email="test@example.com",
status=AccountStatus.UNINITIALIZED,
)
# Assert
assert account.get_status() == AccountStatus.UNINITIALIZED
assert account.initialized_at is None
class TestTenantRelationshipIntegrity:
"""Test suite for tenant relationship integrity."""
def test_account_current_tenant_id_property(self):
"""Test the current_tenant_id property."""
# Arrange
account = Account(
name="Test User",
email="test@example.com",
)
tenant = Tenant(name="Test Tenant")
tenant.id = str(uuid4())
# Act - with tenant
account._current_tenant = tenant
tenant_id = account.current_tenant_id
# Assert
assert tenant_id == tenant.id
# Act - without tenant
account._current_tenant = None
tenant_id_none = account.current_tenant_id
# Assert
assert tenant_id_none is None
def test_current_tenant_is_read_only(self):
"""current_tenant has no setter, so tenant switching cannot open a hidden session."""
account = Account(name="Test User", email="test@example.com")
tenant = Tenant(name="Test Tenant")
with pytest.raises(AttributeError):
account.current_tenant = tenant # type: ignore[misc]
@pytest.mark.parametrize("sqlite_session", [(Account, Tenant, TenantAccountJoin)], indirect=True)
def test_set_current_tenant_with_session_uses_caller_session(self, sqlite_session: Session):
account = Account(name="Test User", email="test@example.com")
account.id = str(uuid4())
tenant = Tenant(name="Test Tenant")
tenant.id = str(uuid4())
decoy_tenant = Tenant(name="Decoy Tenant")
decoy_tenant.id = str(uuid4())
sqlite_session.add_all(
[
account,
tenant,
decoy_tenant,
TenantAccountJoin(
tenant_id=tenant.id,
account_id=account.id,
role=TenantAccountRole.OWNER,
),
TenantAccountJoin(
tenant_id=decoy_tenant.id,
account_id=account.id,
role=TenantAccountRole.NORMAL,
),
]
)
sqlite_session.flush()
account.set_current_tenant_with_session(tenant, session=sqlite_session)
assert account.current_tenant is tenant
assert account.role == TenantAccountRole.OWNER
@pytest.mark.parametrize("sqlite_session", [(Account, Tenant, TenantAccountJoin)], indirect=True)
def test_set_tenant_id_with_session_uses_caller_session(self, sqlite_session: Session):
account = Account(name="Test User", email="test@example.com")
account.id = str(uuid4())
decoy_account = Account(name="Decoy User", email="decoy@example.com")
decoy_account.id = str(uuid4())
tenant = Tenant(name="Test Tenant")
tenant.id = str(uuid4())
sqlite_session.add_all(
[
account,
decoy_account,
tenant,
TenantAccountJoin(
tenant_id=tenant.id,
account_id=account.id,
role=TenantAccountRole.ADMIN,
),
TenantAccountJoin(
tenant_id=tenant.id,
account_id=decoy_account.id,
role=TenantAccountRole.OWNER,
),
]
)
sqlite_session.flush()
account.set_tenant_id_with_session(tenant.id, session=sqlite_session)
assert account.current_tenant is tenant
assert account.role == TenantAccountRole.ADMIN
class TestAccountRolePermissions:
@pytest.fixture(autouse=True)
def _rbac_disabled(self, config_overrides: Callable[..., None]) -> None:
config_overrides(RBAC_ENABLED=False)
"""Test suite for account role permissions."""
def test_is_admin_or_owner_with_admin_role(self):
"""Test is_admin_or_owner property with admin role."""
# Arrange
account = Account(
name="Test User",
email="test@example.com",
)
account.role = TenantAccountRole.ADMIN
assert account.is_admin_or_owner
def test_is_admin_or_owner_with_rbac_enabled(self, config_overrides: Callable[..., None]):
account = Account(name="Test User", email="test@example.com")
account.role = TenantAccountRole.NORMAL
config_overrides(RBAC_ENABLED=True)
assert account.is_admin_or_owner
def test_is_admin_or_owner_with_owner_role(self):
"""Test is_admin_or_owner property with owner role."""
# Arrange
account = Account(
name="Test User",
email="test@example.com",
)
account.role = TenantAccountRole.OWNER
# Act & Assert
assert account.is_admin_or_owner
def test_is_admin_or_owner_with_normal_role(self):
"""Test is_admin_or_owner property with normal role."""
# Arrange
account = Account(
name="Test User",
email="test@example.com",
)
account.role = TenantAccountRole.NORMAL
# Act & Assert
assert not account.is_admin_or_owner
def test_is_admin_property(self):
"""Test is_admin property."""
# Arrange
admin_account = Account(name="Admin", email="admin@example.com")
admin_account.role = TenantAccountRole.ADMIN
owner_account = Account(name="Owner", email="owner@example.com")
owner_account.role = TenantAccountRole.OWNER
assert admin_account.is_admin
assert not owner_account.is_admin
def test_is_admin_with_rbac_enabled(self, config_overrides: Callable[..., None]):
account = Account(name="Test User", email="test@example.com")
account.role = TenantAccountRole.NORMAL
config_overrides(RBAC_ENABLED=True)
assert account.is_admin
def test_has_edit_permission_with_editing_roles(self):
"""Test has_edit_permission property with roles that have edit permission."""
# Arrange
roles_with_edit = [
TenantAccountRole.OWNER,
TenantAccountRole.ADMIN,
TenantAccountRole.EDITOR,
]
for role in roles_with_edit:
account = Account(name="Test User", email=f"test_{role}@example.com")
account.role = role
assert account.has_edit_permission, f"Role {role} should have edit permission"
def test_has_edit_permission_with_rbac_enabled(self, config_overrides: Callable[..., None]):
account = Account(name="Test User", email="test@example.com")
account.role = TenantAccountRole.NORMAL
config_overrides(RBAC_ENABLED=True)
assert account.has_edit_permission
def test_has_edit_permission_without_editing_roles(self):
"""Test has_edit_permission property with roles that don't have edit permission."""
# Arrange
roles_without_edit = [
TenantAccountRole.NORMAL,
TenantAccountRole.DATASET_OPERATOR,
]
for role in roles_without_edit:
account = Account(name="Test User", email=f"test_{role}@example.com")
account.role = role
assert not account.has_edit_permission, f"Role {role} should not have edit permission"
def test_is_dataset_editor_property(self):
"""Test is_dataset_editor property."""
# Arrange
dataset_roles = [
TenantAccountRole.OWNER,
TenantAccountRole.ADMIN,
TenantAccountRole.EDITOR,
TenantAccountRole.DATASET_OPERATOR,
]
for role in dataset_roles:
account = Account(name="Test User", email=f"test_{role}@example.com")
account.role = role
assert account.is_dataset_editor, f"Role {role} should have dataset edit permission"
# Test normal role doesn't have dataset edit permission
normal_account = Account(name="Normal User", email="normal@example.com")
normal_account.role = TenantAccountRole.NORMAL
assert not normal_account.is_dataset_editor
def test_is_dataset_editor_with_rbac_enabled(self, config_overrides: Callable[..., None]):
account = Account(name="Test User", email="test@example.com")
account.role = TenantAccountRole.NORMAL
config_overrides(RBAC_ENABLED=True)
assert account.is_dataset_editor
def test_is_dataset_operator_property(self):
"""Test is_dataset_operator property."""
# Arrange
dataset_operator = Account(name="Dataset Operator", email="operator@example.com")
dataset_operator.role = TenantAccountRole.DATASET_OPERATOR
normal_account = Account(name="Normal User", email="normal@example.com")
normal_account.role = TenantAccountRole.NORMAL
assert dataset_operator.is_dataset_operator
assert not normal_account.is_dataset_operator
def test_is_dataset_operator_with_rbac_enabled(self, config_overrides: Callable[..., None]):
account = Account(name="Test User", email="test@example.com")
account.role = TenantAccountRole.NORMAL
config_overrides(RBAC_ENABLED=True)
assert account.is_dataset_operator
def test_current_role_property(self):
"""Test current_role property."""
# Arrange
account = Account(name="Test User", email="test@example.com")
account.role = TenantAccountRole.EDITOR
# Act
current_role = account.current_role
# Assert
assert current_role == TenantAccountRole.EDITOR
class TestTenantAccountJoinModel:
"""Test suite for TenantAccountJoin model."""
def test_tenant_account_join_creation(self):
"""Test creating a TenantAccountJoin record."""
# Arrange
tenant_id = str(uuid4())
account_id = str(uuid4())
# Act
join = TenantAccountJoin(
tenant_id=tenant_id,
account_id=account_id,
role=TenantAccountRole.NORMAL,
current=True,
)
# Assert
assert join.tenant_id == tenant_id
assert join.account_id == account_id
assert join.role == TenantAccountRole.NORMAL
assert join.current is True
def test_tenant_account_join_default_values(self):
"""Test default values for TenantAccountJoin."""
# Arrange
tenant_id = str(uuid4())
account_id = str(uuid4())
# Act
join = TenantAccountJoin(
tenant_id=tenant_id,
account_id=account_id,
)
# Assert
assert join.current is False # Default value
assert join.role == "normal" # Default value
assert join.invited_by is None # Default value
def test_tenant_account_join_with_invited_by(self):
"""Test TenantAccountJoin with invited_by field."""
# Arrange
tenant_id = str(uuid4())
account_id = str(uuid4())
inviter_id = str(uuid4())
# Act
join = TenantAccountJoin(
tenant_id=tenant_id,
account_id=account_id,
role=TenantAccountRole.EDITOR,
invited_by=inviter_id,
)
# Assert
assert join.invited_by == inviter_id
class TestTenantModel:
"""Test suite for Tenant model."""
def test_tenant_creation(self):
"""Test creating a Tenant."""
# Arrange & Act
tenant = Tenant(name="Test Workspace")
# Assert
assert tenant.name == "Test Workspace"
assert tenant.status == "normal" # Default value
assert tenant.plan == "basic" # Default value
def test_tenant_custom_config_dict_property(self):
"""Test custom_config_dict property getter."""
# Arrange
tenant = Tenant(name="Test Workspace")
tenant.custom_config = '{"feature1": true, "feature2": "value"}'
# Act
result = tenant.custom_config_dict
# Assert
assert result["feature1"] is True
assert result["feature2"] == "value"
def test_tenant_custom_config_dict_property_empty(self):
"""Test custom_config_dict property with empty config."""
# Arrange
tenant = Tenant(name="Test Workspace")
tenant.custom_config = None
# Act
result = tenant.custom_config_dict
# Assert
assert result == {}
def test_tenant_custom_config_dict_setter(self):
"""Test custom_config_dict property setter."""
# Arrange
tenant = Tenant(name="Test Workspace")
config = {"feature1": True, "feature2": "value"}
# Act
tenant.custom_config_dict = config
# Assert
assert tenant.custom_config == '{"feature1": true, "feature2": "value"}'
class TestTenantStatusEnum:
"""Test suite for TenantStatus enum."""
def test_tenant_status_enum_values(self):
"""Test TenantStatus enum values."""
# Arrange & Act
from models.account import TenantStatus
# Assert
assert TenantStatus.NORMAL == "normal"
assert TenantStatus.ARCHIVE == "archive"
class TestAccountIntegration:
"""Integration tests for Account model with related models."""
def test_account_with_multiple_tenants(self):
"""Test account associated with multiple tenants."""
# Arrange
account = Account(name="Multi-Tenant User", email="multi@example.com")
account.id = str(uuid4())
tenant1_id = str(uuid4())
tenant2_id = str(uuid4())
join1 = TenantAccountJoin(
tenant_id=tenant1_id,
account_id=account.id,
role=TenantAccountRole.OWNER,
current=True,
)
join2 = TenantAccountJoin(
tenant_id=tenant2_id,
account_id=account.id,
role=TenantAccountRole.NORMAL,
current=False,
)
# Assert - verify the joins are created correctly
assert join1.account_id == account.id
assert join2.account_id == account.id
assert join1.current is True
assert join2.current is False
def test_account_last_login_tracking(self):
"""Test account last login tracking."""
# Arrange
account = Account(name="Test User", email="test@example.com")
login_time = datetime.now(UTC)
login_ip = "192.168.1.1"
# Act
account.last_login_at = login_time
account.last_login_ip = login_ip
# Assert
assert account.last_login_at == login_time
assert account.last_login_ip == login_ip
def test_account_initialization_tracking(self):
"""Test account initialization tracking."""
# Arrange
account = Account(
name="Test User",
email="test@example.com",
status=AccountStatus.PENDING,
)
# Act - simulate initialization
account.status = AccountStatus.ACTIVE
account.initialized_at = datetime.now(UTC)
# Assert
assert account.get_status() == AccountStatus.ACTIVE
assert account.initialized_at is not None
@@ -0,0 +1,334 @@
import json
from collections.abc import Callable
from typing import Protocol, cast
import pytest
import sqlalchemy as sa
from sqlalchemy.exc import IntegrityError
from models.agent import (
Agent,
AgentConfigDraft,
AgentConfigDraftType,
AgentConfigRevision,
AgentConfigRevisionOperation,
AgentConfigSnapshot,
AgentConfigVersionKind,
AgentDebugConversation,
AgentHomeSnapshot,
AgentIconType,
AgentKind,
AgentScope,
AgentSource,
AgentStatus,
AgentWorkspace,
AgentWorkspaceBinding,
AgentWorkspaceOwnerType,
WorkflowAgentBindingType,
WorkflowAgentNodeBinding,
)
from models.agent_config_entities import AgentSoulConfig, WorkflowNodeJobConfig
from models.base import Base, TypeBase
from models.types import JSONModelColumn, LongText
class _AgentModel(Protocol):
id: str
@pytest.mark.parametrize(
("model", "required_values"),
[
(
Agent,
{
"tenant_id": "tenant-1",
"name": "Agent",
"scope": AgentScope.ROSTER,
"source": AgentSource.ROSTER,
},
),
(AgentHomeSnapshot, {"tenant_id": "tenant-1", "agent_id": "agent-1", "snapshot_ref": "backend-home"}),
(
AgentDebugConversation,
{
"tenant_id": "tenant-1",
"agent_id": "agent-1",
"app_id": "app-1",
"account_id": "account-1",
"conversation_id": "conversation-1",
},
),
(
AgentConfigDraft,
{
"tenant_id": "tenant-1",
"agent_id": "agent-1",
"draft_type": AgentConfigDraftType.DRAFT,
"config_snapshot": AgentSoulConfig(),
},
),
(
AgentConfigSnapshot,
{"tenant_id": "tenant-1", "agent_id": "agent-1", "version": 1, "config_snapshot": AgentSoulConfig()},
),
(
AgentConfigRevision,
{
"tenant_id": "tenant-1",
"agent_id": "agent-1",
"current_snapshot_id": "snapshot-1",
"revision": 1,
"operation": AgentConfigRevisionOperation.CREATE_VERSION,
},
),
(
WorkflowAgentNodeBinding,
{
"tenant_id": "tenant-1",
"app_id": "app-1",
"workflow_id": "workflow-1",
"workflow_version": "draft",
"node_id": "node-1",
"binding_type": WorkflowAgentBindingType.ROSTER_AGENT,
"node_job_config": WorkflowNodeJobConfig(),
},
),
(
AgentWorkspace,
{
"tenant_id": "tenant-1",
"app_id": "app-1",
"owner_type": AgentWorkspaceOwnerType.CONVERSATION,
"owner_id": "conversation-1",
"owner_scope_key": "root",
"backend_workspace_ref": "workspace-ref",
},
),
(
AgentWorkspaceBinding,
{
"tenant_id": "tenant-1",
"app_id": "app-1",
"workspace_id": "workspace-1",
"agent_id": "agent-1",
"agent_config_version_id": "snapshot-1",
"agent_config_version_kind": AgentConfigVersionKind.SNAPSHOT,
"backend_binding_ref": "binding-ref",
},
),
],
)
def test_agent_models_require_fields_and_generate_ids(
model: Callable[..., _AgentModel], required_values: dict[str, object]
) -> None:
typebase_models = {mapper.class_ for mapper in TypeBase.registry.mappers}
base_models = {mapper.class_ for mapper in Base.registry.mappers}
assert model in typebase_models
assert model not in base_models
first = model(**required_values)
second = model(**required_values)
assert first.id
assert second.id != first.id
assert model(id="explicit-id", **required_values).id == "explicit-id"
for missing_field in required_values:
incomplete_values = {key: value for key, value in required_values.items() if key != missing_field}
with pytest.raises(TypeError, match=missing_field):
model(**incomplete_values)
def test_agent_enums_match_prd_boundaries():
assert AgentKind.DIFY_AGENT.value == "dify_agent"
assert AgentIconType.EMOJI.value == "emoji"
assert AgentScope.ROSTER.value == "roster"
assert AgentScope.WORKFLOW_ONLY.value == "workflow_only"
assert AgentSource.ROSTER.value == "roster"
assert AgentSource.AGENT_APP.value == "agent_app"
assert AgentSource.WORKFLOW.value == "workflow"
assert AgentStatus.ACTIVE.value == "active"
assert AgentStatus.ARCHIVED.value == "archived"
assert AgentConfigRevisionOperation.SAVE_CURRENT_VERSION.value == "save_current_version"
assert AgentConfigRevisionOperation.RESTORE_VERSION.value == "restore_version"
assert AgentConfigRevisionOperation.PUBLISH_DRAFT.value == "publish_draft"
assert AgentConfigDraftType.DRAFT.value == "draft"
assert AgentConfigDraftType.DEBUG_BUILD.value == "debug_build"
assert WorkflowAgentBindingType.ROSTER_AGENT.value == "roster_agent"
assert WorkflowAgentBindingType.INLINE_AGENT.value == "inline_agent"
def test_agent_table_uses_db_unique_constraint_for_active_roster_names():
agent_table = cast(sa.Table, Agent.__table__)
unique_constraints = {
str(constraint.name): tuple(column.name for column in constraint.columns)
for constraint in agent_table.constraints
if isinstance(constraint, sa.UniqueConstraint)
}
assert unique_constraints["agents_tenant_id_key"] == ("tenant_id", "roster_unique_name")
roster_unique_name = agent_table.c.roster_unique_name
assert roster_unique_name.computed is not None
computed_sql = str(roster_unique_name.computed.sqltext)
assert "scope = 'roster'" in computed_sql
assert "status = 'active'" in computed_sql
indexes = {str(index.name): tuple(column.name for column in index.columns) for index in agent_table.indexes}
assert indexes["agent_tenant_updated_at_idx"] == ("tenant_id", "updated_at")
assert indexes["agent_tenant_scope_idx"] == ("tenant_id", "scope")
def test_active_roster_agent_name_unique_constraint_allows_archived_and_workflow_only_duplicates(
sqlite_engine: sa.Engine,
):
engine = sqlite_engine
agent_table = cast(sa.Table, Agent.__table__)
insert_agent = agent_table.insert()
with engine.begin() as conn:
conn.execute(
insert_agent,
{
"id": "agent-1",
"tenant_id": "tenant-1",
"name": "Analyst",
"scope": AgentScope.ROSTER.value,
"source": AgentSource.WORKFLOW.value,
"status": AgentStatus.ACTIVE.value,
},
)
conn.execute(
insert_agent,
{
"id": "agent-2",
"tenant_id": "tenant-1",
"name": "Analyst",
"scope": AgentScope.ROSTER.value,
"source": AgentSource.WORKFLOW.value,
"status": AgentStatus.ARCHIVED.value,
},
)
conn.execute(
insert_agent,
{
"id": "agent-3",
"tenant_id": "tenant-1",
"name": "Analyst",
"scope": AgentScope.WORKFLOW_ONLY.value,
"source": AgentSource.WORKFLOW.value,
"status": AgentStatus.ACTIVE.value,
},
)
with pytest.raises(IntegrityError):
conn.execute(
insert_agent,
{
"id": "agent-4",
"tenant_id": "tenant-1",
"name": "Analyst",
"scope": AgentScope.ROSTER.value,
"source": AgentSource.WORKFLOW.value,
"status": AgentStatus.ACTIVE.value,
},
)
def test_current_snapshot_stores_agent_soul_snapshot_as_long_text_json():
config_snapshot = AgentSoulConfig.model_validate(
{
"schema_version": 1,
"prompt": {"system_prompt": "You are a proposal analysis agent."},
"env": {"secret_refs": [{"provider_credential_id": "cred-1"}]},
}
)
version = AgentConfigSnapshot(
tenant_id="tenant-1",
agent_id="agent-1",
version=1,
config_snapshot=config_snapshot,
)
config_snapshot_column = AgentConfigSnapshot.__table__.c.config_snapshot
assert isinstance(config_snapshot_column.type, JSONModelColumn)
assert config_snapshot_column.server_default is None
assert version.config_snapshot_dict == config_snapshot.model_dump(mode="json")
assert version.config_snapshot_dict["env"]["secret_refs"][0]["provider_credential_id"] == "cred-1"
def test_agent_config_draft_stores_editable_agent_soul_as_long_text_json():
config_snapshot = AgentSoulConfig.model_validate({"prompt": {"system_prompt": "draft prompt"}})
draft = AgentConfigDraft(
tenant_id="tenant-1",
agent_id="agent-1",
draft_type=AgentConfigDraftType.DRAFT,
draft_owner_key="",
config_snapshot=config_snapshot,
)
config_snapshot_column = AgentConfigDraft.__table__.c.config_snapshot
assert isinstance(config_snapshot_column.type, JSONModelColumn)
assert config_snapshot_column.server_default is None
assert draft.config_snapshot_dict == config_snapshot.model_dump(mode="json")
assert draft.config_snapshot_dict["prompt"]["system_prompt"] == "draft prompt"
def test_workflow_binding_stores_node_job_config_separately_from_agent_soul():
node_job_config = {
"schema_version": 1,
"workflow_prompt": "Review the bid and identify clarification questions.",
"previous_node_output_refs": [{"node_id": "start", "output": "rfp"}],
"declared_outputs": [{"name": "questions", "type": "array"}],
}
binding = WorkflowAgentNodeBinding(
workflow_version="draft",
tenant_id="tenant-1",
app_id="app-1",
workflow_id="workflow-1",
node_id="agent-node-1",
binding_type=WorkflowAgentBindingType.ROSTER_AGENT,
agent_id="agent-1",
current_snapshot_id="version-1",
node_job_config=json.dumps(node_job_config),
)
node_job_config_column = WorkflowAgentNodeBinding.__table__.c.node_job_config
assert isinstance(node_job_config_column.type, JSONModelColumn)
assert node_job_config_column.server_default is None
assert binding.node_job_config_dict == node_job_config
assert "prompt" not in binding.node_job_config_dict
def test_long_text_columns_do_not_use_mysql_incompatible_server_defaults():
for column in (Agent.__table__.c.description,):
assert isinstance(column.type, LongText)
assert column.server_default is None
assert AgentConfigSnapshot.__table__.c.config_snapshot.server_default is None
assert AgentConfigDraft.__table__.c.config_snapshot.server_default is None
assert WorkflowAgentNodeBinding.__table__.c.node_job_config.server_default is None
def test_agent_config_revision_links_previous_and_current_snapshots():
revision = AgentConfigRevision(
tenant_id="tenant-1",
agent_id="agent-1",
previous_snapshot_id="version-0",
current_snapshot_id="version-1",
revision=2,
operation=AgentConfigRevisionOperation.SAVE_CURRENT_VERSION,
)
revision_table = cast(sa.Table, AgentConfigRevision.__table__)
unique_constraints = {
str(constraint.name): tuple(column.name for column in constraint.columns)
for constraint in revision_table.constraints
if isinstance(constraint, sa.UniqueConstraint)
}
assert unique_constraints["agent_config_revision_agent_revision_unique"] == (
"agent_id",
"revision",
)
assert revision.previous_snapshot_id == "version-0"
assert revision.current_snapshot_id == "version-1"
@@ -0,0 +1,434 @@
import pytest
from pydantic import ValidationError
from core.workflow.file_reference import build_file_reference
from models.agent_config_entities import (
AgentKnowledgeSpaceConfig,
AgentSoulAppFeaturesConfig,
AgentSoulConfig,
AgentSoulKnowledgeConfig,
AgentSoulModelSettings,
DeclaredArrayItem,
DeclaredOutputChildConfig,
DeclaredOutputConfig,
DeclaredOutputType,
WorkflowNodeJobConfig,
effective_declared_outputs,
)
@pytest.mark.parametrize("payload", [{}, {"show_thoughts": None}, {"show_thoughts": {}}])
def test_show_thoughts_defaults_to_disabled(payload: dict[str, object]) -> None:
features = AgentSoulAppFeaturesConfig.model_validate(payload)
assert features.show_thoughts is None or features.show_thoughts.enabled is False
@pytest.mark.parametrize("enabled", [False, True])
def test_show_thoughts_round_trips_in_agent_soul(enabled: bool) -> None:
soul = AgentSoulConfig.model_validate({"app_features": {"show_thoughts": {"enabled": enabled}}})
restored = AgentSoulConfig.model_validate_json(soul.model_dump_json())
assert restored.app_features.show_thoughts is not None
assert restored.app_features.show_thoughts.enabled is enabled
def test_agent_soul_model_settings_preserves_plugin_declared_parameters() -> None:
settings = AgentSoulModelSettings.model_validate(
{
"temperature": 0.7,
"enable_thinking": True,
"thinking_budget": 4096,
}
)
dumped = settings.model_dump(mode="json", exclude_none=True)
assert dumped["temperature"] == 0.7
assert dumped["enable_thinking"] is True
assert dumped["thinking_budget"] == 4096
@pytest.mark.parametrize(
"response_format",
[
"text",
"json_schema",
{"type": "text"},
{"type": "json_schema", "json_schema": {"name": "response", "schema": {"type": "object"}}},
None,
],
)
def test_agent_model_response_format_preserves_plugin_and_legacy_shapes(response_format: object) -> None:
settings = AgentSoulModelSettings.model_validate({"response_format": response_format})
assert settings.model_dump(mode="json", exclude_none=True) == (
{"response_format": response_format} if response_format is not None else {}
)
@pytest.mark.parametrize("response_format", [[], ["text"], 1, True])
def test_agent_model_response_format_rejects_non_string_or_object_values(response_format: object) -> None:
with pytest.raises(ValidationError, match="response_format"):
AgentSoulModelSettings.model_validate({"response_format": response_format})
def test_file_default_value_accepts_canonical_reference_mapping() -> None:
reference = build_file_reference(record_id="tool-file-1")
config = DeclaredOutputConfig.model_validate(
{
"name": "report",
"type": "file",
"failure_strategy": {
"on_failure": "default_value",
"default_value": {
"transfer_method": "tool_file",
"reference": reference,
},
},
}
)
assert config.type == DeclaredOutputType.FILE
def test_file_default_value_rejects_legacy_file_id_shape() -> None:
with pytest.raises(ValueError, match="default_value shape"):
_ = DeclaredOutputConfig.model_validate(
{
"name": "report",
"type": "file",
"failure_strategy": {
"on_failure": "default_value",
"default_value": {
"file_id": "legacy-file-id",
},
},
}
)
def test_file_default_value_rejects_non_canonical_reference() -> None:
with pytest.raises(ValueError, match="default_value shape"):
_ = DeclaredOutputConfig.model_validate(
{
"name": "report",
"type": "file",
"failure_strategy": {
"on_failure": "default_value",
"default_value": {
"transfer_method": "tool_file",
"reference": "raw-tool-file-uuid",
},
},
}
)
def test_array_file_default_value_accepts_canonical_mappings() -> None:
first_reference = build_file_reference(record_id="tool-file-1")
second_reference = build_file_reference(record_id="tool-file-2")
config = DeclaredOutputConfig.model_validate(
{
"name": "reports",
"type": "array",
"array_item": {"type": "file"},
"failure_strategy": {
"on_failure": "default_value",
"default_value": [
{"transfer_method": "tool_file", "reference": first_reference},
{"transfer_method": "tool_file", "reference": second_reference},
],
},
}
)
assert config.type == DeclaredOutputType.ARRAY
def test_array_file_default_value_rejects_legacy_item_shape() -> None:
with pytest.raises(ValueError, match="default_value shape"):
_ = DeclaredOutputConfig.model_validate(
{
"name": "reports",
"type": "array",
"array_item": {"type": "file"},
"failure_strategy": {
"on_failure": "default_value",
"default_value": [{"file_id": "legacy-file-id"}],
},
}
)
def test_declared_array_item_rejects_nested_arrays_and_non_object_children() -> None:
with pytest.raises(ValueError, match="nested arrays"):
DeclaredArrayItem(type=DeclaredOutputType.ARRAY)
with pytest.raises(ValueError, match="array_item.children"):
DeclaredArrayItem(
type=DeclaredOutputType.STRING,
children=[DeclaredOutputChildConfig(name="label", type=DeclaredOutputType.STRING)],
)
def test_declared_output_child_validates_shape_and_defaults() -> None:
file_child = DeclaredOutputChildConfig(name="report", type=DeclaredOutputType.FILE)
assert file_child.file is not None
array_child = DeclaredOutputChildConfig(name="items", type=DeclaredOutputType.ARRAY)
assert array_child.array_item is not None
assert array_child.array_item.type == DeclaredOutputType.OBJECT
with pytest.raises(ValueError, match="output child name"):
DeclaredOutputChildConfig(name="bad-name", type=DeclaredOutputType.STRING)
with pytest.raises(ValueError, match="file metadata"):
DeclaredOutputChildConfig(name="title", type=DeclaredOutputType.STRING, file={})
with pytest.raises(ValueError, match="array_item is only allowed"):
DeclaredOutputChildConfig(
name="title",
type=DeclaredOutputType.STRING,
array_item={"type": DeclaredOutputType.STRING},
)
with pytest.raises(ValueError, match="children is only allowed"):
DeclaredOutputChildConfig(
name="title",
type=DeclaredOutputType.STRING,
children=[DeclaredOutputChildConfig(name="label", type=DeclaredOutputType.STRING)],
)
def test_declared_output_child_schema_matches_nullable_serialization() -> None:
config = DeclaredOutputConfig(
name="response",
type=DeclaredOutputType.OBJECT,
children=[DeclaredOutputChildConfig(name="text", type=DeclaredOutputType.STRING)],
)
child = config.model_dump(mode="json")["children"][0]
assert child["file"] is None
assert child["array_item"] is None
children_schema = DeclaredOutputConfig.model_json_schema(mode="serialization")["properties"]["children"]
child_properties = children_schema["items"]["properties"]
assert {"type": "null"} in child_properties["file"]["anyOf"]
assert {"type": "null"} in child_properties["array_item"]["anyOf"]
def test_declared_output_validates_shape_and_defaults() -> None:
file_output = DeclaredOutputConfig(name="report", type=DeclaredOutputType.FILE)
assert file_output.file is not None
array_output = DeclaredOutputConfig(name="items", type=DeclaredOutputType.ARRAY)
assert array_output.array_item is not None
assert array_output.array_item.type == DeclaredOutputType.OBJECT
default_failure_strategy = DeclaredOutputConfig.model_validate(
{"name": "summary", "type": "string", "failure_strategy": None}
)
assert default_failure_strategy.failure_strategy.on_failure == "stop"
with pytest.raises(ValueError, match="output name"):
DeclaredOutputConfig(name="bad-name", type=DeclaredOutputType.STRING)
with pytest.raises(ValueError, match="file metadata"):
DeclaredOutputConfig(name="summary", type=DeclaredOutputType.STRING, file={})
with pytest.raises(ValueError, match="array_item is only allowed"):
DeclaredOutputConfig(
name="summary",
type=DeclaredOutputType.STRING,
array_item={"type": DeclaredOutputType.STRING},
)
with pytest.raises(ValueError, match="children is only allowed"):
DeclaredOutputConfig(
name="summary",
type=DeclaredOutputType.STRING,
children=[DeclaredOutputChildConfig(name="title", type=DeclaredOutputType.STRING)],
)
with pytest.raises(ValueError, match="output check is only allowed"):
DeclaredOutputConfig.model_validate(
{
"name": "summary",
"type": "string",
"check": {
"enabled": True,
"prompt": "Compare output",
"benchmark_file_ref": {"name": "expected.pdf"},
},
}
)
def test_workflow_node_job_reserves_system_output_names() -> None:
for name in ("text", "switch", "_session"):
with pytest.raises(ValueError, match=f"declared output name '{name}' is reserved"):
WorkflowNodeJobConfig.model_validate({"declared_outputs": [{"name": name, "type": "string"}]})
node_job = WorkflowNodeJobConfig.model_validate(
{
"declared_outputs": [
{"name": "files", "type": "string"},
{"name": "json", "type": "string"},
]
}
)
assert [output.name for output in node_job.declared_outputs] == ["files", "json"]
def test_effective_declared_outputs_prepends_optional_system_text() -> None:
custom = DeclaredOutputConfig(name="summary", type=DeclaredOutputType.STRING)
effective = effective_declared_outputs([custom])
assert [output.name for output in effective] == ["text", "summary"]
assert effective[0].type == DeclaredOutputType.STRING
assert effective[0].required is False
@pytest.mark.parametrize(
("enabled", "count", "expected"),
[(False, 0, ["text"]), (False, 1, ["text"]), (False, 2, ["text"]), (True, 2, ["text", "switch"])],
)
def test_effective_outputs_follow_route_selection_contract(enabled: bool, count: int, expected: list[str]) -> None:
job = WorkflowNodeJobConfig.model_validate(
{
"output_routes": {
"enabled": enabled,
"routes": [{"id": str(index), "name": "condition"} for index in range(count)],
}
}
)
assert [output.name for output in effective_declared_outputs([], job.output_routes)] == expected
@pytest.mark.parametrize("count", [0, 1])
def test_enabled_output_routes_require_at_least_two_routes(count: int) -> None:
with pytest.raises(ValueError, match="at least two routes"):
WorkflowNodeJobConfig.model_validate(
{"output_routes": {"enabled": True, "routes": [{"id": str(index)} for index in range(count)]}}
)
@pytest.mark.parametrize("ids", [[""], ["source"], ["fail-branch"], ["target"], ["same", "same"]])
def test_output_routes_reject_invalid_handle_identities(ids: list[str]) -> None:
with pytest.raises(
ValueError,
match=(
"(?:1 validation error for WorkflowNodeJobConfig\\\noutput_routes|1 "
"validation error for WorkflowNodeJobConfig\\\noutput_routes\\.routes\\"
".0\\.id)"
),
):
WorkflowNodeJobConfig.model_validate({"output_routes": {"routes": [{"id": id, "name": ""} for id in ids]}})
"""KnowledgeFS authoring and historical snapshot decoding contracts."""
def binding(**overrides: object) -> dict[str, object]:
return {
"id": "product",
"control_space_id": "ba0b32b1-6c20-493b-96a7-f2d0031f6ba9",
"name": "产品文档",
**overrides,
}
def test_knowledge_fs_round_trip_keeps_identity_and_missing_references() -> None:
soul = AgentSoulConfig.model_validate({"knowledge": {"spaces": [binding(is_missing=True)]}})
restored = AgentSoulConfig.model_validate_json(soul.model_dump_json())
assert restored.knowledge == soul.knowledge
assert restored.knowledge.spaces[0].is_missing
assert restored.knowledge.sets == []
@pytest.mark.parametrize("value", ["bad-id", "../space", "00000000-0000-0000-0000-not-a-uuid00"])
def test_invalid_control_space_identifiers_are_rejected(value: str) -> None:
with pytest.raises(ValidationError):
AgentKnowledgeSpaceConfig.model_validate(binding(control_space_id=value))
@pytest.mark.parametrize("field", ["id", "name"])
@pytest.mark.parametrize("value", [" ", "line\nbreak", "null\x00byte"])
def test_alias_and_identity_cannot_inject_control_characters(field: str, value: str) -> None:
with pytest.raises(ValidationError):
AgentKnowledgeSpaceConfig.model_validate(binding(**{field: value}))
@pytest.mark.parametrize(
("field", "value"),
[("id", "PRODUCT"), ("name", "产品文档"), ("control_space_id", "ba0b32b1-6c20-493b-96a7-f2d0031f6ba9")],
)
def test_duplicate_binding_identity_is_rejected(field: str, value: str) -> None:
second = binding(id="second", name="Policies", control_space_id="a84a7a42-773a-4ce1-8c30-6b932bc7d6d3")
second[field] = value
with pytest.raises(ValidationError, match="unique"):
AgentSoulKnowledgeConfig.model_validate({"spaces": [binding(), second]})
def test_alias_cannot_shadow_another_stable_id() -> None:
with pytest.raises(ValidationError, match="shadow"):
AgentSoulKnowledgeConfig.model_validate(
{
"spaces": [
binding(),
binding(
id="second",
name="PRODUCT",
control_space_id="a84a7a42-773a-4ce1-8c30-6b932bc7d6d3",
),
]
}
)
def test_empty_historical_knowledge_is_still_readable() -> None:
assert AgentSoulKnowledgeConfig.model_validate({"sets": []}).spaces == []
def test_mixed_legacy_and_knowledge_fs_config_round_trips() -> None:
config = AgentSoulKnowledgeConfig.model_validate(
{
"spaces": [binding()],
"sets": [
{
"id": "old",
"name": "Old",
"datasets": [{"id": "old-dataset"}],
"query": {"mode": "generated_query"},
"retrieval": {"mode": "multiple", "top_k": 5},
}
],
}
)
assert AgentSoulKnowledgeConfig.model_validate_json(config.model_dump_json()) == config
@pytest.mark.parametrize("overrides", [{"id": "PRODUCT"}, {"name": "产品文档"}])
def test_mixed_bindings_cannot_share_identity(overrides: dict[str, str]) -> None:
with pytest.raises(ValidationError, match="unique across Agent and Classic"):
AgentSoulKnowledgeConfig.model_validate(
{
"spaces": [binding()],
"sets": [
{
"id": "old",
"name": "Old",
"datasets": [{"id": "old-dataset"}],
"query": {"mode": "generated_query"},
"retrieval": {"mode": "multiple", "top_k": 5},
**overrides,
}
],
}
)
@@ -0,0 +1,11 @@
from models.base import DefaultFieldsMixin
class FooModel(DefaultFieldsMixin):
def __init__(self, id: str) -> None:
self.id = id
def test_repr() -> None:
foo_model = FooModel(id="test-id")
assert repr(foo_model) == "<FooModel(id=test-id)>"
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,139 @@
import ast
import inspect
import os
from functools import cache
from pathlib import Path
import pytest
from models.enums import CreatorUserRole, EndUserType
from models.model import EndUser
from models.types import EnumText
from services.app_scoped_end_user_service import AppScopedEndUserService
API_ROOT = Path(__file__).resolve().parents[3]
@cache
def _production_calls() -> tuple[tuple[Path, ast.Call], ...]:
"""Every call expression in the API's production sources, with its path relative to ``API_ROOT``.
Parsed once for all source-scanning tests; ``tests`` and ``.venv`` directories are pruned
during the walk instead of being traversed and filtered afterwards.
"""
calls: list[tuple[Path, ast.Call]] = []
for directory, subdirectories, filenames in os.walk(API_ROOT):
subdirectories[:] = [name for name in subdirectories if name not in {"tests", ".venv"}]
for filename in filenames:
if filename.endswith(".py"):
source_path = Path(directory) / filename
tree = ast.parse(source_path.read_text(), filename=str(source_path))
relative_path = source_path.relative_to(API_ROOT)
calls.extend((relative_path, node) for node in ast.walk(tree) if isinstance(node, ast.Call))
return tuple(calls)
def test_end_user_type_covers_persisted_creation_values() -> None:
assert {member.value for member in EndUserType} == {
"app-deploy",
"browser",
"mcp",
"openapi",
"service-api",
"trigger",
}
def test_end_user_type_is_plain_persisted_value_enum() -> None:
assert not hasattr(EndUserType, "from_invoke_from")
def test_end_user_type_rejects_legacy_service_api_value() -> None:
with pytest.raises(ValueError, match="'service_api' is not a valid EndUserType"):
EndUserType("service_api")
assert EndUserType("service-api") is EndUserType.SERVICE_API
def test_end_user_type_still_rejects_unknown_values() -> None:
with pytest.raises(ValueError, match="'not\\-a\\-real\\-end\\-user\\-type' is not a valid EndUserType"):
EndUserType("not-a-real-end-user-type")
def test_end_user_service_creation_methods_accept_end_user_type() -> None:
get_or_create_type = inspect.signature(AppScopedEndUserService.get_or_create_end_user_by_type).parameters["type"]
assert get_or_create_type.annotation is EndUserType
assert inspect.signature(AppScopedEndUserService.create_end_user_batch).parameters["type"].annotation is EndUserType
def test_end_user_service_callers_pass_end_user_type() -> None:
violations: list[str] = []
method_names = {"get_or_create_end_user_by_type", "create_end_user_batch"}
checked_calls = 0
for relative_path, node in _production_calls():
if not isinstance(node.func, ast.Attribute) or node.func.attr not in method_names:
continue
checked_calls += 1
type_arg = next((keyword.value for keyword in node.keywords if keyword.arg == "type"), None)
if type_arg is None and node.args:
type_arg = node.args[0]
if not (
isinstance(type_arg, ast.Attribute)
and isinstance(type_arg.value, ast.Name)
and type_arg.value.id == "EndUserType"
):
violations.append(f"{relative_path}:{node.lineno}")
assert checked_calls > 0
assert violations == []
def test_end_user_type_column_uses_enum_text() -> None:
column_type = EndUser.__table__.c.type.type
assert isinstance(column_type, EnumText)
assert column_type._enum_class is EndUserType
def test_production_end_user_constructors_use_end_user_type_enum() -> None:
violations: list[str] = []
for relative_path, node in _production_calls():
if not isinstance(node.func, ast.Name) or node.func.id != "EndUser":
continue
for keyword in node.keywords:
if keyword.arg != "type":
continue
value = keyword.value
uses_end_user_type_member = (
isinstance(value, ast.Attribute)
and isinstance(value.value, ast.Name)
and value.value.id == "EndUserType"
)
uses_end_user_type_conversion = (
isinstance(value, ast.Call) and isinstance(value.func, ast.Name) and value.func.id == "EndUserType"
)
if not (uses_end_user_type_member or uses_end_user_type_conversion):
violations.append(f"{relative_path}:{node.lineno}")
assert violations == []
def test_creator_user_role_missing_maps_hyphen_to_enum() -> None:
# given an alias with hyphen
value = "end-user"
# when converting to enum (invokes StrEnum._missing_ override)
role = CreatorUserRole(value)
# then it should map to END_USER
assert role is CreatorUserRole.END_USER
def test_creator_user_role_missing_raises_for_unknown() -> None:
with pytest.raises(ValueError, match="'unknown' is not a valid CreatorUserRole"):
CreatorUserRole("unknown")
@@ -0,0 +1,21 @@
import pytest
from models.human_input import ApprovalChannel, RecipientType
@pytest.mark.parametrize(
("recipient_type", "expected_channel"),
[
(RecipientType.EMAIL_MEMBER, ApprovalChannel.EMAIL),
(RecipientType.EMAIL_EXTERNAL, ApprovalChannel.EMAIL),
(RecipientType.CONSOLE, ApprovalChannel.CONSOLE),
(RecipientType.BACKSTAGE, ApprovalChannel.CONSOLE),
(RecipientType.STANDALONE_WEB_APP, ApprovalChannel.WEB_APP),
],
)
def test_approval_channel_collapses_delivery_types(
recipient_type: RecipientType, expected_channel: ApprovalChannel
) -> None:
# Both email types collapse to EMAIL and console/backstage to CONSOLE:
# the user-facing approval channel, not the internal recipient type.
assert recipient_type.approval_channel == expected_channel
@@ -0,0 +1,34 @@
import pytest
from pydantic import ValidationError
from sqlalchemy.dialects import sqlite
from models.human_input_v2 import IMIdentityRawPayload
from models.types import FrozenPydanticModelColumn
def test_im_identity_raw_payload_accepts_only_a_strict_json_object_and_is_frozen() -> None:
raw_payload = IMIdentityRawPayload({"provider_user": {"id": "provider-user-1"}})
assert raw_payload.root == {"provider_user": {"id": "provider-user-1"}}
assert raw_payload.model_config["frozen"] is True
assert raw_payload.model_config["strict"] is True
assert raw_payload.model_config["validate_default"] is True
with pytest.raises(ValidationError):
IMIdentityRawPayload.model_validate([("provider_user", {"id": "provider-user-1"})])
with pytest.raises(ValidationError):
raw_payload.root = {}
def test_im_identity_raw_payload_round_trips_through_its_persistence_column() -> None:
raw_payload = IMIdentityRawPayload(
{
"provider_user": {"id": "provider-user-1"},
"roles": ["reviewer"],
}
)
column_type = FrozenPydanticModelColumn(IMIdentityRawPayload)
serialized = column_type.process_bind_param(raw_payload, sqlite.dialect())
restored = column_type.process_result_value(serialized, sqlite.dialect())
assert restored == raw_payload
@@ -0,0 +1,247 @@
from __future__ import annotations
from collections.abc import Iterable
from datetime import UTC, datetime
import sqlalchemy as sa
from models.knowledge_fs import (
AppKnowledgeFSSpaceJoin,
KnowledgeFSApiCredential,
KnowledgeFSAuthorizationRevision,
KnowledgeFSCapabilityIssuanceAudit,
KnowledgeFSCapabilityIssuanceReservation,
KnowledgeFSCapabilityIssuanceReservationStatus,
KnowledgeFSControlSpace,
KnowledgeFSControlSpacePermission,
KnowledgeFSExternalAccessPolicy,
KnowledgeFSLifecycleOperation,
KnowledgeFSLifecycleOutbox,
KnowledgeFSStagedUpload,
KnowledgeFSStagedUploadStatus,
)
from tests.helpers.unit.services.knowledge_fs_fakes import reservation_summary
_KNOWLEDGE_FS_MODELS = (
KnowledgeFSControlSpace,
KnowledgeFSControlSpacePermission,
KnowledgeFSExternalAccessPolicy,
KnowledgeFSApiCredential,
AppKnowledgeFSSpaceJoin,
KnowledgeFSAuthorizationRevision,
KnowledgeFSCapabilityIssuanceAudit,
KnowledgeFSCapabilityIssuanceReservation,
KnowledgeFSLifecycleOutbox,
KnowledgeFSStagedUpload,
)
_FORBIDDEN_DEPENDENCIES = ("datasets", "documents", "dataset_permissions", "api_tokens")
def _constraint_column_names(constraint: sa.UniqueConstraint) -> tuple[str, ...]:
return tuple(column.name for column in constraint.columns)
def _unique_constraints(table: sa.Table) -> Iterable[sa.UniqueConstraint]:
return (constraint for constraint in table.constraints if isinstance(constraint, sa.UniqueConstraint))
def test_knowledge_fs_tables_are_independent_from_dataset_and_document_models() -> None:
assert {model.__tablename__ for model in _KNOWLEDGE_FS_MODELS} == {
"knowledge_fs_control_spaces",
"knowledge_fs_control_space_permissions",
"knowledge_fs_external_access_policies",
"knowledge_fs_api_credentials",
"app_knowledge_fs_space_joins",
"knowledge_fs_authorization_revisions",
"knowledge_fs_capability_issuance_audits",
"knowledge_fs_capability_issuance_reservations",
"knowledge_fs_lifecycle_outbox",
"knowledge_fs_staged_uploads",
}
for model in _KNOWLEDGE_FS_MODELS:
table = model.metadata.tables[model.__tablename__]
dependency_text = " ".join(
[table.name, *(column.name for column in table.columns), *(fk.target_fullname for fk in table.foreign_keys)]
).lower()
assert not any(forbidden in dependency_text for forbidden in _FORBIDDEN_DEPENDENCIES)
assert not model.__mapper__.relationships
def test_control_space_registration_and_child_tenant_boundaries_are_unique() -> None:
control_table = KnowledgeFSControlSpace.metadata.tables[KnowledgeFSControlSpace.__tablename__]
assert any(
index.unique and tuple(column.name for column in index.columns) == ("tenant_id", "knowledge_space_id")
for index in control_table.indexes
)
assert any(
_constraint_column_names(constraint) == ("provisioning_key",)
for constraint in _unique_constraints(control_table)
)
assert any(
index.name == "kfs_control_space_state_updated_idx"
and tuple(column.name for column in index.columns) == ("state", "updated_at")
for index in control_table.indexes
)
expected_child_identity = {
KnowledgeFSControlSpacePermission: ("tenant_id", "control_space_id", "account_id"),
KnowledgeFSExternalAccessPolicy: ("tenant_id", "control_space_id"),
AppKnowledgeFSSpaceJoin: ("tenant_id", "app_id", "control_space_id", "join_type"),
KnowledgeFSAuthorizationRevision: ("tenant_id", "control_space_id"),
}
for model, columns in expected_child_identity.items():
assert any(
_constraint_column_names(constraint) == columns
for constraint in _unique_constraints(model.metadata.tables[model.__tablename__])
)
def test_external_access_is_fail_closed_and_credentials_never_store_raw_secrets() -> None:
policy = KnowledgeFSExternalAccessPolicy(tenant_id="tenant-1", control_space_id="space-1")
assert policy.service_api_enabled is False
assert policy.agent_enabled is False
assert policy.workflow_enabled is False
assert policy.mcp_enabled is False
assert policy.revision == 0
credential_columns = set(
KnowledgeFSApiCredential.metadata.tables[KnowledgeFSApiCredential.__tablename__].columns.keys()
)
assert {"credential_hash", "credential_prefix", "credential_last4", "allowed_actions"} <= credential_columns
assert "secret" not in credential_columns
assert "token" not in credential_columns
assert "credential_secret" not in credential_columns
def test_lifecycle_outbox_supports_every_p1a_command_and_revision_fences() -> None:
assert {operation.value for operation in KnowledgeFSLifecycleOperation} == {
"provision",
"metadata_update",
"delete",
"revoke",
"repair",
}
outbox_columns = set(
KnowledgeFSLifecycleOutbox.metadata.tables[KnowledgeFSLifecycleOutbox.__tablename__].columns.keys()
)
assert {
"operation_id",
"idempotency_key",
"command_schema_version",
"command_payload",
"expected_control_space_version",
"expected_knowledge_space_revision",
"attempt_count",
"next_attempt_at",
"retain_until",
} <= outbox_columns
control_columns = set(KnowledgeFSControlSpace.metadata.tables[KnowledgeFSControlSpace.__tablename__].columns.keys())
assert {"knowledge_space_revision", "deletion_irreversible_at"} <= control_columns
revision_columns = set(
KnowledgeFSAuthorizationRevision.metadata.tables[KnowledgeFSAuthorizationRevision.__tablename__].columns.keys()
)
assert {
"membership_epoch",
"space_acl_epoch",
"external_access_epoch",
"content_policy_revision",
"revoke_sequence",
} <= revision_columns
def test_staged_upload_has_single_claim_and_resumable_session_fields() -> None:
columns = set(KnowledgeFSStagedUpload.metadata.tables[KnowledgeFSStagedUpload.__tablename__].columns.keys())
assert {
"tenant_id",
"account_id",
"upload_file_id",
"checksum_sha256_base64",
"expires_at",
"control_space_id",
"knowledge_space_id",
"upload_session_id",
"document_asset_id",
"compilation_job_id",
"claimed_at",
"row_version",
} <= columns
staged = KnowledgeFSStagedUpload(
tenant_id="tenant-1",
account_id="account-1",
upload_file_id="upload-1",
file_name="handbook.pdf",
content_type="application/pdf",
size_bytes=10,
checksum_sha256_base64="checksum",
expires_at=datetime(2026, 8, 11, tzinfo=UTC),
)
assert staged.status is KnowledgeFSStagedUploadStatus.UPLOADED
def test_capability_audit_persists_only_sanitized_issuance_evidence() -> None:
audit_columns = set(
KnowledgeFSCapabilityIssuanceAudit.metadata.tables[
KnowledgeFSCapabilityIssuanceAudit.__tablename__
].columns.keys()
)
assert {
"tenant_id",
"control_space_id",
"trace_id",
"jti_hash",
"claims_summary",
} <= audit_columns
assert {"token", "raw_jti", "jti"}.isdisjoint(audit_columns)
reservation_columns = set(
KnowledgeFSCapabilityIssuanceReservation.metadata.tables[
KnowledgeFSCapabilityIssuanceReservation.__tablename__
].columns.keys()
)
assert {
"tenant_id",
"control_space_id",
"grant_id",
"trace_id",
"subject",
"caller_kind",
"request_summary",
"status",
"cleanup_after",
"row_version",
} <= reservation_columns
assert {"token", "bearer", "private_key", "raw_jti", "jti"}.isdisjoint(reservation_columns)
reservation = KnowledgeFSCapabilityIssuanceReservation(
tenant_id="tenant-1",
control_space_id="space-1",
grant_id="20000000-0000-4000-8000-000000000001",
trace_id="trace-1",
subject="dify-account:account-1",
caller_kind="interactive",
request_summary=reservation_summary(
tenant_id="tenant-1",
control_space_id="space-1",
caller_kind="interactive",
grant_id="20000000-0000-4000-8000-000000000001",
subject="dify-account:account-1",
),
)
assert reservation.status is KnowledgeFSCapabilityIssuanceReservationStatus.RESERVED
def test_outbox_schema_requires_lease_and_terminal_state_consistency() -> None:
constraints = {
str(constraint.sqltext)
for constraint in KnowledgeFSLifecycleOutbox.metadata.tables[
KnowledgeFSLifecycleOutbox.__tablename__
].constraints
if isinstance(constraint, sa.CheckConstraint)
}
joined_constraints = " ".join(constraints)
assert "lease_owner" in joined_constraints
assert "lease_expires_at" in joined_constraints
assert "completed_at" in joined_constraints
@@ -0,0 +1,49 @@
from __future__ import annotations
from models.knowledge_fs_cleanup import (
KnowledgeFSCleanupAuthorization,
KnowledgeFSCleanupAuthorizationStatus,
KnowledgeFSCleanupTarget,
)
from models.knowledge_fs_cutover import KnowledgeFSWorkspaceCutoverLedger
def test_cleanup_authorization_is_versioned_four_eyes_audit_state() -> None:
assert [status.value for status in KnowledgeFSCleanupAuthorizationStatus] == [
"requested",
"approved",
"started",
"completed",
]
assert {target.value for target in KnowledgeFSCleanupTarget} == {
"legacy_snapshot_foreign_keys",
"legacy_acl_routes",
"legacy_acl_schema",
"legacy_api_key_schema",
"legacy_v1_auth",
"raw_list_create_proxy",
}
assert {
"readiness_evidence",
"requested_by_account_id",
"readiness_ledger_cas_version",
"approved_by_account_id",
"approval_expires_at",
"approved_ledger_cas_version",
"started_by_account_id",
"started_ledger_cas_version",
"completed_by_account_id",
"completed_at",
"completion_evidence",
"completed_ledger_cas_version",
"row_version",
} <= set(KnowledgeFSCleanupAuthorization.__table__.columns.keys())
def test_cleanup_authorization_only_depends_on_cutover_ledger() -> None:
table = KnowledgeFSCleanupAuthorization.__table__
assert {foreign_key.column.table.name for foreign_key in table.foreign_keys} == {
KnowledgeFSWorkspaceCutoverLedger.__tablename__
}
assert {foreign_key.ondelete for foreign_key in table.foreign_keys} == {None}
assert not KnowledgeFSCleanupAuthorization.__mapper__.relationships
@@ -0,0 +1,130 @@
from __future__ import annotations
import sqlalchemy as sa
from models.knowledge_fs_cutover import (
KnowledgeFSMigrationIssue,
KnowledgeFSMigrationQuarantine,
KnowledgeFSShadowAuthorizationDiff,
KnowledgeFSShadowAuthorizationObservation,
KnowledgeFSWorkspaceCutoverLedger,
KnowledgeFSWorkspaceCutoverPhase,
)
_MODELS = (
KnowledgeFSWorkspaceCutoverLedger,
KnowledgeFSMigrationIssue,
KnowledgeFSMigrationQuarantine,
KnowledgeFSShadowAuthorizationDiff,
KnowledgeFSShadowAuthorizationObservation,
)
def test_cutover_ledger_has_strict_phases_watermarks_fences_and_atomic_switches() -> None:
assert [phase.value for phase in KnowledgeFSWorkspaceCutoverPhase] == [
"inventory",
"backfill",
"shadow",
"frozen",
"cutover",
"observing",
"ready_for_cleanup",
]
columns = set(
KnowledgeFSWorkspaceCutoverLedger.metadata.tables[
KnowledgeFSWorkspaceCutoverLedger.__tablename__
].columns.keys()
)
assert {
"source_revision_watermark",
"final_revision_watermark",
"applied_revision_watermark",
"source_task_watermark",
"final_task_watermark",
"applied_task_watermark",
"freeze_at",
"cutover_at",
"rolled_back_at",
"rollback_cutoff_at",
"observation_started_at",
"observation_window_ends_at",
"observation_completed_at",
"maximum_task_expires_at",
"irreversible_cleanup_at",
"product_routes_enabled",
"capability_v2_enabled",
"integrated_mode_enabled",
"legacy_acl_read_only",
"smoke_results",
"legacy_dependency_report",
"shadow_started_at",
"shadow_completed_at",
"shadow_evidence_digest",
"shadow_observation_count",
"shadow_window_started_at",
"shadow_window_ended_at",
"shadow_traffic_zero",
"shadow_traffic_zero_evidence",
"shadow_latest_observed_revision",
"shadow_producer",
"shadow_completed_by_operator",
"shadow_completed_by_account_id",
"remote_activation_id",
"remote_activation_revision",
"remote_activation_digest",
"remote_activation_control_space_id",
"remote_activation_activated_at",
"remote_activation_updated_at",
"remote_activation_acknowledged_at",
"remote_activation_applied",
"remote_activation_replayed",
"cas_version",
} <= columns
def test_cutover_evidence_models_depend_only_on_the_workspace_ledger() -> None:
ledger_table = KnowledgeFSWorkspaceCutoverLedger.__tablename__
for model in _MODELS:
table = model.metadata.tables[model.__tablename__]
assert not model.__mapper__.relationships
assert {foreign_key.column.table.name for foreign_key in table.foreign_keys} <= {ledger_table}
if model is KnowledgeFSWorkspaceCutoverLedger:
assert any(
isinstance(constraint, sa.UniqueConstraint)
and tuple(column.name for column in constraint.columns) == ("tenant_id",)
for constraint in table.constraints
)
def test_quarantine_resolution_is_fully_auditable_and_versioned() -> None:
assert {
"resolved_by_operator",
"resolved_by_account_id",
"evidence",
"resolved_at",
"row_version",
} <= set(
KnowledgeFSMigrationQuarantine.metadata.tables[KnowledgeFSMigrationQuarantine.__tablename__].columns.keys()
)
def test_shadow_evidence_has_append_only_history_and_versioned_current_disposition() -> None:
assert {
"current_evidence_digest",
"last_observed_at",
"row_version",
} <= set(
KnowledgeFSShadowAuthorizationDiff.metadata.tables[
KnowledgeFSShadowAuthorizationDiff.__tablename__
].columns.keys()
)
assert {
"producer",
"observed_at",
"observed_revision",
"evidence_digest",
} <= set(
KnowledgeFSShadowAuthorizationObservation.metadata.tables[
KnowledgeFSShadowAuthorizationObservation.__tablename__
].columns.keys()
)
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,850 @@
"""
Comprehensive unit tests for Provider models.
This test suite covers:
- ProviderType and ProviderQuotaType enum validation
- Provider model creation and properties
- ProviderModel credential management
- TenantDefaultModel configuration
- TenantPreferredModelProvider settings
- ProviderOrder payment tracking
- ProviderModelSetting load balancing
- LoadBalancingModelConfig management
- ProviderCredential storage
- ProviderModelCredential storage
"""
from datetime import UTC, datetime
from uuid import uuid4
import pytest
from typing_extensions import TypedDict
from graphon.model_runtime.entities.model_entities import ModelType
from models.enums import CredentialSourceType, PaymentStatus
from models.provider import (
LoadBalancingModelConfig,
Provider,
ProviderCredential,
ProviderModel,
ProviderModelCredential,
ProviderModelSetting,
ProviderOrder,
ProviderQuotaType,
ProviderType,
TenantDefaultModel,
TenantPreferredModelProvider,
)
class _ProviderOrderParams(TypedDict, closed=True):
tenant_id: str
provider_name: str
account_id: str
payment_product_id: str
payment_id: str | None
transaction_id: str | None
quantity: int
currency: str | None
total_amount: int | None
paid_at: datetime | None
pay_failed_at: datetime | None
refunded_at: datetime | None
class _LoadBalancingConfigParams(TypedDict, closed=True):
tenant_id: str
provider_name: str
model_name: str
model_type: ModelType
class TestProviderTypeEnum:
"""Test suite for ProviderType enum validation."""
def test_provider_type_custom_value(self) -> None:
"""Test ProviderType CUSTOM enum value."""
# Assert
assert ProviderType.CUSTOM.value == "custom"
def test_provider_type_system_value(self) -> None:
"""Test ProviderType SYSTEM enum value."""
# Assert
assert ProviderType.SYSTEM.value == "system"
def test_provider_type_value_of_custom(self) -> None:
"""Test ProviderType.value_of returns CUSTOM for 'custom' string."""
# Act
result = ProviderType.value_of("custom")
# Assert
assert result == ProviderType.CUSTOM
def test_provider_type_value_of_system(self) -> None:
"""Test ProviderType.value_of returns SYSTEM for 'system' string."""
# Act
result = ProviderType.value_of("system")
# Assert
assert result == ProviderType.SYSTEM
def test_provider_type_value_of_invalid_raises_error(self) -> None:
"""Test ProviderType.value_of raises ValueError for invalid value."""
# Act & Assert
with pytest.raises(ValueError, match="No matching enum found"):
ProviderType.value_of("invalid_type")
def test_provider_type_iteration(self) -> None:
"""Test iterating over ProviderType enum members."""
# Act
members = list(ProviderType)
# Assert
assert len(members) == 2
assert ProviderType.CUSTOM in members
assert ProviderType.SYSTEM in members
class TestProviderQuotaTypeEnum:
"""Test suite for ProviderQuotaType enum validation."""
def test_provider_quota_type_paid_value(self) -> None:
"""Test ProviderQuotaType PAID enum value."""
# Assert
assert ProviderQuotaType.PAID.value == "paid"
def test_provider_quota_type_free_value(self) -> None:
"""Test ProviderQuotaType FREE enum value."""
# Assert
assert ProviderQuotaType.FREE.value == "free"
def test_provider_quota_type_trial_value(self) -> None:
"""Test ProviderQuotaType TRIAL enum value."""
# Assert
assert ProviderQuotaType.TRIAL.value == "trial"
def test_provider_quota_type_value_of_paid(self) -> None:
"""Test ProviderQuotaType.value_of returns PAID for 'paid' string."""
# Act
result = ProviderQuotaType.value_of("paid")
# Assert
assert result == ProviderQuotaType.PAID
def test_provider_quota_type_value_of_free(self) -> None:
"""Test ProviderQuotaType.value_of returns FREE for 'free' string."""
# Act
result = ProviderQuotaType.value_of("free")
# Assert
assert result == ProviderQuotaType.FREE
def test_provider_quota_type_value_of_trial(self) -> None:
"""Test ProviderQuotaType.value_of returns TRIAL for 'trial' string."""
# Act
result = ProviderQuotaType.value_of("trial")
# Assert
assert result == ProviderQuotaType.TRIAL
def test_provider_quota_type_value_of_invalid_raises_error(self) -> None:
"""Test ProviderQuotaType.value_of raises ValueError for invalid value."""
# Act & Assert
with pytest.raises(ValueError, match="No matching enum found"):
ProviderQuotaType.value_of("invalid_quota")
def test_provider_quota_type_iteration(self) -> None:
"""Test iterating over ProviderQuotaType enum members."""
# Act
members = list(ProviderQuotaType)
# Assert
assert len(members) == 3
assert ProviderQuotaType.PAID in members
assert ProviderQuotaType.FREE in members
assert ProviderQuotaType.TRIAL in members
class TestProviderModel:
"""Test suite for Provider model validation and operations."""
def test_provider_creation_with_required_fields(self) -> None:
"""Test creating a provider with all required fields."""
# Arrange
tenant_id = str(uuid4())
provider_name = "openai"
# Act
provider = Provider(
tenant_id=tenant_id,
provider_name=provider_name,
)
# Assert
assert provider.tenant_id == tenant_id
assert provider.provider_name == provider_name
assert provider.provider_type == ProviderType.CUSTOM
assert provider.is_valid is False
assert provider.quota_used == 0
def test_provider_creation_with_all_fields(self) -> None:
"""Test creating a provider with all optional fields."""
# Arrange
tenant_id = str(uuid4())
credential_id = str(uuid4())
# Act
provider = Provider(
tenant_id=tenant_id,
provider_name="anthropic",
provider_type=ProviderType.SYSTEM,
is_valid=True,
credential_id=credential_id,
quota_type=ProviderQuotaType.PAID,
quota_limit=10000,
quota_used=500,
)
# Assert
assert provider.tenant_id == tenant_id
assert provider.provider_name == "anthropic"
assert provider.provider_type == ProviderType.SYSTEM
assert provider.is_valid is True
assert provider.credential_id == credential_id
assert provider.quota_type == ProviderQuotaType.PAID
assert provider.quota_limit == 10000
assert provider.quota_used == 500
def test_provider_default_values(self) -> None:
"""Test provider default values are set correctly."""
# Arrange & Act
provider = Provider(
tenant_id=str(uuid4()),
provider_name="test_provider",
)
# Assert
assert provider.provider_type == ProviderType.CUSTOM
assert provider.is_valid is False
assert provider.quota_type is None
assert provider.quota_limit is None
assert provider.quota_used == 0
assert provider.credential_id is None
def test_provider_repr(self) -> None:
"""Test provider __repr__ method."""
# Arrange
tenant_id = str(uuid4())
provider = Provider(
tenant_id=tenant_id,
provider_name="openai",
provider_type=ProviderType.CUSTOM,
)
# Act
repr_str = repr(provider)
# Assert
assert "Provider" in repr_str
assert "openai" in repr_str
assert "custom" in repr_str
def test_provider_token_is_set_false_when_no_credential(self) -> None:
"""Test token_is_set returns False when no credential."""
# Arrange
provider = Provider(
tenant_id=str(uuid4()),
provider_name="openai",
)
# Act & Assert
assert provider.token_is_set is False
def test_provider_is_enabled_false_when_not_valid(self) -> None:
"""Test is_enabled returns False when provider is not valid."""
# Arrange
provider = Provider(
tenant_id=str(uuid4()),
provider_name="openai",
is_valid=False,
)
# Act & Assert
assert provider.is_enabled is False
def test_provider_is_enabled_true_for_valid_system_provider(self) -> None:
"""Test is_enabled returns True for valid system provider."""
# Arrange
provider = Provider(
tenant_id=str(uuid4()),
provider_name="openai",
provider_type=ProviderType.SYSTEM,
is_valid=True,
)
# Act & Assert
assert provider.is_enabled is True
def test_provider_quota_tracking(self) -> None:
"""Test provider quota tracking fields."""
# Arrange
provider = Provider(
tenant_id=str(uuid4()),
provider_name="openai",
quota_type=ProviderQuotaType.TRIAL,
quota_limit=1000,
quota_used=250,
)
# Assert
assert provider.quota_type == ProviderQuotaType.TRIAL
assert provider.quota_limit == 1000
assert provider.quota_used == 250
remaining = provider.quota_limit - provider.quota_used
assert remaining == 750
class TestProviderModelEntity:
"""Test suite for ProviderModel entity validation."""
def test_provider_model_creation_with_required_fields(self) -> None:
"""Test creating a provider model with required fields."""
# Arrange
tenant_id = str(uuid4())
# Act
provider_model = ProviderModel(
tenant_id=tenant_id,
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
)
# Assert
assert provider_model.tenant_id == tenant_id
assert provider_model.provider_name == "openai"
assert provider_model.model_name == "gpt-4"
assert provider_model.model_type == "llm"
assert provider_model.is_valid is False
def test_provider_model_with_credential(self) -> None:
"""Test provider model with credential ID."""
# Arrange
credential_id = str(uuid4())
# Act
provider_model = ProviderModel(
tenant_id=str(uuid4()),
provider_name="anthropic",
model_name="claude-3",
model_type=ModelType.LLM,
credential_id=credential_id,
is_valid=True,
)
# Assert
assert provider_model.credential_id == credential_id
assert provider_model.is_valid is True
def test_provider_model_default_values(self) -> None:
"""Test provider model default values."""
# Arrange & Act
provider_model = ProviderModel(
tenant_id=str(uuid4()),
provider_name="openai",
model_name="gpt-3.5-turbo",
model_type=ModelType.LLM,
)
# Assert
assert provider_model.is_valid is False
assert provider_model.credential_id is None
def test_provider_model_different_types(self) -> None:
"""Test provider model with different model types."""
# Arrange
tenant_id = str(uuid4())
# Act - LLM type
llm_model = ProviderModel(
tenant_id=tenant_id,
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
)
# Act - Embedding type
embedding_model = ProviderModel(
tenant_id=tenant_id,
provider_name="openai",
model_name="text-embedding-ada-002",
model_type=ModelType.TEXT_EMBEDDING,
)
# Act - Speech2Text type
speech_model = ProviderModel(
tenant_id=tenant_id,
provider_name="openai",
model_name="whisper-1",
model_type=ModelType.SPEECH2TEXT,
)
# Assert
assert llm_model.model_type == "llm"
assert embedding_model.model_type == "text-embedding"
assert speech_model.model_type == "speech2text"
class TestTenantDefaultModel:
"""Test suite for TenantDefaultModel configuration."""
def test_tenant_default_model_creation(self) -> None:
"""Test creating a tenant default model."""
# Arrange
tenant_id = str(uuid4())
# Act
default_model = TenantDefaultModel(
tenant_id=tenant_id,
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
)
# Assert
assert default_model.tenant_id == tenant_id
assert default_model.provider_name == "openai"
assert default_model.model_name == "gpt-4"
assert default_model.model_type == "llm"
def test_tenant_default_model_for_different_types(self) -> None:
"""Test tenant default models for different model types."""
# Arrange
tenant_id = str(uuid4())
# Act
llm_default = TenantDefaultModel(
tenant_id=tenant_id,
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
)
embedding_default = TenantDefaultModel(
tenant_id=tenant_id,
provider_name="openai",
model_name="text-embedding-3-small",
model_type=ModelType.TEXT_EMBEDDING,
)
# Assert
assert llm_default.model_type == "llm"
assert embedding_default.model_type == "text-embedding"
class TestTenantPreferredModelProvider:
"""Test suite for TenantPreferredModelProvider settings."""
def test_tenant_preferred_provider_creation(self) -> None:
"""Test creating a tenant preferred model provider."""
# Arrange
tenant_id = str(uuid4())
# Act
preferred = TenantPreferredModelProvider(
tenant_id=tenant_id,
provider_name="openai",
preferred_provider_type=ProviderType.CUSTOM,
)
# Assert
assert preferred.tenant_id == tenant_id
assert preferred.provider_name == "openai"
assert preferred.preferred_provider_type == ProviderType.CUSTOM
def test_tenant_preferred_provider_system_type(self) -> None:
"""Test tenant preferred provider with system type."""
# Arrange & Act
preferred = TenantPreferredModelProvider(
tenant_id=str(uuid4()),
provider_name="anthropic",
preferred_provider_type=ProviderType.SYSTEM,
)
# Assert
assert preferred.preferred_provider_type == ProviderType.SYSTEM
class TestProviderOrder:
"""Test suite for ProviderOrder payment tracking."""
def test_provider_order_creation_with_required_fields(self) -> None:
"""Test creating a provider order with required fields."""
# Arrange
tenant_id = str(uuid4())
account_id = str(uuid4())
# Act
order = ProviderOrder(
tenant_id=tenant_id,
provider_name="openai",
account_id=account_id,
payment_product_id="prod_123",
payment_id=None,
transaction_id=None,
quantity=1,
currency=None,
total_amount=None,
payment_status=PaymentStatus.WAIT_PAY,
paid_at=None,
pay_failed_at=None,
refunded_at=None,
)
# Assert
assert order.tenant_id == tenant_id
assert order.provider_name == "openai"
assert order.account_id == account_id
assert order.payment_product_id == "prod_123"
assert order.payment_status == PaymentStatus.WAIT_PAY
assert order.quantity == 1
def test_provider_order_with_payment_details(self) -> None:
"""Test provider order with full payment details."""
# Arrange
tenant_id = str(uuid4())
account_id = str(uuid4())
paid_time = datetime.now(UTC)
# Act
order = ProviderOrder(
tenant_id=tenant_id,
provider_name="openai",
account_id=account_id,
payment_product_id="prod_456",
payment_id="pay_789",
transaction_id="txn_abc",
quantity=5,
currency="USD",
total_amount=9999,
payment_status=PaymentStatus.PAID,
paid_at=paid_time,
pay_failed_at=None,
refunded_at=None,
)
# Assert
assert order.payment_id == "pay_789"
assert order.transaction_id == "txn_abc"
assert order.quantity == 5
assert order.currency == "USD"
assert order.total_amount == 9999
assert order.payment_status == PaymentStatus.PAID
assert order.paid_at == paid_time
def test_provider_order_payment_statuses(self) -> None:
"""Test provider order with different payment statuses."""
# Arrange
base_params: _ProviderOrderParams = {
"tenant_id": str(uuid4()),
"provider_name": "openai",
"account_id": str(uuid4()),
"payment_product_id": "prod_123",
"payment_id": None,
"transaction_id": None,
"quantity": 1,
"currency": None,
"total_amount": None,
"paid_at": None,
"pay_failed_at": None,
"refunded_at": None,
}
# Act & Assert - Wait pay status
wait_order = ProviderOrder(**base_params, payment_status=PaymentStatus.WAIT_PAY)
assert wait_order.payment_status == PaymentStatus.WAIT_PAY
# Act & Assert - Paid status
paid_order = ProviderOrder(**base_params, payment_status=PaymentStatus.PAID)
assert paid_order.payment_status == PaymentStatus.PAID
# Act & Assert - Failed status
failed_params: _ProviderOrderParams = {**base_params, "pay_failed_at": datetime.now(UTC)}
failed_order = ProviderOrder(**failed_params, payment_status=PaymentStatus.FAILED)
assert failed_order.payment_status == PaymentStatus.FAILED
assert failed_order.pay_failed_at is not None
# Act & Assert - Refunded status
refunded_params: _ProviderOrderParams = {**base_params, "refunded_at": datetime.now(UTC)}
refunded_order = ProviderOrder(**refunded_params, payment_status=PaymentStatus.REFUNDED)
assert refunded_order.payment_status == PaymentStatus.REFUNDED
assert refunded_order.refunded_at is not None
class TestProviderModelSetting:
"""Test suite for ProviderModelSetting load balancing configuration."""
def test_provider_model_setting_creation(self) -> None:
"""Test creating a provider model setting."""
# Arrange
tenant_id = str(uuid4())
# Act
setting = ProviderModelSetting(
tenant_id=tenant_id,
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
)
# Assert
assert setting.tenant_id == tenant_id
assert setting.provider_name == "openai"
assert setting.model_name == "gpt-4"
assert setting.model_type == "llm"
assert setting.enabled is True
assert setting.load_balancing_enabled is False
def test_provider_model_setting_with_load_balancing(self) -> None:
"""Test provider model setting with load balancing enabled."""
# Arrange & Act
setting = ProviderModelSetting(
tenant_id=str(uuid4()),
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
enabled=True,
load_balancing_enabled=True,
)
# Assert
assert setting.enabled is True
assert setting.load_balancing_enabled is True
def test_provider_model_setting_disabled(self) -> None:
"""Test disabled provider model setting."""
# Arrange & Act
setting = ProviderModelSetting(
tenant_id=str(uuid4()),
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
enabled=False,
)
# Assert
assert setting.enabled is False
class TestLoadBalancingModelConfig:
"""Test suite for LoadBalancingModelConfig management."""
def test_load_balancing_config_creation(self) -> None:
"""Test creating a load balancing model config."""
# Arrange
tenant_id = str(uuid4())
# Act
config = LoadBalancingModelConfig(
tenant_id=tenant_id,
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
name="Primary API Key",
)
# Assert
assert config.tenant_id == tenant_id
assert config.provider_name == "openai"
assert config.model_name == "gpt-4"
assert config.model_type == "llm"
assert config.name == "Primary API Key"
assert config.enabled is True
def test_load_balancing_config_with_credentials(self) -> None:
"""Test load balancing config with credential details."""
# Arrange
credential_id = str(uuid4())
# Act
config = LoadBalancingModelConfig(
tenant_id=str(uuid4()),
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
name="Secondary API Key",
encrypted_config='{"api_key": "encrypted_value"}',
credential_id=credential_id,
credential_source_type=CredentialSourceType.CUSTOM_MODEL,
)
# Assert
assert config.encrypted_config == '{"api_key": "encrypted_value"}'
assert config.credential_id == credential_id
assert config.credential_source_type == CredentialSourceType.CUSTOM_MODEL
def test_load_balancing_config_disabled(self) -> None:
"""Test disabled load balancing config."""
# Arrange & Act
config = LoadBalancingModelConfig(
tenant_id=str(uuid4()),
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
name="Disabled Config",
enabled=False,
)
# Assert
assert config.enabled is False
def test_load_balancing_config_multiple_entries(self) -> None:
"""Test multiple load balancing configs for same model."""
# Arrange
tenant_id = str(uuid4())
base_params: _LoadBalancingConfigParams = {
"tenant_id": tenant_id,
"provider_name": "openai",
"model_name": "gpt-4",
"model_type": ModelType.LLM,
}
# Act
primary = LoadBalancingModelConfig(**base_params, name="Primary Key")
secondary = LoadBalancingModelConfig(**base_params, name="Secondary Key")
backup = LoadBalancingModelConfig(**base_params, name="Backup Key", enabled=False)
# Assert
assert primary.name == "Primary Key"
assert secondary.name == "Secondary Key"
assert backup.name == "Backup Key"
assert primary.enabled is True
assert secondary.enabled is True
assert backup.enabled is False
class TestProviderCredential:
"""Test suite for ProviderCredential storage."""
def test_provider_credential_creation(self) -> None:
"""Test creating a provider credential."""
# Arrange
tenant_id = str(uuid4())
# Act
credential = ProviderCredential(
tenant_id=tenant_id,
provider_name="openai",
credential_name="Production API Key",
encrypted_config='{"api_key": "sk-encrypted..."}',
)
# Assert
assert credential.tenant_id == tenant_id
assert credential.provider_name == "openai"
assert credential.credential_name == "Production API Key"
assert credential.encrypted_config == '{"api_key": "sk-encrypted..."}'
def test_provider_credential_multiple_for_same_provider(self) -> None:
"""Test multiple credentials for the same provider."""
# Arrange
tenant_id = str(uuid4())
# Act
prod_cred = ProviderCredential(
tenant_id=tenant_id,
provider_name="openai",
credential_name="Production",
encrypted_config='{"api_key": "prod_key"}',
)
dev_cred = ProviderCredential(
tenant_id=tenant_id,
provider_name="openai",
credential_name="Development",
encrypted_config='{"api_key": "dev_key"}',
)
# Assert
assert prod_cred.credential_name == "Production"
assert dev_cred.credential_name == "Development"
assert prod_cred.provider_name == dev_cred.provider_name
class TestProviderModelCredential:
"""Test suite for ProviderModelCredential storage."""
def test_provider_model_credential_creation(self) -> None:
"""Test creating a provider model credential."""
# Arrange
tenant_id = str(uuid4())
# Act
credential = ProviderModelCredential(
tenant_id=tenant_id,
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
credential_name="GPT-4 API Key",
encrypted_config='{"api_key": "sk-model-specific..."}',
)
# Assert
assert credential.tenant_id == tenant_id
assert credential.provider_name == "openai"
assert credential.model_name == "gpt-4"
assert credential.model_type == "llm"
assert credential.credential_name == "GPT-4 API Key"
def test_provider_model_credential_different_models(self) -> None:
"""Test credentials for different models of same provider."""
# Arrange
tenant_id = str(uuid4())
# Act
gpt4_cred = ProviderModelCredential(
tenant_id=tenant_id,
provider_name="openai",
model_name="gpt-4",
model_type=ModelType.LLM,
credential_name="GPT-4 Key",
encrypted_config='{"api_key": "gpt4_key"}',
)
embedding_cred = ProviderModelCredential(
tenant_id=tenant_id,
provider_name="openai",
model_name="text-embedding-3-large",
model_type=ModelType.TEXT_EMBEDDING,
credential_name="Embedding Key",
encrypted_config='{"api_key": "embedding_key"}',
)
# Assert
assert gpt4_cred.model_name == "gpt-4"
assert gpt4_cred.model_type == "llm"
assert embedding_cred.model_name == "text-embedding-3-large"
assert embedding_cred.model_type == "text-embedding"
def test_provider_model_credential_with_complex_config(self) -> None:
"""Test provider model credential with complex encrypted config."""
# Arrange
complex_config = (
'{"api_key": "sk-xxx", "organization_id": "org-123", '
'"base_url": "https://api.openai.com/v1", "timeout": 30}'
)
# Act
credential = ProviderModelCredential(
tenant_id=str(uuid4()),
provider_name="openai",
model_name="gpt-4-turbo",
model_type=ModelType.LLM,
credential_name="Custom Config",
encrypted_config=complex_config,
)
# Assert
assert credential.encrypted_config == complex_config
assert "organization_id" in credential.encrypted_config
assert "base_url" in credential.encrypted_config
@@ -0,0 +1,288 @@
from collections.abc import Callable, Iterator
from enum import StrEnum
from typing import Annotated, Literal
import pytest
import sqlalchemy as sa
from pydantic import BaseModel, ConfigDict, Field, TypeAdapter, ValidationError
from pydantic_core import PydanticSerializationError
from sqlalchemy import Engine, select
from sqlalchemy.dialects import mysql, postgresql, sqlite
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column
from sqlalchemy.sql.sqltypes import TEXT
from models.types import FrozenPydanticModelColumn
class _FrozenStrictPayload(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True, strict=True, validate_default=True)
name: str
attributes: dict[str, str] = Field(default_factory=dict)
class _NamePayload(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True, strict=True, validate_default=True)
name: str
class _OtherFrozenStrictPayload(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True, strict=True, validate_default=True)
name: str
class _MutableStrictPayload(BaseModel):
model_config = ConfigDict(extra="forbid", strict=True, validate_default=True)
name: str
class _FrozenNonStrictPayload(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True, validate_default=True)
name: str
class _DeliveryChannel(StrEnum):
EMAIL = "email"
class _FrozenStrictEnumPayload(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True, strict=True, validate_default=True)
channel: _DeliveryChannel
class _CatPayload(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True, strict=True, validate_default=True)
kind: Literal["cat"] = "cat"
lives: int
class _DogPayload(BaseModel):
model_config = ConfigDict(extra="forbid", frozen=True, strict=True, validate_default=True)
kind: Literal["dog"] = "dog"
breed: str
type _PetPayload = Annotated[_CatPayload | _DogPayload, Field(discriminator="kind")]
_PAYLOAD_TYPE = FrozenPydanticModelColumn(
_FrozenStrictPayload,
)
_NAME_TYPE = FrozenPydanticModelColumn(_NamePayload)
_PET_TYPE = FrozenPydanticModelColumn(
TypeAdapter(_PetPayload),
model_types=(_CatPayload, _DogPayload),
)
class _Base(DeclarativeBase):
pass
class _PydanticJSONRecord(_Base):
__tablename__ = "unit_pydantic_json_records"
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True, autoincrement=True)
payload: Mapped[_FrozenStrictPayload | None] = mapped_column(_PAYLOAD_TYPE, nullable=True)
pet: Mapped[_PetPayload | None] = mapped_column(_PET_TYPE, nullable=True)
@pytest.fixture
def sqlite_pydantic_json_engine() -> Iterator[Engine]:
engine = sa.create_engine("sqlite:///:memory:")
_Base.metadata.create_all(engine)
try:
yield engine
finally:
_Base.metadata.drop_all(engine)
engine.dispose()
def test_frozen_pydantic_model_column_accepts_a_frozen_strict_model() -> None:
column_type = FrozenPydanticModelColumn(_FrozenStrictPayload)
assert column_type.model_types == (_FrozenStrictPayload,)
def test_concrete_model_rejects_explicit_model_types() -> None:
# Exercise invalid arguments from an untyped runtime caller.
construct: Callable[..., FrozenPydanticModelColumn[_FrozenStrictPayload]] = FrozenPydanticModelColumn
with pytest.raises(TypeError, match="model_types must not be provided for a concrete model"):
construct(
_FrozenStrictPayload,
model_types=(_FrozenStrictPayload,),
)
def test_type_adapter_requires_explicit_model_types() -> None:
# Exercise invalid arguments from an untyped runtime caller.
construct: Callable[..., FrozenPydanticModelColumn[_CatPayload | _DogPayload]] = FrozenPydanticModelColumn
with pytest.raises(TypeError, match="model_types is required when schema is a TypeAdapter"):
construct(TypeAdapter(_PetPayload))
def test_constructor_rejects_a_model_that_is_not_frozen() -> None:
with pytest.raises(TypeError, match=r"_MutableStrictPayload.*frozen=True"):
FrozenPydanticModelColumn(_MutableStrictPayload)
def test_constructor_rejects_a_model_that_is_not_strict() -> None:
with pytest.raises(TypeError, match=r"_FrozenNonStrictPayload.*strict=True"):
FrozenPydanticModelColumn(_FrozenNonStrictPayload)
def test_bind_rejects_a_bare_dict() -> None:
with pytest.raises(TypeError, match=r"value.*_FrozenStrictPayload"):
_PAYLOAD_TYPE.process_bind_param({"name": "bare-dict"}, sqlite.dialect())
def test_bind_rejects_a_json_string() -> None:
with pytest.raises(TypeError, match=r"value.*_FrozenStrictPayload"):
_PAYLOAD_TYPE.process_bind_param('{"name":"json-string"}', sqlite.dialect())
def test_bind_rejects_an_unlisted_frozen_model() -> None:
with pytest.raises(TypeError, match=r"value.*_FrozenStrictPayload"):
_PAYLOAD_TYPE.process_bind_param(_OtherFrozenStrictPayload(name="other"), sqlite.dialect())
def test_bind_returns_model_dump_json_output() -> None:
assert (
_PAYLOAD_TYPE.process_bind_param(
_FrozenStrictPayload(name="serialized", attributes={"source": "model_dump_json"}),
sqlite.dialect(),
)
== '{"name":"serialized","attributes":{"source":"model_dump_json"}}'
)
def test_bind_processor_does_not_double_encode_model_json() -> None:
bind_processor = _NAME_TYPE.bind_processor(sqlite.dialect())
assert bind_processor is not None
assert bind_processor(_NamePayload(name="Alice")) == '{"name":"Alice"}'
def test_bind_treats_serialization_warnings_as_errors() -> None:
invalid_model = _FrozenStrictPayload.model_construct(name=42, attributes={})
with pytest.raises(PydanticSerializationError):
_PAYLOAD_TYPE.process_bind_param(invalid_model, sqlite.dialect())
def test_validation_errors_are_not_swallowed() -> None:
with pytest.raises(ValidationError):
_PAYLOAD_TYPE.process_result_value('{"name":42}', sqlite.dialect())
def test_result_restores_str_enum_from_strict_json_validation() -> None:
column_type = FrozenPydanticModelColumn(_FrozenStrictEnumPayload)
restored = column_type.process_result_value('{"channel":"email"}', sqlite.dialect())
assert restored == _FrozenStrictEnumPayload(channel=_DeliveryChannel.EMAIL)
assert restored is not None
assert restored.channel is _DeliveryChannel.EMAIL
def test_uses_text_compatible_dialect_types() -> None:
assert isinstance(_NAME_TYPE.load_dialect_impl(postgresql.dialect()), TEXT)
assert isinstance(_NAME_TYPE.load_dialect_impl(sqlite.dialect()), TEXT)
assert isinstance(_NAME_TYPE.load_dialect_impl(mysql.dialect()), LONGTEXT)
def test_sqlite_uses_a_text_column(sqlite_pydantic_json_engine: Engine) -> None:
columns = sa.inspect(sqlite_pydantic_json_engine).get_columns(_PydanticJSONRecord.__tablename__)
payload_column = next(column for column in columns if column["name"] == "payload")
assert isinstance(payload_column["type"], TEXT)
def test_sqlite_round_trips_a_concrete_model_and_discriminated_union(
sqlite_pydantic_json_engine: Engine,
) -> None:
with Session(sqlite_pydantic_json_engine) as session:
record = _PydanticJSONRecord(
payload=_FrozenStrictPayload(name="stored", attributes={"source": "sqlite"}),
pet=_CatPayload(lives=9),
)
session.add(record)
session.commit()
record_id = record.id
with Session(sqlite_pydantic_json_engine) as session:
restored = session.scalar(select(_PydanticJSONRecord).where(_PydanticJSONRecord.id == record_id))
assert restored is not None
assert restored.payload == _FrozenStrictPayload(name="stored", attributes={"source": "sqlite"})
assert restored.pet == _CatPayload(lives=9)
assert isinstance(restored.pet, _CatPayload)
def test_sqlite_round_trips_nullable_values(sqlite_pydantic_json_engine: Engine) -> None:
with Session(sqlite_pydantic_json_engine) as session:
record = _PydanticJSONRecord(payload=None, pet=None)
session.add(record)
session.commit()
record_id = record.id
with Session(sqlite_pydantic_json_engine) as session:
restored = session.scalar(select(_PydanticJSONRecord).where(_PydanticJSONRecord.id == record_id))
assert restored is not None
assert restored.payload is None
assert restored.pet is None
def test_replacing_the_whole_model_marks_the_attribute_dirty_and_persists(
sqlite_pydantic_json_engine: Engine,
) -> None:
with Session(sqlite_pydantic_json_engine) as session:
record = _PydanticJSONRecord(payload=_FrozenStrictPayload(name="before"), pet=None)
session.add(record)
session.commit()
record_id = record.id
record.payload = _FrozenStrictPayload(name="after")
assert session.is_modified(record)
session.commit()
with Session(sqlite_pydantic_json_engine) as session:
restored = session.scalar(select(_PydanticJSONRecord).where(_PydanticJSONRecord.id == record_id))
assert restored is not None
assert restored.payload == _FrozenStrictPayload(name="after")
def test_frozen_models_remain_shallow_and_nested_mutation_is_not_tracked(
sqlite_pydantic_json_engine: Engine,
) -> None:
with Session(sqlite_pydantic_json_engine) as session:
record = _PydanticJSONRecord(
payload=_FrozenStrictPayload(name="stored", attributes={"key": "original"}),
pet=None,
)
session.add(record)
session.commit()
record_id = record.id
assert record.payload is not None
record.payload.attributes["key"] = "mutated"
assert record.payload.attributes == {"key": "mutated"}
assert not session.is_modified(record, include_collections=True)
session.commit()
with Session(sqlite_pydantic_json_engine) as session:
restored = session.scalar(select(_PydanticJSONRecord).where(_PydanticJSONRecord.id == record_id))
assert restored is not None
assert restored.payload == _FrozenStrictPayload(name="stored", attributes={"key": "original"})
@@ -0,0 +1,116 @@
"""Snippet model accessors backed by the shared SQLite test session."""
import json
from sqlalchemy.orm import Session
from models import snippet as snippet_module
from models.account import Account
from models.base import Base, TypeBase
from models.enums import TagType
from models.model import Tag, TagBinding
from models.snippet import CustomizedSnippet
from models.workflow import Workflow, WorkflowType
TENANT_ID = "11111111-1111-1111-1111-111111111111"
WORKFLOW_ID = "22222222-2222-2222-2222-222222222222"
APP_ID = "33333333-3333-3333-3333-333333333333"
SNIPPET_ID = "44444444-4444-4444-4444-444444444444"
ACCOUNT_1_ID = "55555555-5555-5555-5555-555555555555"
ACCOUNT_2_ID = "55555555-5555-5555-5555-555555555556"
def test_customized_snippet_uses_typebase_registry_and_generates_ids() -> None:
snippet = CustomizedSnippet()
another_snippet = CustomizedSnippet()
assert CustomizedSnippet.__mapper__.registry is TypeBase.registry
assert CustomizedSnippet.__mapper__.registry is not Base.registry
assert snippet.id != another_snippet.id
assert CustomizedSnippet(id=SNIPPET_ID).id == SNIPPET_ID
def test_get_graph_dict_returns_empty_without_workflow_id(sqlite_session: Session) -> None:
snippet = CustomizedSnippet(workflow_id=None)
assert snippet.get_graph_dict(session=sqlite_session) == {}
def test_get_graph_dict_loads_published_workflow_graph(sqlite_session: Session) -> None:
workflow = Workflow(
tenant_id=TENANT_ID,
app_id=APP_ID,
type=WorkflowType.WORKFLOW,
version="1",
graph=json.dumps({"nodes": [{"id": "llm-1"}], "edges": []}),
_features="{}",
created_by=ACCOUNT_1_ID,
)
workflow.id = WORKFLOW_ID
sqlite_session.add(workflow)
sqlite_session.commit()
snippet = CustomizedSnippet(workflow_id=WORKFLOW_ID)
assert snippet.get_graph_dict(session=sqlite_session) == {"nodes": [{"id": "llm-1"}], "edges": []}
def test_get_graph_dict_returns_empty_when_workflow_missing(sqlite_session: Session) -> None:
snippet = CustomizedSnippet(workflow_id=WORKFLOW_ID)
assert snippet.get_graph_dict(session=sqlite_session) == {}
def test_input_fields_list_parses_json_or_returns_empty() -> None:
assert CustomizedSnippet(input_fields=None).input_fields_list == []
assert CustomizedSnippet(input_fields=json.dumps([{"variable": "query"}])).input_fields_list == [
{"variable": "query"}
]
def test_get_tags_returns_query_results_or_empty(sqlite_session: Session) -> None:
tag = Tag(tenant_id=TENANT_ID, type=TagType.SNIPPET, name="Reusable", created_by=ACCOUNT_1_ID)
binding = TagBinding(tenant_id=TENANT_ID, tag_id=tag.id, target_id=SNIPPET_ID, created_by=ACCOUNT_1_ID)
sqlite_session.add_all((tag, binding))
sqlite_session.commit()
snippet = CustomizedSnippet(id=SNIPPET_ID, tenant_id=TENANT_ID)
assert snippet.get_tags(session=sqlite_session) == [tag]
sqlite_session.delete(binding)
sqlite_session.commit()
assert snippet.get_tags(session=sqlite_session) == []
def test_get_account_accessors_and_author_name(sqlite_session: Session) -> None:
account = Account(name="Ada", email="ada@example.com")
account.id = ACCOUNT_1_ID
updated_account = Account(name="Grace", email="grace@example.com")
updated_account.id = ACCOUNT_2_ID
sqlite_session.add_all((account, updated_account))
sqlite_session.commit()
snippet = CustomizedSnippet(created_by=ACCOUNT_1_ID, updated_by=ACCOUNT_2_ID)
assert snippet.get_created_by_account(session=sqlite_session) is account
assert snippet.get_author_name(session=sqlite_session) == "Ada"
assert snippet.get_updated_by_account(session=sqlite_session) is updated_account
def test_get_account_accessors_return_none_without_account_ids(sqlite_session: Session) -> None:
snippet = CustomizedSnippet(created_by=None, updated_by=None)
assert snippet.get_created_by_account(session=sqlite_session) is None
assert snippet.get_author_name(session=sqlite_session) is None
assert snippet.get_updated_by_account(session=sqlite_session) is None
def test_version_str_returns_string_value() -> None:
snippet = CustomizedSnippet(version=7)
assert snippet.version_str == "7"
def test_session_backed_lookups_are_not_exposed_as_properties() -> None:
"""Callers must pass a session; the model no longer reads the Flask-global ``db.session``."""
assert not hasattr(snippet_module, "db")
for name in ("graph_dict", "tags", "created_by_account", "author_name", "updated_by_account"):
assert not hasattr(CustomizedSnippet, name)
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,89 @@
"""Regression coverage for ``models.trigger.WorkflowTriggerLog`` account accessors.
Ensures the ``@property``→session-parameter refactor preserves the role-based dispatch:
``created_by_account`` looks up an Account only when role is ACCOUNT; ``created_by_end_user``
looks up an EndUser only when role is END_USER.
Both accessors are exercised against the real ``sqlite_session`` fixture (a genuine
SQLAlchemy ``Session`` bound to a pristine full-schema SQLite database) so the assertions
cover actual query behaviour rather than a mock's recorded call.
"""
from sqlalchemy.orm import Session
from models.account import Account
from models.enums import CreatorUserRole, EndUserType, WorkflowTriggerStatus
from models.model import EndUser
from models.trigger import WorkflowTriggerLog
def _log(role: CreatorUserRole, created_by: str) -> WorkflowTriggerLog:
"""Construct a WorkflowTriggerLog without touching the database."""
return WorkflowTriggerLog(
tenant_id="00000000-0000-0000-0000-000000000001",
app_id="00000000-0000-0000-0000-000000000002",
workflow_id="00000000-0000-0000-0000-000000000003",
workflow_run_id=None,
root_node_id=None,
trigger_metadata="{}",
trigger_type="manual",
trigger_data="{}",
inputs="{}",
outputs=None,
status=WorkflowTriggerStatus.SUCCEEDED,
error=None,
queue_name="default",
celery_task_id=None,
created_by_role=role,
created_by=created_by,
)
class TestCreatedByAccount:
def test_returns_account_lookup_when_role_is_account(self, sqlite_session: Session) -> None:
account = Account(name="Test Account", email="test@example.com")
sqlite_session.add(account)
sqlite_session.flush()
log = _log(CreatorUserRole.ACCOUNT, created_by=account.id)
result = log.created_by_account(session=sqlite_session)
assert result is not None
assert result.id == account.id
def test_returns_none_when_role_is_end_user(self, sqlite_session: Session) -> None:
account = Account(name="Test Account", email="test@example.com")
sqlite_session.add(account)
sqlite_session.flush()
log = _log(CreatorUserRole.END_USER, created_by=account.id)
assert log.created_by_account(session=sqlite_session) is None
class TestCreatedByEndUser:
def test_returns_end_user_lookup_when_role_is_end_user(self, sqlite_session: Session) -> None:
end_user = EndUser(
tenant_id="00000000-0000-0000-0000-000000000001",
type=EndUserType.BROWSER,
session_id="session-1",
)
sqlite_session.add(end_user)
sqlite_session.flush()
log = _log(CreatorUserRole.END_USER, created_by=end_user.id)
result = log.created_by_end_user(session=sqlite_session)
assert result is not None
assert result.id == end_user.id
def test_returns_none_when_role_is_account(self, sqlite_session: Session) -> None:
end_user = EndUser(
tenant_id="00000000-0000-0000-0000-000000000001",
type=EndUserType.BROWSER,
session_id="session-1",
)
sqlite_session.add(end_user)
sqlite_session.flush()
log = _log(CreatorUserRole.ACCOUNT, created_by=end_user.id)
assert log.created_by_end_user(session=sqlite_session) is None
@@ -0,0 +1,75 @@
from typing import cast
import pytest
from pydantic import BaseModel
from sqlalchemy.dialects import mysql, postgresql, sqlite
from sqlalchemy.dialects.mysql import LONGTEXT
from sqlalchemy.sql.sqltypes import TEXT
from models.types import JSONModelColumn
class JsonColumnSample(BaseModel):
name: str
count: int = 0
class NotPydanticModel:
pass
def test_json_model_column_is_deprecated() -> None:
message = (
"JSONModelColumn is a legacy coercing TEXT-backed type. "
"Use FrozenPydanticModelColumn for new model-only persistence."
)
with pytest.warns(DeprecationWarning, match=message):
JSONModelColumn(JsonColumnSample)
def test_json_model_column_serializes_supported_input_shapes() -> None:
column = JSONModelColumn(JsonColumnSample)
dialect = sqlite.dialect()
assert column.process_bind_param(None, dialect) is None
assert column.process_bind_param(JsonColumnSample(name="model", count=2), dialect) == '{"count":2,"name":"model"}'
assert column.process_bind_param({"name": "dict", "count": 3}, dialect) == '{"count":3,"name":"dict"}'
assert column.process_bind_param('{"name":"json","count":4}', dialect) == '{"count":4,"name":"json"}'
def test_json_model_column_deserializes_empty_and_json_values() -> None:
column = JSONModelColumn(JsonColumnSample)
dialect = sqlite.dialect()
assert column.process_result_value(None, dialect) is None
assert column.process_result_value("", dialect) is None
assert column.process_result_value('{"name":"stored","count":5}', dialect) == JsonColumnSample(
name="stored",
count=5,
)
def test_json_model_column_keeps_model_class_directly() -> None:
column = JSONModelColumn(JsonColumnSample)
assert column.process_bind_param({"name": "class", "count": 6}, sqlite.dialect()) == '{"count":6,"name":"class"}'
assert column._model_class is JsonColumnSample
def test_json_model_column_rejects_non_pydantic_model_class() -> None:
with pytest.raises(TypeError, match="must be a Pydantic BaseModel subclass"):
JSONModelColumn(cast(type[BaseModel], NotPydanticModel))
def test_json_model_column_uses_long_text_compatible_dialect_types() -> None:
column = JSONModelColumn(JsonColumnSample)
assert isinstance(column.load_dialect_impl(postgresql.dialect()), TEXT)
assert isinstance(column.load_dialect_impl(sqlite.dialect()), TEXT)
assert isinstance(column.load_dialect_impl(mysql.dialect()), LONGTEXT)
def test_json_model_column_rejects_string_model_paths() -> None:
with pytest.raises(TypeError):
JSONModelColumn(cast(type[BaseModel], "tests.unit_tests.models.test_types.JsonColumnSample"))
@@ -0,0 +1,67 @@
"""Regression coverage for ``models.web.SavedMessage.message`` accessor.
Ensures the property→method refactor (drop of ``db.session`` in favor of a caller-provided
``Session``) preserves query intent: the accessor forwards ``self.message_id`` to the
supplied session and returns the matching :class:`Message` (or ``None`` when absent).
The accessor is exercised against the real ``sqlite_session`` fixture (a genuine SQLAlchemy
``Session`` bound to a pristine full-schema SQLite database) so the assertions cover actual
query behaviour rather than a mock's recorded call.
"""
from decimal import Decimal
from uuid import uuid4
from sqlalchemy.orm import Session
from models.enums import CreatorUserRole
from models.model import ConversationFromSource, Message
from models.web import SavedMessage
def _persist_message(session: Session, *, app_id: str) -> Message:
"""Persist a minimal valid Message row and return it."""
message = Message(
app_id=app_id,
conversation_id=str(uuid4()),
_inputs={},
query="hello",
message=[{"role": "user", "text": "hello"}],
message_unit_price=Decimal(0),
message_price_unit=Decimal(0),
answer="hi",
answer_unit_price=Decimal(0),
answer_price_unit=Decimal(0),
currency="USD",
from_source=ConversationFromSource.API,
)
session.add(message)
session.flush()
return message
def _saved_message(*, app_id: str, message_id: str) -> SavedMessage:
"""Construct a SavedMessage without touching the database."""
return SavedMessage(
app_id=app_id,
message_id=message_id,
created_by_role=CreatorUserRole.END_USER,
created_by=str(uuid4()),
)
def test_message_returns_persisted_message(sqlite_session: Session) -> None:
app_id = str(uuid4())
message = _persist_message(sqlite_session, app_id=app_id)
saved = _saved_message(app_id=app_id, message_id=message.id)
result = saved.message(session=sqlite_session)
assert result is not None
assert result.id == message.id
def test_message_returns_none_when_message_missing(sqlite_session: Session) -> None:
saved = _saved_message(app_id=str(uuid4()), message_id=str(uuid4()))
assert saved.message(session=sqlite_session) is None
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,257 @@
from unittest.mock import MagicMock
import pytest
from pydantic import JsonValue
from graphon.file import File, FileTransferMethod, FileType
from models.utils.file_input_compat import (
build_file_from_mapping_without_lookup,
build_file_from_stored_mapping,
rebuild_serialized_graph_files_without_lookup,
resolve_file_mapping_tenant_id,
restore_input_files,
)
def test_build_file_from_mapping_without_lookup_accepts_legacy_payload() -> None:
file = build_file_from_mapping_without_lookup(
file_mapping={
"id": "test-file",
"tenant_id": "test-tenant-id",
"type": "image",
"transfer_method": "tool_file",
"related_id": "test-related-id",
"filename": "image.png",
"extension": ".png",
"mime_type": "image/png",
"size": 67,
"storage_key": "test-storage-key",
"url": "https://example.com/image.png",
"tool_file_id": "tool-file-123",
"upload_file_id": "upload-file-456",
"datasource_file_id": "datasource-file-789",
}
)
assert file.related_id == "test-related-id"
assert file.storage_key == "test-storage-key"
assert file.type == FileType.IMAGE
@pytest.mark.parametrize(
("file_mapping", "expected_message"),
[
(
{
"id": "test-file",
"transfer_method": "local_file",
},
"file type is required in file mapping",
),
(
{
"id": "test-file",
"type": "image",
},
"transfer_method is required in file mapping",
),
],
)
def test_build_file_from_mapping_without_lookup_requires_required_fields(
file_mapping: dict[str, object],
expected_message: str,
) -> None:
with pytest.raises(ValueError, match=expected_message):
build_file_from_mapping_without_lookup(file_mapping=file_mapping)
def test_build_file_from_mapping_without_lookup_normalizes_invalid_metadata_types() -> None:
file = build_file_from_mapping_without_lookup(
file_mapping={
"id": "test-file",
"type": "image",
"transfer_method": "local_file",
"reference": "upload-file-1",
"filename": 123,
"size": "large",
}
)
assert file.filename is None
assert file.size == -1
def test_build_file_from_mapping_without_lookup_accepts_enum_inputs() -> None:
file = build_file_from_mapping_without_lookup(
file_mapping={
"id": "test-file",
"type": FileType.IMAGE,
"transfer_method": FileTransferMethod.REMOTE_URL,
"url": "https://example.com/image.png",
}
)
assert file.type == FileType.IMAGE
assert file.transfer_method == FileTransferMethod.REMOTE_URL
def test_rebuild_serialized_graph_files_without_lookup_rehydrates_nested_files() -> None:
rebuilt = rebuild_serialized_graph_files_without_lookup(
{
"file": {
"dify_model_identity": "__dify__file__",
"id": "test-file",
"type": "document",
"transfer_method": "local_file",
"reference": "upload-file-123",
"filename": "doc.txt",
},
"items": [
{
"dify_model_identity": "__dify__file__",
"type": "image",
"transfer_method": "remote_url",
"url": "https://example.com/image.png",
"filename": "image.png",
}
],
}
)
assert isinstance(rebuilt["file"], File)
assert rebuilt["file"].related_id == "upload-file-123"
assert isinstance(rebuilt["items"][0], File)
assert rebuilt["items"][0].transfer_method == FileTransferMethod.REMOTE_URL
def test_rebuild_serialized_graph_files_without_lookup_preserves_scalar_values() -> None:
assert rebuild_serialized_graph_files_without_lookup("plain-text") == "plain-text"
def test_build_file_from_stored_mapping_rebuilds_remote_urls_without_record_lookup(
monkeypatch: pytest.MonkeyPatch,
) -> None:
rebuilt_file = File(
file_type=FileType.IMAGE,
transfer_method=FileTransferMethod.REMOTE_URL,
remote_url="https://example.com/image.png",
filename="image.png",
)
build_without_lookup = MagicMock(return_value=rebuilt_file)
monkeypatch.setattr("models.utils.file_input_compat.build_file_from_mapping_without_lookup", build_without_lookup)
result = build_file_from_stored_mapping(
file_mapping={
"type": "image",
"transfer_method": "remote_url",
"url": "https://example.com/image.png",
"filename": "image.png",
},
tenant_id="tenant-1",
)
build_without_lookup.assert_called_once()
assert result is rebuilt_file
def _unexpected_tenant_lookup() -> str:
raise AssertionError("These file inputs must not resolve the app tenant")
def _remote_file_mapping() -> dict[str, JsonValue]:
return {
"dify_model_identity": "__dify__file__",
"type": "image",
"transfer_method": "remote_url",
"url": "https://example.com/image.png",
"filename": "image.png",
}
def test_restore_input_files_rebuilds_single_file_and_file_list_without_tenant_lookup() -> None:
mapping = _remote_file_mapping()
inputs: dict[str, JsonValue | File | list[File]] = {"file": mapping, "files": [mapping]}
restored = restore_input_files(inputs=inputs, tenant_resolver=_unexpected_tenant_lookup)
file = restored["file"]
assert isinstance(file, File)
assert file.remote_url == "https://example.com/image.png"
files = restored["files"]
assert isinstance(files, list)
assert len(files) == 1
assert isinstance(files[0], File)
assert files[0].model_dump() == file.model_dump()
assert inputs == {"file": mapping, "files": [mapping]}
assert restored is not inputs
def test_restore_input_files_preserves_mixed_lists_nested_mappings_and_scalar_values() -> None:
mapping = _remote_file_mapping()
inputs: dict[str, JsonValue | File | list[File]] = {
"mixed": [mapping, "ordinary-value"],
"nested": {"file": mapping},
"empty": [],
"null": None,
"zero": 0,
"false": False,
"string": "",
}
restored = restore_input_files(inputs=inputs, tenant_resolver=_unexpected_tenant_lookup)
assert restored == inputs
assert restored["mixed"] is inputs["mixed"]
assert restored["nested"] is inputs["nested"]
def test_restore_input_files_preserves_existing_file_objects() -> None:
file = build_file_from_mapping_without_lookup(file_mapping=_remote_file_mapping())
files = [file]
restored = restore_input_files(
inputs={"file": file, "files": files},
tenant_resolver=_unexpected_tenant_lookup,
)
assert restored["file"] is file
assert restored["files"] is files
def test_restore_input_files_propagates_missing_owner_for_storage_backed_files() -> None:
def resolve_missing_tenant() -> str:
raise ValueError("Unable to resolve tenant_id for app app-1")
with pytest.raises(ValueError, match="Unable to resolve tenant_id for app app-1"):
restore_input_files(
inputs={
"file": {
"dify_model_identity": "__dify__file__",
"type": "document",
"transfer_method": "local_file",
"related_id": "upload-file-id",
}
},
tenant_resolver=resolve_missing_tenant,
)
def test_resolve_file_mapping_tenant_id_preserves_legacy_serialized_owner() -> None:
assert (
resolve_file_mapping_tenant_id(
file_mapping={"tenant_id": "serialized-tenant"},
tenant_resolver=_unexpected_tenant_lookup,
)
== "serialized-tenant"
)
@pytest.mark.parametrize("tenant_id", [None, "", 42])
def test_resolve_file_mapping_tenant_id_falls_back_for_missing_or_invalid_owner(tenant_id: JsonValue) -> None:
assert (
resolve_file_mapping_tenant_id(
file_mapping={"tenant_id": tenant_id},
tenant_resolver=lambda: "app-tenant",
)
== "app-tenant"
)