3 <script src="../../../lib/OpenLayers.js"></script>
4 <script type="text/javascript">
6 function test_initialize(t) {
7 var protocol = new OpenLayers.Protocol.SQL.Gears();
8 if (!protocol.supported()) {
15 t.eq(protocol.CLASS_NAME, "OpenLayers.Protocol.SQL.Gears",
16 "ctor returns correct value");
18 t.eq(protocol.jsonParser.CLASS_NAME,
19 "OpenLayers.Format.JSON",
20 "ctor creates a JSON parser");
22 t.eq(protocol.wktParser.CLASS_NAME,
23 "OpenLayers.Format.WKT",
24 "ctor creates a WKT parser");
26 var str = protocol.FID_PREFIX + "foo_bar";
27 t.ok(str.match(protocol.fidRegExp),
28 "ctor creates correct regexp");
30 t.ok(typeof protocol.db == "object",
31 "ctor creates a db object");
37 function test_destroy(t) {
38 var protocol = new OpenLayers.Protocol.SQL.Gears();
39 if (!protocol.supported()) {
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");
56 function test_read(t) {
57 var protocolCallback, readCallback;
58 var protocolOptions = {callback: protocolCallback};
59 var readOptions = {callback: readCallback};
61 var protocol = new OpenLayers.Protocol.SQL.Gears(protocolOptions);
62 if (!protocol.supported()) {
67 function okCallback(resp) {
68 t.eq(resp.CLASS_NAME, "OpenLayers.Protocol.Response",
69 "read calls correct callback with a response object");
72 function failCallback(resp) {
73 t.fail("read calls incorrect callback");
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");
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");
98 function test_unfreezeFeature(t) {
99 var protocol = new OpenLayers.Protocol.SQL.Gears();
100 if (!protocol.supported()) {
108 var wkt, json, fid, state;
110 json = "{\"fake\":\"properties\"}";
112 state = OpenLayers.State.INSERT;
115 fieldByName: function(str) {
116 if (str == "geometry") {
119 if (str == "properties") {
125 if (str == "state") {
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");
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");
164 function test_extractFidFromField(t) {
165 var protocol = new OpenLayers.Protocol.SQL.Gears();
166 if (!protocol.supported()) {
175 // fid is a string, field is not prefixed with FID_PREFIX
178 res = protocol.extractFidFromField(field);
180 "extractFidFromField returns expected string");
182 // fid is a string, field is prefixed with FID_PREFIX
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");
189 // fid is a number, field is not prefixed with FIX_PREFIX
191 protocol.typeOfFid = "number";
193 res = protocol.extractFidFromField(field);
195 "extractFidFromField returns expected number");
197 // fid is a number, field is prefixed with FIX_PREFIX
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");
206 function test_freezeFeature(t) {
207 var protocol = new OpenLayers.Protocol.SQL.Gears();
208 if (!protocol.supported()) {
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");
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");
253 function test_create(t) {
254 var protocol = new OpenLayers.Protocol.SQL.Gears();
255 if (!protocol.supported()) {
263 var scope = {"fake": "scope"};
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");
272 "user callback called with correct scope");
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");
286 // check what we have in the DB
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");
302 function test_createOrUpdate(t) {
303 var protocol = new OpenLayers.Protocol.SQL.Gears();
304 if (!protocol.supported()) {
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");
320 // check what we have in the DB
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");
336 function test_delete(t) {
337 var protocol = new OpenLayers.Protocol.SQL.Gears();
338 if (!protocol.supported()) {
345 function createOneAndDeleteOne(fid, deleteOptions) {
346 var feature = new OpenLayers.Feature.Vector();
348 feature.attributes.fake = "properties";
349 feature.state = OpenLayers.State.INSERT;
350 var r = protocol.create([feature]);
351 protocol["delete"](r.reqFeatures, deleteOptions);
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");
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");
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");
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");
395 function test_callUserCallback(t) {
396 var protocol = new OpenLayers.Protocol.SQL.Gears();
397 if (!protocol.supported()) {
405 var scope = {'fake': 'scope'};
407 // test commit callback
410 'callback': function() {
411 t.ok(this == scope, 'callback called with correct scope');
415 resp = {'requestType': 'create', 'last': true};
416 protocol.callUserCallback(options, resp);
418 resp = {'requestType': 'create', 'last': false};
419 protocol.callUserCallback(options, resp);
421 // test create callback
425 'callback': function(r) {
426 t.ok(this == scope, 'callback called with correct scope');
427 t.ok(r == resp, 'callback called with correct response');
432 resp = {'requestType': 'create'};
433 protocol.callUserCallback(options, resp);
435 // test with both callbacks set
439 'callback': function(r) {
440 t.ok(this == scope, 'callback called with correct scope');
441 t.ok(r == resp, 'callback called with correct response');
445 'callback': function() {
446 t.ok(this == scope, 'callback called with correct scope');
450 resp = {'requestType': 'create', 'last': true};
451 protocol.callUserCallback(options, resp);
457 'callback': function(resp) {
458 t.fail('callback should not get called');
462 resp = {'requestType': 'create'};
463 protocol.callUserCallback(options, resp);