Skip to content

Commit 272ef7a

Browse files
[ui-importer] Add (Composite) Primary key column in bulk column modal (#4281)
* [ui-importer] Add Primary key column in bulk column modal * Making the headings in the modal centre align * Updated the unit tests after adding the pKey * nits * Remove accidentally added hue submodule * Removing the condition make first key as p Key * nits * Added unit test assertions for the case where pkey case is true * Composite Primary Key
1 parent 5835719 commit 272ef7a

File tree

3 files changed

+139
-10
lines changed

3 files changed

+139
-10
lines changed

desktop/core/src/desktop/js/apps/newimporter/FilePreviewTab/EditColumnsModal/EditColumnsModal.test.tsx

Lines changed: 119 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,14 @@ describe('EditColumnsModal', () => {
159159

160160
await waitFor(() => {
161161
expect(setColumns).toHaveBeenCalledWith([
162-
{ ...DEFAULT_COLUMNS[0], title: 'newCol1', type: 'STRING', comment: 'new comment' },
163-
{ ...DEFAULT_COLUMNS[1], type: 'INT' }
162+
{
163+
...DEFAULT_COLUMNS[0],
164+
title: 'newCol1',
165+
type: 'STRING',
166+
comment: 'new comment',
167+
isPrimaryKey: false
168+
},
169+
{ ...DEFAULT_COLUMNS[1], type: 'INT', isPrimaryKey: false }
164170
]);
165171
expect(closeModal).toHaveBeenCalled();
166172
});
@@ -177,6 +183,107 @@ describe('EditColumnsModal', () => {
177183
const typeSelects = getColumnTypeSelects();
178184
expect(typeSelects).toHaveLength(2);
179185
});
186+
187+
it('should set isPrimaryKey to true when primary key checkbox is clicked', async () => {
188+
const { setColumns, closeModal } = renderModal();
189+
const user = userEvent.setup();
190+
191+
await waitFor(() => {
192+
expect(screen.getByDisplayValue('col1')).toBeInTheDocument();
193+
});
194+
195+
const primaryKeyCheckboxes = screen.getAllByLabelText('Set as primary key');
196+
expect(primaryKeyCheckboxes).toHaveLength(2);
197+
await user.click(primaryKeyCheckboxes[0]);
198+
199+
const doneButton = screen.getByRole('button', { name: 'Done' });
200+
await user.click(doneButton);
201+
202+
await waitFor(() => {
203+
expect(setColumns).toHaveBeenCalledWith([
204+
{
205+
...DEFAULT_COLUMNS[0],
206+
type: 'STRING',
207+
comment: 'comment1',
208+
isPrimaryKey: true
209+
},
210+
{ ...DEFAULT_COLUMNS[1], type: 'INT', comment: 'comment2', isPrimaryKey: false }
211+
]);
212+
expect(closeModal).toHaveBeenCalled();
213+
});
214+
});
215+
216+
it('should allow multiple columns to be selected as composite primary key', async () => {
217+
const { setColumns, closeModal } = renderModal();
218+
const user = userEvent.setup();
219+
220+
await waitFor(() => {
221+
expect(screen.getByDisplayValue('col1')).toBeInTheDocument();
222+
});
223+
224+
const primaryKeyCheckboxes = screen.getAllByLabelText('Set as primary key');
225+
226+
await user.click(primaryKeyCheckboxes[0]);
227+
await user.click(primaryKeyCheckboxes[1]);
228+
229+
const doneButton = screen.getByRole('button', { name: 'Done' });
230+
await user.click(doneButton);
231+
232+
await waitFor(() => {
233+
expect(setColumns).toHaveBeenCalledWith([
234+
{
235+
...DEFAULT_COLUMNS[0],
236+
type: 'STRING',
237+
comment: 'comment1',
238+
isPrimaryKey: true
239+
},
240+
{
241+
...DEFAULT_COLUMNS[1],
242+
type: 'INT',
243+
comment: 'comment2',
244+
isPrimaryKey: true
245+
}
246+
]);
247+
expect(closeModal).toHaveBeenCalled();
248+
});
249+
});
250+
251+
it('should allow toggling primary key checkboxes on and off', async () => {
252+
const { setColumns, closeModal } = renderModal();
253+
const user = userEvent.setup();
254+
255+
await waitFor(() => {
256+
expect(screen.getByDisplayValue('col1')).toBeInTheDocument();
257+
});
258+
259+
const primaryKeyCheckboxes = screen.getAllByLabelText('Set as primary key');
260+
261+
await user.click(primaryKeyCheckboxes[0]);
262+
await user.click(primaryKeyCheckboxes[1]);
263+
264+
await user.click(primaryKeyCheckboxes[0]);
265+
266+
const doneButton = screen.getByRole('button', { name: 'Done' });
267+
await user.click(doneButton);
268+
269+
await waitFor(() => {
270+
expect(setColumns).toHaveBeenCalledWith([
271+
{
272+
...DEFAULT_COLUMNS[0],
273+
type: 'STRING',
274+
comment: 'comment1',
275+
isPrimaryKey: false
276+
},
277+
{
278+
...DEFAULT_COLUMNS[1],
279+
type: 'INT',
280+
comment: 'comment2',
281+
isPrimaryKey: true
282+
}
283+
]);
284+
expect(closeModal).toHaveBeenCalled();
285+
});
286+
});
180287
});
181288

182289
describe('Edge cases with column data', () => {
@@ -263,8 +370,8 @@ describe('EditColumnsModal', () => {
263370

264371
await waitFor(() => {
265372
expect(setColumns).toHaveBeenCalledWith([
266-
{ ...duplicateColumns[0], title: 'col1', type: 'STRING' },
267-
{ ...duplicateColumns[1], title: 'col2_fixed', type: 'INT' }
373+
{ ...duplicateColumns[0], title: 'col1', type: 'STRING', isPrimaryKey: false },
374+
{ ...duplicateColumns[1], title: 'col2_fixed', type: 'INT', isPrimaryKey: false }
268375
]);
269376
});
270377
});
@@ -312,8 +419,14 @@ describe('EditColumnsModal', () => {
312419

313420
await waitFor(() => {
314421
expect(setColumns).toHaveBeenCalledWith([
315-
{ ...columnsWithEmpty[0], title: 'fixed_name', type: 'STRING' },
316-
{ ...columnsWithEmpty[1], type: 'INT' }
422+
{
423+
...columnsWithEmpty[0],
424+
title: 'fixed_name',
425+
type: 'STRING',
426+
comment: 'comment1',
427+
isPrimaryKey: false
428+
},
429+
{ ...columnsWithEmpty[1], type: 'INT', comment: 'comment2', isPrimaryKey: false }
317430
]);
318431
});
319432
});

desktop/core/src/desktop/js/apps/newimporter/FilePreviewTab/EditColumnsModal/EditColumnsModal.tsx

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import Modal from 'cuix/dist/components/Modal';
2020
import Table from 'cuix/dist/components/Table';
2121
import Input from 'cuix/dist/components/Input';
2222
import Select from 'cuix/dist/components/Select';
23-
import { Alert } from 'antd';
23+
import { Alert, Checkbox } from 'antd';
2424
import { SQL_TYPE_MAPPING_API_URL } from '../../../admin/Components/utils';
2525
import useLoadData from '../../../../utils/hooks/useLoadData/useLoadData';
2626
import LoadingErrorWrapper from '../../../../reactComponents/LoadingErrorWrapper/LoadingErrorWrapper';
@@ -39,6 +39,7 @@ interface EditableRow extends Required<BaseColumnProperties> {
3939
type: string;
4040
sample: string;
4141
comment: string;
42+
isPrimaryKey: boolean;
4243
}
4344

4445
interface EditColumnsModalProps {
@@ -190,12 +191,13 @@ const EditColumnsModal = ({
190191
title: col.title,
191192
type: (col.type || 'string').toUpperCase(),
192193
sample: sample && sample[col.dataIndex] !== undefined ? String(sample[col.dataIndex]) : '',
193-
comment: col.comment || ''
194+
comment: col.comment || '',
195+
isPrimaryKey: col.isPrimaryKey || false
194196
}))
195197
);
196198
}, [columns, sample]);
197199

