Skip to content

Commit

Permalink
feat: merge query to nextCursor in response
Browse files Browse the repository at this point in the history
  • Loading branch information
jiho-kr committed Aug 14, 2023
1 parent 16ff0ca commit b9bb2bb
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe('CustomEntity - ReadMany', () => {
it('should return next 20 entities after cursor', async () => {
const firstResponse = await request(app.getHttpServer()).get('/base').expect(HttpStatus.OK);
const nextResponse = await request(app.getHttpServer()).get('/base').query({
query: firstResponse.body.metadata.query,
query: firstResponse.body.metadata.nextCursor,
});

expect(nextResponse.statusCode).toEqual(HttpStatus.OK);
Expand Down
5 changes: 2 additions & 3 deletions spec/pagination/pagination.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,13 @@ describe('Pagination with interceptor', () => {
const { body: nextResponseBody } = await request(app.getHttpServer())
.get(`/${PaginationType.CURSOR}`)
.query({
nextCursor: responseBodyAfterDelete.metadata.nextCursor,
query: responseBodyAfterDelete.metadata.query,
query: responseBodyAfterDelete.metadata.nextCursor,
})
.expect(HttpStatus.OK);
const { body: offsetNextResponseBody } = await request(app.getHttpServer())
.get(`/${PaginationType.OFFSET}`)
.query({
query: offsetResponseBodyAfterDelete.metadata.query,
query: offsetResponseBodyAfterDelete.metadata.nextCursor,
offset: offsetResponseBodyAfterDelete.metadata.offset,
})
.expect(HttpStatus.OK);
Expand Down
68 changes: 33 additions & 35 deletions spec/pagination/pagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,20 @@ describe('Pagination', () => {
expect(cursorResponseBody.metadata).toEqual({
nextCursor: expect.any(String),
limit: defaultLimit,
query: expect.any(String),
total: 1,
});
expect(offsetResponseBody.metadata).toEqual({ page: 1, pages: 1, total: 1, offset: 1, query: expect.any(String) });
expect(offsetResponseBody.metadata).toEqual({ page: 1, pages: 1, total: 1, offset: 1, nextCursor: expect.any(String) });

const { body: nextResponseBody } = await request(app.getHttpServer())
.get(`/${PaginationType.CURSOR}`)
.query({
nextCursor: cursorResponseBody.metadata.nextCursor,
query: cursorResponseBody.metadata.query,
query: cursorResponseBody.metadata.nextCursor,
})
.expect(HttpStatus.OK);
const { body: offsetNextResponseBody } = await request(app.getHttpServer())
.get(`/${PaginationType.OFFSET}`)
.query({
query: offsetResponseBody.metadata.query,
query: offsetResponseBody.metadata.nextCursor,
offset: offsetResponseBody.metadata.offset,
})
.expect(HttpStatus.OK);
Expand All @@ -96,7 +94,7 @@ describe('Pagination', () => {
expect(nextResponseBody.data).toHaveLength(0);
expect(nextResponseBody.metadata.nextCursor).not.toEqual(cursorResponseBody.metadata.nextCursor);
expect(nextResponseBody.metadata.limit).toEqual(20);
expect(offsetNextResponseBody.metadata).toEqual({ page: 1, pages: 1, total: 1, offset: 1, query: expect.any(String) });
expect(offsetNextResponseBody.metadata).toEqual({ page: 1, pages: 1, total: 1, offset: 1, nextCursor: expect.any(String) });
});

describe('Cursor', () => {
Expand All @@ -107,7 +105,6 @@ describe('Pagination', () => {
expect(cursorBody.metadata).toEqual({
nextCursor: expect.any(String),
limit: defaultLimit,
query: expect.any(String),
total: totalCount,
});
});
Expand All @@ -118,8 +115,7 @@ describe('Pagination', () => {
const { body: nextResponseBody } = await request(app.getHttpServer())
.get(`/${PaginationType.CURSOR}`)
.query({
nextCursor: firstResponseBody.metadata.nextCursor,
query: firstResponseBody.metadata.query,
query: firstResponseBody.metadata.nextCursor,
})
.expect(HttpStatus.OK);

Expand All @@ -129,7 +125,6 @@ describe('Pagination', () => {
expect(nextResponseBody.metadata).toEqual({
nextCursor: expect.any(String),
limit: defaultLimit,
query: expect.any(String),
total: totalCount - defaultLimit,
});

Expand Down Expand Up @@ -157,23 +152,20 @@ describe('Pagination', () => {
expect(responseBody.metadata).toEqual({
nextCursor: expect.any(String),
limit: defaultLimit,
query: expect.any(String),
total: totalCount,
});

const { body: nextResponseBody } = await request(app.getHttpServer())
.get(`/${PaginationType.CURSOR}`)
.query({
nextCursor: responseBody.metadata.nextCursor,
query: responseBody.metadata.query,
query: responseBody.metadata.nextCursor,
})
.expect(HttpStatus.OK);

expect(nextResponseBody.data).toHaveLength(20);
expect(nextResponseBody.metadata).toEqual({
nextCursor: expect.any(String),
limit: defaultLimit,
query: expect.any(String),
total: totalCount - defaultLimit,
});
expect(nextResponseBody.metadata.nextCursor).not.toEqual(responseBody.metadata.nextCursor);
Expand Down Expand Up @@ -207,11 +199,11 @@ describe('Pagination', () => {
it('should return 20 entities as default', async () => {
const { body } = await request(app.getHttpServer()).get(`/${PaginationType.OFFSET}`).expect(HttpStatus.OK);
expect(body.data).toHaveLength(defaultLimit);
expect(body.metadata).toEqual({ page: 1, pages: 5, total: 100, offset: defaultLimit, query: expect.any(String) });
expect(body.metadata).toEqual({ page: 1, pages: 5, total: 100, offset: defaultLimit, nextCursor: expect.any(String) });

const { body: searchBody } = await request(app.getHttpServer()).post(`/${PaginationType.OFFSET}/search`).expect(HttpStatus.OK);
expect(searchBody.data).toHaveLength(defaultLimit);
expect(searchBody.metadata).toEqual({ page: 1, pages: 5, total: 100, offset: defaultLimit, query: expect.any(String) });
expect(searchBody.metadata).toEqual({ page: 1, pages: 5, total: 100, offset: defaultLimit, nextCursor: expect.any(String) });
});

it('should return next page from offset on readMany', async () => {
Expand All @@ -220,7 +212,7 @@ describe('Pagination', () => {
const { body: nextResponseBody } = await request(app.getHttpServer())
.get(`/${PaginationType.OFFSET}`)
.query({
query: firstResponseBody.metadata.query,
query: firstResponseBody.metadata.nextCursor,
offset: firstResponseBody.metadata.offset,
})
.expect(HttpStatus.OK);
Expand All @@ -230,15 +222,15 @@ describe('Pagination', () => {
pages: 5,
total: 100,
offset: defaultLimit,
query: expect.any(String),
nextCursor: expect.any(String),
});

expect(nextResponseBody.metadata).toEqual({
page: 2,
pages: 5,
total: 100,
offset: defaultLimit * 2,
query: expect.any(String),
nextCursor: expect.any(String),
});
});

Expand All @@ -250,7 +242,7 @@ describe('Pagination', () => {
const { body: nextResponseBody } = await request(app.getHttpServer())
.post(`/${PaginationType.OFFSET}/search`)
.send({
query: firstResponseBody.metadata.query,
query: firstResponseBody.metadata.nextCursor,
offset: firstResponseBody.metadata.offset,
})
.expect(HttpStatus.OK);
Expand All @@ -260,15 +252,15 @@ describe('Pagination', () => {
pages: 5,
total: 100,
offset: defaultLimit,
query: expect.any(String),
nextCursor: expect.any(String),
});

expect(nextResponseBody.metadata).toEqual({
page: 2,
pages: 5,
total: 100,
offset: defaultLimit * 2,
query: expect.any(String),
nextCursor: expect.any(String),
});
});

Expand All @@ -288,7 +280,7 @@ describe('Pagination', () => {
const { body: readManyNextResponseBody } = await request(app.getHttpServer())
.get(`/${PaginationType.OFFSET}`)
.query({
query: readManyResponseBody.metadata.query,
query: readManyResponseBody.metadata.nextCursor,
offset: readManyResponseBody.metadata.offset,
})
.expect(HttpStatus.OK);
Expand All @@ -298,14 +290,14 @@ describe('Pagination', () => {
pages: 5,
total: 100,
offset: 20,
query: expect.any(String),
nextCursor: expect.any(String),
});
expect(readManyNextResponseBody.metadata).toEqual({
page: 2,
pages: 5,
total: 100,
offset: 40,
query: expect.any(String),
nextCursor: expect.any(String),
});

// search
Expand All @@ -321,14 +313,14 @@ describe('Pagination', () => {

const { body: searchNextResponseBody } = await request(app.getHttpServer())
.post(`/${PaginationType.OFFSET}/search`)
.send({ query: searchResponseBody.metadata.query, offset: searchResponseBody.metadata.offset })
.send({ query: searchResponseBody.metadata.nextCursor, offset: searchResponseBody.metadata.offset })
.expect(HttpStatus.OK);
expect(searchNextResponseBody.metadata).toEqual({
page: 2,
pages: 5,
total: 100,
offset: 40,
query: expect.any(String),
nextCursor: expect.any(String),
});

for (const data of searchNextResponseBody.data) {
Expand All @@ -338,14 +330,14 @@ describe('Pagination', () => {

const { body: searchNextNextResponseBody } = await request(app.getHttpServer())
.post(`/${PaginationType.OFFSET}/search`)
.send({ query: searchNextResponseBody.metadata.query, offset: searchNextResponseBody.metadata.offset })
.send({ query: searchNextResponseBody.metadata.nextCursor, offset: searchNextResponseBody.metadata.offset })
.expect(HttpStatus.OK);
expect(searchNextNextResponseBody.metadata).toEqual({
page: 3,
pages: 5,
total: 100,
offset: 60,
query: expect.any(String),
nextCursor: expect.any(String),
});
for (const data of searchNextResponseBody.data) {
expect(data.name).toEqual('same name');
Expand All @@ -360,12 +352,12 @@ describe('Pagination', () => {
.query({ limit })
.expect(HttpStatus.OK);
expect(responseBody.data).toHaveLength(limit);
expect(responseBody.metadata).toEqual({ page: 1, pages: 7, total: 100, offset: limit, query: expect.any(String) });
expect(responseBody.metadata).toEqual({ page: 1, pages: 7, total: 100, offset: limit, nextCursor: expect.any(String) });

const { body: nextResponseBody } = await request(app.getHttpServer())
.get(`/${PaginationType.OFFSET}`)
.query({
query: responseBody.metadata.query,
query: responseBody.metadata.nextCursor,
offset: responseBody.metadata.offset,
limit,
})
Expand All @@ -376,7 +368,7 @@ describe('Pagination', () => {
pages: 7,
total: 100,
offset: limit * 2,
query: expect.any(String),
nextCursor: expect.any(String),
});
});

Expand All @@ -386,21 +378,27 @@ describe('Pagination', () => {
const { body: nextResponseBody } = await request(app.getHttpServer())
.get(`/${PaginationType.OFFSET}`)
.query({
query: firstResponseBody.metadata.query,
query: firstResponseBody.metadata.nextCursor,
offset: firstResponseBody.metadata.offset,
limit,
})
.expect(HttpStatus.OK);

expect(firstResponseBody.data).toHaveLength(defaultLimit);
expect(nextResponseBody.data).toHaveLength(limit);
expect(firstResponseBody.metadata).toEqual({ page: 1, pages: 5, total: 100, offset: defaultLimit, query: expect.any(String) });
expect(firstResponseBody.metadata).toEqual({
page: 1,
pages: 5,
total: 100,
offset: defaultLimit,
nextCursor: expect.any(String),
});
expect(nextResponseBody.metadata).toEqual({
page: 2,
pages: Math.ceil(100 / limit),
total: 100,
offset: defaultLimit + limit,
query: expect.any(String),
nextCursor: expect.any(String),
});

const lastOneOfFirstResponse = firstResponseBody.data.pop();
Expand Down
14 changes: 5 additions & 9 deletions spec/read-many/read-many.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('ReadMany - Options', () => {
it('should return next 20 entities after cursor in ascending order', async () => {
const firstResponse = await request(app.getHttpServer()).get('/sort-asc').expect(HttpStatus.OK);
const nextResponse = await request(app.getHttpServer()).get('/sort-asc').query({
query: firstResponse.body.metadata.query,
query: firstResponse.body.metadata.nextCursor,
});

expect(nextResponse.statusCode).toEqual(HttpStatus.OK);
Expand Down Expand Up @@ -100,23 +100,19 @@ describe('ReadMany - Options', () => {
const { body: nextResponse } = await request(app.getHttpServer())
.get('/sort-desc')
.query({
query: firstResponse.metadata.query,
query: firstResponse.metadata.nextCursor,
})
.expect(HttpStatus.OK);
const { body: secondNextResponse } = await request(app.getHttpServer())
.get('/sort-desc')
.query({
query: nextResponse.metadata.query,
query: nextResponse.metadata.nextCursor,
})
.expect(HttpStatus.OK);

expect(nextResponse.metadata.query).toEqual(expect.any(String));
expect(nextResponse.metadata.query).not.toEqual(firstResponse.metadata.query);
expect(secondNextResponse.metadata.query).toEqual(expect.any(String));
expect(secondNextResponse.metadata.query).not.toEqual(firstResponse.metadata.query);

expect(nextResponse.metadata.nextCursor).not.toEqual(firstResponse.metadata.nextCursor);
expect(secondNextResponse.metadata.nextCursor).not.toEqual(nextResponse.metadata.nextCursor);
expect(secondNextResponse.metadata.nextCursor).toEqual(expect.any(String));
expect(secondNextResponse.metadata.nextCursor).not.toEqual(firstResponse.metadata.nextCursor);

expect(nextResponse.metadata.total).toEqual(firstResponse.metadata.total - nextResponse.metadata.limit);
expect(secondNextResponse.metadata.total).toEqual(nextResponse.metadata.total - secondNextResponse.metadata.limit);
Expand Down
15 changes: 7 additions & 8 deletions spec/search/search-cursor-pagination.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ describe('Search Cursor Pagination', () => {
.expect(HttpStatus.OK);
expect(firstResponse.data).toHaveLength(10);

const lastEntity = PaginationHelper.deserialize<TestEntity>(firstResponse.metadata.nextCursor);
expect(lastEntity).toEqual({ col1: 'col1_29' });
const preCondition: Record<string, unknown> = PaginationHelper.deserialize(firstResponse.metadata.query);
const preCondition: Record<string, unknown> = PaginationHelper.deserialize(firstResponse.metadata.nextCursor);
expect(preCondition).toEqual({
where: expect.any(String),
nextCursor: expect.any(String),
total: expect.any(Number),
});
const lastEntity = PaginationHelper.deserialize<TestEntity>(preCondition.nextCursor as string);
expect(lastEntity).toEqual({ col1: 'col1_29' });
expect(PaginationHelper.deserialize(preCondition.where as string)).toEqual({
where: [
{
Expand All @@ -88,7 +88,7 @@ describe('Search Cursor Pagination', () => {

const { body: secondResponse } = await request(app.getHttpServer())
.post('/base/search')
.send({ nextCursor: firstResponse.metadata.nextCursor, query: firstResponse.metadata.query })
.send({ query: firstResponse.metadata.nextCursor })
.expect(HttpStatus.OK);

expect(secondResponse.data).toHaveLength(10);
Expand All @@ -98,15 +98,14 @@ describe('Search Cursor Pagination', () => {
expect(firstDataCol1.has(nextData.col1)).not.toBeTruthy();
}

const nextLastEntity = PaginationHelper.deserialize(secondResponse.metadata.nextCursor);
expect(nextLastEntity).toEqual({ col1: 'col1_1' });

const nextPreCondition: Record<string, unknown> = PaginationHelper.deserialize(secondResponse.metadata.query);
const nextPreCondition: Record<string, unknown> = PaginationHelper.deserialize(secondResponse.metadata.nextCursor);
expect(nextPreCondition).toEqual({
where: expect.any(String),
nextCursor: expect.any(String),
total: expect.any(Number),
});
const nextLastEntity = PaginationHelper.deserialize(nextPreCondition.nextCursor as string);
expect(nextLastEntity).toEqual({ col1: 'col1_1' });
expect(PaginationHelper.deserialize(nextPreCondition.where as string)).toEqual({
...searchRequestBody,
withDeleted: false,
Expand Down
Loading

0 comments on commit b9bb2bb

Please sign in to comment.