Files
whale-town-end/test-users-functionality.ts
moyin 8591f23505 chore:清理空的gitkeep文件和临时文件
- 删除不再需要的.gitkeep占位文件
- 清理开发过程中的临时测试文件
2025-12-17 11:03:40 +08:00

95 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 用户功能测试脚本
*
* 使用方法npx ts-node test-users-functionality.ts
*/
import 'reflect-metadata';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './src/app.module';
import { UsersService } from './src/core/db/users/users.service';
import { CreateUserDto } from './src/core/db/users/users.dto';
async function testUsersFunctionality() {
console.log('🚀 启动用户功能测试...\n');
try {
// 创建NestJS应用
const app = await NestFactory.createApplicationContext(AppModule, {
logger: false, // 禁用日志以保持输出清洁
});
// 获取用户服务
const usersService = app.get(UsersService);
console.log('✅ 成功获取UsersService实例');
// 测试数据
const testUserDto: CreateUserDto = {
username: `testuser_${Date.now()}`,
email: `test_${Date.now()}@example.com`,
phone: `+86138${Date.now().toString().slice(-8)}`,
password_hash: 'hashed_password_123',
nickname: '功能测试用户',
github_id: `github_${Date.now()}`,
avatar_url: 'https://example.com/avatar.jpg',
role: 1
};
console.log('\n📝 测试创建用户...');
const createdUser = await usersService.create(testUserDto);
console.log('✅ 用户创建成功:', {
id: createdUser.id.toString(),
username: createdUser.username,
nickname: createdUser.nickname,
email: createdUser.email
});
console.log('\n🔍 测试查询用户...');
const foundUser = await usersService.findOne(createdUser.id);
console.log('✅ 用户查询成功:', foundUser.username);
console.log('\n📊 测试用户统计...');
const userCount = await usersService.count();
console.log('✅ 当前用户总数:', userCount);
console.log('\n🔍 测试根据用户名查询...');
const userByUsername = await usersService.findByUsername(createdUser.username);
console.log('✅ 根据用户名查询成功:', userByUsername?.nickname);
console.log('\n✏ 测试更新用户...');
const updatedUser = await usersService.update(createdUser.id, {
nickname: '更新后的昵称'
});
console.log('✅ 用户更新成功:', updatedUser.nickname);
console.log('\n📋 测试查询所有用户...');
const allUsers = await usersService.findAll(5); // 限制5个
console.log('✅ 查询到用户数量:', allUsers.length);
console.log('\n🔍 测试搜索功能...');
const searchResults = await usersService.search('测试');
console.log('✅ 搜索结果数量:', searchResults.length);
console.log('\n🗑 测试删除用户...');
const deleteResult = await usersService.remove(createdUser.id);
console.log('✅ 用户删除成功:', deleteResult.message);
// 验证删除
console.log('\n✅ 验证删除结果...');
try {
await usersService.findOne(createdUser.id);
console.log('❌ 删除验证失败:用户仍然存在');
} catch (error) {
console.log('✅ 删除验证成功:用户已不存在');
}
await app.close();
console.log('\n🎉 所有功能测试通过!');
} catch (error) {
console.error('❌ 测试失败:', error);
process.exit(1);
}
}
testUsersFunctionality();