198-
const handleChange = (rowIndex: number, field: keyof EditableRow, value: string) => {
200+
const handleChange = (rowIndex: number, field: keyof EditableRow, value: string | boolean) => {
199201
setEditableRows(rows =>
200202
rows.map((row, i) => (i === rowIndex ? { ...row, [field]: value } : row))
201203
);
@@ -210,13 +212,26 @@ const EditColumnsModal = ({
210212
...columns[row.key],
211213
title: row.title.trim(),
212214
type: row.type,
213-
comment: row.comment
215+
comment: row.comment,
216+
isPrimaryKey: row.isPrimaryKey
214217
}));
215218
setColumns(updatedColumns);
216219
closeModal();
217220
};
218221

219222
const modalColumns = [
223+
{
224+
title: t('P Key'),
225+
dataIndex: 'isPrimaryKey',
226+
className: 'hue-importer-edit-columns-modal__primary-key',
227+
render: (isPrimaryKey: boolean, _: EditableRow, rowIndex: number) => (
228+
<Checkbox
229+
checked={isPrimaryKey}
230+
onChange={e => handleChange(rowIndex, 'isPrimaryKey', e.target.checked)}
231+
aria-label={t('Set as primary key')}
232+
/>
233+
)
234+
},
220235
{
221236
title: t('Title'),
222237
dataIndex: 'title',

desktop/core/src/desktop/js/apps/newimporter/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export interface FileMetaData {
6969
export interface BaseColumnProperties {
7070
type?: string;
7171
comment?: string;
72+
isPrimaryKey?: boolean;
7273
}
7374

7475
export interface FilePreviewTableColumn extends BaseColumnProperties {

0 commit comments

Comments
 (0)