关于 qe-database-testing
qe-database-testing 是一项数据库测试技能,为验证数据库结构、数据完整性、迁移、事务和查询性能提供检查要点与示例。适用于开发和测试人员,重点涵盖可回滚迁移及并发事务验证。
主要功能
- 01验证表、列与约束
- 02检查唯一性和外键等数据完整性
- 03测试迁移执行与回滚
- 04验证事务隔离及 ACID 属性
- 05测量查询性能与索引使用
适用场景
- 01验证新建表和数据库结构
- 02检查迁移能否回滚且保留数据
- 03测试错误时的事务回滚
- 04定位查询性能问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ proffesor-for-testing/ agentic-qe --skill qe-database-testing安装后在 Claude Code 中输入/qe-database-testing确认已加载。
详细介绍
数据库测试
<default_to_action> 测试数据库操作时:
- 验证架构(表、列、约束是否按预期存在)
- 测试数据完整性(唯一约束、外键约束、检查约束)
- 验证迁移(升级是否成功、回滚是否成功、数据是否保留)
- 检查事务隔离(ACID 属性、并发访问)
- 衡量查询性能(是否使用索引、执行时间)
数据库测试快速检查清单:
- 架构符合规范
- 唯一约束可防止重复数据
- 外键可防止孤立记录
- 迁移可逆
- 出错时事务会回滚
关键成功因素:
- 数据库错误会导致数据丢失或损坏(后果严重)
- 在生产环境部署前,先在预发布环境测试迁移
- 事务测试可捕获并发错误 </default_to_action>
快速参考卡
适用场景
- 新建表或架构
- 开发迁移
- 验证数据完整性
- 优化查询性能
数据库测试类型
| 类型 | 关注点 | 时机 |
|---|---|---|
| 架构 | 结构是否正确 | 创建表时 |
| 完整性 | 约束是否生效 | 数据操作时 |
| 迁移 | 升级/回滚是否正常 | 架构变更时 |
| 事务 | ACID 属性 | 并发访问时 |
| 性能 | 查询速度 | 优化时 |
ACID 属性
| 属性 | 测试重点 |
|---|---|
| 原子性 | 要么全部成功,要么全部失败(出错时回滚) |
| 一致性 | 约束始终有效 |
| 隔离性 | 并发事务互不干扰 |
| 持久性 | 已提交的数据会持久保存 |
架构测试
test('users table has correct schema', async () => {
const schema = await db.raw(`
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_name = 'users'
`);
expect(schema).toContainEqual({
column_name: 'id',
data_type: 'integer',
is_nullable: 'NO'
});
expect(schema).toContainEqual({
column_name: 'email',
data_type: 'character varying',
is_nullable: 'NO'
});
});
数据完整性测试
test('email must be unique', async () => {
await db.users.create({ email: '[email protected]' });
await expect(
db.users.create({ email: '[email protected]' })
).rejects.toThrow('unique constraint violation');
});
test('foreign key prevents orphaned records', async () => {
const user = await db.users.create({ email: '[email protected]' });
await db.orders.create({ userId: user.id, total: 100 });
await expect(
db.users.delete({ id: user.id })
).rejects.toThrow('foreign key constraint');
});
迁移测试
test('migration is reversible', async () => {
await migrate('add-users-table');
// Table exists after migration
const tables = await db.raw(`SELECT table_name FROM information_schema.tables`);
expect(tables.map(t => t.table_name)).toContain('users');
await rollback('add-users-table');
// Table gone after rollback
const tablesAfter = await db.raw(`SELECT table_name FROM information_schema.tables`);
expect(tablesAfter.map(t => t.table_name)).not.toContain('users');
});
test('migration preserves existing data', async () => {
await db.users.create({ email: '[email protected]' });
await migrate('add-age-column');
const user = await db.users.findOne({ email: '[email protected]' });
expect(user).toBeDefined();
expect(user.age).toBeNull(); // New column, null default
});
事务测试
test('transaction rolls back on error', async () => {
const initialCount = await db.users.count();
try {
await db.transaction(async (trx) => {
await trx('users').insert({ email: '[email protected]' });
await trx('users').insert({ email: '[email protected]' });
throw new Error('Rollback test');
});
} catch (error) { /* Expected */ }
expect(await db.users.count()).toBe(initialCount);
});
test('concurrent transactions isolated', async () => {
const user = await db.users.create({ balance: 100 });
// Two concurrent withdrawals (race condition test)
await Promise.all([
db.transaction(async (trx) => {
const current = await trx('users').where({ id: user.id }).first();
await trx('users').update({ balance: current.balance - 50 });
}),
db.transaction(async (trx) => {
const current = await trx('users').where({ id: user.id }).first();
await trx('users').update({ balance: current.balance - 50 });
})
]);
const final = await db.users.findOne({ id: user.id });
expect(final.balance).toBe(0); // Proper isolation
});
Agent 驱动的数据库测试
// Generate test data with integrity
await Task("Generate Test Data", {
schema: 'ecommerce',
tables: ['users', 'products', 'orders'],
count: { users: 1000, products: 500, orders: 5000 },
preserveReferentialIntegrity: true
}, "qe-test-data-architect");
// Test migration safety
await Task("Migration Test", {
migration: 'add-payment-status-column',
tests: ['forward', 'rollback', 'data-preservation'],
environment: 'staging'
}, "qe-test-executor");
Agent 协调提示
记忆命名空间
aqe/database-testing/
├── schema-snapshots/* - Current schema state
├── migrations/* - Migration test results
├── integrity/* - Constraint validation
└── performance/* - Query benchmarks
集群协调
const dbFleet = await FleetManager.coordinate({
strategy: 'database-testing',
agents: [
'qe-test-data-architect', // Generate test data
'qe-test-executor', // Run DB tests
'qe-performance-tester' // Query performance
],
topology: 'sequential'
});
相关技能
- test-data-management - 生成测试数据
- performance-testing - 查询性能
- compliance-testing - 数据保护
请记住
数据库错误后果严重。 数据丢失后无法恢复。损坏会悄无声息地蔓延。性能问题会不断累积。
在生产环境部署前测试迁移: 升级正常、回滚正常、数据得到保留、性能可接受。切勿部署未经测试的迁移。
使用 Agent: qe-test-data-architect 可生成具有引用完整性的逼真测试数据。qe-test-executor 可在 CI/CD 中自动验证迁移。
评论
支持 Markdown