From 0ec3498bfbe3e9bd7618e0d0e07b00973f8ceb98 Mon Sep 17 00:00:00 2001 From: Alejandro Peralta Date: Wed, 13 Nov 2024 14:32:08 +0100 Subject: [PATCH] fix(api): Fix addChartDataToWidget method response of PostgresSurveyAnswerRepository and add more tests --- .../postgres-survey-answers.repository.ts | 15 ++--- api/test/e2e/widgets/base-widgets.spec.ts | 66 ++++++++++++------- 2 files changed, 48 insertions(+), 33 deletions(-) diff --git a/api/src/infrastructure/postgres-survey-answers.repository.ts b/api/src/infrastructure/postgres-survey-answers.repository.ts index 45b8abb3..79fed3a1 100644 --- a/api/src/infrastructure/postgres-survey-answers.repository.ts +++ b/api/src/infrastructure/postgres-survey-answers.repository.ts @@ -126,17 +126,16 @@ export class PostgresSurveyAnswerRepository widget: BaseWidgetWithData, filterClauseWithParams: FilterClauseWithParams, ): Promise { - const [whereClauseSql, whereClauseParams] = - this.sqlAdapter.addExpressionToFilterClause(filterClauseWithParams, [ - 'question_indicator', - '=', - widget.indicator, - ]); + const [filterClause, queryParams] = filterClauseWithParams; + const newParams = [...queryParams, widget.indicator]; const totalsSql = `SELECT answer as "key", count(answer)::integer as "count", SUM(COUNT(answer)) OVER ()::integer AS total - FROM ${this.answersTable} ${whereClauseSql} GROUP BY answer ORDER BY answer`; + FROM ${this.answersTable} + WHERE survey_id IN (SELECT survey_id FROM ${this.answersTable} ${filterClause}) AND question_indicator = $${newParams.length} + GROUP BY answer ORDER BY answer`; + const totalsResult: { key: string; count: number; total: number }[] = - await this.dataSource.query(totalsSql, whereClauseParams); + await this.dataSource.query(totalsSql, newParams); const arr: WidgetChartData = []; for (let rowIdx = 0; rowIdx < totalsResult.length; rowIdx++) { diff --git a/api/test/e2e/widgets/base-widgets.spec.ts b/api/test/e2e/widgets/base-widgets.spec.ts index 2908f2e3..2dde76f0 100644 --- a/api/test/e2e/widgets/base-widgets.spec.ts +++ b/api/test/e2e/widgets/base-widgets.spec.ts @@ -126,35 +126,51 @@ describe('Base Widgets', () => { expect(returnedWidget).toStrictEqual(createdWidgetWithData); }); - it('Should retrieve a widget with its filtered data by its id (indicator)', async () => { - // Given - const dataSourceManager = testManager.testApp.get(DataSourceManager); - await dataSourceManager.loadQuestionIndicatorMap(); - await dataSourceManager.loadSurveyData(TEST_SURVEYS_DATA_PATH); + it.each([ + [ + 'total-surveys', + '?filters[0][name]=location-country-region&filters[0][operator]==&filters[0][values][0]=Belgium', + { + counter: { value: 2, total: 5 }, + }, + ], + [ + 'type-of-stakeholder', + '?filters[0][name]=sector&filters[0][operator]==&filters[0][values][0]=Agriculture', + { + chart: [ + { label: 'Farmer/agricultural producers', value: 1, total: 2 }, + { label: 'Platform provider', value: 1, total: 2 }, + ], + }, + ], + ])( + 'Should retrieve a widget with its filtered data by its id (indicator)', + async (indicator: string, urlParams: string, expectedData: unknown) => { + // Given + const dataSourceManager = testManager.testApp.get(DataSourceManager); + await dataSourceManager.loadQuestionIndicatorMap(); + await dataSourceManager.loadSurveyData(TEST_SURVEYS_DATA_PATH); - const indicator = 'total-surveys'; - const createdWidget = await mocks.createBaseWidget({ - indicator, - }); + const createdWidget = await mocks.createBaseWidget({ + indicator, + }); - // When - const result = await testManager - .request() - .get( - `/widgets/${indicator}?filters[0][name]=location-country-region&filters[0][operator]==&filters[0][values][0]=Belgium`, - ); + // When + const result = await testManager + .request() + .get(`/widgets/${indicator}${urlParams}`); - const returnedWidget = result.body.data; + const returnedWidget = result.body.data; - const createdWidgetWithData = { - ...ObjectUtils.normalizeDates(createdWidget), - data: { - counter: { value: 2, total: 5 }, - }, - }; - // Then - expect(returnedWidget).toStrictEqual(createdWidgetWithData); - }); + const createdWidgetWithData = { + ...ObjectUtils.normalizeDates(createdWidget), + data: expectedData, + }; + // Then + expect(returnedWidget).toStrictEqual(createdWidgetWithData); + }, + ); it.each([ [