forked from DavideViolante/investing-com-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add map volume from historical data (DavideViolante#143)
- Loading branch information
Showing
2 changed files
with
86 additions
and
83 deletions.
There are no files selected for viewing
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,121 +1,123 @@ | ||
const nock = require('nock') | ||
const nock = require('nock'); | ||
const { getHistoricalData } = require('../index'); | ||
|
||
describe('Tests for getHistoricalData()', () => { | ||
const scope = nock('https://tvc6.investing.com') | ||
const scope = nock('https://tvc6.investing.com'); | ||
|
||
afterEach(() => { | ||
nock.cleanAll() | ||
}) | ||
afterEach(() => { | ||
nock.cleanAll(); | ||
}); | ||
|
||
it('should return an async array', async () => { | ||
scope | ||
it('should return an async array', async () => { | ||
scope | ||
.get('/d8f62270e64f9eb6e4e6a07c3ffeab0b/1729428526/9/9/16/history') | ||
.query(true) | ||
.reply(200, { s: "ok" }) | ||
await expect(getHistoricalData()).resolves.toBeInstanceOf(Array) | ||
}) | ||
.reply(200, { s: 'ok' }); | ||
await expect(getHistoricalData()).resolves.toBeInstanceOf(Array); | ||
}); | ||
|
||
it('should throw an error if investing.com API doesn\'t respond with a 200', async () => { | ||
scope | ||
it('should throw an error if investing.com API doesn\'t respond with a 200', async () => { | ||
scope | ||
.get('/d8f62270e64f9eb6e4e6a07c3ffeab0b/1729428526/9/9/16/history') | ||
.query(true) | ||
.reply(403) | ||
await expect(getHistoricalData()).rejects.toThrow() | ||
}) | ||
.reply(403); | ||
await expect(getHistoricalData()).rejects.toThrow(); | ||
}); | ||
|
||
it('should throw an error if investing.com API responds with a 200 and not "ok" in body', async () => { | ||
scope | ||
it('should throw an error if investing.com API responds with a 200 and not "ok" in body', async () => { | ||
scope | ||
.get('/d8f62270e64f9eb6e4e6a07c3ffeab0b/1729428526/9/9/16/history') | ||
.query(true) | ||
.reply(200, { s: 'error' }) | ||
await expect(getHistoricalData()).rejects.toThrow() | ||
}) | ||
.reply(200, { s: 'error' }); | ||
await expect(getHistoricalData()).rejects.toThrow(); | ||
}); | ||
|
||
it('should call investing.com history api', async () => { | ||
scope | ||
it('should call investing.com history api', async () => { | ||
scope | ||
.get('/d8f62270e64f9eb6e4e6a07c3ffeab0b/1729428526/9/9/16/history') | ||
.query(true) | ||
.reply(200, { s: "ok" }) | ||
await getHistoricalData({ | ||
input: '1', | ||
resolution: 'D', | ||
from: new Date(1729123200000), | ||
to: new Date(1729209600000) | ||
}) | ||
expect(scope.isDone()).toBeTruthy() | ||
}) | ||
.reply(200, { s: 'ok' }); | ||
await getHistoricalData({ | ||
input: '1', | ||
resolution: 'D', | ||
from: new Date(1729123200000), | ||
to: new Date(1729209600000), | ||
}); | ||
expect(scope.isDone()).toBeTruthy(); | ||
}); | ||
|
||
it('should return historical data for a specified data range', async () => { | ||
scope | ||
it('should return historical data for a specified data range', async () => { | ||
scope | ||
.get('/d8f62270e64f9eb6e4e6a07c3ffeab0b/1729428526/9/9/16/history') | ||
.query({ | ||
symbol: '1', | ||
resolution: 'D', | ||
from: 1729123200, | ||
to: 1729209600 | ||
to: 1729209600, | ||
}) | ||
.reply(200, { | ||
"t": [ | ||
't': [ | ||
1729123200, | ||
1729209600 | ||
1729209600, | ||
], | ||
"c": [ | ||
'c': [ | ||
1.08309996128082008937099089962430298328399658203125, | ||
1.0865999460220299166479662744677625596523284912109375 | ||
1.0865999460220299166479662744677625596523284912109375, | ||
], | ||
"o": [ | ||
'o': [ | ||
1.086099982261659935289799250313080847263336181640625, | ||
1.08309996128082008937099089962430298328399658203125 | ||
1.08309996128082008937099089962430298328399658203125, | ||
], | ||
"h": [ | ||
'h': [ | ||
1.0872999429702800977537435755948536098003387451171875, | ||
1.086899995803829899188031049561686813831329345703125 | ||
1.086899995803829899188031049561686813831329345703125, | ||
], | ||
"l": [ | ||
'l': [ | ||
1.0809999704360999661645337255322374403476715087890625, | ||
1.0822999477386499034281541753443889319896697998046875 | ||
1.0822999477386499034281541753443889319896697998046875, | ||
], | ||
"v": [ | ||
"n/a", | ||
"n/a" | ||
'v': [ | ||
1, | ||
2, | ||
], | ||
"vo": [ | ||
"n/a", | ||
"n/a" | ||
'vo': [ | ||
'n/a', | ||
'n/a', | ||
], | ||
"vac": [ | ||
"n/a", | ||
"n/a" | ||
'vac': [ | ||
'n/a', | ||
'n/a', | ||
], | ||
"s": "ok" | ||
}) | ||
's': 'ok', | ||
}); | ||
|
||
const data = await getHistoricalData({ | ||
input: '1', | ||
resolution: 'D', | ||
from: new Date(1729123200000), | ||
to: new Date(1729209600000) | ||
}) | ||
const data = await getHistoricalData({ | ||
input: '1', | ||
resolution: 'D', | ||
from: new Date(1729123200000), | ||
to: new Date(1729209600000), | ||
}); | ||
|
||
const expected = [ | ||
{ | ||
date: 1729123200000, | ||
value: 1.086099982261659935289799250313080847263336181640625, | ||
price_open: 1.086099982261659935289799250313080847263336181640625, | ||
price_high: 1.0872999429702800977537435755948536098003387451171875, | ||
price_low: 1.0809999704360999661645337255322374403476715087890625, | ||
price_close: 1.08309996128082008937099089962430298328399658203125, | ||
}, | ||
{ | ||
date: 1729209600000, | ||
value: 1.08309996128082008937099089962430298328399658203125, | ||
price_open: 1.08309996128082008937099089962430298328399658203125, | ||
price_high: 1.086899995803829899188031049561686813831329345703125, | ||
price_low: 1.0822999477386499034281541753443889319896697998046875, | ||
price_close: 1.0865999460220299166479662744677625596523284912109375, | ||
} | ||
] | ||
expect(data).toEqual(expected) | ||
}) | ||
}) | ||
const expected = [ | ||
{ | ||
date: 1729123200000, | ||
value: 1.08309996128082008937099089962430298328399658203125, | ||
price_open: 1.086099982261659935289799250313080847263336181640625, | ||
price_high: 1.0872999429702800977537435755948536098003387451171875, | ||
price_low: 1.0809999704360999661645337255322374403476715087890625, | ||
price_close: 1.08309996128082008937099089962430298328399658203125, | ||
volume: 1 | ||
}, | ||
{ | ||
date: 1729209600000, | ||
value: 1.0865999460220299166479662744677625596523284912109375, | ||
price_open: 1.08309996128082008937099089962430298328399658203125, | ||
price_high: 1.086899995803829899188031049561686813831329345703125, | ||
price_low: 1.0822999477386499034281541753443889319896697998046875, | ||
price_close: 1.0865999460220299166479662744677625596523284912109375, | ||
volume: 2 | ||
}, | ||
]; | ||
expect(data).toEqual(expected); | ||
}); | ||
}); |