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

Tweak predictProblems logic, add test for it #2117

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 34 additions & 0 deletions src/features/import/utils/problems/predictProblems.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,40 @@ describe('predictProblem()', () => {
]);
});

it('returns problem when either the first name or last name value is missing', () => {
const sheet = makeFullSheet({
columns: [
{
field: 'first_name',
kind: ColumnKind.FIELD,
selected: true,
Comment on lines +453 to +455
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try creating another test that is a copy of this one, except that this column is not configured (ColumnKind.UNKNOWN and selected: false). In my experimentation, that seems to not return a problem even though it should return UNCONFIGURED_ID_AND_NAME because not the full name has been configured.

},
{
field: 'last_name',
kind: ColumnKind.FIELD,
selected: true,
},
{
field: 'email',
kind: ColumnKind.FIELD,
selected: true,
},
],
rows: [
{ data: ['Clara', 'Zetkin', '[email protected]'] },
{ data: ['John', '', '[email protected]'] },
],
});

const problems = predictProblems(sheet, 'SE', []);
expect(problems).toEqual([
{
indices: [1],
kind: ImportProblemKind.MISSING_ID_AND_NAME,
},
]);
});

it('returns no problems when date column is configured and all cells have a value', () => {
const sheet = makeFullSheet({
columns: [
Expand Down
29 changes: 16 additions & 13 deletions src/features/import/utils/problems/predictProblems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,6 @@ export function predictProblems(
(col) => col.kind == ColumnKind.FIELD && col.field == 'last_name'
);

if (!sheetHasId) {
if (sheetHasFirstName && sheetHasLastName) {
problems.push({
kind: ImportProblemKind.UNCONFIGURED_ID,
});
} else {
problems.push({
kind: ImportProblemKind.UNCONFIGURED_ID_AND_NAME,
});
}
}

function accumulateFieldProblem(field: string, row: number) {
const existing = problemByField[field];
if (existing) {
Expand All @@ -75,6 +63,7 @@ export function predictProblems(
customFields.forEach((field) => {
customFieldsBySlug[field.slug] = field;
});
let isMissingValue = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not really sure this variable is doing so much 😅 If I remove it from the code, the only difference is that I get both unconfigured ID error and "not all rows have identifiers" error, instead of just the latter.

Copy link
Contributor Author

@kaulfield23 kaulfield23 Sep 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just for removing unconfigured ID error(yellow box). Thought that it is weird to show yellow box with text "I understand and want to proceed anyway" when there is an error. Should I remove isMissingValue? @ziggabyte

image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to do what you've tried to do @kaulfield23, but I think the best way to do it is to write a test that reproduces the problem you found, and then fix it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@richardolsson Okay! will do 👍

Copy link
Contributor Author

@kaulfield23 kaulfield23 Sep 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that case is already covered with the test I wrote ( test : returns problem when either the first name or last name value is missing ) . And the case that ziggi found is already covered with another test that already exists ( test : returns problem when ID or name are not configured )

Is there a case that I have not added? @richardolsson


sheet.rows.forEach((row, index) => {
const rowIndex = sheet.firstRowIsHeaders ? index - 1 : index;
Expand Down Expand Up @@ -133,11 +122,13 @@ export function predictProblems(
}
}
hadImpact = true;
} else {
isMissingValue = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This made me a bit confused - this will be set to true if any "cell" in the sheet is empty, no matter if it is name/id or not.

}
}
});

if (sheetHasId && sheetHasFirstName && sheetHasLastName) {
if (sheetHasFirstName && sheetHasLastName) {
if (!rowHasId && (!rowHasFirstName || !rowHasLastName)) {
if (!rowProblemByKind[ImportProblemKind.MISSING_ID_AND_NAME]) {
rowProblemByKind[ImportProblemKind.MISSING_ID_AND_NAME] = {
Expand All @@ -153,6 +144,18 @@ export function predictProblems(
}
});

if (!sheetHasId && !isMissingValue) {
if (sheetHasFirstName && sheetHasLastName) {
problems.push({
kind: ImportProblemKind.UNCONFIGURED_ID,
});
} else {
problems.push({
kind: ImportProblemKind.UNCONFIGURED_ID_AND_NAME,
});
}
}

if (!hadImpact) {
problems.push({
kind: ImportProblemKind.NO_IMPACT,
Expand Down
Loading