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

SIMSBIOHUB-8-2: Update duplicate key validation rule to account for empty cells. #1037

Merged
merged 7 commits into from
May 29, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
203 changes: 122 additions & 81 deletions api/src/utils/media/csv/validation/csv-row-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,87 +605,128 @@ describe('getValidFormatFieldsValidator', () => {
}
]);
});
});

describe('getUniqueColumnsValidator', () => {
it('adds no errors when no config is supplied', () => {
const validator = getUniqueColumnsValidator();
const worksheet = xlsx.utils.aoa_to_sheet([['Header1'], ['stuff']]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.be.empty;
});

it('adds no errors when no columns are specified in config', () => {
const config: FileColumnUniqueValidatorConfig = {
file_column_unique_validator: {
column_names: ['']
}
};
const validator = getUniqueColumnsValidator(config);
const worksheet = xlsx.utils.aoa_to_sheet([['Header1'], ['stuff']]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.be.empty;
});

it('adds no errors when specified key column is missing from the worksheet', () => {
const config: FileColumnUniqueValidatorConfig = {
file_column_unique_validator: {
column_names: ['Header1', 'Header2']
}
};
const validator = getUniqueColumnsValidator(config);
const worksheet = xlsx.utils.aoa_to_sheet([['Header1'], ['stuff']]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.be.empty;
});

it('adds no errors when key cells are empty', () => {
const config: FileColumnUniqueValidatorConfig = {
file_column_unique_validator: {
column_names: ['Header1', 'Header2']
}
};
const validator = getUniqueColumnsValidator(config);
const worksheet = xlsx.utils.aoa_to_sheet([
['Header1', 'Header2', 'Header3'],
[, , 3],
[, , 3],
[, , 3]
]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.be.empty;
});

it('adds no errors when all keys specified are unique', () => {
const config: FileColumnUniqueValidatorConfig = {
file_column_unique_validator: {
column_names: ['Header1', 'Header2']
}
};
const validator = getUniqueColumnsValidator(config);
const worksheet = xlsx.utils.aoa_to_sheet([
['Header1', 'Header2', 'Header3'],
[1, 2, 3],
[2, 2, 3],
[3, 2, 3]
]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.be.empty;
});

it('adds errors when not all keys are unique', () => {
const config: FileColumnUniqueValidatorConfig = {
file_column_unique_validator: {
column_names: ['Header1', 'Header2']
}
};
const validator = getUniqueColumnsValidator(config);
const worksheet = xlsx.utils.aoa_to_sheet([
['Header1', 'Header2', 'Header3'],
[1, 2, 3],
[2, 2, 3], // produces key: `2, 2`
[2, 2, 3] // produces duplicate key: `2, 2`
]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.not.be.empty;
expect(csvWorkSheet.csvValidation.rowErrors[0].errorCode).to.be.eql(SUBMISSION_MESSAGE_TYPE.NON_UNIQUE_KEY);
});

it('adds errors when not all keys are unique and some columns are empty', () => {
const config: FileColumnUniqueValidatorConfig = {
file_column_unique_validator: {
column_names: ['Header1', 'Header2']
}
};
const validator = getUniqueColumnsValidator(config);
const worksheet = xlsx.utils.aoa_to_sheet([
['Header1', 'Header2', 'Header3'],
[1, , 3],
[, 2, 3], // produces key: `2`
[2, , 3] // produces duplicate key: `2`
]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

describe('getValidFormatFieldsValidator', () => {
it('adds no errors when no config is supplied', () => {
const validator = getUniqueColumnsValidator();
const worksheet = xlsx.utils.aoa_to_sheet([['Header1'], ['stuff']]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.be.empty;
});

it('adds no errors when no columns are specified in config', () => {
const config: FileColumnUniqueValidatorConfig = {
file_column_unique_validator: {
column_names: ['']
}
};
const validator = getUniqueColumnsValidator(config);
const worksheet = xlsx.utils.aoa_to_sheet([['Header1'], ['stuff']]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.be.empty;
});

it('adds no errors when specified key column is missing from the worksheet', () => {
const config: FileColumnUniqueValidatorConfig = {
file_column_unique_validator: {
column_names: ['Header1', 'Header2']
}
};
const validator = getUniqueColumnsValidator(config);
const worksheet = xlsx.utils.aoa_to_sheet([['Header1'], ['stuff']]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.be.empty;
});

it('adds no errors when all keys specified are unique', () => {
const config: FileColumnUniqueValidatorConfig = {
file_column_unique_validator: {
column_names: ['Header1', 'Header2']
}
};
const validator = getUniqueColumnsValidator(config);
const worksheet = xlsx.utils.aoa_to_sheet([
['Header1', 'Header2', 'Header3'],
[1, 2, 3],
[2, 2, 3],
[3, 2, 3]
]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.be.empty;
});

it('adds errors when not all keys are unique', () => {
const config: FileColumnUniqueValidatorConfig = {
file_column_unique_validator: {
column_names: ['Header1', 'Header2']
}
};
const validator = getUniqueColumnsValidator(config);
const worksheet = xlsx.utils.aoa_to_sheet([
['Header1', 'Header2', 'Header3'],
[1, 2, 3],
[2, 2, 3],
[2, 2, 3]
]);
const csvWorkSheet = new CSVWorksheet('Sheet', worksheet);

validator(csvWorkSheet);

expect(csvWorkSheet.csvValidation.rowErrors).to.not.be.empty;
expect(csvWorkSheet.csvValidation.rowErrors[0].errorCode).to.be.eql(SUBMISSION_MESSAGE_TYPE.NON_UNIQUE_KEY);
});
expect(csvWorkSheet.csvValidation.rowErrors).to.not.be.empty;
expect(csvWorkSheet.csvValidation.rowErrors[0].errorCode).to.be.eql(SUBMISSION_MESSAGE_TYPE.NON_UNIQUE_KEY);
});
});
44 changes: 41 additions & 3 deletions api/src/utils/media/csv/validation/csv-row-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,35 @@ export type FileColumnUniqueValidatorConfig = {
};
};

/**
* Checks for duplicate key values between rows, an adds an error for each duplicate found.
*
* Note: A key is one or more columns combined into a single string.
* Note: Empty cells are ignored when performing this check.
*
* @example
* // Valid
* | Col1 | Col2 |
* |------|------|
* | A | B | // key = 'A, B'
* | A | | // key = 'A'
* | B | A | // key = 'B, A'
*
* // Invalid
* | Col1 | Col2 |
* |------|------|
* | A | B | // key = 'A, B'
* | A | B | // key = 'A, B'
*
* // Invalid
* | Col1 | Col2 |
* |------|------|
* | A | | // key = 'A'
* | | A | // key = 'A'
*
* @param {FileColumnUniqueValidatorConfig} [config]
* @return {*} {CSVValidator}
*/
export const getUniqueColumnsValidator = (config?: FileColumnUniqueValidatorConfig): CSVValidator => {
return (csvWorksheet) => {
if (!config) {
Expand All @@ -377,8 +406,16 @@ export const getUniqueColumnsValidator = (config?: FileColumnUniqueValidatorConf

rows.forEach((row, rowIndex) => {
const key = config.file_column_unique_validator.column_names
.map((columnIndex) => `${row[columnIndex] || ''}`.trim().toLowerCase())
.map((columnName) => `${row[columnName] || ''}`.trim().toLowerCase())
.filter(Boolean)
.join(', ');

if (!key) {
// If key is empty, ignore this check.
// It is the job of a separate validation rule to ensure non-empty (required) cells
return;
}

NickPhura marked this conversation as resolved.
Show resolved Hide resolved
// check if key exists already
if (!keySet.has(key)) {
keySet.add(key);
Expand All @@ -389,15 +426,16 @@ export const getUniqueColumnsValidator = (config?: FileColumnUniqueValidatorConf
errorCode: SUBMISSION_MESSAGE_TYPE.NON_UNIQUE_KEY,
message: `Row ${
rowIndex + 2
} has duplicate values ${key} to another row. The combination of values in columns: ${config.file_column_unique_validator.column_names.join(
} has duplicate values ${key} to another row. The combination of values in columns: ${config.file_column_unique_validator.column_names.join(
', '
)} must be unique across rows. Details: `,
)} must be unique across rows. Details: `,
col: key,
row: rowIndex + 2
}
]);
}
});

return csvWorksheet;
};
};
2 changes: 1 addition & 1 deletion api/src/utils/media/xlsx/transformation/xlsx-transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export class XLSXTransform {
.map((columnName: string) => {
return RowObject[columnName];
})
.filter((value) => !isNaN || value)
.filter(Boolean)
.join(':');

return primaryKey;
Expand Down