]> dev.renevier.net Git - syp.git/blob - openlayers/tests/Protocol/SQL/Gears.html
initial commit
[syp.git] / openlayers / tests / Protocol / SQL / Gears.html
1 <html>
2 <head>
3   <script src="../../../lib/OpenLayers.js"></script>
4   <script type="text/javascript">
5
6     function test_initialize(t) {
7         var protocol = new OpenLayers.Protocol.SQL.Gears();
8         if (!protocol.supported()) {
9             t.plan(0);
10             return;
11         }
12
13         t.plan(5);
14
15         t.eq(protocol.CLASS_NAME, "OpenLayers.Protocol.SQL.Gears",
16              "ctor returns correct value");
17
18         t.eq(protocol.jsonParser.CLASS_NAME,
19              "OpenLayers.Format.JSON",
20              "ctor creates a JSON parser");
21
22         t.eq(protocol.wktParser.CLASS_NAME,
23              "OpenLayers.Format.WKT",
24              "ctor creates a WKT parser");
25
26         var str = protocol.FID_PREFIX + "foo_bar";
27         t.ok(str.match(protocol.fidRegExp),
28              "ctor creates correct regexp");
29
30         t.ok(typeof protocol.db == "object",
31              "ctor creates a db object");
32
33         protocol.clear();
34         protocol.destroy();
35     }
36
37     function test_destroy(t) {
38         var protocol = new OpenLayers.Protocol.SQL.Gears();
39         if (!protocol.supported()) {
40             t.plan(0);
41             return;
42         }
43
44         t.plan(3);
45
46         protocol.destroy();
47
48         t.eq(protocol.db, null,
49              "destroy nullifies db");
50         t.eq(protocol.jsonParser, null,
51              "destroy nullifies jsonParser");
52         t.eq(protocol.wktParser, null,
53              "destroy nullifies wktParser");
54      }
55
56     function test_read(t) {
57         var protocolCallback, readCallback;
58         var protocolOptions = {callback: protocolCallback};
59         var readOptions = {callback: readCallback};
60
61         var protocol = new OpenLayers.Protocol.SQL.Gears(protocolOptions);
62         if (!protocol.supported()) {
63             t.plan(0);
64             return;
65         }
66
67         function okCallback(resp) {
68             t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
69                  "read calls correct callback with a response object");
70         }
71
72         function failCallback(resp) {
73             t.fail("read calls incorrect callback");
74         }
75
76         t.plan(4);
77
78         var resp;
79
80         // 2 tests
81         protocolOptions.callback = okCallback;
82         readOptions.callback = failCallback;
83         resp = protocol.read();
84         t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
85              "read returns a response object");
86
87         // 2 test
88         protocolOptions.callback = failCallback;
89         readOptions.callback = okCallback;
90         resp = protocol.read(readOptions);
91         t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
92              "read returns a response object");
93
94         protocol.clear();
95         protocol.destroy();
96     }
97
98     function test_unfreezeFeature(t) {
99         var protocol = new OpenLayers.Protocol.SQL.Gears();
100         if (!protocol.supported()) {
101             t.plan(0);
102             return;
103         }
104
105         t.plan(10);
106
107         var feature;
108         var wkt, json, fid, state;
109
110         json = "{\"fake\":\"properties\"}";
111         fid = "1000";
112         state = OpenLayers.State.INSERT;
113
114         var row = {
115             fieldByName: function(str) {
116                 if (str == "geometry") {
117                     return wkt;
118                 }
119                 if (str == "properties") {
120                     return json;
121                 }
122                 if (str == "fid") {
123                     return fid;
124                 }
125                 if (str == "state") {
126                     return state;
127                 }
128             }
129         };
130
131         // 5 tests
132         wkt = "POINT(1 2)";
133         feature = protocol.unfreezeFeature(row);
134         t.eq(feature.CLASS_NAME, "OpenLayers.Feature.Vector",
135              "unfreezeFeature returns an OpenLayers.Feature.Vector");
136         t.ok(feature.geometry.x == 1 && feature.geometry.y == 2,
137              "unfreezeFeature returns a feature with correct geometry");
138         t.eq(feature.attributes.fake, "properties",
139              "unfreezeFeature returns a feature with correct attributes");
140         t.eq(feature.fid, fid,
141              "unfreezeFeature returns a feature with fid");
142         t.eq(feature.state, state,
143              "unfreezeFeature returns a feature with state");
144
145         // 5 tests
146         wkt = protocol.NULL_GEOMETRY;
147         state = protocol.NULL_FEATURE_STATE;
148         feature = protocol.unfreezeFeature(row);
149         t.eq(feature.CLASS_NAME, "OpenLayers.Feature.Vector",
150              "unfreezeFeature returns an OpenLayers.Feature.Vector");
151         t.eq(feature.geometry, null,
152              "unfreezeFeature returns a feature with correct geometry");
153         t.eq(feature.attributes.fake, "properties",
154              "unfreezeFeature returns a feature with correct attributes");
155         t.eq(feature.fid, fid,
156              "unfreezeFeature returns a feature with fid");
157         t.eq(feature.state, null,
158              "unfreezeFeature returns a feature with state");
159
160         protocol.clear();
161         protocol.destroy();
162     }
163
164     function test_extractFidFromField(t) {
165         var protocol = new OpenLayers.Protocol.SQL.Gears();
166         if (!protocol.supported()) {
167             t.plan(0);
168             return;
169         }
170
171         t.plan(4);
172
173         var field, fid;
174
175         // fid is a string, field is not prefixed with FID_PREFIX
176         // 1 test
177         field = "10";
178         res = protocol.extractFidFromField(field);
179         t.eq(res, "10",
180              "extractFidFromField returns expected string");
181
182         // fid is a string, field is prefixed with FID_PREFIX
183         // 1 test
184         field = protocol.FIX_PREFIX + "10";
185         res = protocol.extractFidFromField(field);
186         t.eq(res, protocol.FIX_PREFIX + "10",
187              "extractFidFromField returns expected prefixed string");
188
189         // fid is a number, field is not prefixed with FIX_PREFIX
190         // 1 test
191         protocol.typeOfFid = "number";
192         field = "10";
193         res = protocol.extractFidFromField(field);
194         t.eq(res, 10,
195              "extractFidFromField returns expected number");
196
197         // fid is a number, field is prefixed with FIX_PREFIX
198         // 1 test
199         protocol.typeOfFid = "number";
200         field = protocol.FID_PREFIX + "10";
201         res = protocol.extractFidFromField(field);
202         t.eq(res, protocol.FID_PREFIX + "10",
203              "extractFidFromField returns expected prefixed string");
204     }
205
206     function test_freezeFeature(t) {
207         var protocol = new OpenLayers.Protocol.SQL.Gears();
208         if (!protocol.supported()) {
209             t.plan(0);
210             return;
211         }
212
213         t.plan(8);
214
215         var feature, res;
216
217         // 4 tests
218         feature = new OpenLayers.Feature.Vector();
219         feature.geometry = new OpenLayers.Geometry.Point(1, 2);
220         feature.attributes.fake = "properties";
221         feature.fid = "1000";
222         feature.state = OpenLayers.State.INSERT;
223         res = protocol.freezeFeature(feature);
224         t.eq(res[0], feature.fid,
225              "freezeFeature returns correct fid");
226         t.eq(res[1], "POINT(1 2)",
227              "freezeFeature returns correct WKT");
228         t.eq(res[2], "{\"fake\":\"properties\"}",
229              "freezeFeature returns correct JSON");
230         t.eq(res[3], feature.state,
231              "freezeFeature returns correct feature state");
232
233         // 4 tests
234         protocol.saveFeatureState = false;
235         feature = new OpenLayers.Feature.Vector();
236         feature.attributes.fake = "properties";
237         feature.fid = "1000";
238         feature.state = OpenLayers.State.INSERT;
239         res = protocol.freezeFeature(feature);
240         t.eq(res[0], feature.fid,
241              "freezeFeature returns correct fid");
242         t.eq(res[1], protocol.NULL_GEOMETRY,
243              "freezeFeature returns expected null geom string");
244         t.eq(res[2], "{\"fake\":\"properties\"}",
245              "freezeFeature returns correct JSON");
246         t.eq(res[3], protocol.NULL_FEATURE_STATE,
247              "freezeFeature returns expected null feature state string");
248
249         protocol.clear();
250         protocol.destroy();
251      }
252
253      function test_create(t) {
254         var protocol = new OpenLayers.Protocol.SQL.Gears();
255         if (!protocol.supported()) {
256             t.plan(0);
257             return;
258         }
259
260         t.plan(8);
261
262         var resp;
263         var scope = {"fake": "scope"};
264
265         var options = {
266             callback: function(resp) {
267                 t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
268                      "user callback is passed a response");
269                 t.eq(resp.requestType, "create",
270                      "user callback is passed correct request type in resp");
271                 t.ok(this == scope,
272                      "user callback called with correct scope");
273             },
274             scope: scope
275         };
276
277         // 4 tests
278         var feature = new OpenLayers.Feature.Vector();
279         feature.fid = "1000";
280         feature.attributes.fake = "properties";
281         feature.state = OpenLayers.State.INSERT;
282         resp = protocol.create([feature], options);
283         t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
284              "create returns a response");
285
286         // check what we have in the DB
287         // 4 tests
288         resp = protocol.read({"noFeatureStateReset": true});
289         t.eq(resp.features.length, 1,
290              "create inserts feature in the DB");
291         t.eq(resp.features[0].fid, feature.fid,
292              "create inserts feature with correct fid");
293         t.eq(resp.features[0].attributes.fake, feature.attributes.fake,
294              "create inserts feature with correct attributes");
295         t.eq(resp.features[0].state, feature.state,
296              "create inserts feature with correct state");
297
298         protocol.clear();
299         protocol.destroy();
300     }
301
302      function test_createOrUpdate(t) {
303         var protocol = new OpenLayers.Protocol.SQL.Gears();
304         if (!protocol.supported()) {
305             t.plan(0);
306             return;
307         }
308
309         t.plan(5);
310
311         // 1 test
312         var feature = new OpenLayers.Feature.Vector();
313         feature.fid = "1000";
314         feature.attributes.fake = "properties";
315         feature.state = OpenLayers.State.INSERT;
316         resp = protocol.createOrUpdate([feature]);
317         t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
318              "createOrUpdate returns a response");
319
320         // check what we have in the DB
321         // 4 tests
322         resp = protocol.read({"noFeatureStateReset": true});
323         t.eq(resp.features.length, 1,
324              "createOrUpdate inserts feature in the DB");
325         t.eq(resp.features[0].fid, feature.fid,
326              "createOrUpdate inserts feature with correct fid");
327         t.eq(resp.features[0].attributes.fake, feature.attributes.fake,
328              "createOrUpdate inserts feature with correct attributes");
329         t.eq(resp.features[0].state, feature.state,
330              "createOrUpdate inserts feature with correct state");
331
332         protocol.clear();
333         protocol.destroy();
334     }
335
336     function test_delete(t) {
337         var protocol = new OpenLayers.Protocol.SQL.Gears();
338         if (!protocol.supported()) {
339             t.plan(0);
340             return;
341         }
342
343         t.plan(4);
344
345         function createOneAndDeleteOne(fid, deleteOptions) {
346             var feature = new OpenLayers.Feature.Vector();
347             feature.fid = fid;
348             feature.attributes.fake = "properties";
349             feature.state = OpenLayers.State.INSERT;
350             var r = protocol.create([feature]);
351             protocol["delete"](r.reqFeatures, deleteOptions);
352         }
353
354         var resp, fid;
355
356         // 1 test
357         fid = 1000;
358         protocol.saveFeatureState = false;
359         createOneAndDeleteOne(fid)
360         resp = protocol.read();
361         t.eq(resp.features.length, 0,
362              "delete deletes feature if saveFeatureState is false");
363         protocol.clear();
364
365         // 1 test
366         fid = 1000;
367         protocol.saveFeatureState = true;
368         createOneAndDeleteOne(fid);
369         resp = protocol.read();
370         t.eq(resp.features.length, 1,
371              "delete does not delete feature if saveFeatureState is true");
372         protocol.clear();
373
374         // 1 test
375         fid = "1000";
376         protocol.saveFeatureState = true;
377         createOneAndDeleteOne(fid);
378         resp = protocol.read();
379         t.eq(resp.features.length, 1,
380              "delete does not delete feature if saveFeatureState is true");
381         protocol.clear();
382
383         // 1 test
384         fid = protocol.FID_PREFIX + "1000";
385         protocol.saveFeatureState = true;
386         createOneAndDeleteOne(fid, {dontDelete: true});
387         resp = protocol.read();
388         t.eq(resp.features.length, 0,
389              "delete deletes feature if saveFeatureState is true and fid is prefixed");
390         protocol.clear();
391
392         protocol.destroy();
393     }
394
395     function test_callUserCallback(t) {
396         var protocol = new OpenLayers.Protocol.SQL.Gears();
397         if (!protocol.supported()) {
398             t.plan(0);
399             return;
400         }
401
402         t.plan(6);
403
404         var options, resp;
405         var scope = {'fake': 'scope'};
406
407         // test commit callback
408         // 1 tests
409         options = {
410             'callback': function() {
411                 t.ok(this == scope, 'callback called with correct scope');
412             },
413             'scope': scope
414         };
415         resp = {'requestType': 'create', 'last': true};
416         protocol.callUserCallback(options, resp);
417         // 0 test
418         resp = {'requestType': 'create', 'last': false};
419         protocol.callUserCallback(options, resp);
420
421         // test create callback
422         // 2 tests
423         options = {
424             'create': {
425                 'callback': function(r) {
426                     t.ok(this == scope, 'callback called with correct scope');
427                     t.ok(r == resp, 'callback called with correct response');
428                 },
429                 'scope': scope
430             }
431         };
432         resp = {'requestType': 'create'};
433         protocol.callUserCallback(options, resp);
434
435         // test with both callbacks set
436         // 3 tests
437         options = {
438             'create': {
439                 'callback': function(r) {
440                     t.ok(this == scope, 'callback called with correct scope');
441                     t.ok(r == resp, 'callback called with correct response');
442                 },
443                 'scope': scope
444             },
445             'callback': function() {
446                 t.ok(this == scope, 'callback called with correct scope');
447             },
448             'scope': scope
449         };
450         resp = {'requestType': 'create', 'last': true};
451         protocol.callUserCallback(options, resp);
452
453         // no callback set
454         // 0 test
455         options = {
456             'delete': {
457                 'callback': function(resp) {
458                     t.fail('callback should not get called');
459                 }
460             }
461         };
462         resp = {'requestType': 'create'};
463         protocol.callUserCallback(options, resp);
464
465         // cleanup
466         protocol.destroy();
467     }
468
469   </script>
470 </head>
471 <body>
472 </body>
473 </html>