-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquerybuilder.test.js
184 lines (148 loc) · 5.62 KB
/
querybuilder.test.js
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
var qb = require('./querybuilder')
var sbuild = require('./querybuilder').staticBuild;
var staticBuild = new sbuild({
useDotNotation: true,
forceIndex: false,
orderByDir: "ASC"
})
var build = qb.build;
var queryObject = {},expectedObject;
var defaults = {
options: {
useDotNotation: false,
forceIndex: false,
orderByDir: "ASC"
}
};
describe('assign defaults', () => {
test('to empty object', () => {
expect(qb.defaults(queryObject)).toEqual(defaults);
});
test('to object with values', () => {
queryObject = {
type: "select"
};
expectedObject = {
type:"select",
options: {
useDotNotation: false,
forceIndex: false,
orderByDir: "ASC"
}
};
expect(qb.defaults(queryObject)).toEqual(expectedObject);
});
test('object with values and options', () => {
queryObject = {
type: "insert",
};
defaults.options.forceIndex = true;
expectedObject = Object.assign({},queryObject,defaults);
expect(qb.defaults(queryObject, {forceIndex: true})).toEqual(expectedObject);
});
});
describe('checks for errors in object', () => {
test('with missing required fields', () => {
{};
expect(qb.check({})).toEqual('Error: type is required but undefined.');
});
test('with required field that has wrong datatype', () => {
var params1 = {
type: 12,
table: 'newtable',
values: '*'
};
expect(qb.check(params1)).toEqual('Error: expected \'[object String]\' but found \'[object Number]\' at key \'type\'');
});
test('with required field that has variant datatype and does not match', () => {
var params2 = {
type: 'insert',
table: 'newtable',
values: {}
}
expect(qb.check(params2)).toEqual('Error: expected \'[object String]\' or \'[object Array]\' but found \'[object Object]\' at key \'values\'');
});
test('with required field that has right datatype but empty value', () => {
var params3 = {
type: 'insert',
table: '',
values: '*'
}
expect(qb.check(params3)).toEqual('Error: expected \'[object String]\' but found empty \'[object String]\' at key \'table\'')
});
test('with required field that has variant datatype, does match options, but empty value', () => {
var params4 = {
type: 'insert',
table: 'newtable',
values: []
}
expect(qb.check(params4)).toEqual('Error: expected \'[object String]\' or \'[object Array]\' but found empty \'[object Array]\' at key \'values\'');
});
test('with optional field that has wrong datatype', () => {
var params5 = {
type: 'insert',
table: 'newtable',
values: '*',
database: 0
}
expect(qb.check(params5)).toEqual('Error: expected \'[object String]\' but found \'[object Number]\' at key \'database\'');
});
test('with optional field that has wrong datatype at nested level', () => {
var params6 = {
type: 'insert',
table: 'newtable',
values: '*',
database: 0,
pagination: {
page: "1",
limit: 50
}
}
expect(qb.check(params6)).toEqual('Error: expected \'[object Number]\' but found \'[object String]\' at key \'pagination.page\'');
});
});
describe('test building', () => {
test('select with most params and no errors', () => {
var params7 = {
database: 'gatsbyezypo',
type: 'select',
table: 'GLMSCAN',
values: '*',
where: 'glms_tran = 1',
pagination: {
page: 1,
limit: 50
}
}
expect(build(params7)).toEqual('Select * from GLMSCAN where glms_tran = 1 limit 1,50;')
});
test('delete with params and no errors', () => {
var params8 = {
database: 'gatsbyezypo',
type: 'delete',
table: 'GLMSCAN',
values: 'nil',
where: 'glms_tran = 1'
}
expect(build(params8)).toEqual('Delete from GLMSCAN where glms_tran = 1;');
});
test('insert with params and no errors, values as array of objects', () => {
var params9 = {
database: 'gatsbyezypo',
type: 'insert',
table: 'GLMSCAN',
values: [{GLMS_Document:'document'},{GLMS_MimeType:'application/pdf'}]
}
expect(build(params9,{useDotNotation:false})).toEqual('Insert into GLMSCAN (GLMS_Document,GLMS_MimeType) values(\'document\',\'application/pdf\');');
});
test('update with params and no errors, values as array of objects. using staticBuild', () => {
var params10 = {
database: 'gatsbyezypo',
type: 'update',
table: 'GLMSCAN',
values: [{GLMS_Document:'document'},{GLMS_MimeType:'application/pdf'}],
where: 'glms_tran = 1'
}
expect(staticBuild(params10)).toEqual('Update GLMSCAN Set GLMS_Document = \'document\',GLMS_MimeType = \'application/pdf\' where glms_tran = 1;')
})
});