-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DUOS-2806] Fix empty columns for new study datasets (#2429)
- Loading branch information
Showing
4 changed files
with
109 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { firstNonEmptyPropertyValue } from '../../../src/utils/DatasetUtils'; | ||
|
||
describe('firstNonEmptyPropertyValue', () => { | ||
it('ensure no errors when no study properties', async () => { | ||
const dataset = { id: 1, study: { id: 2 }}; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'test' ]); | ||
expect(result).to.be.empty; | ||
}); | ||
it('ensure no errors when no dataset properties', async () => { | ||
const dataset = { id: 1 }; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'test' ]); | ||
expect(result).to.be.empty; | ||
}); | ||
it('ensure no errors when incorrect properties', async () => { | ||
const dataset = { id: 1, study: { id: 2, properties: [ { key: 'hello', value: 'goodbye' } ]}}; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'test' ]); | ||
expect(result).to.be.empty; | ||
}); | ||
it('ensure no errors when empty study property values', async () => { | ||
const dataset = { id: 1, study: { id: 2, properties: [ { key: 'hello' } ]}}; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'hello' ]); | ||
expect(result).to.be.empty; | ||
}); | ||
it('ensure no errors when empty dataset property values', async () => { | ||
const dataset = { id: 1, properties: [ { propertyName: 'hello' } ]}; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'hello' ]); | ||
expect(result).to.be.empty; | ||
}); | ||
it('extract hello property from study', async () => { | ||
const dataset = { id: 1, study: { id: 2, properties: [ { key: 'hello', value: 'goodbye' } ]}}; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'hello' ]); | ||
expect(result).to.equal('goodbye'); | ||
}); | ||
it('extract hello property from dataset', async () => { | ||
const dataset = { id: 1, properties: [ { propertyName: 'hello', propertyValue: 'goodbye' } ]}; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'hello' ]); | ||
expect(result).to.equal('goodbye'); | ||
}); | ||
it('prioritize study property over dataset property', async () => { | ||
const dataset = { id: 1, properties: [ { propertyName: 'hello', propertyValue: 'goodbye' } ], study: { id: 2, properties: [ { key: 'hello', value: 'world' } ]}}; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'hello' ]); | ||
expect(result).to.equal('world'); | ||
}); | ||
it('extract first available property from study', async () => { | ||
const dataset = { id: 1, study: { id: 2, properties: [ { key: 'hello', value: 'goodbye' }, { key: 'world', value: 'hello' } ]}}; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'hello', 'world' ]); | ||
expect(result).to.equal('goodbye'); | ||
}); | ||
it('extract first available property from dataset', async () => { | ||
const dataset = { id: 1, properties: [ { propertyName: 'hello', propertyValue: 'goodbye' }, { propertyName: 'world', propertyValue: 'hello' } ]}; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'hello', 'world' ]); | ||
expect(result).to.equal('goodbye'); | ||
}); | ||
it('extract mix of properties from study and dataset', async () => { | ||
const dataset = { id: 1, properties: [ { propertyName: 'hello', propertyValue: 'goodbye' } ], study: { id: 2, properties: [ { key: 'world', value: 'hello' } ]}}; | ||
const result = firstNonEmptyPropertyValue(dataset, [ 'world', 'hello' ]); | ||
expect(result).to.equal('hello'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export const firstNonEmptyPropertyValue = (dataset: any, propertyNames: string[]): string => { | ||
for (const propertyName of propertyNames) { | ||
let propertyValue: string[] = []; | ||
if ('study' in dataset && 'properties' in dataset.study) { | ||
const property = dataset.study.properties.find((property: any) => property.key === propertyName); | ||
const value = property?.value; | ||
if (value !== undefined) { | ||
const valueAsIterable = typeof value === 'string' ? [value] : value; | ||
propertyValue.push(...valueAsIterable); | ||
} | ||
} | ||
if ('properties' in dataset && propertyValue.length === 0) { | ||
const property = dataset.properties.find((property: any) => property.propertyName === propertyName); | ||
const value = property?.propertyValue; | ||
if (value !== undefined) { | ||
const valueAsIterable = typeof value === 'string' ? [value] : value; | ||
propertyValue.push(...valueAsIterable); | ||
} | ||
} | ||
if (propertyValue.length > 0) { | ||
return propertyValue.join(', '); | ||
} | ||
} | ||
return ''; | ||
} |