-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from KendelChopp/add-testing
Add Tests and Make Backwards Compatible
- Loading branch information
Showing
17 changed files
with
3,840 additions
and
2,026 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* @KendelChopp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dist | ||
node_modules | ||
coverage |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
const Security = require('../Security/Security.js'); | ||
|
||
const MarketData = require('./MarketData.js'); | ||
|
||
describe('MarketData', () => { | ||
let marketData; | ||
|
||
beforeEach(() => { | ||
marketData = new MarketData(); | ||
}); | ||
|
||
test('sets the correct defaults', () => { | ||
expect(marketData.maxTime).toBe(0); | ||
expect(marketData.time).toBe(0); | ||
expect(marketData.securities).toEqual({}); | ||
}); | ||
|
||
describe('addSecurity', () => { | ||
let data, symbol; | ||
|
||
beforeEach(() => { | ||
data = [{ some: 'data' }, { some: 'other data' }]; | ||
symbol = 'AAPL'; | ||
}); | ||
|
||
test('adds a new security', () => { | ||
marketData.addSecurity(symbol, data); | ||
const newSecurity = marketData.securities[symbol]; | ||
expect(newSecurity).toBeInstanceOf(Security); | ||
expect(newSecurity.data).toBe(data); | ||
expect(newSecurity.symbol).toBe(symbol); | ||
}); | ||
|
||
describe('when max time is less than data.length', () => { | ||
beforeEach(() => { | ||
marketData.maxTime = 0; | ||
}); | ||
|
||
test('sets maxTime to data.length', () => { | ||
marketData.addSecurity(symbol, data); | ||
expect(marketData.maxTime).toBe(data.length); | ||
}); | ||
}); | ||
|
||
describe('when max time is less than data.length', () => { | ||
let maxTime; | ||
|
||
beforeEach(() => { | ||
maxTime = 10; | ||
marketData.maxTime = maxTime; | ||
}); | ||
|
||
test('does not change maxTime', () => { | ||
marketData.addSecurity(symbol, data); | ||
expect(marketData.maxTime).toBe(maxTime); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('simulateMinute', () => { | ||
let closePrice, validSecurity, invalidSecurity; | ||
|
||
beforeEach(() => { | ||
closePrice = 123; | ||
validSecurity = new Security('SPY', [{ closePrice }]); | ||
invalidSecurity = new Security('AAPL', []); | ||
|
||
marketData.securities = [validSecurity, invalidSecurity]; | ||
marketData.time = 0; | ||
}); | ||
|
||
test('updates the valid security price', () => { | ||
marketData.simulateMinute(); | ||
expect(validSecurity.price).toBe(closePrice); | ||
}); | ||
|
||
test('adds one to the current time', () => { | ||
marketData.simulateMinute(); | ||
expect(marketData.time).toBe(1); | ||
}); | ||
|
||
test('returns a stringified map of the valid securities', () => { | ||
const simulation = marketData.simulateMinute(); | ||
const expected = JSON.stringify([{ | ||
closePrice, | ||
ev: 'AM', | ||
sym: validSecurity.symbol | ||
}]); | ||
|
||
expect(simulation).toEqual(expected); | ||
}); | ||
}); | ||
|
||
describe('hasData', () => { | ||
describe('when time is less than maxTime', () => { | ||
test('returns true', () => { | ||
marketData.time = 0; | ||
marketData.maxTime = 1; | ||
expect(marketData.hasData).toBe(true); | ||
}); | ||
}); | ||
|
||
describe('when time is equal to maxTime', () => { | ||
test('returns false', () => { | ||
marketData.time = 1; | ||
marketData.maxTime = 1; | ||
expect(marketData.hasData).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('when time is greater than maxTime', () => { | ||
test('returns false', () => { | ||
marketData.time = 2; | ||
marketData.maxTime = 1; | ||
expect(marketData.hasData).toBe(false); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('getPrice', () => { | ||
let price, symbol; | ||
|
||
beforeEach(() => { | ||
symbol = 'AAPL'; | ||
price = 12; | ||
const security = new Security(symbol, []); | ||
security.price = price; | ||
marketData.securities = { [symbol]: security }; | ||
}); | ||
|
||
test('returns the price of the requested security', () => { | ||
expect(marketData.getPrice(symbol)).toBe(price); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.