From fedb0ccdaacfbda41a0333a7e62f9cc5aa05ff74 Mon Sep 17 00:00:00 2001 From: bre1470 <40056287+bre1470@users.noreply.github.com> Date: Fri, 2 Aug 2024 18:49:20 +0200 Subject: [PATCH] Fixed JSON validation in TimeSeries and improved unit-tests. --- .../Converters/DividendSeriesConverter.ts | 5 +- .../Converters/GrowthSeriesConverter.ts | 2 +- .../Converters/RatingSeriesConverter.ts | 2 +- src/TimeSeries/TimeSeriesJSON.ts | 4 +- .../{api.test.ts => MorningstarAPI.test.ts} | 0 .../TimeSeries/DividendConverter.test.ts | 40 +++++++++++++++ test/unit-tests/TimeSeries/dividend.test.ts | 21 -------- test/unit-tests/runtime.ts | 50 ++++++++++++------- 8 files changed, 78 insertions(+), 46 deletions(-) rename test/unit-tests/Shared/{api.test.ts => MorningstarAPI.test.ts} (100%) create mode 100644 test/unit-tests/TimeSeries/DividendConverter.test.ts delete mode 100644 test/unit-tests/TimeSeries/dividend.test.ts diff --git a/src/TimeSeries/Converters/DividendSeriesConverter.ts b/src/TimeSeries/Converters/DividendSeriesConverter.ts index ae3ecfe..0d59063 100644 --- a/src/TimeSeries/Converters/DividendSeriesConverter.ts +++ b/src/TimeSeries/Converters/DividendSeriesConverter.ts @@ -98,7 +98,8 @@ export class DividendSeriesConverter extends MorningstarConverter { // Validate JSON - if (!TimeSeriesJSON.isResponse(json)) { + if (!TimeSeriesJSON.isTimeSeriesResponse(json)) { + console.log(json); throw new Error('Invalid data'); } @@ -107,7 +108,7 @@ export class DividendSeriesConverter extends MorningstarConverter { const securityIds: Array = []; const sortedDividends: Array = []; - for (const security of json.TimeSeries.Security) { + for (const security of json.Security) { if (!security.DividendSeries) { continue; diff --git a/src/TimeSeries/Converters/GrowthSeriesConverter.ts b/src/TimeSeries/Converters/GrowthSeriesConverter.ts index 78323fc..9a56ac6 100644 --- a/src/TimeSeries/Converters/GrowthSeriesConverter.ts +++ b/src/TimeSeries/Converters/GrowthSeriesConverter.ts @@ -96,7 +96,7 @@ export class GrowthSeriesConverter extends MorningstarConverter { // Validate JSON - if (!TimeSeriesJSON.isTimeSeriesReponse(json)) { + if (!TimeSeriesJSON.isTimeSeriesResponse(json)) { throw new Error('Invalid data'); } diff --git a/src/TimeSeries/Converters/RatingSeriesConverter.ts b/src/TimeSeries/Converters/RatingSeriesConverter.ts index 623fcab..3720b97 100644 --- a/src/TimeSeries/Converters/RatingSeriesConverter.ts +++ b/src/TimeSeries/Converters/RatingSeriesConverter.ts @@ -96,7 +96,7 @@ export class RatingSeriesConverter extends MorningstarConverter { // Validate JSON - if (!TimeSeriesJSON.isTimeSeriesReponse(json)) { + if (!TimeSeriesJSON.isTimeSeriesResponse(json)) { throw new Error('Invalid data'); } diff --git a/src/TimeSeries/TimeSeriesJSON.ts b/src/TimeSeries/TimeSeriesJSON.ts index 2414014..590bc9d 100644 --- a/src/TimeSeries/TimeSeriesJSON.ts +++ b/src/TimeSeries/TimeSeriesJSON.ts @@ -120,7 +120,7 @@ namespace TimeSeriesJSON { !!json && typeof json === 'object' && typeof (json as Response).TimeSeries === 'object' && - isTimeSeriesReponse((json as Response).TimeSeries) + isTimeSeriesResponse((json as Response).TimeSeries) ); } @@ -148,7 +148,7 @@ namespace TimeSeriesJSON { } - export function isTimeSeriesReponse( + export function isTimeSeriesResponse( json?: unknown ): json is TimeSeriesResponse { return ( diff --git a/test/unit-tests/Shared/api.test.ts b/test/unit-tests/Shared/MorningstarAPI.test.ts similarity index 100% rename from test/unit-tests/Shared/api.test.ts rename to test/unit-tests/Shared/MorningstarAPI.test.ts diff --git a/test/unit-tests/TimeSeries/DividendConverter.test.ts b/test/unit-tests/TimeSeries/DividendConverter.test.ts new file mode 100644 index 0000000..2bcc3c7 --- /dev/null +++ b/test/unit-tests/TimeSeries/DividendConverter.test.ts @@ -0,0 +1,40 @@ +import * as Assert from 'node:assert/strict'; +import * as MorningstarConnectors from '../../../code/morningstar-connectors.src'; + +export async function rating_load( + api: MorningstarConnectors.Shared.MorningstarAPIOptions +) { + const connector = new MorningstarConnectors.TimeSeriesConnector({ + api, + currencyId: 'EUR', + endDate: '2020-12-31', + securities: [{ + id: 'F0GBR04S23', + idType: 'MSID' + }], + series: { + type: 'Dividend' + }, + startDate: '2020-01-01' + }); + + Assert.ok( + connector instanceof MorningstarConnectors.TimeSeriesConnector, + 'Connector should be instance of TimeSeries class.' + ); + + Assert.ok( + connector.converter instanceof + MorningstarConnectors.TimeSeriesConverters.DividendSeriesConverter, + 'Converter should be instance of TimeSeries DividendSeriesConverter.' + ); + + await connector.load(); + + Assert.deepStrictEqual( + connector.table.getColumnNames(), + ['Date', connector.options.securities?.[0].id], + 'Connector table should exist of expected columns.' + ); + +} diff --git a/test/unit-tests/TimeSeries/dividend.test.ts b/test/unit-tests/TimeSeries/dividend.test.ts deleted file mode 100644 index 74bbe8d..0000000 --- a/test/unit-tests/TimeSeries/dividend.test.ts +++ /dev/null @@ -1,21 +0,0 @@ -import * as Assert from 'node:assert/strict'; -import * as MorningstarConnectors from '../../../code/morningstar-connectors.src'; - -export function dividend_load( - access: MorningstarConnectors.Shared.MorningstarAccessOptions -) { - const connector = new MorningstarConnectors.TimeSeriesConnector({ - api: { - access - }, - series: { - type: 'Dividend' - } - }); - - Assert.ok( - connector instanceof MorningstarConnectors.TimeSeriesConnector, - 'TimeSeries connector should be instance of expected class.' - ); - -} diff --git a/test/unit-tests/runtime.ts b/test/unit-tests/runtime.ts index c760a79..62f5d58 100644 --- a/test/unit-tests/runtime.ts +++ b/test/unit-tests/runtime.ts @@ -24,8 +24,8 @@ import type { Shared } from '../../code/morningstar-connectors.src'; -import * as Assert from 'node:assert/strict'; import * as FS from 'node:fs/promises'; +import * as FSSync from 'node:fs'; import * as JSDOM from 'jsdom'; @@ -36,12 +36,12 @@ import * as JSDOM from 'jsdom'; * */ -async function getAPIOptions(): Promise { +function getAPIOptions(): Shared.MorningstarAPIOptions { const apiOptions: Shared.MorningstarAPIOptions = { url: 'https://www.emea-api.morningstar.com/ecint/v1/' }; - if ((await FS.lstat('.env')).isFile()) { + if (FSSync.existsSync('.env')) { process.loadEnvFile('.env'); } @@ -148,15 +148,15 @@ async function runUnitTests() { stdWrite('Test', testName, '...'); - await test(await getAPIOptions()); + await test(getAPIOptions()); - stdout.write(' OK.\n'); + stdout.write(' ✅ OK.\n'); successes.push(testName); } catch (error) { - stdout.write(' ERROR.\n'); + stdout.write(' ❌ ERROR.\n'); await logError(error); @@ -170,25 +170,37 @@ async function runUnitTests() { const total = successes.length + failures.length; - console.info( - successes.length, - 'of', - total, - (total === 1 ? 'test' : 'tests'), - 'succeeded.' - ); + if (successes.length === total) { - Assert.deepEqual( - failures.length, - 0, - `${failures.length} ${(failures.length === 1 ? 'test' : 'tests')} failed.` - ); + stdWrite( + '✅', (total === 1 ? 'This' : 'All'), total, + (total === 1 ? 'test' : 'tests'), + 'succeeded.\n' + ); + + } else { + + stdWrite( + '✅', successes.length, 'of', total, + (total === 1 ? 'test' : 'tests'), + 'succeeded.\n' + ); + + stdWrite( + '❌', failures.length, + (failures.length === 1 ? 'test' : 'tests'), + 'failed.\n' + ); + + } + + process.exit(0); } function stdWrite( - ...text: Array + ...text: Array ): void { process.stdout.write([ '[',