From a92ad374e124e638c9ba273c4e1bcf7d6dd1081c Mon Sep 17 00:00:00 2001 From: liuyi Date: Wed, 4 Jun 2025 22:50:04 +0800 Subject: [PATCH] add base subscriber --- src/modules/content/entities/post.entity.ts | 2 +- .../content/services/category.service.ts | 4 ++++ src/modules/database/base/subscriber.ts | 2 +- test/all-case.test.ts | 22 ++++++++++++++----- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/modules/content/entities/post.entity.ts b/src/modules/content/entities/post.entity.ts index d977de9..3348673 100644 --- a/src/modules/content/entities/post.entity.ts +++ b/src/modules/content/entities/post.entity.ts @@ -48,7 +48,7 @@ export class PostEntity extends BaseEntity { type: PostBodyType; @Expose() - @Column({ comment: '发布时间', type: 'varchar', nullable: true }) + @Column({ comment: '发布时间', type: 'timestamp', nullable: true }) publishedAt?: Date | null; @Expose() diff --git a/src/modules/content/services/category.service.ts b/src/modules/content/services/category.service.ts index 09e0634..0b49386 100644 --- a/src/modules/content/services/category.service.ts +++ b/src/modules/content/services/category.service.ts @@ -19,6 +19,10 @@ export class CategoryService extends BaseService = @EventSubscriber() export abstract class BaseSubscriber - implements EntitySubscriberInterface + implements EntitySubscriberInterface { protected abstract entity: ObjectType; diff --git a/test/all-case.test.ts b/test/all-case.test.ts index e35759d..be43b26 100644 --- a/test/all-case.test.ts +++ b/test/all-case.test.ts @@ -16,6 +16,8 @@ import { TagRepository, } from '@/modules/content/repositories'; +import { MeiliService } from '@/modules/meilisearch/meili.service'; + import { generateRandomNumber, generateUniqueRandomNumbers } from './generate-mock-data'; import { categoriesData, commentData, INIT_DATA, postData, tagData } from './test-data'; @@ -31,6 +33,7 @@ describe('nest app test', () => { let categories: CategoryEntity[]; let tags: TagEntity[]; let comments: CommentEntity[]; + let searchService: MeiliService; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ @@ -45,11 +48,15 @@ describe('nest app test', () => { tagRepository = module.get(TagRepository); postRepository = module.get(PostRepository); commentRepository = module.get(CommentRepository); + searchService = module.get(MeiliService); datasource = module.get(DataSource); if (!datasource.isInitialized) { await datasource.initialize(); } if (INIT_DATA) { + const client = searchService.getClient(); + client.deleteIndex('content'); + const queryRunner = datasource.createQueryRunner(); try { await queryRunner.query('SET FOREIGN_KEY_CHECKS = 0'); @@ -377,7 +384,10 @@ describe('nest app test', () => { }, }); expect(result.json()).toEqual({ - message: ['The format of the parent category ID is incorrect.'], + message: [ + 'The format of the parent category ID is incorrect.', + 'The parent category does not exist', + ], error: 'Bad Request', statusCode: 400, }); @@ -592,7 +602,7 @@ describe('nest app test', () => { }); it('update category with duplicate name in same parent', async () => { - const parentCategory = categories.find((c) => c.children && c.children.length > 1); + const parentCategory = categories.find((c) => c.children?.length > 1); const [child1, child2] = parentCategory.children; const result = await app.inject({ @@ -1120,8 +1130,8 @@ describe('nest app test', () => { }); expect(result.json()).toEqual({ message: [ - 'body should not be empty', - 'body must be shorter than or equal to 1000 characters', + 'Comment content cannot be empty', + 'The length of the comment content cannot exceed 1000', ], error: 'Bad Request', statusCode: 400, @@ -1135,7 +1145,7 @@ describe('nest app test', () => { body: { body: 'Test comment' }, }); expect(result.json()).toEqual({ - message: ['The ID must be specified', 'The ID format is incorrect'], + message: ['The post ID must be specified', 'The ID format is incorrect'], error: 'Bad Request', statusCode: 400, }); @@ -1152,7 +1162,7 @@ describe('nest app test', () => { }, }); expect(result.json()).toEqual({ - message: ['body must be shorter than or equal to 1000 characters'], + message: ['The length of the comment content cannot exceed 1000'], error: 'Bad Request', statusCode: 400, });