From 3100bd498509716a15d89507dbb7da88b79de42c Mon Sep 17 00:00:00 2001 From: "Phong.Do" Date: Tue, 7 Nov 2023 14:17:10 +0700 Subject: [PATCH] fix: correct unit-test --- spec/relation-entities/comment.entity.ts | 5 +++++ spec/relation-entities/question.entity.ts | 6 +++++- spec/relation-entities/relation-entities-read.spec.ts | 5 +++++ spec/relation-entities/relation-entities-search.spec.ts | 7 ++++--- src/lib/interceptor/search-request.interceptor.spec.ts | 4 ++-- 5 files changed, 21 insertions(+), 6 deletions(-) diff --git a/spec/relation-entities/comment.entity.ts b/spec/relation-entities/comment.entity.ts index b2bb4bf..1ab0578 100644 --- a/spec/relation-entities/comment.entity.ts +++ b/spec/relation-entities/comment.entity.ts @@ -2,6 +2,7 @@ import { Type } from 'class-transformer'; import { IsString, IsOptional, IsNotEmpty, IsNumber } from 'class-validator'; import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm'; +import { QuestionEntity } from './question.entity'; import { WriterEntity } from './writer.entity'; import { GROUP } from '../../src'; import { CrudAbstractEntity } from '../crud.abstract.entity'; @@ -15,6 +16,10 @@ export class CommentEntity extends CrudAbstractEntity { @IsOptional({ groups: [GROUP.READ_MANY] }) questionId: number; + @ManyToOne(() => QuestionEntity) + @JoinColumn({ name: 'questionId' }) + question: QuestionEntity; + @Column('varchar', { nullable: false }) @IsString({ groups: [GROUP.CREATE, GROUP.UPDATE, GROUP.UPSERT] }) @IsNotEmpty({ groups: [GROUP.CREATE, GROUP.UPDATE, GROUP.UPSERT] }) diff --git a/spec/relation-entities/question.entity.ts b/spec/relation-entities/question.entity.ts index 4674c65..a25b7d0 100644 --- a/spec/relation-entities/question.entity.ts +++ b/spec/relation-entities/question.entity.ts @@ -1,8 +1,9 @@ import { Type } from 'class-transformer'; import { IsNotEmpty, IsNumber, IsOptional, IsString } from 'class-validator'; -import { Column, Entity, JoinColumn, ManyToOne } from 'typeorm'; +import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm'; import { CategoryEntity } from './category.entity'; +import { CommentEntity } from './comment.entity'; import { WriterEntity } from './writer.entity'; import { GROUP } from '../../src'; import { CrudAbstractEntity } from '../crud.abstract.entity'; @@ -42,4 +43,7 @@ export class QuestionEntity extends CrudAbstractEntity { @IsNotEmpty({ groups: [GROUP.CREATE, GROUP.UPSERT] }) @IsOptional({ groups: [GROUP.UPDATE, GROUP.SEARCH, GROUP.READ_MANY] }) content: string; + + @OneToMany(() => CommentEntity, (comment) => comment.question) + comments: CommentEntity[]; } diff --git a/spec/relation-entities/relation-entities-read.spec.ts b/spec/relation-entities/relation-entities-read.spec.ts index 5bd7c7e..0ea8ff4 100644 --- a/spec/relation-entities/relation-entities-read.spec.ts +++ b/spec/relation-entities/relation-entities-read.spec.ts @@ -135,6 +135,7 @@ describe('Relation Entities Read', () => { lastModifiedAt: writer1Body.lastModifiedAt, name: writer1Body.name, }, + comments: [comment1Body, comment2Body], }); const { body: getQuestionBody } = await request(app.getHttpServer()).get(`/question/${questionBody.id}`).expect(HttpStatus.OK); @@ -161,6 +162,7 @@ describe('Relation Entities Read', () => { lastModifiedAt: writer1Body.lastModifiedAt, name: writer1Body.name, }, + comments: [comment1Body, comment2Body], }); const { body: commentListBody } = await request(app.getHttpServer()) @@ -185,6 +187,7 @@ describe('Relation Entities Read', () => { createdAt: comment.writerId === writer1Body.id ? writer1Body.createdAt : writer2Body.createdAt, lastModifiedAt: comment.writerId === writer1Body.id ? writer1Body.lastModifiedAt : writer2Body.lastModifiedAt, }, + question: questionBody, }); } @@ -210,6 +213,7 @@ describe('Relation Entities Read', () => { createdAt: comment.writerId === writer1Body.id ? writer1Body.createdAt : writer2Body.createdAt, lastModifiedAt: comment.writerId === writer1Body.id ? writer1Body.lastModifiedAt : writer2Body.lastModifiedAt, }, + question: questionBody, }); } @@ -231,6 +235,7 @@ describe('Relation Entities Read', () => { createdAt: commentBody.writerId === writer1Body.id ? writer1Body.createdAt : writer2Body.createdAt, lastModifiedAt: commentBody.writerId === writer1Body.id ? writer1Body.lastModifiedAt : writer2Body.lastModifiedAt, }, + question: questionBody, }); }); }); diff --git a/spec/relation-entities/relation-entities-search.spec.ts b/spec/relation-entities/relation-entities-search.spec.ts index 950f7b7..7509939 100644 --- a/spec/relation-entities/relation-entities-search.spec.ts +++ b/spec/relation-entities/relation-entities-search.spec.ts @@ -14,7 +14,7 @@ describe('Relation Entities Search', () => { RelationEntitiesModule({ category: {}, writer: {}, - question: { search: { relations: ['writer', 'category', 'comment'] } }, + question: { search: { relations: ['writer', 'category', 'comments'] } }, comment: {}, }), ], @@ -52,11 +52,11 @@ describe('Relation Entities Search', () => { .expect(HttpStatus.CREATED); // Create 2 comments - await request(app.getHttpServer()) + const { body: comment1Body } = await request(app.getHttpServer()) .post('/comment') .send({ questionId: questionBody.id, message: 'Comment Message#1', writerId: writer2Body.id }) .expect(HttpStatus.CREATED); - await request(app.getHttpServer()) + const { body: comment2Body } = await request(app.getHttpServer()) .post('/comment') .send({ questionId: questionBody.id, message: 'Comment Message#2', writerId: writer1Body.id }) .expect(HttpStatus.CREATED); @@ -94,6 +94,7 @@ describe('Relation Entities Search', () => { lastModifiedAt: writer1Body.lastModifiedAt, name: writer1Body.name, }, + comments: [comment1Body, comment2Body], }); }); }); diff --git a/src/lib/interceptor/search-request.interceptor.spec.ts b/src/lib/interceptor/search-request.interceptor.spec.ts index e15659a..ef0282c 100644 --- a/src/lib/interceptor/search-request.interceptor.spec.ts +++ b/src/lib/interceptor/search-request.interceptor.spec.ts @@ -256,7 +256,7 @@ describe('SearchRequestInterceptor', () => { expect(interceptor.getRelations({})).toEqual([]); const InterceptorWithOptions = SearchRequestInterceptor( - { entity: {} as typeof BaseEntity, routes: { readMany: { relations: ['option'] } } }, + { entity: {} as typeof BaseEntity, routes: { search: { relations: ['option'] } } }, { relations: [], logger: new CrudLogger() }, ); const interceptorWithOptions = new InterceptorWithOptions(); @@ -265,7 +265,7 @@ describe('SearchRequestInterceptor', () => { expect(interceptorWithOptions.getRelations({})).toEqual(['option']); const InterceptorWithFalseOptions = SearchRequestInterceptor( - { entity: {} as typeof BaseEntity, routes: { readMany: { relations: false } } }, + { entity: {} as typeof BaseEntity, routes: { search: { relations: false } } }, { relations: [], logger: new CrudLogger() }, ); const interceptorWithFalseOptions = new InterceptorWithFalseOptions();