You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
4.4 KiB
Python
158 lines
4.4 KiB
Python
"""
|
|
配置测试
|
|
"""
|
|
import os
|
|
|
|
import pytest
|
|
from iti.applications import create_app
|
|
from iti.config import DevConfig, TestConfig, ProdConfig, _load_env_file, get_config
|
|
|
|
|
|
class TestConfig2:
|
|
"""配置测试类"""
|
|
|
|
def test_dev_config(self):
|
|
"""测试开发环境配置"""
|
|
app = create_app('dev')
|
|
|
|
assert app.config['DEBUG'] is True
|
|
assert app.config['TESTING'] is False
|
|
assert app.config['SQLALCHEMY_DATABASE_URI']
|
|
|
|
def test_test_config(self):
|
|
"""测试测试环境配置"""
|
|
app = create_app('test')
|
|
|
|
assert app.config['DEBUG'] is False
|
|
assert app.config['TESTING'] is True
|
|
assert ':memory:' in app.config['SQLALCHEMY_DATABASE_URI']
|
|
assert app.config['RATELIMIT_ENABLED'] is False
|
|
|
|
def test_prod_config(self):
|
|
"""测试生产环境配置"""
|
|
app = create_app('prod')
|
|
|
|
assert app.config['DEBUG'] is False
|
|
assert app.config['TESTING'] is False
|
|
|
|
def test_config_classes(self):
|
|
"""测试配置类"""
|
|
dev_config = DevConfig()
|
|
test_config = TestConfig()
|
|
prod_config = ProdConfig()
|
|
|
|
# 开发环境
|
|
assert dev_config.DEBUG is True
|
|
assert dev_config.SQLALCHEMY_ECHO is True
|
|
|
|
# 测试环境
|
|
assert test_config.TESTING is True
|
|
assert test_config.RATELIMIT_ENABLED is False
|
|
|
|
# 生产环境
|
|
assert prod_config.DEBUG is False
|
|
|
|
def test_get_config(self):
|
|
"""测试配置获取函数"""
|
|
dev_config = get_config('dev')
|
|
assert dev_config == DevConfig
|
|
|
|
test_config = get_config('test')
|
|
assert test_config == TestConfig
|
|
|
|
prod_config = get_config('prod')
|
|
assert prod_config == ProdConfig
|
|
|
|
# 测试默认配置
|
|
default_config = get_config('unknown')
|
|
assert default_config == DevConfig
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
"""创建测试应用"""
|
|
return create_app('test')
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app):
|
|
"""创建测试客户端"""
|
|
return app.test_client()
|
|
|
|
|
|
def test_app_creation(app):
|
|
"""测试应用创建"""
|
|
assert app is not None
|
|
assert app.config['TESTING'] is True
|
|
|
|
|
|
def test_app_context(app):
|
|
"""测试应用上下文"""
|
|
with app.app_context():
|
|
assert app.config['TESTING'] is True
|
|
|
|
|
|
def test_load_env_file_reads_project_directory(monkeypatch, tmp_path):
|
|
monkeypatch.delenv("DATABASE_URL", raising=False)
|
|
(tmp_path / ".env.dev").write_text("DATABASE_URL=sqlite:///project.db\n")
|
|
|
|
assert _load_env_file(tmp_path) is True
|
|
assert os.getenv("DATABASE_URL") == "sqlite:///project.db"
|
|
|
|
|
|
def test_load_env_file_does_not_read_framework_package_dir(monkeypatch, tmp_path):
|
|
monkeypatch.chdir(tmp_path)
|
|
monkeypatch.delenv("DATABASE_URL", raising=False)
|
|
monkeypatch.delenv("ITI_ENV_DIR", raising=False)
|
|
|
|
assert _load_env_file() is False
|
|
assert os.getenv("DATABASE_URL") is None
|
|
|
|
|
|
def test_frontend_disabled_by_default():
|
|
app = create_app("test")
|
|
client = app.test_client()
|
|
|
|
response = client.get("/", headers={"Accept": "application/json"})
|
|
|
|
assert response.status_code == 200
|
|
assert response.json["code"] == 404
|
|
|
|
|
|
def test_default_error_page_is_available_for_html_clients():
|
|
app = create_app("test")
|
|
client = app.test_client()
|
|
|
|
response = client.get("/missing-page", headers={"Accept": "text/html"})
|
|
|
|
assert response.status_code == 404
|
|
assert b"404" in response.data
|
|
|
|
|
|
def test_frontend_serves_business_spa_from_configured_path(tmp_path):
|
|
frontend_dir = tmp_path / "web-dist"
|
|
frontend_dir.mkdir()
|
|
(frontend_dir / "index.html").write_text("<div>business spa</div>")
|
|
(frontend_dir / "asset.txt").write_text("asset")
|
|
|
|
class FrontendConfig(TestConfig):
|
|
FRONTEND_ENABLED = True
|
|
FRONTEND_PATH = str(frontend_dir)
|
|
|
|
app = create_app(config_mapping={"test": FrontendConfig, "default": FrontendConfig})
|
|
client = app.test_client()
|
|
|
|
index_response = client.get("/")
|
|
fallback_response = client.get("/business/route")
|
|
asset_response = client.get("/asset.txt")
|
|
|
|
assert index_response.status_code == 200
|
|
assert b"business spa" in index_response.data
|
|
assert fallback_response.status_code == 200
|
|
assert b"business spa" in fallback_response.data
|
|
assert asset_response.status_code == 200
|
|
assert asset_response.data == b"asset"
|
|
|
|
|
|
|