Squashed 'paste-framework/' content from commit 34e8684

git-subtree-dir: paste-framework
git-subtree-split: 34e8684c4bc3cebbe177509f42ab4ef5b5425a7a
This commit is contained in:
zwf
2026-06-02 19:09:22 +08:00
commit 4729698049
107 changed files with 21484 additions and 0 deletions
View File
+39
View File
@@ -0,0 +1,39 @@
"""
数据库连接集成测试。
需要真实数据库连接,默认被跳过。
运行方式:pytest tests/integration/ -v
"""
import pytest
from paste.db.basetable import BaseTable
from paste.db.baseadapter import BaseAdapter
@pytest.mark.integration
class TestDbConnection:
"""数据库连接基础测试"""
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_ping_database(self):
"""测试数据库连通性"""
result = BaseAdapter.ping()
assert result is True
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_tables_in_db(self):
"""测试获取表列表"""
tables = await BaseTable.tables_in_db()
assert isinstance(tables, list)
# 验证返回的表名称都是字符串
for table in tables:
assert isinstance(table, str)
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_is_table_exist(self):
"""测试表存在性判断"""
# 测试存在的表(information_schema.tables 必然存在)
exists = await BaseTable.is_table_exist('information_schema')
assert isinstance(exists, bool)
+28
View File
@@ -0,0 +1,28 @@
"""
数据库集成测试。
需要真实数据库连接,默认跳过。
通过 `--run-integration` 参数运行。
"""
import pytest
from paste.db.basetable import BaseTable
@pytest.mark.integration
class TestDatabaseIntegration:
"""数据库集成测试"""
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_tables_in_db(self):
"""测试获取数据库表列表"""
tables = await BaseTable.tables_in_db()
assert isinstance(tables, list)
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_is_table_exist(self):
"""测试表存在性判断"""
exists = await BaseTable.is_table_exist('hat_article')
assert isinstance(exists, bool)
+113
View File
@@ -0,0 +1,113 @@
"""
RBAC 模型集成测试。
需要真实数据库连接,默认被跳过。
运行方式:pytest tests/integration/ -v
"""
import pandas as pd
import pytest
from paste.rbac.rbac_user import RbacUser, RbacItem, RbacPermission, RbacAssignment
@pytest.mark.integration
class TestRbacUser:
"""RBAC 用户模型集成测试"""
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_user_query_generates_sql(self):
"""验证用户查询能生成 SQL"""
query = RbacUser().gen_query()
sql = RbacUser.raw_sql(query)
assert sql is not None
assert 'SELECT' in str(sql)
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_user_query_all(self):
"""测试查询所有用户"""
query = RbacUser().gen_query()
users = await RbacUser.query_all(query)
assert isinstance(users, list)
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_user_query_as_dataframe(self):
"""测试用户查询返回 DataFrame"""
query = RbacUser().gen_query()
df = await RbacUser.query_as_df(query)
assert isinstance(df, pd.DataFrame)
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_find_by_username(self):
"""测试根据用户名查询"""
user = await RbacUser.find_by_username('test')
# 如果用户不存在,返回 None
if user is not None:
assert isinstance(user, RbacUser)
assert user.username == 'test'
@pytest.mark.integration
class TestRbacItem:
"""RBAC 授权项模型集成测试"""
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_item_query_all(self):
"""测试查询所有授权项"""
query = RbacItem().gen_query()
items = await RbacItem.query_all(query)
assert isinstance(items, list)
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_item_query_as_dataframe(self):
"""测试授权项查询返回 DataFrame"""
query = RbacItem().gen_query()
df = await RbacItem.query_as_df(query)
assert isinstance(df, pd.DataFrame)
@pytest.mark.integration
class TestRbacPermission:
"""RBAC 权限模型集成测试"""
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_permission_query_all(self):
"""测试查询所有权限"""
query = RbacPermission().gen_query()
permissions = await RbacPermission.query_all(query)
assert isinstance(permissions, list)
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_permission_query_as_dataframe(self):
"""测试权限查询返回 DataFrame"""
query = RbacPermission().gen_query()
df = await RbacPermission.query_as_df(query)
assert isinstance(df, pd.DataFrame)
@pytest.mark.integration
class TestRbacAssignment:
"""RBAC 分配关系集成测试"""
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_assignment_query_all(self):
"""测试查询所有分配关系"""
query = RbacAssignment().gen_query()
assignments = await RbacAssignment.query_all(query)
assert isinstance(assignments, list)
@pytest.mark.skip(reason="需要真实数据库连接")
@pytest.mark.asyncio
async def test_assignment_query_as_dataframe(self):
"""测试分配关系查询返回 DataFrame"""
query = RbacAssignment().gen_query()
df = await RbacAssignment.query_as_df(query)
assert isinstance(df, pd.DataFrame)
+45
View File
@@ -0,0 +1,45 @@
"""
Redis 集成测试。
需要真实 Redis 服务,默认被跳过。
运行方式:pytest tests/integration/ -v
"""
import pytest
from paste.db.redis import Redis
@pytest.mark.integration
class TestRedisConnection:
"""Redis 集成测试"""
@pytest.mark.skip(reason="需要真实 Redis 服务")
@pytest.mark.asyncio
async def test_redis_ping(self):
"""测试 Redis 连通性"""
result = await Redis.ping()
assert result is True
@pytest.mark.skip(reason="需要真实 Redis 服务")
@pytest.mark.asyncio
async def test_redis_get_set(self):
"""测试 Redis 基本读写"""
from paste.db.redis import Redis
async with await Redis.get_redis() as r:
# 写入测试
await r.set("test_key", "test_value")
# 读取验证
value = await r.get("test_key")
assert value == b"test_value"
# 清理
await r.delete("test_key")
@pytest.mark.skip(reason="需要真实 Redis 服务")
@pytest.mark.asyncio
async def test_redis_get_keys(self):
"""测试获取所有 keys"""
from paste.db.redis import Redis
keys = await Redis.keys()
assert isinstance(keys, list)