"""Isolated SQLite integration checks; never reads shared DB or Redis.""" import importlib import json import os import sys import tempfile import unittest from pathlib import Path root = Path(__file__).resolve().parents[1] sys.path.insert(0, str(root / 'apps')) os.environ['DJANGO_SETTINGS_MODULE'] = 'maxkb.settings' from django.apps import AppConfig from django.conf import settings settings.LOGGING_CONFIG = None settings.USE_I18N = False settings.LANGUAGE_CODE = 'zh-hans' settings.CACHES = {'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}} with tempfile.TemporaryDirectory(prefix='mcp-engine-', ignore_cleanup_errors=True) as directory: settings.DATABASES = {'default': {'ENGINE': 'django.db.backends.sqlite3', 'NAME': str(Path(directory) / 'test.sqlite3'), 'OPTIONS': {'timeout': 30}}} settings.AI_HUB_ASSET_DIR = str(Path(directory) / 'assets') settings.INSTALLED_APPS = [AppConfig(name, importlib.import_module(name)) for name in ( 'django.contrib.contenttypes', 'users', 'tools', 'knowledge', 'common', 'system_manage', 'models_provider', 'application', 'chat', 'oss')] import django django.setup() from django.db import connection, connections from users.models import User from users.models.user_manage_ext import UserActivationApplication from models_provider.models import Model from tools.models import Tool, ToolFolder from application.models import Application, ApplicationFolder, ApplicationVersion, ApplicationKnowledgeMapping from system_manage.models import SystemSetting, UserMenuSetting, WorkspaceUserResourcePermission, Log from system_manage.models.platform_access import PlatformRole, PlatformRoleMember, PlatformGrant from system_manage.models.ai_hub import AIParserResource, AIFlow, AIService, AITask, AIArtifact from knowledge.models import KnowledgeFolder, Knowledge # PostgreSQL-only array columns are storage-neutral in these tests (no array operators). from django.contrib.postgres.fields import ArrayField ArrayField.db_type = lambda self, connection: 'text' ArrayField.cast_db_type = lambda self, connection: 'text' ArrayField.get_placeholder = lambda self, value, compiler, connection: '%s' ArrayField.get_db_prep_value = lambda self, value, connection, prepared=False: json.dumps(value) if value is not None else None with connection.schema_editor() as editor: for model in (User, UserActivationApplication, Model, ToolFolder, Tool, ApplicationFolder, Application, ApplicationVersion, ApplicationKnowledgeMapping, SystemSetting, UserMenuSetting, Log, WorkspaceUserResourcePermission, PlatformRole, PlatformRoleMember, PlatformGrant, KnowledgeFolder, Knowledge, AIParserResource, AIFlow, AIService, AITask, AIArtifact): editor.create_model(model) suite = unittest.defaultTestLoader.loadTestsFromNames(['system_manage.test_mcp_engine', 'system_manage.test_mcp_business', 'system_manage.test_ai_hub_organize', 'system_manage.test_summary_sections', 'system_manage.test_workbench_defaults', 'models_provider.test_context_policy']) outcome = unittest.TextTestRunner(verbosity=2).run(suite) connections.close_all() sys.exit(0 if outcome.wasSuccessful() else 1)