|
|
@@ -1,7 +1,8 @@
|
|
|
-import { describe, it, expect, beforeEach, afterEach, beforeAll, afterAll } from 'vitest';
|
|
|
+import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
|
import { testClient } from 'hono/testing';
|
|
|
import {
|
|
|
IntegrationTestDatabase,
|
|
|
+ setupIntegrationDatabaseHooks,
|
|
|
TestDataFactory
|
|
|
} from '../../../__test_utils__/integration-test-db';
|
|
|
import { IntegrationTestAssertions } from '../../../__test_utils__/integration-test-utils';
|
|
|
@@ -9,18 +10,9 @@ import { userRoutes } from '../../../api';
|
|
|
import { AuthService } from '../../../modules/auth/auth.service';
|
|
|
import { UserService } from '../../../modules/users/user.service';
|
|
|
|
|
|
-// 设置集成测试钩子
|
|
|
-beforeAll(async () => {
|
|
|
- await IntegrationTestDatabase.initialize();
|
|
|
-});
|
|
|
-
|
|
|
-afterEach(async () => {
|
|
|
- await IntegrationTestDatabase.clearAllData();
|
|
|
-});
|
|
|
|
|
|
-afterAll(async () => {
|
|
|
- await IntegrationTestDatabase.cleanup();
|
|
|
-});
|
|
|
+// 设置集成测试钩子
|
|
|
+setupIntegrationDatabaseHooks()
|
|
|
|
|
|
describe('用户API集成测试 (使用hono/testing)', () => {
|
|
|
let client: ReturnType<typeof testClient<typeof userRoutes>>['api']['v1'];
|
|
|
@@ -31,8 +23,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
|
|
|
client = testClient(userRoutes).api.v1;
|
|
|
|
|
|
// 创建测试用户并生成token
|
|
|
- const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
- if (!dataSource) throw new Error('Database not initialized');
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
|
|
|
const userService = new UserService(dataSource);
|
|
|
const authService = new AuthService(userService);
|
|
|
@@ -80,7 +71,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
|
|
|
|
it('应该拒绝创建重复用户名的用户', async () => {
|
|
|
- const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
// 先创建一个用户
|
|
|
@@ -152,7 +143,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
|
|
|
|
|
|
describe('用户读取测试', () => {
|
|
|
it('应该成功获取用户列表', async () => {
|
|
|
- const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
// 创建几个测试用户
|
|
|
@@ -177,7 +168,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
|
|
|
|
it('应该成功获取单个用户详情', async () => {
|
|
|
- const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
const testUser = await TestDataFactory.createTestUser(dataSource, {
|
|
|
@@ -222,7 +213,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
|
|
|
|
|
|
describe('用户更新测试', () => {
|
|
|
it('应该成功更新用户信息', async () => {
|
|
|
- const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
const testUser = await TestDataFactory.createTestUser(dataSource, {
|
|
|
@@ -296,7 +287,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
|
|
|
|
|
|
describe('用户删除测试', () => {
|
|
|
it('应该成功删除用户', async () => {
|
|
|
- const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
const testUser = await TestDataFactory.createTestUser(dataSource, {
|
|
|
@@ -349,7 +340,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
|
|
|
|
|
|
describe('用户搜索测试', () => {
|
|
|
it('应该能够按用户名搜索用户', async () => {
|
|
|
- const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
await TestDataFactory.createTestUser(dataSource, { username: 'search_user_1', email: 'search1@example.com' });
|
|
|
@@ -380,7 +371,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
|
|
|
});
|
|
|
|
|
|
it('应该能够按邮箱搜索用户', async () => {
|
|
|
- const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
await TestDataFactory.createTestUser(dataSource, { username: 'user_email_1', email: 'test.email1@example.com' });
|
|
|
@@ -409,7 +400,7 @@ describe('用户API集成测试 (使用hono/testing)', () => {
|
|
|
|
|
|
describe('性能测试', () => {
|
|
|
it('用户列表查询响应时间应小于200ms', async () => {
|
|
|
- const dataSource = IntegrationTestDatabase.getDataSource();
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
if (!dataSource) throw new Error('Database not initialized');
|
|
|
|
|
|
// 创建一些测试数据
|