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

fix(widgets): Prevent case change in table column names #26

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 13 additions & 2 deletions examples/components/widgets/table-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class TableWidget extends BaseWidget {
return widgetSource.getTable({
columns,
...(sortBy && {sortBy, sortDirection}),
rowsPerPage: limit,
limit,
spatialFilter: this.getSpatialFilterOrViewState(),
});
},
Expand Down Expand Up @@ -132,6 +132,17 @@ function renderTableBody(response: TableResponse) {

function renderTableRow(row: unknown[]) {
return html`<tr>
${map(row, (value) => html`<td>${value}</td>`)}
${map(row, renderTableCell)}
</tr>`;
}

const _numberFormatter = new Intl.NumberFormat();
function renderTableCell(value: unknown) {
let formattedValue: string;
if (typeof value === 'number') {
return _numberFormatter.format(value);
} else {
formattedValue = String(value);
}
return html`<td>${formattedValue}</td>`;
}
5 changes: 3 additions & 2 deletions src/sources/widget-base-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,9 @@ export abstract class WidgetBaseSource<Props extends WidgetBaseSourceProps> {
},
opts: {abortController},
}).then((res: TableModelResponse) => ({
rows: normalizeObjectKeys(res.rows),
totalCount: res.metadata.total,
// Avoid `normalizeObjectKeys()`, which changes column names.
rows: res.rows ?? (res as any).ROWS,
totalCount: res.metadata?.total ?? (res as any).METADATA?.TOTAL,
}));
}

Expand Down
47 changes: 47 additions & 0 deletions test/sources/widget-base-source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,53 @@ test('getTable', async () => {
});
});

test('getTable - snowflake', async () => {
const widgetSource = new WidgetTestSource({
accessToken: '<token>',
connectionName: 'carto_dw',
});

const expectedRows = [
{name: 'Veggie Mart', rEVenUe: 1200},
{name: 'EZ Drive Thru', rEVenUe: 400},
{name: "Buddy's Convenience", rEVenUe: 800},
];

// NOTICE: Snowflake returns uppercase keys.
const mockFetch = vi
.fn()
.mockResolvedValueOnce(
createMockResponse({ROWS: expectedRows, METADATA: {TOTAL: 3}})
);
vi.stubGlobal('fetch', mockFetch);

const actualTable = await widgetSource.getTable({
columns: ['name', 'revenue'],
limit: 20,
offset: 10,
});

expect(mockFetch).toHaveBeenCalledOnce();
expect(actualTable).toEqual({
rows: expectedRows,
totalCount: 3,
});

const params = new URL(mockFetch.mock.lastCall[0]).searchParams.entries();
expect(Object.fromEntries(params)).toMatchObject({
type: 'test',
source: 'test-data',
params: JSON.stringify({
column: ['name', 'revenue'],
limit: 20,
offset: 10,
}),
queryParameters: '',
filters: JSON.stringify({}),
filtersLogicalOperator: 'and',
});
});

/******************************************************************************
* getScatter
*/
Expand Down