You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
6.0 KiB
165 lines
6.0 KiB
import { expect } from 'chai'
|
|
import BaseModel from '../../src/db/models/BaseModel.js'
|
|
import db from '../../src/db/index.js'
|
|
|
|
// 创建测试模型类
|
|
class TestModel extends BaseModel {
|
|
static get tableName() {
|
|
return 'test_table'
|
|
}
|
|
}
|
|
|
|
describe('BaseModel', () => {
|
|
before(async () => {
|
|
// 创建测试表
|
|
await db.schema.createTableIfNotExists('test_table', (table) => {
|
|
table.increments('id').primary()
|
|
table.string('name')
|
|
table.string('email')
|
|
table.integer('age')
|
|
table.timestamp('created_at').defaultTo(db.fn.now())
|
|
table.timestamp('updated_at').defaultTo(db.fn.now())
|
|
})
|
|
})
|
|
|
|
after(async () => {
|
|
// 清理测试表
|
|
await db.schema.dropTableIfExists('test_table')
|
|
})
|
|
|
|
beforeEach(async () => {
|
|
// 清空测试数据
|
|
await db('test_table').del()
|
|
})
|
|
|
|
describe('CRUD Operations', () => {
|
|
it('应该正确创建记录', async () => {
|
|
const data = { name: 'Test User', email: 'test@example.com', age: 25 }
|
|
const result = await TestModel.create(data)
|
|
|
|
expect(result).to.have.property('id')
|
|
expect(result.name).to.equal('Test User')
|
|
expect(result.email).to.equal('test@example.com')
|
|
expect(result.age).to.equal(25)
|
|
expect(result).to.have.property('created_at')
|
|
expect(result).to.have.property('updated_at')
|
|
})
|
|
|
|
it('应该正确查找记录', async () => {
|
|
// 先创建一条记录
|
|
const data = { name: 'Test User', email: 'test@example.com', age: 25 }
|
|
const created = await TestModel.create(data)
|
|
|
|
// 按ID查找
|
|
const found = await TestModel.findById(created.id)
|
|
expect(found).to.deep.equal(created)
|
|
|
|
// 查找不存在的记录
|
|
const notFound = await TestModel.findById(999999)
|
|
expect(notFound).to.be.null
|
|
})
|
|
|
|
it('应该正确更新记录', async () => {
|
|
// 先创建一条记录
|
|
const data = { name: 'Test User', email: 'test@example.com', age: 25 }
|
|
const created = await TestModel.create(data)
|
|
|
|
// 更新记录
|
|
const updateData = { name: 'Updated User', age: 30 }
|
|
const updated = await TestModel.update(created.id, updateData)
|
|
|
|
expect(updated.name).to.equal('Updated User')
|
|
expect(updated.age).to.equal(30)
|
|
expect(updated.email).to.equal('test@example.com') // 未更新的字段保持不变
|
|
})
|
|
|
|
it('应该正确删除记录', async () => {
|
|
// 先创建一条记录
|
|
const data = { name: 'Test User', email: 'test@example.com', age: 25 }
|
|
const created = await TestModel.create(data)
|
|
|
|
// 删除记录
|
|
await TestModel.delete(created.id)
|
|
|
|
// 验证记录已被删除
|
|
const found = await TestModel.findById(created.id)
|
|
expect(found).to.be.null
|
|
})
|
|
})
|
|
|
|
describe('Query Methods', () => {
|
|
beforeEach(async () => {
|
|
// 插入测试数据
|
|
await TestModel.createMany([
|
|
{ name: 'User 1', email: 'user1@example.com', age: 20 },
|
|
{ name: 'User 2', email: 'user2@example.com', age: 25 },
|
|
{ name: 'User 3', email: 'user3@example.com', age: 30 }
|
|
])
|
|
})
|
|
|
|
it('应该正确查找所有记录', async () => {
|
|
const results = await TestModel.findAll()
|
|
expect(results).to.have.length(3)
|
|
})
|
|
|
|
it('应该正确分页查找记录', async () => {
|
|
const results = await TestModel.findAll({ page: 1, limit: 2 })
|
|
expect(results).to.have.length(2)
|
|
})
|
|
|
|
it('应该正确按条件查找记录', async () => {
|
|
const results = await TestModel.findWhere({ age: 25 })
|
|
expect(results).to.have.length(1)
|
|
expect(results[0].name).to.equal('User 2')
|
|
})
|
|
|
|
it('应该正确统计记录数量', async () => {
|
|
const count = await TestModel.count()
|
|
expect(count).to.equal(3)
|
|
|
|
const filteredCount = await TestModel.count({ age: 25 })
|
|
expect(filteredCount).to.equal(1)
|
|
})
|
|
|
|
it('应该正确检查记录是否存在', async () => {
|
|
const exists = await TestModel.exists({ age: 25 })
|
|
expect(exists).to.be.true
|
|
|
|
const notExists = await TestModel.exists({ age: 99 })
|
|
expect(notExists).to.be.false
|
|
})
|
|
|
|
it('应该正确分页查询', async () => {
|
|
const result = await TestModel.paginate({ page: 1, limit: 2, orderBy: 'age' })
|
|
expect(result.data).to.have.length(2)
|
|
expect(result.pagination).to.have.property('total', 3)
|
|
expect(result.pagination).to.have.property('totalPages', 2)
|
|
})
|
|
})
|
|
|
|
describe('Batch Operations', () => {
|
|
it('应该正确批量创建记录', async () => {
|
|
const data = [
|
|
{ name: 'Batch User 1', email: 'batch1@example.com', age: 20 },
|
|
{ name: 'Batch User 2', email: 'batch2@example.com', age: 25 }
|
|
]
|
|
|
|
const results = await TestModel.createMany(data)
|
|
expect(results).to.have.length(2)
|
|
expect(results[0].name).to.equal('Batch User 1')
|
|
expect(results[1].name).to.equal('Batch User 2')
|
|
})
|
|
})
|
|
|
|
describe('Error Handling', () => {
|
|
it('应该正确处理数据库错误', async () => {
|
|
try {
|
|
// 尝试创建违反约束的记录(如果有的话)
|
|
await TestModel.create({ name: null }) // 假设name是必需的
|
|
} catch (error) {
|
|
expect(error).to.be.instanceOf(Error)
|
|
expect(error.message).to.include('数据库操作失败')
|
|
}
|
|
})
|
|
})
|
|
})
|