Skip to content

Commit

Permalink
fix(api): Fix addChartDataToWidget method response of PostgresSurveyA…
Browse files Browse the repository at this point in the history
…nswerRepository and add more tests
  • Loading branch information
alepefe committed Nov 13, 2024
1 parent 3caf3f8 commit 0ec3498
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 33 deletions.
15 changes: 7 additions & 8 deletions api/src/infrastructure/postgres-survey-answers.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,17 +126,16 @@ export class PostgresSurveyAnswerRepository
widget: BaseWidgetWithData,
filterClauseWithParams: FilterClauseWithParams,
): Promise<void> {
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++) {
Expand Down
66 changes: 41 additions & 25 deletions api/test/e2e/widgets/base-widgets.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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([
[
Expand Down

0 comments on commit 0ec3498

Please sign in to comment.