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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
|
<html>
<head>
<script src="../../../OLLoader.js"></script>
<script src="../../../../lib/deprecated.js"></script>
<script type="text/javascript">
function test_initialize(t) {
var protocol = new OpenLayers.Protocol.SQL.Gears();
if (!protocol.supported()) {
t.plan(0);
return;
}
t.plan(5);
t.eq(protocol.CLASS_NAME, "OpenLayers.Protocol.SQL.Gears",
"ctor returns correct value");
t.eq(protocol.jsonParser.CLASS_NAME,
"OpenLayers.Format.JSON",
"ctor creates a JSON parser");
t.eq(protocol.wktParser.CLASS_NAME,
"OpenLayers.Format.WKT",
"ctor creates a WKT parser");
var str = protocol.FID_PREFIX + "foo_bar";
t.ok(str.match(protocol.fidRegExp),
"ctor creates correct regexp");
t.ok(typeof protocol.db == "object",
"ctor creates a db object");
protocol.clear();
protocol.destroy();
}
function test_destroy(t) {
var protocol = new OpenLayers.Protocol.SQL.Gears();
if (!protocol.supported()) {
t.plan(0);
return;
}
t.plan(3);
protocol.destroy();
t.eq(protocol.db, null,
"destroy nullifies db");
t.eq(protocol.jsonParser, null,
"destroy nullifies jsonParser");
t.eq(protocol.wktParser, null,
"destroy nullifies wktParser");
}
function test_read(t) {
var protocolCallback, readCallback;
var protocolOptions = {callback: protocolCallback};
var readOptions = {callback: readCallback};
var protocol = new OpenLayers.Protocol.SQL.Gears(protocolOptions);
if (!protocol.supported()) {
t.plan(0);
return;
}
function okCallback(resp) {
t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
"read calls correct callback with a response object");
}
function failCallback(resp) {
t.fail("read calls incorrect callback");
}
t.plan(4);
var resp;
// 2 tests
protocolOptions.callback = okCallback;
readOptions.callback = failCallback;
resp = protocol.read();
t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
"read returns a response object");
// 2 test
protocolOptions.callback = failCallback;
readOptions.callback = okCallback;
resp = protocol.read(readOptions);
t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
"read returns a response object");
protocol.clear();
protocol.destroy();
}
function test_unfreezeFeature(t) {
var protocol = new OpenLayers.Protocol.SQL.Gears();
if (!protocol.supported()) {
t.plan(0);
return;
}
t.plan(10);
var feature;
var wkt, json, fid, state;
json = "{\"fake\":\"properties\"}";
fid = "1000";
state = OpenLayers.State.INSERT;
var row = {
fieldByName: function(str) {
if (str == "geometry") {
return wkt;
}
if (str == "properties") {
return json;
}
if (str == "fid") {
return fid;
}
if (str == "state") {
return state;
}
}
};
// 5 tests
wkt = "POINT(1 2)";
feature = protocol.unfreezeFeature(row);
t.eq(feature.CLASS_NAME, "OpenLayers.Feature.Vector",
"unfreezeFeature returns an OpenLayers.Feature.Vector");
t.ok(feature.geometry.x == 1 && feature.geometry.y == 2,
"unfreezeFeature returns a feature with correct geometry");
t.eq(feature.attributes.fake, "properties",
"unfreezeFeature returns a feature with correct attributes");
t.eq(feature.fid, fid,
"unfreezeFeature returns a feature with fid");
t.eq(feature.state, state,
"unfreezeFeature returns a feature with state");
// 5 tests
wkt = protocol.NULL_GEOMETRY;
state = protocol.NULL_FEATURE_STATE;
feature = protocol.unfreezeFeature(row);
t.eq(feature.CLASS_NAME, "OpenLayers.Feature.Vector",
"unfreezeFeature returns an OpenLayers.Feature.Vector");
t.eq(feature.geometry, null,
"unfreezeFeature returns a feature with correct geometry");
t.eq(feature.attributes.fake, "properties",
"unfreezeFeature returns a feature with correct attributes");
t.eq(feature.fid, fid,
"unfreezeFeature returns a feature with fid");
t.eq(feature.state, null,
"unfreezeFeature returns a feature with state");
protocol.clear();
protocol.destroy();
}
function test_extractFidFromField(t) {
var protocol = new OpenLayers.Protocol.SQL.Gears();
if (!protocol.supported()) {
t.plan(0);
return;
}
t.plan(4);
var field, fid;
// fid is a string, field is not prefixed with FID_PREFIX
// 1 test
field = "10";
res = protocol.extractFidFromField(field);
t.eq(res, "10",
"extractFidFromField returns expected string");
// fid is a string, field is prefixed with FID_PREFIX
// 1 test
field = protocol.FIX_PREFIX + "10";
res = protocol.extractFidFromField(field);
t.eq(res, protocol.FIX_PREFIX + "10",
"extractFidFromField returns expected prefixed string");
// fid is a number, field is not prefixed with FIX_PREFIX
// 1 test
protocol.typeOfFid = "number";
field = "10";
res = protocol.extractFidFromField(field);
t.eq(res, 10,
"extractFidFromField returns expected number");
// fid is a number, field is prefixed with FIX_PREFIX
// 1 test
protocol.typeOfFid = "number";
field = protocol.FID_PREFIX + "10";
res = protocol.extractFidFromField(field);
t.eq(res, protocol.FID_PREFIX + "10",
"extractFidFromField returns expected prefixed string");
}
function test_freezeFeature(t) {
var protocol = new OpenLayers.Protocol.SQL.Gears();
if (!protocol.supported()) {
t.plan(0);
return;
}
t.plan(8);
var feature, res;
// 4 tests
feature = new OpenLayers.Feature.Vector();
feature.geometry = new OpenLayers.Geometry.Point(1, 2);
feature.attributes.fake = "properties";
feature.fid = "1000";
feature.state = OpenLayers.State.INSERT;
res = protocol.freezeFeature(feature);
t.eq(res[0], feature.fid,
"freezeFeature returns correct fid");
t.eq(res[1], "POINT(1 2)",
"freezeFeature returns correct WKT");
t.eq(res[2], "{\"fake\":\"properties\"}",
"freezeFeature returns correct JSON");
t.eq(res[3], feature.state,
"freezeFeature returns correct feature state");
// 4 tests
protocol.saveFeatureState = false;
feature = new OpenLayers.Feature.Vector();
feature.attributes.fake = "properties";
feature.fid = "1000";
feature.state = OpenLayers.State.INSERT;
res = protocol.freezeFeature(feature);
t.eq(res[0], feature.fid,
"freezeFeature returns correct fid");
t.eq(res[1], protocol.NULL_GEOMETRY,
"freezeFeature returns expected null geom string");
t.eq(res[2], "{\"fake\":\"properties\"}",
"freezeFeature returns correct JSON");
t.eq(res[3], protocol.NULL_FEATURE_STATE,
"freezeFeature returns expected null feature state string");
protocol.clear();
protocol.destroy();
}
function test_create(t) {
var protocol = new OpenLayers.Protocol.SQL.Gears();
if (!protocol.supported()) {
t.plan(0);
return;
}
t.plan(8);
var resp;
var scope = {"fake": "scope"};
var options = {
callback: function(resp) {
t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
"user callback is passed a response");
t.eq(resp.requestType, "create",
"user callback is passed correct request type in resp");
t.ok(this == scope,
"user callback called with correct scope");
},
scope: scope
};
// 4 tests
var feature = new OpenLayers.Feature.Vector();
feature.fid = "1000";
feature.attributes.fake = "properties";
feature.state = OpenLayers.State.INSERT;
resp = protocol.create([feature], options);
t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
"create returns a response");
// check what we have in the DB
// 4 tests
resp = protocol.read({"noFeatureStateReset": true});
t.eq(resp.features.length, 1,
"create inserts feature in the DB");
t.eq(resp.features[0].fid, feature.fid,
"create inserts feature with correct fid");
t.eq(resp.features[0].attributes.fake, feature.attributes.fake,
"create inserts feature with correct attributes");
t.eq(resp.features[0].state, feature.state,
"create inserts feature with correct state");
protocol.clear();
protocol.destroy();
}
function test_createOrUpdate(t) {
var protocol = new OpenLayers.Protocol.SQL.Gears();
if (!protocol.supported()) {
t.plan(0);
return;
}
t.plan(5);
// 1 test
var feature = new OpenLayers.Feature.Vector();
feature.fid = "1000";
feature.attributes.fake = "properties";
feature.state = OpenLayers.State.INSERT;
resp = protocol.createOrUpdate([feature]);
t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
"createOrUpdate returns a response");
// check what we have in the DB
// 4 tests
resp = protocol.read({"noFeatureStateReset": true});
t.eq(resp.features.length, 1,
"createOrUpdate inserts feature in the DB");
t.eq(resp.features[0].fid, feature.fid,
"createOrUpdate inserts feature with correct fid");
t.eq(resp.features[0].attributes.fake, feature.attributes.fake,
"createOrUpdate inserts feature with correct attributes");
t.eq(resp.features[0].state, feature.state,
"createOrUpdate inserts feature with correct state");
protocol.clear();
protocol.destroy();
}
function test_delete(t) {
var protocol = new OpenLayers.Protocol.SQL.Gears();
if (!protocol.supported()) {
t.plan(0);
return;
}
t.plan(4);
function createOneAndDeleteOne(fid, deleteOptions) {
var feature = new OpenLayers.Feature.Vector();
feature.fid = fid;
feature.attributes.fake = "properties";
feature.state = OpenLayers.State.INSERT;
var r = protocol.create([feature]);
protocol["delete"](r.reqFeatures, deleteOptions);
}
var resp, fid;
// 1 test
fid = 1000;
protocol.saveFeatureState = false;
createOneAndDeleteOne(fid)
resp = protocol.read();
t.eq(resp.features.length, 0,
"delete deletes feature if saveFeatureState is false");
protocol.clear();
// 1 test
fid = 1000;
protocol.saveFeatureState = true;
createOneAndDeleteOne(fid);
resp = protocol.read();
t.eq(resp.features.length, 1,
"delete does not delete feature if saveFeatureState is true");
protocol.clear();
// 1 test
fid = "1000";
protocol.saveFeatureState = true;
createOneAndDeleteOne(fid);
resp = protocol.read();
t.eq(resp.features.length, 1,
"delete does not delete feature if saveFeatureState is true");
protocol.clear();
// 1 test
fid = protocol.FID_PREFIX + "1000";
protocol.saveFeatureState = true;
createOneAndDeleteOne(fid, {dontDelete: true});
resp = protocol.read();
t.eq(resp.features.length, 0,
"delete deletes feature if saveFeatureState is true and fid is prefixed");
protocol.clear();
protocol.destroy();
}
function test_callUserCallback(t) {
var protocol = new OpenLayers.Protocol.SQL.Gears();
if (!protocol.supported()) {
t.plan(0);
return;
}
t.plan(6);
var options, resp;
var scope = {'fake': 'scope'};
// test commit callback
// 1 tests
options = {
'callback': function() {
t.ok(this == scope, 'callback called with correct scope');
},
'scope': scope
};
resp = {'requestType': 'create', 'last': true};
protocol.callUserCallback(options, resp);
// 0 test
resp = {'requestType': 'create', 'last': false};
protocol.callUserCallback(options, resp);
// test create callback
// 2 tests
options = {
'create': {
'callback': function(r) {
t.ok(this == scope, 'callback called with correct scope');
t.ok(r == resp, 'callback called with correct response');
},
'scope': scope
}
};
resp = {'requestType': 'create'};
protocol.callUserCallback(options, resp);
// test with both callbacks set
// 3 tests
options = {
'create': {
'callback': function(r) {
t.ok(this == scope, 'callback called with correct scope');
t.ok(r == resp, 'callback called with correct response');
},
'scope': scope
},
'callback': function() {
t.ok(this == scope, 'callback called with correct scope');
},
'scope': scope
};
resp = {'requestType': 'create', 'last': true};
protocol.callUserCallback(options, resp);
// no callback set
// 0 test
options = {
'delete': {
'callback': function(resp) {
t.fail('callback should not get called');
}
}
};
resp = {'requestType': 'create'};
protocol.callUserCallback(options, resp);
// cleanup
protocol.destroy();
}
</script>
</head>
<body>
</body>
</html>
|