Skip to content

Commit

Permalink
Merge pull request #9 from mike-north/ex3-tests
Browse files Browse the repository at this point in the history
Exercise 3 tests
  • Loading branch information
mike-north authored Dec 1, 2017
2 parents 0064cab + 73cc0ff commit 15b1996
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/data/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ import { getDb } from '../db/utils';
const ALL_ORDERS_COLUMNS = ['*'];

interface AllOrdersOptions {
page: number;
perPage: number;
page?: number;
perPage?: number;
order?: 'asc' | 'desc';
sort?: string;
}

export async function getAllOrders(opts: AllOrdersOptions): Promise<Order[]> {
export async function getAllOrders(
{ page = 1, perPage = 20, sort = 'Id', order = 'asc' }: AllOrdersOptions = {
order: 'asc',
page: 1,
perPage: 20,
sort: 'Id'
}
): Promise<Order[]> {
const db = await getDb('dev');
return await db.all(`
SELECT ${ALL_ORDERS_COLUMNS.join(',')}
Expand Down
2 changes: 1 addition & 1 deletion src/routers/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logger } from '../log';
const router = express.Router();

router.get('/', async (req, res) => {
let { page = 1, perPage = 25, sort, order } = req.query;
let { page, perPage, sort, order } = req.query;
let orders = await getAllOrders({ page, perPage, sort, order });
res.render('orders', { orders, page });
});
Expand Down
2 changes: 1 addition & 1 deletion tests/ex1.collection-cols.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class EmployeeDataTest {

@test('All orders')
public async allOrders() {
let result = await getAllOrders({ perPage: 20, page: 1 });
let result = await getAllOrders({ perPage: 50000 });
assert.isArray(result, 'Expected result to be an array');
assert.equal(result.length, 16818, 'Expected 16818 orders in array');
assert.hasAllKeys(
Expand Down
51 changes: 51 additions & 0 deletions tests/ex3.pagination-orders.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { assert } from 'chai';
import { slow, suite, test, timeout } from 'mocha-typescript';
import { getAllOrders } from '../src/data/orders';

@suite('EX3: Order List Query - Pagination tests')
class EmployeeDataTest {
@test('First item is the same, regardless of page size')
public async firstPage() {
let first40Result = await getAllOrders({ perPage: 40, page: 1 });
let first20Result = await getAllOrders({ perPage: 20, page: 1 });
assert.isArray(first20Result, 'Expected result to be an array');
assert.equal(first20Result.length, 20, 'Expected 20 orders in array when perPage = 20');
assert.equal(first40Result.length, 40, 'Expected 40 orders in array when perPage = 40');
}

@test('When perPage = 20, page 2 starts at item 20')
public async offset() {
let first40Result = await getAllOrders({ perPage: 40, page: 1 });
let first20Result = await getAllOrders({ perPage: 20, page: 1 });
let second20Result = await getAllOrders({ perPage: 20, page: 2 });

assert.isArray(second20Result, 'Expected result to be an array');
assert.equal(second20Result.length, 20, 'Expected 20 orders in array');

assert.deepEqual(
second20Result[0],
first40Result[20],
'First item on the second page of 20 is the 20th item on the first page of 40'
);
}

@test('If no perPage option is specified, page size is 25')
public async pageOf25ByDefault() {
let firstPageResult = await getAllOrders();
assert.isArray(firstPageResult, 'Expected result to be an array');
assert.equal(firstPageResult.length, 20, 'Expected 20 orders in array');
}

@test('If no page option is specified, first page is returned')
public async firstPageByDefault() {
let result = await getAllOrders();
let firstPageResult = await getAllOrders({ page: 1 });
assert.isArray(result, 'Expected result to be an array');
assert.isArray(firstPageResult, 'Expected result to be an array');
assert.deepEqual(
result[0],
firstPageResult[0],
'First item is the same, regardless of whether page=1 or page option is not provided at all'
);
}
}
24 changes: 24 additions & 0 deletions tests/ex3.sort-orders.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { assert } from 'chai';
import { sortBy } from 'lodash';
import { slow, suite, test, timeout } from 'mocha-typescript';
import { getAllOrders } from '../src/data/orders';

@suite('EX3: Order List Query - Sort tests')
class EmployeeDataTest {
@test('By default, order list is sorted ascending by Id')
public async orderListDefaults() {
let firstPageResult = await getAllOrders({ perPage: 3 });
let sortedById = sortBy(firstPageResult, 'Id');
assert.deepEqual(firstPageResult, sortedById);
}

@test('using order="desc" (and specifying no column to sort on) sorts decending by Id')
public async orderListDesc() {
let firstPageResult = await getAllOrders({ perPage: 3, order: 'desc' });
let sortedById = sortBy(firstPageResult, o => {
let id: number = typeof o.Id === 'string' ? parseInt(o.Id, 10) : o.Id;
return -1 * id;
});
assert.deepEqual(firstPageResult, sortedById);
}
}
2 changes: 1 addition & 1 deletion tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "../tsconfig",
"include": ["./"]
"include": ["./", "../src/types"]
}

0 comments on commit 15b1996

Please sign in to comment.