Files
xiaoxia-saas/tests/unit/test_schema_guard.py
T
xiaoxia e9c5f2b78f
CI/CD Pipeline / Check if frontend-only change (push) Has been cancelled
CI/CD Pipeline / Validate - Code Quality (push) Has been cancelled
CI/CD Pipeline / Validate - Type Check (mypy) (push) Has been cancelled
CI/CD Pipeline / Validate - Migration (alembic) (push) Has been cancelled
CI/CD Pipeline / Unit Tests (push) Has been cancelled
CI/CD Pipeline / Integration Tests (push) Has been cancelled
CI/CD Pipeline / Frontend Lint (push) Has been cancelled
CI/CD Pipeline / Frontend Unit Tests (push) Has been cancelled
CI/CD Pipeline / PR Build API Image (push) Has been cancelled
CI/CD Pipeline / PR Build Web Image (push) Has been cancelled
CI/CD Pipeline / PR Build Worker Image (push) Has been cancelled
CI/CD Pipeline / Build Staging API Image (push) Has been cancelled
CI/CD Pipeline / Build Staging Web Image (push) Has been cancelled
CI/CD Pipeline / Build Staging Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (push) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (push) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (push) Has been cancelled
CI/CD Pipeline / Build Production API Image (push) Has been cancelled
CI/CD Pipeline / Build Production Web Image (push) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (push) Has been cancelled
CI/CD Pipeline / Deploy Production (push) Has been cancelled
CI/CD Pipeline / Production Browser E2E (push) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (push) Has been cancelled
test: P3-1 第45波单元测试(schema_guard/sms_service/job_use_cases) (#827)
2026-07-24 16:44:39 +08:00

85 lines
2.9 KiB
Python
Executable File

"""Schema Guard 单元测试"""
from __future__ import annotations
import pytest
from packages.adapters.sqlalchemy_impl.schema_guard import (
BLOCKED_AUTO_CREATE_ENVIRONMENTS,
assert_auto_create_schema_allowed,
normalize_environment,
)
class TestNormalizeEnvironment:
"""normalize_environment 测试"""
def test_development(self):
assert normalize_environment("development") == "development"
def test_staging(self):
assert normalize_environment("staging") == "staging"
def test_production(self):
assert normalize_environment("production") == "production"
def test_none_returns_development(self):
assert normalize_environment(None) == "development"
def test_empty_string_returns_development(self):
assert normalize_environment("") == "development"
def test_case_insensitive(self):
assert normalize_environment("PRODUCTION") == "production"
assert normalize_environment("Staging") == "staging"
def test_strips_whitespace(self):
assert normalize_environment(" production ") == "production"
class TestAssertAutoCreateSchemaAllowed:
"""assert_auto_create_schema_allowed 测试"""
def test_development_enabled_ok(self):
# development 环境允许 auto_create
assert_auto_create_schema_allowed("development", True)
def test_development_disabled_ok(self):
assert_auto_create_schema_allowed("development", False)
def test_staging_disabled_ok(self):
# staging 禁用时没问题
assert_auto_create_schema_allowed("staging", False)
def test_production_disabled_ok(self):
assert_auto_create_schema_allowed("production", False)
def test_staging_enabled_raises(self):
with pytest.raises(RuntimeError, match="AUTO_CREATE_SCHEMA"):
assert_auto_create_schema_allowed("staging", True)
def test_production_enabled_raises(self):
with pytest.raises(RuntimeError, match="AUTO_CREATE_SCHEMA"):
assert_auto_create_schema_allowed("production", True)
def test_case_insensitive_blocked(self):
with pytest.raises(RuntimeError):
assert_auto_create_schema_allowed("PRODUCTION", True)
with pytest.raises(RuntimeError):
assert_auto_create_schema_allowed("Staging", True)
def test_none_environment_enabled_ok(self):
# None 视为 development,允许
assert_auto_create_schema_allowed(None, True)
def test_custom_env_enabled_ok(self):
# 其他环境不受限制
assert_auto_create_schema_allowed("test", True)
assert_auto_create_schema_allowed("qa", True)
def test_blocked_environments_count(self):
# 确认只有 staging 和 production 被阻止
assert "staging" in BLOCKED_AUTO_CREATE_ENVIRONMENTS
assert "production" in BLOCKED_AUTO_CREATE_ENVIRONMENTS
assert len(BLOCKED_AUTO_CREATE_ENVIRONMENTS) == 2