import { expect } from 'chai' import { UserModel } from '../../src/db/models/UserModel.js' import db from '../../src/db/index.js' describe('UserModel', () => { before(async () => { // 确保users表存在 const exists = await db.schema.hasTable('users') if (!exists) { await db.schema.createTable('users', (table) => { table.increments('id').primary() table.string('username').unique() table.string('email').unique() table.string('password') table.string('role').defaultTo('user') table.string('status').defaultTo('active') table.string('phone') table.integer('age') table.string('name') table.text('bio') table.string('avatar') table.timestamp('created_at').defaultTo(db.fn.now()) table.timestamp('updated_at').defaultTo(db.fn.now()) }) } }) after(async () => { // 清理测试数据 await db('users').del() }) beforeEach(async () => { // 清空用户数据 await db('users').del() }) describe('User Creation', () => { it('应该正确创建用户', async () => { const userData = { username: 'testuser', email: 'test@example.com', password: 'password123', name: 'Test User' } const user = await UserModel.create(userData) expect(user).to.have.property('id') expect(user.username).to.equal('testuser') expect(user.email).to.equal('test@example.com') expect(user.name).to.equal('Test User') expect(user.role).to.equal('user') expect(user.status).to.equal('active') }) it('应该防止重复用户名', async () => { const userData1 = { username: 'duplicateuser', email: 'test1@example.com', password: 'password123' } const userData2 = { username: 'duplicateuser', email: 'test2@example.com', password: 'password123' } await UserModel.create(userData1) try { await UserModel.create(userData2) expect.fail('应该抛出错误') } catch (error) { expect(error.message).to.include('用户名已存在') } }) it('应该防止重复邮箱', async () => { const userData1 = { username: 'user1', email: 'duplicate@example.com', password: 'password123' } const userData2 = { username: 'user2', email: 'duplicate@example.com', password: 'password123' } await UserModel.create(userData1) try { await UserModel.create(userData2) expect.fail('应该抛出错误') } catch (error) { expect(error.message).to.include('邮箱已存在') } }) }) describe('User Queries', () => { let testUser beforeEach(async () => { testUser = await UserModel.create({ username: 'testuser', email: 'test@example.com', password: 'password123', name: 'Test User' }) }) it('应该按ID查找用户', async () => { const user = await UserModel.findById(testUser.id) expect(user).to.deep.equal(testUser) }) it('应该按用户名查找用户', async () => { const user = await UserModel.findByUsername('testuser') expect(user).to.deep.equal(testUser) }) it('应该按邮箱查找用户', async () => { const user = await UserModel.findByEmail('test@example.com') expect(user).to.deep.equal(testUser) }) it('应该查找所有用户', async () => { await UserModel.create({ username: 'anotheruser', email: 'another@example.com', password: 'password123' }) const users = await UserModel.findAll() expect(users).to.have.length(2) }) }) describe('User Updates', () => { let testUser beforeEach(async () => { testUser = await UserModel.create({ username: 'testuser', email: 'test@example.com', password: 'password123', name: 'Test User' }) }) it('应该正确更新用户', async () => { const updated = await UserModel.update(testUser.id, { name: 'Updated Name', phone: '123456789' }) expect(updated.name).to.equal('Updated Name') expect(updated.phone).to.equal('123456789') expect(updated.email).to.equal('test@example.com') // 未更新的字段保持不变 }) it('应该防止更新为重复的用户名', async () => { await UserModel.create({ username: 'anotheruser', email: 'another@example.com', password: 'password123' }) try { await UserModel.update(testUser.id, { username: 'anotheruser' }) expect.fail('应该抛出错误') } catch (error) { expect(error.message).to.include('用户名已存在') } }) it('应该防止更新为重复的邮箱', async () => { await UserModel.create({ username: 'anotheruser', email: 'another@example.com', password: 'password123' }) try { await UserModel.update(testUser.id, { email: 'another@example.com' }) expect.fail('应该抛出错误') } catch (error) { expect(error.message).to.include('邮箱已存在') } }) }) describe('User Status Management', () => { let testUser beforeEach(async () => { testUser = await UserModel.create({ username: 'testuser', email: 'test@example.com', password: 'password123' }) }) it('应该激活用户', async () => { await UserModel.deactivate(testUser.id) let user = await UserModel.findById(testUser.id) expect(user.status).to.equal('inactive') await UserModel.activate(testUser.id) user = await UserModel.findById(testUser.id) expect(user.status).to.equal('active') }) it('应该停用用户', async () => { await UserModel.deactivate(testUser.id) const user = await UserModel.findById(testUser.id) expect(user.status).to.equal('inactive') }) }) describe('User Statistics', () => { beforeEach(async () => { await db('users').del() await UserModel.create({ username: 'activeuser1', email: 'active1@example.com', password: 'password123', status: 'active' }) await UserModel.create({ username: 'activeuser2', email: 'active2@example.com', password: 'password123', status: 'active' }) await UserModel.create({ username: 'inactiveuser', email: 'inactive@example.com', password: 'password123', status: 'inactive' }) }) it('应该正确获取用户统计', async () => { const stats = await UserModel.getUserStats() expect(stats.total).to.equal(3) expect(stats.active).to.equal(2) expect(stats.inactive).to.equal(1) }) }) })