From bd2f205f6b4ce9898035467e9fbd2995a9b6b747 Mon Sep 17 00:00:00 2001 From: bre1470 <40056287+bre1470@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:42:38 +0200 Subject: [PATCH] Tools: Fixed unit tests. --- src/CLI/morningstar-connectors.ts | 13 +++++ src/RNANews/RNANewsConnector.ts | 6 ++- src/TimeSeries/TimeSeriesConnector.ts | 48 ++++++++++--------- .../TimeSeries/DividendConverter.test.ts | 10 ++-- test/unit-tests/runtime.ts | 2 +- 5 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/CLI/morningstar-connectors.ts b/src/CLI/morningstar-connectors.ts index be7fe7b..f2c2c69 100644 --- a/src/CLI/morningstar-connectors.ts +++ b/src/CLI/morningstar-connectors.ts @@ -126,6 +126,19 @@ export async function main (): Promise { console.info(`Content available at http://localhost:${port}.`); + process.stdin.on('data', (data) => { + const input = data.toString('utf8'); + + if ( + input.startsWith('exit') || + input.startsWith('quit') || + input.startsWith('stop') + ) { + server.stop(); + process.exit(0); + } + }); + } diff --git a/src/RNANews/RNANewsConnector.ts b/src/RNANews/RNANewsConnector.ts index f2c50d8..2ecbcb0 100644 --- a/src/RNANews/RNANewsConnector.ts +++ b/src/RNANews/RNANewsConnector.ts @@ -133,6 +133,8 @@ export class RNANewsConnector extends MorningstarConnector { * Same connector instance with modified table. */ public override async load (): Promise { + await super.load(); + const options = this.options; const { security, @@ -146,8 +148,8 @@ export class RNANewsConnector extends MorningstarConnector { return this; } - const api = new MorningstarAPI(options.api); - const url = new MorningstarURL('timeseries/rna-news'); + const api = this.api = this.api || new MorningstarAPI(options.api); + const url = new MorningstarURL('timeseries/rna-news', api.baseURL); url.setSecuritiesOptions([security]); diff --git a/src/TimeSeries/TimeSeriesConnector.ts b/src/TimeSeries/TimeSeriesConnector.ts index db59f97..c16e286 100644 --- a/src/TimeSeries/TimeSeriesConnector.ts +++ b/src/TimeSeries/TimeSeriesConnector.ts @@ -124,6 +124,8 @@ export class TimeSeriesConnector extends MorningstarConnector { public override async load (): Promise { + await super.load(); + const options = this.options; const currencyId = options.currencyId; const endDate = options.endDate; @@ -131,36 +133,38 @@ export class TimeSeriesConnector extends MorningstarConnector { const startDate = options.startDate; const tax = options.tax; - if (securities) { - const api = new MorningstarAPI(options.api); - const url = new MorningstarURL(this.path, api.baseURL); + if (!securities) { + return this; + } + + const api = this.api = this.api || new MorningstarAPI(options.api); + const url = new MorningstarURL(this.path, api.baseURL); - url.setSecuritiesOptions(securities); + url.setSecuritiesOptions(securities); - if (currencyId) { - url.searchParams.set('currencyId', currencyId); - } + if (currencyId) { + url.searchParams.set('currencyId', currencyId); + } - if (endDate) { - url.setDate('endDate', endDate); - } + if (endDate) { + url.setDate('endDate', endDate); + } - if (startDate) { - url.setDate('startDate', startDate); - } + if (startDate) { + url.setDate('startDate', startDate); + } - if (tax) { - url.searchParams.set('tax', tax); - } + if (tax) { + url.searchParams.set('tax', tax); + } - const response = await api.fetch(url); - const json = await response.json() as unknown; + const response = await api.fetch(url); + const json = await response.json() as unknown; - this.converter.parse({ json }); + this.converter.parse({ json }); - this.table.deleteColumns(); - this.table.setColumns(this.converter.getTable().getColumns()); - } + this.table.deleteColumns(); + this.table.setColumns(this.converter.getTable().getColumns()); return this; } diff --git a/test/unit-tests/TimeSeries/DividendConverter.test.ts b/test/unit-tests/TimeSeries/DividendConverter.test.ts index 27dc596..c371e3c 100644 --- a/test/unit-tests/TimeSeries/DividendConverter.test.ts +++ b/test/unit-tests/TimeSeries/DividendConverter.test.ts @@ -1,10 +1,10 @@ import * as Assert from 'node:assert/strict'; -import * as MorningstarConnectors from '../../../code/morningstar-connectors.src'; +import * as MC from '../../../code/morningstar-connectors.src'; export async function ratingLoad ( - api: MorningstarConnectors.Shared.MorningstarAPIOptions + api: MC.Shared.MorningstarAPIOptions ) { - const connector = new MorningstarConnectors.TimeSeriesConnector({ + const connector = new MC.TimeSeriesConnector({ api, currencyId: 'EUR', endDate: '2020-12-31', @@ -19,13 +19,13 @@ export async function ratingLoad ( }); Assert.ok( - connector instanceof MorningstarConnectors.TimeSeriesConnector, + connector instanceof MC.TimeSeriesConnector, 'Connector should be instance of TimeSeries class.' ); Assert.ok( connector.converter instanceof - MorningstarConnectors.TimeSeriesConverters.DividendSeriesConverter, + MC.TimeSeriesConverters.DividendSeriesConverter, 'Converter should be instance of TimeSeries DividendSeriesConverter.' ); diff --git a/test/unit-tests/runtime.ts b/test/unit-tests/runtime.ts index ad270a5..663f35e 100644 --- a/test/unit-tests/runtime.ts +++ b/test/unit-tests/runtime.ts @@ -143,7 +143,7 @@ async function runUnitTests () { for (let testName of Object.keys(unitTests)) { test = unitTests[testName]; - testName = testName.replace(/[A-Z]+/u, ' $0').toLowerCase(); + testName = testName.replace(/([A-Z]+)/u, ' $1').toLowerCase(); if (typeof test === 'function') {