Skip to content

Commit

Permalink
fix: correct unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
WBDC-Phong-Do committed Nov 7, 2023
1 parent 311bc52 commit 3100bd4
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
5 changes: 5 additions & 0 deletions spec/relation-entities/comment.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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] })
Expand Down
6 changes: 5 additions & 1 deletion spec/relation-entities/question.entity.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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[];
}
5 changes: 5 additions & 0 deletions spec/relation-entities/relation-entities-read.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -161,6 +162,7 @@ describe('Relation Entities Read', () => {
lastModifiedAt: writer1Body.lastModifiedAt,
name: writer1Body.name,
},
comments: [comment1Body, comment2Body],
});

const { body: commentListBody } = await request(app.getHttpServer())
Expand All @@ -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,
});
}

Expand All @@ -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,
});
}

Expand All @@ -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,
});
});
});
7 changes: 4 additions & 3 deletions spec/relation-entities/relation-entities-search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Relation Entities Search', () => {
RelationEntitiesModule({
category: {},
writer: {},
question: { search: { relations: ['writer', 'category', 'comment'] } },
question: { search: { relations: ['writer', 'category', 'comments'] } },
comment: {},
}),
],
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -94,6 +94,7 @@ describe('Relation Entities Search', () => {
lastModifiedAt: writer1Body.lastModifiedAt,
name: writer1Body.name,
},
comments: [comment1Body, comment2Body],
});
});
});
4 changes: 2 additions & 2 deletions src/lib/interceptor/search-request.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 3100bd4

Please sign in to comment.