Skip to content

Commit

Permalink
test: add integration test with Postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamin658 committed Mar 3, 2020
1 parent b721eaf commit 0e76eee
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 3 deletions.
10 changes: 10 additions & 0 deletions test/entities/Example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {
Entity,
PrimaryGeneratedColumn,
} from 'typeorm';

@Entity({ name: 'example' })
export class Example {
@PrimaryGeneratedColumn()
public id!: number;
}
103 changes: 100 additions & 3 deletions test/integration.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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 () => {
Expand Down
8 changes: 8 additions & 0 deletions test/utils/createQueryBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { getConnection, SelectQueryBuilder } from 'typeorm';
import { Example } from '../entities/Example';

export function createQueryBuilder(): SelectQueryBuilder<Example> {
return getConnection()
.getRepository(Example)
.createQueryBuilder('example');
}

0 comments on commit 0e76eee

Please sign in to comment.