From 9c3cd780e32e76bf03b3532bde2eac24e130a470 Mon Sep 17 00:00:00 2001 From: Sean Sundberg Date: Mon, 6 Nov 2023 13:40:01 -0600 Subject: [PATCH] Add test for undefined array value in negative news flow (#137) Signed-off-by: Sean Sundberg --- .../negative-news/negative-news.impl.ts | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/services/negative-news/negative-news.impl.ts b/src/services/negative-news/negative-news.impl.ts index 5bfeeb0..2fb8b0f 100644 --- a/src/services/negative-news/negative-news.impl.ts +++ b/src/services/negative-news/negative-news.impl.ts @@ -546,6 +546,12 @@ export class NegativeNewsImpl implements NegativeNewsApi { } async validateUrls(data: SearchResult[]): Promise<{validUrls: ValidatedSearchResult[], badUrls: ValidatedSearchResult[]}> { + if (!data) { + const message: string = 'validateUrls() data value is undefined' + console.log(message) + throw new Error(message) + } + const validatedData: ValidatedSearchResult[] = await Promise.all( data.map(this.validateUrl.bind(this)) ) @@ -568,6 +574,12 @@ export class NegativeNewsImpl implements NegativeNewsApi { async checkAllNegativeNews(news: ValidatedSearchResult[], generate: GenerateFunction): Promise<{negativeNews: ScoredSearchResult[], positiveNews: ScoredSearchResult[]}> { + if (!news) { + const message: string = 'checkAllNegativeNews() news value is undefined' + console.log(message) + throw new Error(message) + } + const results: ScoredSearchResult[] = await Promise.all(news.map(val => this.checkNegativeNews(val, generate))) return { @@ -609,6 +621,12 @@ export class NegativeNewsImpl implements NegativeNewsApi { async summarizeAllNews(news: ScoredSearchResult[], generate: GenerateFunction): Promise { + if (!news) { + const message: string = 'summarizeAllNews() news value is undefined' + console.log(message) + throw new Error(message) + } + return Promise.all(news.map(news => this.summarizeNews(news, generate))) } @@ -624,6 +642,12 @@ export class NegativeNewsImpl implements NegativeNewsApi { async filterAllNews(negativeNews: SummarizedSearchResult[], subjectName: string, generate: GenerateFunction, filterParams?: {countryOfResidence?: string, dateOfBirth?: string}): Promise<{tp: FilteredSearchResult[], fp: FilteredSearchResult[]}> { + if (!negativeNews) { + const message: string = 'filterAllNews() negativeNews value is undefined' + console.log(message) + throw new Error(message) + } + const result = await Promise.all( negativeNews.map(news => this.filterNews(news, subjectName, generate, filterParams)) ) @@ -727,6 +751,12 @@ export class NegativeNewsImpl implements NegativeNewsApi { } extractTopics(vals: FilteredSearchResult[]) { + if (!vals) { + const message: string = 'extractTopics() vals parameter is undefined' + console.log(message) + throw new Error(message) + } + return vals .map(val => val.negativeNewsTopics) .reduce((topics: string[], current: string[]) => {