Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: avoid undefined in totals DHIS2-14511 v38 #1585

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions src/modules/pivotTable/PivotTableEngine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import times from 'lodash/times'
import { DIMENSION_ID_ORGUNIT } from '../predefinedDimensions.js'
import { AdaptiveClippingController } from './AdaptiveClippingController.js'
import { addToTotalIfNumber } from './addToTotalIfNumber.js'
import { parseValue } from './parseValue.js'
import {
AGGREGATE_TYPE_NA,
Expand Down Expand Up @@ -703,9 +704,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
totalCell[field] = (totalCell[field] || 0) + value
}
totalCell[field] = addToTotalIfNumber(
value,
totalCell[field]
)
})
}
totalCell.count += 1
Expand All @@ -722,10 +724,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})

if (totals.columnSubtotal) {
Expand All @@ -740,10 +742,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})
}

Expand All @@ -759,10 +761,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})
}
}
Expand All @@ -778,10 +780,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})

if (totals.rowSubtotal) {
Expand All @@ -796,10 +798,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})
}

Expand All @@ -815,10 +817,10 @@ export class PivotTableEngine {
dataFields.forEach((field) => {
const headerIndex = this.dimensionLookup.dataHeaders[field]
const value = parseValue(dataRow[headerIndex])
if (value && !isNaN(value)) {
percentageTotal[field] =
(percentageTotal[field] || 0) + value
}
percentageTotal[field] = addToTotalIfNumber(
value,
percentageTotal[field]
)
})
}
}
Expand Down
84 changes: 84 additions & 0 deletions src/modules/pivotTable/__tests__/addToTotalIfNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { addToTotalIfNumber } from '../addToTotalIfNumber.js'

const tests = [
{
testName: 'negative value',
value: -1,
total: undefined,
expected: -1,
},
{
testName: 'zero value',
value: 0,
total: undefined,
expected: 0,
},
{
testName: 'positive value',
value: 1,
total: undefined,
expected: 1,
},
{
testName: 'null value',
value: null,
total: undefined,
expected: undefined,
},
{
testName: 'undefined value',
value: undefined,
total: undefined,
expected: undefined,
},
{
testName: 'string value',
value: 'string',
total: undefined,
expected: undefined,
},
{
testName: 'negative value with existing total',
value: -1,
total: 100,
expected: 99,
},
{
testName: 'zero value with existing total',
value: 0,
total: 100,
expected: 100,
},
{
testName: 'positive value with existing total',
value: 1,
total: 100,
expected: 101,
},
{
testName: 'null value with existing total',
value: null,
total: 100,
expected: 100,
},
{
testName: 'undefined value with existing total',
value: undefined,
total: 100,
expected: 100,
},
{
testName: 'string value with existing total',
value: 'string',
total: 100,
expected: 100,
},
]

describe('addToTotalIfNumber', () => {
tests.forEach((t) => {
it(t.testName, () => {
expect(addToTotalIfNumber(t.value, t.total)).toEqual(t.expected)
})
})
})
4 changes: 4 additions & 0 deletions src/modules/pivotTable/addToTotalIfNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const addToTotalIfNumber = (value, total) =>
typeof value === 'number' && Number.isFinite(value)
? (total ?? 0) + value
: total
Loading