|
|
@@ -1,36 +1,38 @@
|
|
|
-import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
|
|
-import { AppDataSource } from '@d8d/shared-utils';
|
|
|
+import { describe, it, expect, beforeEach } from 'vitest';
|
|
|
+import { IntegrationTestDatabase, setupIntegrationDatabaseHooksWithEntities } from '@d8d/shared-test-util';
|
|
|
+import { UserEntityMt, RoleMt } from '@d8d/core-module-mt/user-module-mt';
|
|
|
+import { FileMt } from '@d8d/core-module-mt/file-module-mt';
|
|
|
import { UnifiedAdvertisementService } from '../../src/services/unified-advertisement.service';
|
|
|
import { UnifiedAdvertisementTypeService } from '../../src/services/unified-advertisement-type.service';
|
|
|
-import { UnifiedAdvertisementTestDataFactory } from '../utils/test-data-factory';
|
|
|
+import { UnifiedAdvertisement } from '../../src/entities/unified-advertisement.entity';
|
|
|
+import { UnifiedAdvertisementType } from '../../src/entities/unified-advertisement-type.entity';
|
|
|
+
|
|
|
+// 设置集成测试钩子
|
|
|
+setupIntegrationDatabaseHooksWithEntities([UserEntityMt, RoleMt, FileMt, UnifiedAdvertisement, UnifiedAdvertisementType])
|
|
|
|
|
|
describe('UnifiedAdvertisementService', () => {
|
|
|
let service: UnifiedAdvertisementService;
|
|
|
let typeService: UnifiedAdvertisementTypeService;
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
- await AppDataSource.initialize();
|
|
|
- service = new UnifiedAdvertisementService(AppDataSource);
|
|
|
- typeService = new UnifiedAdvertisementTypeService(AppDataSource);
|
|
|
- });
|
|
|
-
|
|
|
- afterEach(async () => {
|
|
|
- await UnifiedAdvertisementTestDataFactory.cleanupTestData();
|
|
|
- await AppDataSource.destroy();
|
|
|
+ const dataSource = await IntegrationTestDatabase.getDataSource();
|
|
|
+ service = new UnifiedAdvertisementService(dataSource);
|
|
|
+ typeService = new UnifiedAdvertisementTypeService(dataSource);
|
|
|
});
|
|
|
|
|
|
describe('create', () => {
|
|
|
- it('should create advertisement with default status=1', async () => {
|
|
|
+ it('应该创建广告,默认状态为1', async () => {
|
|
|
+ const timestamp = Date.now();
|
|
|
const adType = await typeService.create({
|
|
|
name: '测试类型',
|
|
|
- code: 'test_type',
|
|
|
+ code: `tt_${timestamp}`,
|
|
|
status: 1
|
|
|
}, 1);
|
|
|
|
|
|
const advertisement = await service.create({
|
|
|
title: '测试广告',
|
|
|
typeId: adType.id,
|
|
|
- code: 'test_ad',
|
|
|
+ code: `ta_${timestamp}`,
|
|
|
url: 'https://example.com',
|
|
|
sort: 10
|
|
|
}, 1);
|
|
|
@@ -40,17 +42,20 @@ describe('UnifiedAdvertisementService', () => {
|
|
|
expect(advertisement.status).toBe(1); // 默认启用
|
|
|
});
|
|
|
|
|
|
- it('should create advertisement with custom status', async () => {
|
|
|
+ it('应该创建广告,使用自定义状态', async () => {
|
|
|
+ const timestamp = Date.now();
|
|
|
const adType = await typeService.create({
|
|
|
name: '测试类型',
|
|
|
- code: 'test_type',
|
|
|
+ code: `tt_${timestamp}`,
|
|
|
status: 1
|
|
|
}, 1);
|
|
|
|
|
|
const advertisement = await service.create({
|
|
|
title: '测试广告',
|
|
|
typeId: adType.id,
|
|
|
- code: 'test_ad',
|
|
|
+ code: `ta_${timestamp}`,
|
|
|
+ url: 'https://example.com',
|
|
|
+ sort: 10,
|
|
|
status: 0
|
|
|
}, 1);
|
|
|
|
|
|
@@ -60,9 +65,10 @@ describe('UnifiedAdvertisementService', () => {
|
|
|
|
|
|
describe('getList', () => {
|
|
|
beforeEach(async () => {
|
|
|
+ const timestamp = Date.now();
|
|
|
const adType = await typeService.create({
|
|
|
name: '测试类型',
|
|
|
- code: 'test_type',
|
|
|
+ code: `tt_${timestamp}`,
|
|
|
status: 1
|
|
|
}, 1);
|
|
|
|
|
|
@@ -70,7 +76,7 @@ describe('UnifiedAdvertisementService', () => {
|
|
|
await service.create({
|
|
|
title: '首页轮播1',
|
|
|
typeId: adType.id,
|
|
|
- code: 'home_banner_1',
|
|
|
+ code: `hb1_${timestamp}`,
|
|
|
status: 1,
|
|
|
sort: 1
|
|
|
}, 1);
|
|
|
@@ -78,7 +84,7 @@ describe('UnifiedAdvertisementService', () => {
|
|
|
await service.create({
|
|
|
title: '首页轮播2',
|
|
|
typeId: adType.id,
|
|
|
- code: 'home_banner_2',
|
|
|
+ code: `hb2_${timestamp}`,
|
|
|
status: 1,
|
|
|
sort: 2
|
|
|
}, 1);
|
|
|
@@ -86,27 +92,27 @@ describe('UnifiedAdvertisementService', () => {
|
|
|
await service.create({
|
|
|
title: '禁用广告',
|
|
|
typeId: adType.id,
|
|
|
- code: 'disabled_ad',
|
|
|
+ code: `dis_${timestamp}`,
|
|
|
status: 0,
|
|
|
sort: 3
|
|
|
}, 1);
|
|
|
});
|
|
|
|
|
|
- it('should return paginated list', async () => {
|
|
|
+ it('应该返回分页列表', async () => {
|
|
|
const [list, total] = await service.getList(1, 10);
|
|
|
|
|
|
expect(total).toBe(3);
|
|
|
expect(list).toHaveLength(3);
|
|
|
});
|
|
|
|
|
|
- it('should filter by keyword', async () => {
|
|
|
+ it('应该支持关键词过滤', async () => {
|
|
|
const [list, total] = await service.getList(1, 10, '首页');
|
|
|
|
|
|
expect(total).toBe(2);
|
|
|
expect(list.every(ad => ad.title?.includes('首页'))).toBe(true);
|
|
|
});
|
|
|
|
|
|
- it('should filter by status', async () => {
|
|
|
+ it('应该支持状态过滤', async () => {
|
|
|
const [list, total] = await service.getList(
|
|
|
1,
|
|
|
10,
|
|
|
@@ -119,7 +125,7 @@ describe('UnifiedAdvertisementService', () => {
|
|
|
expect(list.every(ad => ad.status === 1)).toBe(true);
|
|
|
});
|
|
|
|
|
|
- it('should support sorting', async () => {
|
|
|
+ it('应该支持排序', async () => {
|
|
|
const [list] = await service.getList(
|
|
|
1,
|
|
|
10,
|
|
|
@@ -135,17 +141,18 @@ describe('UnifiedAdvertisementService', () => {
|
|
|
});
|
|
|
|
|
|
describe('getById', () => {
|
|
|
- it('should return advertisement with relations', async () => {
|
|
|
+ it('应该返回广告及其关联', async () => {
|
|
|
+ const timestamp = Date.now();
|
|
|
const adType = await typeService.create({
|
|
|
name: '测试类型',
|
|
|
- code: 'test_type',
|
|
|
+ code: `tt_${timestamp}`,
|
|
|
status: 1
|
|
|
}, 1);
|
|
|
|
|
|
const created = await service.create({
|
|
|
title: '测试广告',
|
|
|
typeId: adType.id,
|
|
|
- code: 'test_ad',
|
|
|
+ code: `ta_${timestamp}`,
|
|
|
status: 1
|
|
|
}, 1);
|
|
|
|
|
|
@@ -157,24 +164,25 @@ describe('UnifiedAdvertisementService', () => {
|
|
|
expect(found?.advertisementType?.id).toBe(adType.id);
|
|
|
});
|
|
|
|
|
|
- it('should return null for non-existent id', async () => {
|
|
|
+ it('应该返回null对于不存在的ID', async () => {
|
|
|
const found = await service.getById(99999);
|
|
|
expect(found).toBeNull();
|
|
|
});
|
|
|
});
|
|
|
|
|
|
describe('update', () => {
|
|
|
- it('should update advertisement', async () => {
|
|
|
+ it('应该更新广告', async () => {
|
|
|
+ const timestamp = Date.now();
|
|
|
const adType = await typeService.create({
|
|
|
name: '测试类型',
|
|
|
- code: 'test_type',
|
|
|
+ code: `tt_${timestamp}`,
|
|
|
status: 1
|
|
|
}, 1);
|
|
|
|
|
|
const created = await service.create({
|
|
|
title: '原始标题',
|
|
|
typeId: adType.id,
|
|
|
- code: 'test_ad',
|
|
|
+ code: `ta_${timestamp}`,
|
|
|
status: 1
|
|
|
}, 1);
|
|
|
|
|
|
@@ -186,24 +194,25 @@ describe('UnifiedAdvertisementService', () => {
|
|
|
expect(updated?.title).toBe('更新标题');
|
|
|
});
|
|
|
|
|
|
- it('should return null for non-existent id', async () => {
|
|
|
+ it('应该返回null对于不存在的ID', async () => {
|
|
|
const result = await service.update(99999, { title: '更新标题' }, 1);
|
|
|
expect(result).toBeNull();
|
|
|
});
|
|
|
});
|
|
|
|
|
|
describe('delete', () => {
|
|
|
- it('should soft delete by setting status=0', async () => {
|
|
|
+ it('应该通过设置status=0实现软删除', async () => {
|
|
|
+ const timestamp = Date.now();
|
|
|
const adType = await typeService.create({
|
|
|
name: '测试类型',
|
|
|
- code: 'test_type',
|
|
|
+ code: `tt_${timestamp}`,
|
|
|
status: 1
|
|
|
}, 1);
|
|
|
|
|
|
const created = await service.create({
|
|
|
title: '测试广告',
|
|
|
typeId: adType.id,
|
|
|
- code: 'test_ad',
|
|
|
+ code: `ta_${timestamp}`,
|
|
|
status: 1
|
|
|
}, 1);
|
|
|
|
|
|
@@ -216,7 +225,7 @@ describe('UnifiedAdvertisementService', () => {
|
|
|
expect(found?.status).toBe(0);
|
|
|
});
|
|
|
|
|
|
- it('should return false for non-existent id', async () => {
|
|
|
+ it('应该返回false对于不存在的ID', async () => {
|
|
|
const result = await service.delete(99999, 1);
|
|
|
expect(result).toBe(false);
|
|
|
});
|