From 5e6e248b7d81f5dcf4b86edeec20b20a0c3b16fe Mon Sep 17 00:00:00 2001 From: Emanuele Liardo Date: Tue, 22 Oct 2024 23:29:23 +0200 Subject: [PATCH] Add map volume from historical data (#143) --- src/getHistoricalData.js | 3 +- test/getHistoricalData.spec.js | 166 +++++++++++++++++---------------- 2 files changed, 86 insertions(+), 83 deletions(-) diff --git a/src/getHistoricalData.js b/src/getHistoricalData.js index 4a38f18..f3156e6 100644 --- a/src/getHistoricalData.js +++ b/src/getHistoricalData.js @@ -35,7 +35,8 @@ const getHistoricalData = async (params) => { json.o[index], json.h[index], json.l[index], - json.c[index] + json.c[index], + json.v[index] ]) } } diff --git a/test/getHistoricalData.spec.js b/test/getHistoricalData.spec.js index 8ab1dc5..4077fd8 100644 --- a/test/getHistoricalData.spec.js +++ b/test/getHistoricalData.spec.js @@ -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) - }) - }) \ No newline at end of file + 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); + }); +});