/
odata.qunit.js
270 lines (226 loc) · 8.34 KB
/
odata.qunit.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
const { test } = require("qunit");
const { ODataQuery } = require("../index")
/**
* {@link https://odata.org}.
* {@link https://services.odata.org/V2/Northwind/Northwind.svc}.
*/
QUnit.module('Odata Query Operation with odata version 2.0', hooks =>{
var testOdataServiceURL = "https://services.odata.org/V2/Northwind/Northwind.svc";
var entity = "Customers";
/**
* if we only get the filters , it will get query with the default eq operator
*/
test('test filter', assert =>{
let odataQuery = new ODataQuery(testOdataServiceURL, entity);
let query = {
"filter" : {
"Country" : "Germany",
"City": "Berlin"
}
}
assert.equal(odataQuery.createQuery(query).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and City eq 'Berlin'")
});
/**
* if we only get the filters , it will get query with the default eq operator
*/
test('test filter withe array', assert =>{
let odataQuery = new ODataQuery(testOdataServiceURL, entity);
let query = {
"filter": {
"Country" : "Germany",
"City" : ["Berlin", "Mannheim", "Aachen", "Brandenburg" ]
}
}
assert.equal(odataQuery.createQuery(query).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and (City eq 'Berlin' or City eq 'Mannheim' or City eq 'Aachen' or City eq 'Brandenburg')")
});
/**
* if we only get the filters , it will get query with the given operator
*/
test("test filter + fitlerOption", assert =>{
let odataQuery1 = new ODataQuery(testOdataServiceURL, entity);
let query1 = {
"filter": {
"Country" : "Germany",
"City": "Berlin"
},
"filterOption" : {
"City" : "ne"
}
}
assert.equal(odataQuery1.createQuery(query1).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and City ne 'Berlin'")
let odataQuery2 = new ODataQuery(testOdataServiceURL, entity);
let query2 = {
"filter": {
"Country" : "Germany",
"PostalCode": null,
},
"filterOption" : {
"PostalCode" : {
"low" : { "operator" : "ge", "value": "100" },
"high" : { "operator" : "le", "value": "102" }
}
}
}
assert.equal(odataQuery2.createQuery(query2).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and (PostalCode ge '100' and PostalCode le '102')")
})
test("test filter + fitlerOption + pagination", assert =>{
let odataQuery1 = new ODataQuery(testOdataServiceURL, entity);
let query1 = {
"filter": {
"Country" : "Germany",
"City": "Berlin"
},
"filterOption" : {
"City" : "ne"
},
"pagination": {
"current" : 1,
"pageSize" : 10
}
}
assert.equal(odataQuery1.createQuery(query1).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and City ne 'Berlin'&$top=10")
let odataQuery2 = new ODataQuery(testOdataServiceURL, entity);
let query2 = {
"filter": {
"Country" : "Germany",
"City": "Berlin"
},
"filterOption" : {
"City" : "ne"
},
"pagination": {
"current" : 2,
"pageSize" : 10
}
}
assert.equal(odataQuery2.createQuery(query2).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and City ne 'Berlin'&$top=10&$skip=10")
})
test("test filter + fitlerOption + pagination + serach", assert =>{
let odataQuery = new ODataQuery(testOdataServiceURL, entity);
let query = {
"filter": {
"Country" : "Germany",
"City": "Berlin"
},
"filterOption" : {
"City" : "ne"
},
"pagination": {
"current" : 1,
"pageSize" : 10
},
"serach" : "Jack"
}
assert.equal(odataQuery.createQuery(query).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and City ne 'Berlin'&$top=10&$search=Jack")
})
test("test filter + fitlerOption + pagination + orderby", assert =>{
let odataQuery = new ODataQuery(testOdataServiceURL, entity);
let query = {
"filter": {
"Country" : "Germany",
"City": "Berlin"
},
"filterOption" : {
"City" : "ne"
},
"pagination": {
"current" : 1,
"pageSize" : 10
},
"orderby": {
"Country" : "asc",
"CustomerID" : "desc"
}
}
assert.equal(odataQuery.createQuery(query).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and City ne 'Berlin'&$top=10&$orderby=Country asc,CustomerID desc")
})
test("test filter + fitlerOption + pagination + orderby + select", assert =>{
let odataQuery = new ODataQuery(testOdataServiceURL, entity);
let query = {
"filter": {
"Country" : "Germany",
"City": "Berlin"
},
"filterOption" : {
"City" : "ne"
},
"pagination": {
"current" : 1,
"pageSize" : 10
},
"orderby": {
"Country" : "asc",
"CustomerID" : "desc"
},
"select" : ["CustomerID", "CompanyName", "ContactName", "ContactTitle", "Address", "City", "Region", "PostalCode"]
}
assert.equal(odataQuery.createQuery(query).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and City ne 'Berlin'"
+ "&$top=10&$orderby=Country asc,CustomerID desc"
+ "&$select=CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode")
})
test("test filter + fitlerOption + pagination + orderby + select + format", assert =>{
let odataQuery = new ODataQuery(testOdataServiceURL, entity);
let query = {
"filter": {
"Country" : "Germany",
"City": "Berlin"
},
"filterOption" : {
"City" : "ne"
},
"pagination": {
"current" : 1,
"pageSize" : 10
},
"orderby": {
"Country" : "asc",
"CustomerID" : "desc"
},
"select" : ["CustomerID", "CompanyName", "ContactName", "ContactTitle", "Address", "City", "Region", "PostalCode"],
"format" : "json"
}
assert.equal(odataQuery.createQuery(query).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and City ne 'Berlin'"
+ "&$top=10&$orderby=Country asc,CustomerID desc"
+ "&$select=CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode&$format=json")
})
/**
* for SAP language usage
*/
test("test filter + fitlerOption + pagination + orderby + select + format + lang", assert =>{
let odataQuery = new ODataQuery(testOdataServiceURL, entity);
let query = {
"filter": {
"Country" : "Germany",
"City": "Berlin"
},
"filterOption" : {
"City" : "ne"
},
"pagination": {
"current" : 1,
"pageSize" : 10
},
"orderby": {
"Country" : "asc",
"CustomerID" : "desc"
},
"select" : ["CustomerID", "CompanyName", "ContactName", "ContactTitle", "Address", "City", "Region", "PostalCode"],
"format" : "json",
"lang" : "zh"
}
assert.equal(odataQuery.createQuery(query).getOdataQueryURL(),
"https://services.odata.org/V2/Northwind/Northwind.svc/Customers?$filter=Country eq 'Germany' and City ne 'Berlin'"
+ "&$top=10&$orderby=Country asc,CustomerID desc"
+ "&$select=CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode&$format=json&sap-language=zh")
})
})