From 0e76eee4254585b589a99240c04d009b791f57e4 Mon Sep 17 00:00:00 2001 From: BenBenHu Date: Wed, 4 Mar 2020 01:01:11 +0800 Subject: [PATCH] test: add integration test with Postgres --- test/entities/Example.ts | 10 +++ test/integration.ts | 103 ++++++++++++++++++++++++++++++- test/utils/createQueryBuilder.ts | 8 +++ 3 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 test/entities/Example.ts create mode 100644 test/utils/createQueryBuilder.ts diff --git a/test/entities/Example.ts b/test/entities/Example.ts new file mode 100644 index 0000000..658d81f --- /dev/null +++ b/test/entities/Example.ts @@ -0,0 +1,10 @@ +import { + Entity, + PrimaryGeneratedColumn, +} from 'typeorm'; + +@Entity({ name: 'example' }) +export class Example { + @PrimaryGeneratedColumn() + public id!: number; +} diff --git a/test/integration.ts b/test/integration.ts index fa31ea8..6050176 100644 --- a/test/integration.ts +++ b/test/integration.ts @@ -1,7 +1,12 @@ import { expect } from 'chai'; import { createConnection, getConnection } from 'typeorm'; -describe('test', () => { +import { createQueryBuilder } from './utils/createQueryBuilder'; +import { buildPaginator } from '../src/index'; +import { Cursor } from '../src/Paginator'; +import { Example } from './entities/Example'; + +describe('TypeORM cursor-based pagination test', () => { before(async () => { await createConnection({ type: 'postgres', @@ -10,13 +15,105 @@ describe('test', () => { username: 'test', password: 'test', database: 'test', + entities: [Example], }); + await getConnection().query('DROP TABLE IF EXISTS example;'); await getConnection().query('CREATE TABLE example as SELECT generate_series(1, 10) AS id;'); }); - it('should ok', () => { - expect(1).to.eq(1); + let firstPageResult: Example[]; + let nextPageResult: Example[]; + let cursor: Cursor; + + it('should have afterCursor if the result set has next page', async () => { + const queryBuilder = createQueryBuilder(); + const paginator = buildPaginator({ + entity: Example, + query: { + limit: 1, + }, + }); + + firstPageResult = await paginator.paginate(queryBuilder); + cursor = paginator.getCursor(); + + expect(cursor.afterCursor).to.not.eq(null); + expect(cursor.beforeCursor).to.eq(null); + expect(firstPageResult[0].id).to.eq(10); + }); + + it('should have beforeCursor if the result set has prev page', async () => { + const queryBuilder = createQueryBuilder(); + const paginator = buildPaginator({ + entity: Example, + query: { + limit: 1, + afterCursor: cursor.afterCursor as string, + }, + }); + + nextPageResult = await paginator.paginate(queryBuilder); + cursor = paginator.getCursor(); + + expect(cursor.afterCursor).to.not.eq(null); + expect(cursor.beforeCursor).to.not.eq(null); + expect(nextPageResult[0].id).to.eq(9); + }); + + it('should return prev page result set if beforeCursor is set', async () => { + const queryBuilder = createQueryBuilder(); + const paginator = buildPaginator({ + entity: Example, + query: { + limit: 1, + beforeCursor: cursor.beforeCursor as string, + }, + }); + + const result = await paginator.paginate(queryBuilder); + + expect(result[0].id).to.eq(10); + }); + + it('should return entities with given order', async () => { + const ascQueryBuilder = createQueryBuilder(); + const ascPaginator = buildPaginator({ + entity: Example, + query: { + limit: 1, + order: 'ASC', + }, + }); + + const descQueryBuilder = createQueryBuilder(); + const descPaginator = buildPaginator({ + entity: Example, + query: { + limit: 1, + order: 'DESC', + }, + }); + + const ascResult = await ascPaginator.paginate(ascQueryBuilder); + const descResult = await descPaginator.paginate(descQueryBuilder); + + expect(ascResult[0].id).to.eq(1); + expect(descResult[0].id).to.eq(10); + }); + + it('should return entities with given limit', async () => { + const queryBuilder = createQueryBuilder(); + const paginator = buildPaginator({ + entity: Example, + query: { + limit: 10, + }, + }); + + const result = await paginator.paginate(queryBuilder); + + expect(result).length(10); }); after(async () => { diff --git a/test/utils/createQueryBuilder.ts b/test/utils/createQueryBuilder.ts new file mode 100644 index 0000000..3a8ba2e --- /dev/null +++ b/test/utils/createQueryBuilder.ts @@ -0,0 +1,8 @@ +import { getConnection, SelectQueryBuilder } from 'typeorm'; +import { Example } from '../entities/Example'; + +export function createQueryBuilder(): SelectQueryBuilder { + return getConnection() + .getRepository(Example) + .createQueryBuilder('example'); +}