Skip to content

Commit

Permalink
test: add test case for date comparison in search route (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
JadenKim-dev authored Jun 1, 2024
1 parent de43a6b commit 062b3fa
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
4 changes: 4 additions & 0 deletions spec/search/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ export class TestEntity extends BaseEntity {
@Column({ nullable: true })
@IsOptional({ groups: [GROUP.SEARCH] })
col3: number;

@Column({ nullable: true })
@IsOptional({ groups: [GROUP.SEARCH] })
col4: Date;
}

@Injectable()
Expand Down
28 changes: 14 additions & 14 deletions spec/search/search-cursor-pagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ describe('Search Cursor Pagination', () => {

expect(body).toEqual({
data: [
{ col1: 'col1_1', col2: 1, col3: 49 },
{ col1: 'col1_11', col2: 11, col3: 39 },
{ col1: 'col1_13', col2: 13, col3: 37 },
{ col1: 'col1_15', col2: 15, col3: 35 },
{ col1: 'col1_17', col2: 17, col3: 33 },
{ col1: 'col1_1', col2: 1, col3: 49, col4: null },
{ col1: 'col1_11', col2: 11, col3: 39, col4: null },
{ col1: 'col1_13', col2: 13, col3: 37, col4: null },
{ col1: 'col1_15', col2: 15, col3: 35, col4: null },
{ col1: 'col1_17', col2: 17, col3: 33, col4: null },
],
metadata: {
limit: 5,
Expand All @@ -216,11 +216,11 @@ describe('Search Cursor Pagination', () => {
.expect(HttpStatus.OK);
expect(nextBody).toEqual({
data: [
{ col1: 'col1_19', col2: 19, col3: 31 },
{ col1: 'col1_21', col2: 21, col3: 29 },
{ col1: 'col1_23', col2: 23, col3: 27 },
{ col1: 'col1_25', col2: 25, col3: null },
{ col1: 'col1_27', col2: 27, col3: null },
{ col1: 'col1_19', col2: 19, col3: 31, col4: null },
{ col1: 'col1_21', col2: 21, col3: 29, col4: null },
{ col1: 'col1_23', col2: 23, col3: 27, col4: null },
{ col1: 'col1_25', col2: 25, col3: null, col4: null },
{ col1: 'col1_27', col2: 27, col3: null, col4: null },
],
metadata: {
limit: 5,
Expand All @@ -234,16 +234,16 @@ describe('Search Cursor Pagination', () => {
.send({ where: [{ col1: { operator: 'IN', operand: ['col1_19', 'col1_21', 'col1_23', 'col1_25', 'col1_27'] } }], take: 2 })
.expect(HttpStatus.OK);
expect(searchInBody.data).toEqual([
{ col1: 'col1_27', col2: 27, col3: null },
{ col1: 'col1_25', col2: 25, col3: null },
{ col1: 'col1_27', col2: 27, col3: null, col4: null },
{ col1: 'col1_25', col2: 25, col3: null, col4: null },
]);
const { body: nextInBody } = await request(app.getHttpServer())
.post('/base/search')
.send({ nextCursor: searchInBody.metadata.nextCursor })
.expect(HttpStatus.OK);
expect(nextInBody.data).toEqual([
{ col1: 'col1_23', col2: 23, col3: 27 },
{ col1: 'col1_21', col2: 21, col3: 29 },
{ col1: 'col1_23', col2: 23, col3: 27, col4: null },
{ col1: 'col1_21', col2: 21, col3: 29, col4: null },
]);
});

Expand Down
19 changes: 17 additions & 2 deletions spec/search/search-query-operator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@ describe('Search Query Operator', () => {
* col3
* - when index is [0-4], it has [10-6]
* - when index is [5-9], is has null
*
* col4
* - when index is [0-9], it has [2000-01-01 ~ 2000-10-01]
*/
await service.repository.save(
Array.from({ length: 5 }, (_, index) => index).map((no: number) =>
service.repository.create({ col1: `col${no % 2 === 0 ? '0' : '1'}_${no}`, col2: no, col3: 10 - no }),
service.repository.create({
col1: `col${no % 2 === 0 ? '0' : '1'}_${no}`,
col2: no,
col3: 10 - no,
col4: new Date(2000, no, 1),
}),
),
);
await service.repository.save(
Array.from({ length: 5 }, (_, index) => index + 5).map((no: number) =>
service.repository.create({ col1: `col${no % 2 === 0 ? '0' : '1'}_${no}`, col2: no }),
service.repository.create({ col1: `col${no % 2 === 0 ? '0' : '1'}_${no}`, col2: no, col4: new Date(2000, no, 1) }),
),
);

Expand Down Expand Up @@ -77,21 +85,27 @@ describe('Search Query Operator', () => {
const fixtures: Array<[RequestSearchDto<TestEntity>, number[]]> = [
[{ where: [{ col2: { operator: '=', operand: 5 } }] }, [5]],
[{ where: [{ col2: { operator: '=', operand: 5, not: true } }] }, [0, 1, 2, 3, 4, 6, 7, 8, 9]],
[{ where: [{ col4: { operator: '=', operand: '2000-06-01' } }] }, [5]],

[{ where: [{ col2: { operator: '!=', operand: 5 } }] }, [1, 2, 3, 4, 0, 6, 7, 8, 9]],
[{ where: [{ col2: { operator: '!=', operand: 5, not: true } }] }, [5]],
[{ where: [{ col4: { operator: '!=', operand: '2000-06-01' } }] }, [1, 2, 3, 4, 0, 6, 7, 8, 9]],

[{ where: [{ col2: { operator: '>', operand: 5 } }] }, [6, 7, 8, 9]],
[{ where: [{ col2: { operator: '>', operand: 5, not: true } }] }, [0, 1, 2, 3, 4, 5]],
[{ where: [{ col4: { operator: '>', operand: '2000-06-01' } }] }, [6, 7, 8, 9]],

[{ where: [{ col2: { operator: '>=', operand: 5 } }] }, [5, 6, 7, 8, 9]],
[{ where: [{ col2: { operator: '>=', operand: 5, not: true } }] }, [0, 1, 2, 3, 4]],
[{ where: [{ col4: { operator: '>=', operand: '2000-06-01' } }] }, [5, 6, 7, 8, 9]],

[{ where: [{ col2: { operator: '<', operand: 5 } }] }, [0, 1, 2, 3, 4]],
[{ where: [{ col2: { operator: '<', operand: 5, not: true } }] }, [5, 6, 7, 8, 9]],
[{ where: [{ col4: { operator: '<', operand: '2000-06-01' } }] }, [0, 1, 2, 3, 4]],

[{ where: [{ col2: { operator: '<=', operand: 5 } }] }, [0, 1, 2, 3, 4, 5]],
[{ where: [{ col2: { operator: '<=', operand: 5, not: true } }] }, [6, 7, 8, 9]],
[{ where: [{ col4: { operator: '<=', operand: '2000-06-01' } }] }, [0, 1, 2, 3, 4, 5]],

[{ where: [{ col1: { operator: 'LIKE', operand: 'col0_%' } }] }, [0, 2, 4, 6, 8]],
[{ where: [{ col1: { operator: 'LIKE', operand: 'col0_%', not: true } }] }, [1, 3, 5, 7, 9]],
Expand All @@ -101,6 +115,7 @@ describe('Search Query Operator', () => {

[{ where: [{ col2: { operator: 'BETWEEN', operand: [5, 7] } }] }, [5, 6, 7]],
[{ where: [{ col2: { operator: 'BETWEEN', operand: [5, 7], not: true } }] }, [0, 1, 2, 3, 4, 8, 9]],
[{ where: [{ col4: { operator: 'BETWEEN', operand: ['2000-03-01', '2000-05-02'] } }] }, [2, 3, 4]],

[{ where: [{ col2: { operator: 'IN', operand: [5, 7, 9] } }] }, [5, 7, 9]],
[{ where: [{ col2: { operator: 'IN', operand: [5, 7, 9], not: true } }] }, [0, 1, 2, 3, 4, 6, 8]],
Expand Down
39 changes: 30 additions & 9 deletions src/lib/interceptor/search-request.interceptor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { UnprocessableEntityException } from '@nestjs/common';
import { IsNotEmpty, IsNumber, IsOptional, IsString } from 'class-validator';
import { Type } from 'class-transformer';
import { IsDate, IsNotEmpty, IsNumber, IsOptional, IsString } from 'class-validator';
import { BaseEntity } from 'typeorm';

import { SearchRequestInterceptor } from './search-request.interceptor';
Expand All @@ -20,6 +21,11 @@ describe('SearchRequestInterceptor', () => {
@IsNumber({}, { groups: ['search'] })
@IsOptional({ groups: ['search'] })
col3: number;

@Type(() => Date)
@IsDate({ groups: ['search'] })
@IsOptional({ groups: ['search'] })
col4: Date;
}

let interceptor: InstanceType<ReturnType<typeof SearchRequestInterceptor>>;
Expand All @@ -38,6 +44,7 @@ describe('SearchRequestInterceptor', () => {
{ name: 'col1', type: 'string', isPrimary: false },
{ name: 'col2', type: 'number', isPrimary: false },
{ name: 'col3', type: 'number', isPrimary: false },
{ name: 'col4', type: 'datetime', isPrimary: false },
],
relations: [],
logger: new CrudLogger(),
Expand Down Expand Up @@ -81,15 +88,29 @@ describe('SearchRequestInterceptor', () => {

describe('body.where', () => {
describe('return search request dto when input is valid', () => {
it('BETWEEN', async () => {
expect(
await interceptor.validateBody({
describe('BETWEEN', () => {
it('number type', async () => {
expect(
await interceptor.validateBody({
where: [{ col3: { operator: 'BETWEEN', operand: [0, 5] } }],
}),
).toEqual({
where: [{ col3: { operator: 'BETWEEN', operand: [0, 5] } }],
}),
).toEqual({
where: [{ col3: { operator: 'BETWEEN', operand: [0, 5] } }],
take: 20,
withDeleted: false,
take: 20,
withDeleted: false,
});
});

it('date type', async () => {
expect(
await interceptor.validateBody({
where: [{ col4: { operator: 'BETWEEN', operand: ['2000-01-01', '2000-02-01'] } }],
}),
).toEqual({
where: [{ col4: { operator: 'BETWEEN', operand: ['2000-01-01', '2000-02-01'] } }],
take: 20,
withDeleted: false,
});
});
});
it('IN', async () => {
Expand Down

0 comments on commit 062b3fa

Please sign in to comment.