forked from pgadmin-org/pgadmin4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunique_constraint.ui.spec.js
More file actions
197 lines (164 loc) · 5.95 KB
/
unique_constraint.ui.spec.js
File metadata and controls
197 lines (164 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2026, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import { SCHEMA_STATE_ACTIONS } from '../../../pgadmin/static/js/SchemaView';
import BaseUISchema from '../../../pgadmin/static/js/SchemaView/base_schema.ui';
import _ from 'lodash';
import UniqueConstraintSchema from '../../../pgadmin/browser/server_groups/servers/databases/schemas/tables/constraints/index_constraint/static/js/unique_constraint.ui';
import TableSchema from '../../../pgadmin/browser/server_groups/servers/databases/schemas/tables/static/js/table.ui';
import {addNewDatagridRow, genericBeforeEach, getCreateView, getEditView, getPropertiesView} from '../genericFunctions';
class SchemaInColl extends BaseUISchema {
constructor(schemaObj) {
super();
this.schemaObj = schemaObj;
}
get baseFields() {
return [{
id: 'collection', label: '', type: 'collection',
schema: this.schemaObj,
editable: false,
canAdd: true, canEdit: false, canDelete: true, hasRole: true,
columns : ['name', 'columns'],
}];
}
}
function getFieldDepChange(schema, id) {
return _.find(schema.fields, (f)=>f.id==id)?.depChange;
}
describe('UniqueConstraintSchema', ()=>{
let schemaObj;
let getInitData = ()=>Promise.resolve({});
beforeAll(()=>{
schemaObj = new UniqueConstraintSchema({
spcname: ()=>Promise.resolve([]),
}, {});
});
beforeEach(()=>{
genericBeforeEach();
});
it('create', async ()=>{
await getCreateView(schemaObj);
});
it('edit', async ()=>{
await getEditView(schemaObj, getInitData);
});
it('properties', async ()=>{
await getPropertiesView(schemaObj, getInitData);
});
it('create collection', async ()=>{
let schemaCollObj = new SchemaInColl(schemaObj);
const {ctrl, user} = await getCreateView(schemaCollObj);
/* Make sure you hit every corner */
await addNewDatagridRow(user, ctrl);
});
it('depChange', ()=>{
let state = {columns: [{column: 'id'}]};
state.name = '';
expect(getFieldDepChange(schemaObj, 'comment')(state)).toEqual({
comment: '',
});
state.index = 'someindex';
expect(getFieldDepChange(schemaObj, 'spcname')(state)).toEqual({
spcname: '',
});
expect(getFieldDepChange(schemaObj, 'include')(state)).toEqual({
include: [],
});
expect(getFieldDepChange(schemaObj, 'fillfactor')(state)).toEqual({
fillfactor: null,
});
expect(getFieldDepChange(schemaObj, 'condeferrable')(state)).toEqual({
condeferrable: false,
});
expect(getFieldDepChange(schemaObj, 'condeferred')(state)).toEqual({
condeferred: false,
});
state.index = undefined;
state.condeferrable = true;
expect(getFieldDepChange(schemaObj, 'spcname')(state)).toEqual({});
expect(getFieldDepChange(schemaObj, 'include')(state)).toEqual({});
expect(getFieldDepChange(schemaObj, 'fillfactor')(state)).toEqual({});
expect(getFieldDepChange(schemaObj, 'condeferrable')(state)).toEqual({});
expect(getFieldDepChange(schemaObj, 'condeferred')(state)).toEqual({});
schemaObj.top = new TableSchema({}, null);
expect(getFieldDepChange(schemaObj, 'columns')(state, ['columns', 0], null, {
type: SCHEMA_STATE_ACTIONS.DELETE_ROW,
oldState: {
columns: [
{name: 'id'}
],
},
path: ['columns'],
value: 0,
})).toEqual({
columns: [],
});
expect(getFieldDepChange(schemaObj, 'columns')(state, ['columns', 0], {
columns: [
{name: 'id123'}
],
}, {
type: SCHEMA_STATE_ACTIONS.SET_VALUE,
oldState: {
columns: [
{name: 'id'}
],
},
path: ['columns', 0, 'name'],
value: 'id123',
})).toEqual({
columns: [{column: 'id123'}],
});
});
it('columns cell formatter', ()=>{
let cellFormatter = _.find(schemaObj.fields, (f)=>f.id=='columns').cell().controlProps.formatter;
expect(cellFormatter.fromRaw([{
column: 'user_id',
},{
column: 'client_order_id',
}])).toBe('user_id,client_order_id');
expect(cellFormatter.fromRaw([])).toBe('');
});
it('columns type formatter preserves constraint column order', ()=>{
let typeFormatter = _.find(schemaObj.fields, (f)=>f.id=='columns').type().controlProps.formatter;
/* allOptions are in table column position order (alphabetical here) */
let allOptions = [
{value: 'alpha', label: 'alpha'},
{value: 'beta', label: 'beta'},
{value: 'gamma', label: 'gamma'},
];
/* backendVal comes from the constraint definition in a different order */
let backendVal = [{column: 'gamma'}, {column: 'alpha'}];
let result = typeFormatter.fromRaw(backendVal, allOptions);
/* result must preserve backendVal order, not allOptions order */
expect(result).toEqual([
{value: 'gamma', label: 'gamma'},
{value: 'alpha', label: 'alpha'},
]);
/* empty and null values should be handled gracefully */
expect(typeFormatter.fromRaw([], allOptions)).toEqual([]);
expect(typeFormatter.fromRaw(null, allOptions)).toEqual([]);
});
it('columns type formatter toRaw', ()=>{
let typeFormatter = _.find(schemaObj.fields, (f)=>f.id=='columns').type().controlProps.formatter;
expect(typeFormatter.toRaw([{value: 'user_id'}, {value: 'client_order_id'}])).toEqual([
{column: 'user_id'},
{column: 'client_order_id'},
]);
expect(typeFormatter.toRaw([])).toEqual([]);
expect(typeFormatter.toRaw(null)).toEqual([]);
});
it('validate', ()=>{
let state = {};
let setError = jest.fn();
schemaObj.validate(state, setError);
expect(setError).toHaveBeenCalledWith('columns', 'Please specify columns for Unique constraint.');
state.columns = [{columns: 'id'}];
expect(schemaObj.validate(state, setError)).toBe(false);
});
});