pgadmin4/web/regression/javascript/schema_ui_files/column.ui.spec.js
Ashesh Vashi e9af0c3226
Improved the extendability of the SchemaView and DataGridView. (#7876)
Restructured these modules for ease of maintenance and apply the single
responsibility principle (wherever applicable).

* SchemaView

 - Split the code based on the functionality and responsibility.
 - Introduced a new View 'InlineView' instead of using the
   'nextInline' configuration of the fields to have a better, and
   manageable view.
 - Using the separate class 'SchemaState' for managing the data and
   states of the SchemaView (separated from the 'useSchemaState'
   custom hook).
 - Introduced three new custom hooks 'useFieldValue',
   'useFieldOptions', 'useFieldError' for the individual control to
   use for each Schema Field.
 - Don't pass value as the parameter props, and let the
   'useFieldValue' and other custom hooks to decide, whether to
   rerender the control itself or the whole dialog/view. (single
   responsibility principle)
 - Introduced a new data store with a subscription facility.
 - Moving the field metadata (option) evaluation to a separate place
   for better management, and each option can be defined for a
   particular kind of field (for example - collection, row, cell,
   general, etc).
 - Allow to provide custom control for all kind of Schema field.

* DataGridView

 - Same as SchemaView, split the DataGridView call into smaller,
   manageable chunks. (For example - grid, row, mappedCell, etc).
 - Use context based approach for providing the row and table data
   instead of passing them as parameters to every component
   separately.
 - Have a facility to extend this feature separately in future.
   (for example - selectable cell, column grouping, etc.)
 - Separated the features like deletable, editable, reorder,
   expandable etc. cells using the above feature support.
 - Added ability to provide the CustomHeader, and CustomRow through the
   Schema field, which will extend the ability to customize better.
 - Removed the 'DataGridViewWithHeaderForm' as it has been achieved
   through providing 'CustomHeader', and also introduced
   'DataGridFormHeader' (a custom header) to achieve the same feature
   as 'DataGridViewWithHeaderForm'.
2024-09-09 14:27:31 +05:30

236 lines
7.2 KiB
JavaScript

/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2024, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import ColumnSchema from '../../../pgadmin/browser/server_groups/servers/databases/schemas/tables/columns/static/js/column.ui';
import BaseUISchema from '../../../pgadmin/static/js/SchemaView/base_schema.ui';
import _ from 'lodash';
import {addNewDatagridRow, genericBeforeEach, getCreateView, getEditView, getPropertiesView} from '../genericFunctions';
import { initializeSchemaWithData } from './utils';
class MockSchema extends BaseUISchema {
get baseFields() {
return [];
}
}
class ColumnInColl extends BaseUISchema {
constructor() {
super();
}
get baseFields() {
return [{
id: 'columns', label: '', type: 'collection',
schema: new ColumnSchema(
()=>new MockSchema(),
{},
()=>Promise.resolve([]),
()=>Promise.resolve([]),
),
editable: false,
canAdd: true, canEdit: false, canDelete: true, hasRole: true,
columns : ['name' , 'cltype', 'attlen', 'attprecision', 'attnotnull', 'is_primary_key'],
}];
}
}
function getFieldDepChange(schema, id) {
return _.find(schema.fields, (f)=>f.id==id)?.depChange;
}
describe('ColumnSchema', ()=>{
const createSchemaObj = () => new ColumnSchema(
()=>new MockSchema(),
{},
()=>Promise.resolve([]),
()=>Promise.resolve([]),
);
let schemaObj = createSchemaObj();
let datatypes = [
{value: 'numeric', length: true, precision: true, min_val: 1, max_val: 140391},
{value: 'character varying', length: true, precision: false, min_val: 1, max_val: 140391},
];
let getInitData = ()=>Promise.resolve({});
beforeEach(()=>{
genericBeforeEach();
});
it('create', async ()=>{
await getCreateView(createSchemaObj());
});
it('edit', async ()=>{
await getEditView(createSchemaObj(), getInitData);
});
it('properties', async ()=>{
await getPropertiesView(createSchemaObj(), getInitData);
});
it('create collection', async ()=>{
let schemaCollObj = new ColumnInColl();
const {ctrl, user} = await getCreateView(schemaCollObj);
/* Make sure you hit every corner */
await addNewDatagridRow(user, ctrl);
});
it('isTypeIdentity', ()=>{
let state = {colconstype: 'i'};
expect(schemaObj.isTypeIdentity(state)).toBe(true);
});
it('isTypeGenerated', ()=>{
let state = {colconstype: 'g'};
expect(schemaObj.isTypeGenerated(state)).toBe(true);
});
it('inSchemaWithModelCheck', ()=>{
let state = {attnum: 1};
schemaObj.nodeInfo.schema = {};
expect(schemaObj.inSchemaWithModelCheck(state)).toBe(true);
state.attnum = null;
expect(schemaObj.inSchemaWithModelCheck(state)).toBe(false);
schemaObj.nodeInfo = {};
expect(schemaObj.inSchemaWithModelCheck(state)).toBe(true);
});
it('attlenRange', ()=>{
schemaObj.datatypes = datatypes;
let state = {cltype: 'character varying'};
expect(schemaObj.attlenRange(state)).toEqual({min: 1, max: 140391});
});
it('attprecisionRange', ()=>{
schemaObj.datatypes = datatypes;
let state = {cltype: 'numeric'};
expect(schemaObj.attprecisionRange(state)).toEqual({min: -140391, max: 140391});
});
it('inSchemaWithColumnCheck', ()=>{
let state = {};
schemaObj.nodeInfo = {schema: {}};
expect(schemaObj.inSchemaWithColumnCheck(state)).toBe(false);
state.attnum = -1;
expect(schemaObj.inSchemaWithColumnCheck(state)).toBe(true);
state.inheritedfrom = 140391;
expect(schemaObj.inSchemaWithColumnCheck(state)).toBe(true);
schemaObj.nodeInfo.view = {};
expect(schemaObj.inSchemaWithColumnCheck(state)).toBe(true);
schemaObj.nodeInfo = {};
expect(schemaObj.inSchemaWithColumnCheck(state)).toBe(false);
});
it('editableCheckForTable', ()=>{
let state = {};
schemaObj.nodeInfo = {};
expect(schemaObj.editableCheckForTable(state)).toBe(true);
});
it('depChange', ()=>{
schemaObj.datatypes = datatypes;
let state = {cltype: 'numeric'};
getFieldDepChange(schemaObj, 'collspcname')(state);
expect(getFieldDepChange(schemaObj, 'attlen')(state)).toEqual({
cltype: 'numeric',
min_val_attlen: 1,
max_val_attlen: 140391,
});
expect(getFieldDepChange(schemaObj, 'attprecision')(state)).toEqual({
cltype: 'numeric',
min_val_attprecision: -140391,
max_val_attprecision: 140391,
});
});
it('validate', ()=>{
let state = {};
let setError = jest.fn();
state.cltype = 'bigint';
state.min_val_attlen = 5;
state.max_val_attlen = 10;
state.attlen = 3;
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('attlen', 'Length/Precision should not be less than: 5');
state.attlen = 11;
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('attlen', 'Length/Precision should not be greater than: 10');
state.attlen = 6;
state.min_val_attprecision = 5;
state.max_val_attprecision = 10;
state.attprecision = 3;
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('attprecision', 'Scale should not be less than: 5');
state.attprecision = 11;
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('attprecision', 'Scale should not be greater than: 10');
state.attprecision = 6;
state.colconstype = 'g';
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('genexpr', 'Expression value cannot be empty.');
state.attnum = 1;
state.attidentity = 'a';
state.colconstype = 'i';
initializeSchemaWithData(schemaObj, {attidentity:'a'});
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('seqincrement', 'Increment value cannot be empty.');
state.seqincrement = 1;
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('seqmin', 'Minimum value cannot be empty.');
state.seqmin = 1;
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('seqmax', 'Maximum value cannot be empty.');
state.seqmax = 1;
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('seqcache', 'Cache value cannot be empty.');
state.attnum = null;
state.seqmin = null;
state.seqmax = null;
initializeSchemaWithData(schemaObj, {attidentity: undefined});
expect(schemaObj.validate(state, setError)).toBe(false);
state.seqmin = 3;
state.seqmax = 2;
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('seqmin', 'Minimum value must be less than maximum value.');
state.seqmin = 3;
state.seqmax = 5;
state.seqstart = 2;
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('seqstart', 'Start value cannot be less than minimum value.');
state.seqstart = 6;
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('seqstart', 'Start value cannot be greater than maximum value.');
state.seqstart = 4;
expect(schemaObj.validate(state, setError)).toBe(false);
});
});