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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
<html>
<head>
<script src="../OLLoader.js"></script>
<script type="text/javascript">
function test_CQL_Constructor(t) {
t.plan(5);
var options = {'foo': 'bar'};
var format = new OpenLayers.Format.CQL(options);
t.ok(format instanceof OpenLayers.Format.CQL,
"new OpenLayers.Format.CQL object");
t.eq(format.foo, "bar", "constructor sets options correctly")
t.eq(typeof format.read, 'function', 'format has a read function');
t.eq(typeof format.write, 'function', 'format has a write function');
t.eq(format.options, options, "format.options correctly set");
}
function test_Comparison_string(t) {
t.plan(5);
var test_cql, format, filter;
test_cql = "X >= 'B'";
format = new OpenLayers.Format.CQL();
filter = format.read(test_cql);
t.ok(filter instanceof OpenLayers.Filter.Comparison,
"Parsing a simple >= filter produces a Filter.Comparison");
t.eq(filter.type, OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO,
">= parsed as Filter.Comparison.GREATER_THAN_OR_EQUAL_TO");
t.eq(filter.property, 'X',
"Property extracted from CQL text");
t.eq(filter.value, 'B',
"Value extracted from CQL text");
t.eq(format.write(filter), test_cql, "write returned test cql");
}
function test_read_whitespace(t) {
t.plan(4);
var cql = "TYPEDESC = 'BOE Numbered Plans'";
var format = new OpenLayers.Format.CQL();
var filter = format.read(cql);
t.ok(filter instanceof OpenLayers.Filter.Comparison, "filter parsed correctly with whitespace in string");
t.eq(filter.property, 'TYPEDESC', "filter property parsed correctly");
t.eq(filter.value, 'BOE Numbered Plans', "value parsed correctly");
t.eq(filter.type, '==', 'filter type parsed correctly');
}
function test_read_escaped_quotes(t) {
t.plan(14);
var cql = "PROP = 'don''t worry' or PROP = 'value''s value' or PROP = 'foo'";
var format = new OpenLayers.Format.CQL();
var filter = format.read(cql);
t.ok(filter instanceof OpenLayers.Filter.Logical, "filter type");
t.eq(filter.filters.length, 2, "filter children");
var f0 = filter.filters[0];
t.ok(f0 instanceof OpenLayers.Filter.Logical, "f0 type");
t.eq(f0.filters.length, 2, "f0 children");
var f00 = f0.filters[0];
t.eq(f00.property, "PROP", "f000 property");
t.eq(f00.type, "==", "f000 type");
t.eq(f00.value, "don't worry", "f000 value");
var f01 = f0.filters[1];
t.eq(f01.property, "PROP", "f001 property");
t.eq(f01.type, "==", "f001 type");
t.eq(f01.value, "value's value", "f001 value");
var f1 = filter.filters[1];
t.ok(f1 instanceof OpenLayers.Filter.Comparison, "f1 type");
t.eq(f1.property, "PROP", "f1 property");
t.eq(f1.type, "==", "f1 type");
t.eq(f1.value, "foo", "f1 value");
}
function test_write_escaped_quotes(t) {
t.plan(1);
var filter = new OpenLayers.Filter.Logical({
type: OpenLayers.Filter.Logical.OR,
filters: [
new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "PROP",
value: "quot'd string"
}),
new OpenLayers.Filter.Comparison({
type: OpenLayers.Filter.Comparison.EQUAL_TO,
property: "PROP",
value: "don't quote's"
})
]
});
var format = new OpenLayers.Format.CQL();
var cql = format.write(filter);
t.eq(cql, "(PROP = 'quot''d string') OR (PROP = 'don''t quote''s')", "escaped");
}
function test_Comparison_number(t) {
t.plan(5);
var test_cql, format, filter;
test_cql = "X >= 10";
format = new OpenLayers.Format.CQL();
filter = format.read(test_cql);
t.ok(filter instanceof OpenLayers.Filter.Comparison,
"Parsing a simple >= filter produces a Filter.Comparison");
t.eq(filter.type, OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO,
">= parsed as Filter.Comparison.GREATER_THAN_OR_EQUAL_TO");
t.eq(filter.property, 'X',
"Property extracted from CQL text");
t.eq(filter.value, 10,
"Value extracted from CQL text");
t.eq(format.write(filter), test_cql, "write returned test cql");
}
function test_Logical(t) {
t.plan(7);
var test_cql, format, filter;
test_cql = "X >= 'B' AND X < 'M'";
format = new OpenLayers.Format.CQL();
filter = format.read(test_cql);
t.ok(filter instanceof OpenLayers.Filter.Logical,
"Parsing ANDed filters produces a Filter.Logical");
t.eq(filter.type, OpenLayers.Filter.Logical.AND,
"AND parsed as Filter.Logical.AND");
t.eq(filter.filters.length, 2,
"AND Filter contains two subfilters");
t.ok(filter.filters[0] instanceof OpenLayers.Filter.Comparison,
"First sub-filter is a Filter.Comparison");
t.eq(filter.filters[0].type, OpenLayers.Filter.Comparison.GREATER_THAN_OR_EQUAL_TO,
"First sub-filter is the first filter in the CQL text");
t.ok(filter.filters[1] instanceof OpenLayers.Filter.Comparison,
"Second sub-filter is a Filter.Comparison");
t.eq(filter.filters[1].type, OpenLayers.Filter.Comparison.LESS_THAN,
"Second sub-filter is the second filter in the CQL text");
}
function test_Logical_write(t) {
t.plan(1);
var cql = "(X >= 'B') AND (X < 'M')";
var format = new OpenLayers.Format.CQL();
var filter = format.read(cql);
t.eq(format.write(filter), cql, "write returned test cql");
}
function test_Logical_spatial(t) {
t.plan(9);
var test_cql, format, filter;
test_cql = "INTERSECTS(the_geom, POLYGON((-111 41,-115 41,-115 45,-110 45,-111 41))) AND CONTAINS(the_geom, POINT(-111 41))";
format = new OpenLayers.Format.CQL();
filter = format.read(test_cql);
t.ok(filter instanceof OpenLayers.Filter.Logical,
"Parsing ANDed filters produces a Filter.Logical");
t.eq(filter.type, OpenLayers.Filter.Logical.AND,
"AND parsed as Filter.Logical.AND");
t.eq(filter.filters.length, 2,
"AND Filter contains two subfilters");
t.ok(filter.filters[0] instanceof OpenLayers.Filter.Spatial,
"First sub-filter is a Filter.Spatial");
t.eq(filter.filters[0].type, OpenLayers.Filter.Spatial.INTERSECTS,
"First sub-filter is the first filter in the CQL text");
t.geom_eq(filter.filters[0].value, OpenLayers.Geometry.fromWKT("POLYGON((-111 41,-115 41,-115 45,-110 45,-111 41))"),
"First sub-filter is has correct geometry");
t.ok(filter.filters[1] instanceof OpenLayers.Filter.Spatial,
"Second sub-filter is a Filter.Comparison");
t.eq(filter.filters[1].type, OpenLayers.Filter.Spatial.CONTAINS,
"Second sub-filter is the second filter in the CQL text");
t.geom_eq(filter.filters[1].value, OpenLayers.Geometry.fromWKT("POINT(-111 41)"),
"Second sub-filter is has correct geometry");
}
function test_Logical_spatial_write(t) {
// TODO: remove this if extra parentheses are avoided by checking logical operator precedence
t.plan(1);
var cql = "(INTERSECTS(the_geom, POLYGON((-111 41,-115 41,-115 45,-110 45,-111 41)))) AND (CONTAINS(the_geom, POINT(-111 41)))";
var format = new OpenLayers.Format.CQL();
var filter = format.read(cql);
t.eq(format.write(filter), cql, "write returned test cql");
}
function test_Parentheticals(t) {
t.plan(2);
var format, cqlA, filterA, cqlB, filterB;
format = new OpenLayers.Format.CQL();
cqlA = "A = '1' AND B = '2' OR C = '3'";
cqlB = "A = '1' AND (B = '2' OR C = '3')";
filterA = format.read(cqlA);
filterB = format.read(cqlB);
t.ok(filterA instanceof OpenLayers.Filter.Logical &&
filterA.filters[0] instanceof OpenLayers.Filter.Logical &&
filterA.filters[1] instanceof OpenLayers.Filter.Comparison,
"Unparenthesized expression groups left to right");
t.ok(filterB instanceof OpenLayers.Filter.Logical &&
filterB.filters[0] instanceof OpenLayers.Filter.Comparison &&
filterB.filters[1] instanceof OpenLayers.Filter.Logical,
"Parenthesized expression groups as specified by parentheses");
}
function test_Parentheticals_write(t) {
// TODO: remove this if extra parentheses are avoided by checking logical operator precedence
t.plan(1);
var format = new OpenLayers.Format.CQL();
var cql = "(A = '1') AND ((B = '2') OR (C = '3'))";
var filter = format.read(cql);
t.eq(format.write(filter), cql, "write returned test cql");
}
function test_BBOX(t) {
t.plan(5);
var format = new OpenLayers.Format.CQL(),
cql = "BBOX(the_geom,1,2,3,4)",
filter = format.read(cql);
t.ok(filter instanceof OpenLayers.Filter.Spatial,
"Parsing BBOX expression produces Filter.Spatial");
t.eq(filter.type, OpenLayers.Filter.Spatial.BBOX,
"Spatial filter is a bbox filter");
t.eq(filter.property, "the_geom",
"Property name is as specified in CQL");
t.eq(filter.value.toBBOX(), "1,2,3,4",
"Value is as specified in CQL");
t.eq(format.write(filter), cql, "write returned test cql");
}
function test_INTERSECTS(t) {
t.plan(5);
var format = new OpenLayers.Format.CQL(),
cql = "INTERSECTS(the_geom, POINT(1 2))",
filter = format.read(cql);
t.ok(filter instanceof OpenLayers.Filter.Spatial,
"Parsing BBOX expression produces Filter.Spatial");
t.eq(filter.type, OpenLayers.Filter.Spatial.INTERSECTS,
"Spatial filter is an intersects filter");
t.eq(filter.property, "the_geom",
"Property name is as specified in CQL");
t.ok(filter.value instanceof OpenLayers.Geometry,
"Value is a geometry");
t.eq(format.write(filter), cql, "write returned test cql");
}
function test_WITHIN(t) {
t.plan(5);
var format = new OpenLayers.Format.CQL(),
cql = "WITHIN(the_geom, POLYGON((1 2,3 4,5 6,3 8,1 6,1 2)))",
filter = format.read(cql);
t.ok(filter instanceof OpenLayers.Filter.Spatial,
"Parsing BBOX expression produces Filter.Spatial");
t.eq(filter.type, OpenLayers.Filter.Spatial.WITHIN,
"Spatial filter is a within filter");
t.eq(filter.property, "the_geom",
"Property name is as specified in CQL");
t.ok(filter.value instanceof OpenLayers.Geometry,
"Value is a geometry");
t.eq(format.write(filter), cql, "write returned test cql");
}
function test_DWITHIN(t) {
t.plan(6);
var format = new OpenLayers.Format.CQL(),
cql = "DWITHIN(the_geom, POINT(1 2), 6)",
filter = format.read(cql);
t.ok(filter instanceof OpenLayers.Filter.Spatial,
"Parsing DWITHIN expression produces Filter.Spatial");
t.eq(filter.type, OpenLayers.Filter.Spatial.DWITHIN,
"Spatial filter is a DWITHIN filter");
t.eq(filter.property, "the_geom",
"Property name is as specified in CQL");
t.ok(filter.value instanceof OpenLayers.Geometry,
"Value is a geometry");
t.eq(filter.distance, 6,
"Distance is as specified in CQL");
t.eq(format.write(filter), cql, "write returned test cql");
}
function test_CONTAINS(t) {
t.plan(5);
var format = new OpenLayers.Format.CQL(),
cql = "CONTAINS(the_geom, POINT(1 2))",
filter = format.read(cql);
t.ok(filter instanceof OpenLayers.Filter.Spatial,
"Parsing BBOX expression produces Filter.Spatial");
t.eq(filter.type, OpenLayers.Filter.Spatial.CONTAINS,
"Spatial filter is a within filter");
t.eq(filter.property, "the_geom",
"Property name is as specified in CQL");
t.ok(filter.value instanceof OpenLayers.Geometry,
"Value is a geometry");
t.eq(format.write(filter), cql, "write returned test cql");
}
function test_NOT(t) {
t.plan(4);
var format = new OpenLayers.Format.CQL(),
cql = "NOT X < 12",
filter = format.read(cql);
t.ok(filter instanceof OpenLayers.Filter.Logical,
"Parsing NOT expression produces Logical.Not");
t.eq(filter.type, OpenLayers.Filter.Logical.NOT,
"Logical filter is a NOT filter");
t.eq(filter.filters[0].property, "X",
"Property name is as specified in CQL");
t.eq(filter.filters[0].value, 12, "Value is as specified in CQL");
}
function test_NOT_write(t) {
// TODO: remove this if extra parentheses are avoided by checking logical operator precedence
t.plan(1);
var format = new OpenLayers.Format.CQL(),
cql = "NOT (X < 12)",
filter = format.read(cql);
t.eq(format.write(filter), cql, "write returned test cql");
}
function test_BETWEEN(t) {
t.plan(6);
var format = new OpenLayers.Format.CQL(),
cql = "A BETWEEN 0 AND 5",
filter = format.read(cql);
t.ok(filter instanceof OpenLayers.Filter.Comparison,
"Parsing BETWEEN expression produces Filter.Comparison");
t.eq(filter.type, OpenLayers.Filter.Comparison.BETWEEN,
"Comparison filter is a between filter");
t.eq(filter.property, "A",
"Property name is as specified in CQL");
t.eq(filter.lowerBoundary, 0, 'Lower boundary is as specified in CQL');
t.eq(filter.upperBoundary, 5, 'Upper bondary is as specified in CQL');
t.eq(format.write(filter), cql, "write returned test cql");
}
function test_NULL(t) {
t.plan(3);
var filter = new OpenLayers.Filter.Comparison({
property: "GEOM",
type: "NULL"
});
var format = new OpenLayers.Format.CQL();
var str = 'GEOM IS NULL';
t.eq(format.write(filter), str, "NULL filter written correctly");
filter = format.read(str);
t.eq(filter.type, OpenLayers.Filter.Comparison.IS_NULL, "filter type is correctly parsed");
t.eq(filter.property, "GEOM", "filter property is correctly parsed");
}
</script>
</head>
<body></body>
</html>
|