mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
A column the header guess had no field for - "Story points", "Sprint", any column of the user's own - was imported only when ticked in the import page's mapping step, and dropped without a word otherwise: always for a REST import, which sends no mapping, and on the page unless someone ticked it. guessCsvMapping now puts every other named column in customFieldColumns, and the mapping step starts with them ticked, so nothing is left behind unless it is unticked. WeKan's own Number, Last activity and Voting columns, a blank header and a number cell of a sparse row (#6620) are not made fields. tests/csvCreator.headerMapping.test.cjs pinned "unknown headers are not mapped" and now pins that the column comes across, with the negative cases. Thanks to xet7 !
129 lines
5.6 KiB
JavaScript
129 lines
5.6 KiB
JavaScript
/**
|
|
* Test: CSV import header mapping
|
|
*
|
|
* Covers guessCsvMapping() and customFieldsOf() in
|
|
* models/lib/csvImportMapping.js, the logic that turns a CSV/TSV header row
|
|
* into the columns models/csvCreator.js reads each card row with when the
|
|
* import page's mapping step was not used (a REST import, "Import many
|
|
* boards"). This file used to test a copy of the creator's own header switch;
|
|
* the logic now lives in a plain module, so the test runs the real code.
|
|
* Failures throw (assert), so a regression exits non-zero.
|
|
*/
|
|
|
|
const assert = require('assert');
|
|
|
|
async function main() {
|
|
const { guessCsvMapping, customFieldsOf } = await import('../models/lib/csvImportMapping.js');
|
|
const columnsOf = header => guessCsvMapping(header).columns;
|
|
|
|
// 1. Standard headers, case-insensitive, whitespace-tolerant.
|
|
{
|
|
const columns = columnsOf([
|
|
' Title ', 'Description', 'STATUS', 'Owner', 'Member', 'Label',
|
|
'Due Date', 'Start Date', 'Finish Date', 'Created At', 'Updated At',
|
|
]);
|
|
assert.strictEqual(columns.title, 0);
|
|
assert.strictEqual(columns.description, 1);
|
|
assert.strictEqual(columns.list, 2);
|
|
assert.strictEqual(columns.owner, 3);
|
|
assert.strictEqual(columns.members, 4);
|
|
assert.strictEqual(columns.labels, 5);
|
|
assert.strictEqual(columns.dueAt, 6);
|
|
assert.strictEqual(columns.startAt, 7);
|
|
assert.strictEqual(columns.endAt, 8);
|
|
assert.strictEqual(columns.createdAt, 9);
|
|
assert.strictEqual(columns.modifiedAt, 10);
|
|
}
|
|
|
|
// 2. Header aliases resolve to the same field.
|
|
{
|
|
for (const name of ['stage', 'state', 'status', 'List']) assert.strictEqual(columnsOf(['Title', name]).list, 1, name);
|
|
assert.strictEqual(columnsOf(['Title', 'deadline']).dueAt, 1);
|
|
assert.strictEqual(columnsOf(['Title', 'modified on']).modifiedAt, 1);
|
|
}
|
|
|
|
// 3. An unknown header is no card field, and not fatal - and its column
|
|
// still comes across, as a text custom field of its name. It used to be
|
|
// dropped without a word whenever no mapping came from the import page.
|
|
{
|
|
const mapping = guessCsvMapping(['title', 'Some Unknown Column']);
|
|
assert.deepStrictEqual(mapping.columns, { title: 0 });
|
|
assert.deepStrictEqual(mapping.customFieldColumns, [1]);
|
|
assert.deepStrictEqual(customFieldsOf(['title', 'Some Unknown Column'], mapping),
|
|
[{ name: 'Some Unknown Column', type: 'text', position: 1 }]);
|
|
// Negative: WeKan's own columns with no card field, an empty header and a
|
|
// CustomField-* column (read with its type) are not made text fields.
|
|
// The server passes every language's export column names (_headerNames).
|
|
const en = JSON.parse(require('node:fs').readFileSync(require('node:path').join(__dirname, '../imports/i18n/data/en.i18n.json'), 'utf8'));
|
|
const names = { en: { number: en.number, voting: en.voting, title: en.title } };
|
|
const own = guessCsvMapping(['Title', en.number, en.voting, '', 'customfield-Budget-number', 'Sprint'], names);
|
|
assert.deepStrictEqual(own.customFieldColumns, [5]);
|
|
assert.deepStrictEqual(customFieldsOf(['Title', en.number, en.voting, '', 'customfield-Budget-number', 'Sprint'], own),
|
|
[{ name: 'Budget', type: 'number', position: 4 }, { name: 'Sprint', type: 'text', position: 5 }]);
|
|
}
|
|
|
|
// 4. customfield-<name>-<type> plain custom field.
|
|
{
|
|
const header = ['Title', 'customfield-Budget-text'];
|
|
assert.deepStrictEqual(customFieldsOf(header, guessCsvMapping(header)), [
|
|
{ name: 'Budget', type: 'text', position: 1 },
|
|
]);
|
|
}
|
|
|
|
// 5. customfield-<name>-dropdown-<opt1/opt2> dropdown custom field.
|
|
{
|
|
const header = ['Title', 'customfield-Priority-dropdown-Low/Medium/High'];
|
|
assert.deepStrictEqual(customFieldsOf(header, guessCsvMapping(header)), [
|
|
{ name: 'Priority', type: 'dropdown', options: ['Low', 'Medium', 'High'], position: 1 },
|
|
]);
|
|
}
|
|
|
|
// 6. customfield-<name>-dropdownMultiSelect-<opt1/opt2> variant.
|
|
{
|
|
const header = ['Title', 'CustomField-Tags-dropdownMultiSelect-A/B/C'];
|
|
const [field] = customFieldsOf(header, guessCsvMapping(header));
|
|
assert.strictEqual(field.type, 'dropdownMultiSelect');
|
|
assert.deepStrictEqual(field.options, ['A', 'B', 'C']);
|
|
}
|
|
|
|
// 7. customfield-<name>-currency-<code> currency custom field.
|
|
{
|
|
const header = ['Title', 'customfield-Cost-currency-USD'];
|
|
assert.deepStrictEqual(customFieldsOf(header, guessCsvMapping(header)), [
|
|
{ name: 'Cost', type: 'currency', currencyCode: 'USD', position: 1 },
|
|
]);
|
|
}
|
|
|
|
// 8. Multiple custom fields keep their own position and order.
|
|
{
|
|
const header = ['title', 'customfield-Cost-currency-EUR', 'customfield-Priority-dropdown-Low/High'];
|
|
const mapping = guessCsvMapping(header);
|
|
assert.strictEqual(mapping.columns.title, 0);
|
|
const fields = customFieldsOf(header, mapping);
|
|
assert.deepStrictEqual(fields.map(f => f.position), [1, 2]);
|
|
}
|
|
|
|
// 9. #6620: undefined/null/number cells (a sparse row) do not throw and are
|
|
// simply skipped rather than mapped to a field.
|
|
{
|
|
const mapping = guessCsvMapping(['title', undefined, null, 42]);
|
|
assert.strictEqual(mapping.columns.title, 0);
|
|
assert.strictEqual(Object.keys(mapping.columns).length, 1);
|
|
assert.doesNotThrow(() => guessCsvMapping([undefined, null, 42, 'title']));
|
|
assert.strictEqual(customFieldsOf(['title', undefined, null, 42], mapping).length, 0);
|
|
}
|
|
|
|
// 10. No title column: the first ordinary column is the title, so a list of
|
|
// tasks under any heading imports; a known column is not taken for it.
|
|
{
|
|
assert.deepStrictEqual(columnsOf(['Status', 'Task to do']), { list: 0, title: 1 });
|
|
}
|
|
|
|
console.log('ok - csvCreator header mapping tests passed');
|
|
}
|
|
|
|
main().catch(error => {
|
|
console.error(error);
|
|
process.exit(1);
|
|
});
|