Skip to content

Commit

Permalink
Update ID matching to match by name sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
wylieconlon committed May 13, 2021
1 parent 41789fb commit 628cf8d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,14 @@ export const mapColumn: ExpressionFunctionDefinition<
return Promise.all(rowPromises).then((rows) => {
const existingColumnIndex = columns.findIndex(({ id, name }) => {
// Columns that have IDs are allowed to have duplicate names, for example esaggs
if (args.id) {
return id === args.id;
}
// If the column has an ID, but there is no ID argument to mapColumn
if (id) {
return id === args.id && name === args.name;
return id === args.name;
}
// If no ID, name is the unique key. For example, SQL output does not have IDs
// Columns without ID use name as the unique key. For example, SQL output does not have IDs
return name === args.name;
});
const type = rows.length ? getType(rows[0][columnId]) : 'null';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ describe('mapColumn', () => {
expect(result.rows[arbitraryRowIndex]).toHaveProperty('pricePlusTwo');
});

it('matches name to id when mapColumn is called without an id', async () => {
const result = await runFn(testTable, { name: 'name', expression: pricePlusTwo });
const nameColumnIndex = result.columns.findIndex(({ name }) => name === 'name');
const arbitraryRowIndex = 4;

expect(result.type).toBe('datatable');
expect(result.columns).toHaveLength(sqlTable.columns.length);
expect(result.columns[nameColumnIndex]).toHaveProperty('name', 'name');
expect(result.columns[nameColumnIndex].meta).toHaveProperty('type', 'number');
expect(result.rows[arbitraryRowIndex]).toHaveProperty('name', 202);
});

it('overwrites existing column with the new column if an existing column name is missing an id', async () => {
const result = await runFn(sqlTable, { name: 'name', expression: pricePlusTwo });
const nameColumnIndex = result.columns.findIndex(({ name }) => name === 'name');
Expand Down

0 comments on commit 628cf8d

Please sign in to comment.