Skip to content

Commit

Permalink
Fixed JSON validation in TimeSeries and improved unit-tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
bre1470 committed Aug 2, 2024
1 parent 7b09284 commit fedb0cc
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 46 deletions.
5 changes: 3 additions & 2 deletions src/TimeSeries/Converters/DividendSeriesConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -107,7 +108,7 @@ export class DividendSeriesConverter extends MorningstarConverter {
const securityIds: Array<string> = [];
const sortedDividends: Array<Dividend> = [];

for (const security of json.TimeSeries.Security) {
for (const security of json.Security) {

if (!security.DividendSeries) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/TimeSeries/Converters/GrowthSeriesConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class GrowthSeriesConverter extends MorningstarConverter {

// Validate JSON

if (!TimeSeriesJSON.isTimeSeriesReponse(json)) {
if (!TimeSeriesJSON.isTimeSeriesResponse(json)) {
throw new Error('Invalid data');
}

Expand Down
2 changes: 1 addition & 1 deletion src/TimeSeries/Converters/RatingSeriesConverter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class RatingSeriesConverter extends MorningstarConverter {

// Validate JSON

if (!TimeSeriesJSON.isTimeSeriesReponse(json)) {
if (!TimeSeriesJSON.isTimeSeriesResponse(json)) {
throw new Error('Invalid data');
}

Expand Down
4 changes: 2 additions & 2 deletions src/TimeSeries/TimeSeriesJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down Expand Up @@ -148,7 +148,7 @@ namespace TimeSeriesJSON {
}


export function isTimeSeriesReponse(
export function isTimeSeriesResponse(
json?: unknown
): json is TimeSeriesResponse {
return (
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions test/unit-tests/TimeSeries/DividendConverter.test.ts
Original file line number Diff line number Diff line change
@@ -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.'
);

}
21 changes: 0 additions & 21 deletions test/unit-tests/TimeSeries/dividend.test.ts

This file was deleted.

50 changes: 31 additions & 19 deletions test/unit-tests/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand All @@ -36,12 +36,12 @@ import * as JSDOM from 'jsdom';
* */


async function getAPIOptions(): Promise<Shared.MorningstarAPIOptions> {
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');
}

Expand Down Expand Up @@ -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);

Expand All @@ -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<string>
...text: Array<unknown>
): void {
process.stdout.write([
'[',
Expand Down

0 comments on commit fedb0cc

Please sign in to comment.