-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
88 lines (64 loc) · 3.66 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const { expect } = require('chai')
const { describe, it } = require('mocha')
const reconcileOrder = require('./orderBook')
describe('Order Book', () => {
describe('reconcileOrder', () => {
it('adds an order to the book when the book is empty and thus cannot fulfill the order', () => {
const existingBook = []
const incomingOrder = { type: 'sell', quantity: 10, price: 6150 }
const updatedBook = reconcileOrder(existingBook, incomingOrder)
expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 10, price: 6150 }])
})
it('adds an order to the book when the book has orders of the corresponding type (i.e. a sell with no buys)', () => {
const existingBook = [{ type: 'sell', quantity: 10, price: 6150 }]
const incomingOrder = { type: 'sell', quantity: 12, price: 6000 }
const updatedBook = reconcileOrder(existingBook, incomingOrder)
expect(updatedBook).to.deep.equal([
{ type: 'sell', quantity: 10, price: 6150 },
{ type: 'sell', quantity: 12, price: 6000 }
])
})
it('adds an order to the book when the book has a corresponding order type but it does not match', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }]
const incomingOrder = { type: 'sell', quantity: 12, price: 6000 }
const updatedBook = reconcileOrder(existingBook, incomingOrder)
expect(updatedBook).to.deep.equal([
{ type: 'buy', quantity: 10, price: 6150 },
{ type: 'sell', quantity: 12, price: 6000 }
])
})
it('fulfills an order and removes the matching order when the book contains a matching order of the same quantity', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }]
const incomingOrder = { type: 'sell', quantity: 10, price: 6150 }
const updatedBook = reconcileOrder(existingBook, incomingOrder)
expect(updatedBook).to.deep.equal([])
})
it('fulfills an order and reduces the matching order when the book contains a matching order of a larger quantity', () => {
const existingBook = [{ type: 'buy', quantity: 15, price: 6150 }]
const incomingOrder = { type: 'sell', quantity: 10, price: 6150 }
const updatedBook = reconcileOrder(existingBook, incomingOrder)
expect(updatedBook).to.deep.equal([{ type: 'buy', quantity: 5, price: 6150 }])
})
it('partially fulfills an order, removes the matching order and adds the remainder of the order to the book when the book contains a matching order of a smaller quantity', () => {
const existingBook = [{ type: 'buy', quantity: 10, price: 6150 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 6150 }
const updatedBook = reconcileOrder(existingBook, incomingOrder)
expect(updatedBook).to.deep.equal([{ type: 'sell', quantity: 5, price: 6150 }])
})
it.skip('Extra Credit: it fulfills a mismatched order when both parties benefit', () => {
const existingBook = [{ type: 'buy', quantity: 15, price: 6000 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 5900 }
const updatedBook = reconcileOrder(existingBook, incomingOrder)
expect(updatedBook).to.deep.equal([])
})
it.skip('Extra Credit: it does not fulfill a mismatched order when it does not benefit both parties', () => {
const existingBook = [{ type: 'buy', quantity: 15, price: 5900 }]
const incomingOrder = { type: 'sell', quantity: 15, price: 6000 }
const updatedBook = reconcileOrder(existingBook, incomingOrder)
expect(updatedBook).to.deep.equal([
{ type: 'buy', quantity: 15, price: 5900 },
{ type: 'sell', quantity: 15, price: 6000 },
])
})
})
})