关于 database-testing
database-testing 是用于验证数据库结构、数据完整性和操作行为的测试技能,适合开发与测试团队。它涵盖迁移回滚、事务隔离和查询性能检查。
主要功能
- 01验证表结构与约束
- 02检查唯一键和外键完整性
- 03测试迁移与回滚
- 04验证事务回滚和并发行为
- 05测量查询性能与索引使用
适用场景
- 01测试新建表和数据库结构
- 02验证数据库迁移可前进并回滚
- 03排查数据完整性与查询性能问题
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ proffesor-for-testing/ agentic-qe --skill database-testing安装后在 Claude Code 中输入/database-testing确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
evals/database-testing.yamlschemas/output.jsonscripts/validate-config.json详细介绍
数据库测试
<default_to_action> 测试数据库操作时:
- 验证架构(表、列和约束是否符合预期)
- 测试数据完整性(唯一性、外键和检查约束)
- 验证迁移(正向迁移正常、回滚正常、数据得到保留)
- 检查事务隔离(ACID 属性、并发访问)
- 衡量查询性能(是否使用索引、执行时间)
数据库测试快速检查清单:
- 架构符合规范
- 唯一约束可防止重复数据
- 外键可防止产生孤立记录
- 迁移可逆
- 发生错误时事务会回滚
关键成功因素:
- 数据库错误会导致数据丢失或损坏(后果严重)
- 在生产环境部署前,先在预发布环境测试迁移
- 事务测试可发现并发错误 </default_to_action>
快速参考卡
适用场景
- 新建表或架构
- 开发迁移
- 验证数据完整性
- 优化查询性能
数据库测试类型
| 类型 | 重点 | 适用时机 |
|---|---|---|
| 架构 | 结构正确 | 创建表 |
| 完整性 | 约束有效 | 数据操作 |
| 迁移 | 正向和回退正常 | 架构变更 |
| 事务 | 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
Fleet 协作
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'
});
相关技能
请记住
**在生产环境部署前测试迁移:**正向迁移正常、回滚正常、数据得到保留、性能可接受。切勿部署未经测试的迁移。
使用 Agent:qe-test-data-architect 可生成具有引用完整性的真实测试数据。qe-test-executor 可在 CI/CD 中自动验证迁移。
评论
支持 Markdown