Commit 9c0c1922 authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: catch error properly in put

parent 6cd7c2ab
/*jslint indent:2, maxlen: 80, nomen: true */ /*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory, /*global jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query*/ Query*/
(function (jIO, RSVP) { (function (jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory,
Query) {
"use strict"; "use strict";
function MappingStorage(spec) { function MappingStorage(spec) {
...@@ -52,8 +53,6 @@ ...@@ -52,8 +53,6 @@
function getAttachmentId(storage, sub_id, attachment_id, method) { function getAttachmentId(storage, sub_id, attachment_id, method) {
var mapping_dict = storage._attachment_mapping_dict; var mapping_dict = storage._attachment_mapping_dict;
return new RSVP.Queue()
.push(function () {
if (mapping_dict !== undefined if (mapping_dict !== undefined
&& mapping_dict[attachment_id] !== undefined && mapping_dict[attachment_id] !== undefined
&& mapping_dict[attachment_id][method] !== undefined && mapping_dict[attachment_id][method] !== undefined
...@@ -63,7 +62,6 @@ ...@@ -63,7 +62,6 @@
).expand({id: sub_id}); ).expand({id: sub_id});
} }
return attachment_id; return attachment_id;
});
} }
function getSubStorageId(storage, id) { function getSubStorageId(storage, id) {
...@@ -95,7 +93,10 @@ ...@@ -95,7 +93,10 @@
}) })
.push(function (data) { .push(function (data) {
if (data.data.rows.length === 0) { if (data.data.rows.length === 0) {
return undefined; throw new jIO.util.jIOError(
"Can not find id",
404
);
} }
if (data.data.rows.length > 1) { if (data.data.rows.length > 1) {
throw new TypeError("id must be unique field: " + id throw new TypeError("id must be unique field: " + id
...@@ -194,6 +195,23 @@ ...@@ -194,6 +195,23 @@
return sub_doc; return sub_doc;
} }
function handleAttachment(context, argument_list, method) {
return getSubStorageId(context, argument_list[0])
.push(function (sub_id) {
argument_list[0] = sub_id;
argument_list[1] = getAttachmentId(
context,
sub_id,
argument_list[1],
method
);
return context._sub_storage[method + "Attachment"].apply(
context._sub_storage,
argument_list
);
});
}
MappingStorage.prototype.get = function (id) { MappingStorage.prototype.get = function (id) {
var context = this; var context = this;
return getSubStorageId(this, id) return getSubStorageId(this, id)
...@@ -202,10 +220,6 @@ ...@@ -202,10 +220,6 @@
}) })
.push(function (sub_doc) { .push(function (sub_doc) {
return mapToMainDocument(context, sub_doc, true); return mapToMainDocument(context, sub_doc, true);
})
.push(undefined, function (error) {
throw new jIO.util.jIOError("Cannot find document " + id
+ ", cause: " + error.message, 404);
}); });
}; };
...@@ -220,20 +234,18 @@ ...@@ -220,20 +234,18 @@
}; };
MappingStorage.prototype.put = function (id, doc) { MappingStorage.prototype.put = function (id, doc) {
doc.id = id;
var context = this, var context = this,
sub_doc = mapToSubstorageDocument(this, doc); sub_doc = mapToSubstorageDocument(this, doc);
return getSubStorageId(this, id) return getSubStorageId(this, id)
.push(function (sub_id) { .push(function (sub_id) {
if (context._id_is_mapped) {
sub_doc[context._mapping_dict.id.equal] = id;
}
if (id === undefined) {
throw new Error();
}
return context._sub_storage.put(sub_id, sub_doc); return context._sub_storage.put(sub_id, sub_doc);
}) })
.push(undefined, function () { .push(undefined, function (error) {
if (error instanceof jIO.util.jIOError && error.status_code === 404) {
return context._sub_storage.post(sub_doc); return context._sub_storage.post(sub_doc);
}
throw error;
}) })
.push(function () { .push(function () {
return id; return id;
...@@ -252,48 +264,18 @@ ...@@ -252,48 +264,18 @@
}; };
MappingStorage.prototype.putAttachment = function (id, attachment_id) { MappingStorage.prototype.putAttachment = function (id, attachment_id) {
var context = this, argument_list = arguments; return handleAttachment(this, arguments, "put", id)
return getSubStorageId(context, id)
.push(function (sub_id) {
argument_list[0] = sub_id;
return getAttachmentId(context, sub_id, attachment_id, "put");
})
.push(function (sub_attachment_id) {
argument_list[1] = sub_attachment_id;
return context._sub_storage.putAttachment.apply(context._sub_storage,
argument_list);
})
.push(function () { .push(function () {
return attachment_id; return attachment_id;
}); });
}; };
MappingStorage.prototype.getAttachment = function (id, attachment_id) { MappingStorage.prototype.getAttachment = function () {
var context = this, argument_list = arguments; return handleAttachment(this, arguments, "get");
return getSubStorageId(context, id)
.push(function (sub_id) {
argument_list[0] = sub_id;
return getAttachmentId(context, sub_id, attachment_id, "get");
})
.push(function (sub_attachment_id) {
argument_list[1] = sub_attachment_id;
return context._sub_storage.getAttachment.apply(context._sub_storage,
argument_list);
});
}; };
MappingStorage.prototype.removeAttachment = function (id, attachment_id) { MappingStorage.prototype.removeAttachment = function (id, attachment_id) {
var context = this, argument_list = arguments; return handleAttachment(this, arguments, "remove", id)
return getSubStorageId(context, id)
.push(function (sub_id) {
argument_list[0] = sub_id;
return getAttachmentId(context, sub_id, attachment_id, "remove");
})
.push(function (sub_attachment_id) {
argument_list[1] = sub_attachment_id;
return context._sub_storage.removeAttachment.apply(context._sub_storage,
argument_list);
})
.push(function () { .push(function () {
return attachment_id; return attachment_id;
}); });
...@@ -308,24 +290,25 @@ ...@@ -308,24 +290,25 @@
}; };
MappingStorage.prototype.bulk = function (id_list) { MappingStorage.prototype.bulk = function (id_list) {
var i, var context = this;
context = this,
mapped_result = [], function mapId(parameter) {
promise_list = id_list.map(function (parameter) {
return getSubStorageId(context, parameter.parameter_list[0]) return getSubStorageId(context, parameter.parameter_list[0])
.push(function (id) { .push(function (id) {
return {"method": parameter.method, "parameter_list": [id]}; return {"method": parameter.method, "parameter_list": [id]};
}); });
}); }
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
var promise_list = id_list.map(mapId);
return RSVP.all(promise_list); return RSVP.all(promise_list);
}) })
.push(function (id_list_mapped) { .push(function (id_list_mapped) {
return context._sub_storage.bulk(id_list_mapped); return context._sub_storage.bulk(id_list_mapped);
}) })
.push(function (result) { .push(function (result) {
var mapped_result = [], i;
for (i = 0; i < result.length; i += 1) { for (i = 0; i < result.length; i += 1) {
mapped_result.push(mapToMainDocument(context, result[i], false)); mapped_result.push(mapToMainDocument(context, result[i], false));
} }
...@@ -342,10 +325,10 @@ ...@@ -342,10 +325,10 @@
sort_on = []; sort_on = [];
function mapQuery(one_query) { function mapQuery(one_query) {
var i, query_list = []; var j, query_list = [];
if (one_query.type === "complex") { if (one_query.type === "complex") {
for (i = 0; i < one_query.query_list.length; i += 1) { for (j = 0; j < one_query.query_list.length; j += 1) {
query_list.push(mapQuery(one_query.query_list[i])); query_list.push(mapQuery(one_query.query_list[j]));
} }
one_query.query_list = query_list; one_query.query_list = query_list;
return one_query; return one_query;
...@@ -430,4 +413,4 @@ ...@@ -430,4 +413,4 @@
}; };
jIO.addStorage('mapping', MappingStorage); jIO.addStorage('mapping', MappingStorage);
}(jIO, RSVP)); }(jIO, RSVP, UriTemplate, SimpleQuery, ComplexQuery, QueryFactory, Query));
\ No newline at end of file \ No newline at end of file
...@@ -23,7 +23,6 @@ ...@@ -23,7 +23,6 @@
// mappingStorage.constructor // mappingStorage.constructor
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("mappingStorage.constructor"); module("mappingStorage.constructor");
test("create substorage", function () { test("create substorage", function () {
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
...@@ -34,10 +33,12 @@ ...@@ -34,10 +33,12 @@
ok(jio.__storage._sub_storage instanceof jio.constructor); ok(jio.__storage._sub_storage instanceof jio.constructor);
equal(jio.__storage._sub_storage.__type, "mappingstorage2713"); equal(jio.__storage._sub_storage.__type, "mappingstorage2713");
deepEqual(jio.__storage._mapping_dict, {}); deepEqual(jio.__storage._mapping_dict, {});
deepEqual(jio.__storage._attachment_mapping_dict, {}); deepEqual(jio.__storage._attachment_mapping_dict, {});
deepEqual(jio.__storage._query, {}); deepEqual(jio.__storage._query, {});
equal(jio.__storage._map_all_property, true); equal(jio.__storage._map_all_property, true);
}); });
test("accept parameters", function () { test("accept parameters", function () {
...@@ -58,13 +59,13 @@ ...@@ -58,13 +59,13 @@
equal(jio.__storage._query.query.type, "simple"); equal(jio.__storage._query.query.type, "simple");
deepEqual(jio.__storage._attachment_mapping_dict, {"foo": {"get": "bar"}}); deepEqual(jio.__storage._attachment_mapping_dict, {"foo": {"get": "bar"}});
equal(jio.__storage._map_all_property, false); equal(jio.__storage._map_all_property, false);
}); });
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// mappingStorage.get // mappingStorage.get
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("mappingStorage.get"); module("mappingStorage.get");
test("get called substorage get", function () { test("get called substorage get", function () {
stop(); stop();
expect(2); expect(2);
...@@ -82,7 +83,6 @@ ...@@ -82,7 +83,6 @@
return {title: "foo"}; return {title: "foo"};
}; };
start();
jio.get("bar") jio.get("bar")
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
...@@ -91,6 +91,9 @@ ...@@ -91,6 +91,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -111,7 +114,6 @@ ...@@ -111,7 +114,6 @@
return {otherTitle: "foo"}; return {otherTitle: "foo"};
}; };
start();
jio.get("bar") jio.get("bar")
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
...@@ -119,6 +121,9 @@ ...@@ -119,6 +121,9 @@
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -151,7 +156,6 @@ ...@@ -151,7 +156,6 @@
return {"otherTitle": "foo"}; return {"otherTitle": "foo"};
}; };
start();
jio.get("42") jio.get("42")
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
...@@ -159,6 +163,9 @@ ...@@ -159,6 +163,9 @@
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -196,7 +203,6 @@ ...@@ -196,7 +203,6 @@
return {"otherTitle": "foo"}; return {"otherTitle": "foo"};
}; };
start();
jio.get("42") jio.get("42")
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
...@@ -204,6 +210,9 @@ ...@@ -204,6 +210,9 @@
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -239,7 +248,6 @@ ...@@ -239,7 +248,6 @@
return {"title": "foo"}; return {"title": "foo"};
}; };
start();
jio.get("42") jio.get("42")
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
...@@ -247,6 +255,9 @@ ...@@ -247,6 +255,9 @@
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -273,19 +284,22 @@ ...@@ -273,19 +284,22 @@
return id; return id;
}; };
start();
jio.put("bar", {"title": "foo"}) jio.put("bar", {"title": "foo"})
.push(function (result) { .push(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
test("put with default values", function () { test("put with default values", function () {
stop(); stop();
expect(3); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: { sub_storage: {
...@@ -300,19 +314,22 @@ ...@@ -300,19 +314,22 @@
return id; return id;
}; };
start();
jio.put("bar", {}) jio.put("bar", {})
.push(function (result) { .push(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
test("put with id and prop mapped", function () { test("put with id and prop mapped", function () {
stop(); stop();
expect(3); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: { sub_storage: {
...@@ -339,19 +356,22 @@ ...@@ -339,19 +356,22 @@
return []; return [];
}; };
start();
jio.put("42", {"title": "foo"}) jio.put("42", {"title": "foo"})
.push(function (result) { .push(function (result) {
equal(result, "42"); equal(result, "42");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
test("put with map_all_property", function () { test("put with map_all_property", function () {
stop(); stop();
expect(3); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: { sub_storage: {
...@@ -370,13 +390,15 @@ ...@@ -370,13 +390,15 @@
return id; return id;
}; };
start();
jio.put("42", {"title": "foo", "smth": "bar", "smth2": "bar2"}) jio.put("42", {"title": "foo", "smth": "bar", "smth2": "bar2"})
.push(function (result) { .push(function (result) {
equal(result, "42"); equal(result, "42");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -388,24 +410,28 @@ ...@@ -388,24 +410,28 @@
test("remove with substorage remove", function () { test("remove with substorage remove", function () {
stop(); stop();
expect(2); expect(2);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
} }
}); });
Storage2713.prototype.remove = function (id) { Storage2713.prototype.remove = function (id) {
equal(id, "bar", "remove 2713 called"); equal(id, "bar", "remove 2713 called");
return id; return id;
}; };
start();
jio.remove("bar", {"title": "foo"}) jio.remove("bar", {"title": "foo"})
.push(function (result) { .push(function (result) {
equal(result, "bar"); equal(result, "bar");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -437,12 +463,14 @@ ...@@ -437,12 +463,14 @@
return "foo"; return "foo";
}; };
start();
jio.remove("42") jio.remove("42")
.push(function (result) { .push(function (result) {
equal(result, "42"); equal(result, "42");
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -454,6 +482,7 @@ ...@@ -454,6 +482,7 @@
test("post with mapped property", function () { test("post with mapped property", function () {
stop(); stop();
expect(2); expect(2);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: { sub_storage: {
...@@ -461,24 +490,28 @@ ...@@ -461,24 +490,28 @@
}, },
mapping_dict: {"title": {"equal": "otherTitle"}} mapping_dict: {"title": {"equal": "otherTitle"}}
}); });
Storage2713.prototype.post = function (doc) { Storage2713.prototype.post = function (doc) {
deepEqual(doc, {"otherTitle": "foo"}, "remove 2713 called"); deepEqual(doc, {"otherTitle": "foo"}, "remove 2713 called");
return "42"; return "42";
}; };
start();
jio.post({"title": "foo"}) jio.post({"title": "foo"})
.push(function (result) { .push(function (result) {
equal(result, "42"); equal(result, "42");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
test("post with id mapped", function () { test("post with id mapped", function () {
stop(); stop();
expect(2); expect(2);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: { sub_storage: {
...@@ -491,11 +524,13 @@ ...@@ -491,11 +524,13 @@
return false; return false;
}; };
start();
jio.post({"title": "foo"}) jio.post({"title": "foo"})
.push(undefined, function (error) { .push(undefined, function (error) {
equal(error.message, "post is not supported with id mapped"); equal(error.message, "post is not supported with id mapped");
equal(error.status_code, 400); equal(error.status_code, 400);
})
.always(function () {
start();
}); });
}); });
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -511,8 +546,8 @@ ...@@ -511,8 +546,8 @@
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
} }
}), }), blob = new Blob([""]);
blob = new Blob([""]);
Storage2713.prototype.putAttachment = function (doc_id, Storage2713.prototype.putAttachment = function (doc_id,
attachment_id, attachment) { attachment_id, attachment) {
equal(doc_id, "42", "putAttachment 2713 called"); equal(doc_id, "42", "putAttachment 2713 called");
...@@ -521,28 +556,32 @@ ...@@ -521,28 +556,32 @@
return doc_id; return doc_id;
}; };
start();
jio.putAttachment("42", "2713", blob) jio.putAttachment("42", "2713", blob)
.push(function (result) { .push(function (result) {
equal(result, "2713"); equal(result, "2713");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
test("putAttachment with UriTemplate", function () { test("putAttachment with UriTemplate", function () {
stop(); stop();
expect(4); expect(4);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, },
attachment_mapping_dict: {"2713": {"put": attachment_mapping_dict: {
{"uri_template": "www.2713.foo/{id}"}}} "2713": {"put": {"uri_template": "www.2713.foo/{id}"}}
}), }
blob = new Blob([""]); }), blob = new Blob([""]);
Storage2713.prototype.putAttachment = function (doc_id, Storage2713.prototype.putAttachment = function (doc_id,
attachment_id, attachment) { attachment_id, attachment) {
equal(doc_id, "42", "putAttachment 2713 called"); equal(doc_id, "42", "putAttachment 2713 called");
...@@ -551,13 +590,15 @@ ...@@ -551,13 +590,15 @@
return doc_id; return doc_id;
}; };
start();
jio.putAttachment("42", "2713", blob) jio.putAttachment("42", "2713", blob)
.push(function (result) { .push(function (result) {
equal(result, "2713"); equal(result, "2713");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -570,10 +611,10 @@ ...@@ -570,10 +611,10 @@
type: "mappingstorage2713" type: "mappingstorage2713"
}, },
mapping_dict: {"id": {"equal": "otherId"}}, mapping_dict: {"id": {"equal": "otherId"}},
attachment_mapping_dict: {"2713": {"put": attachment_mapping_dict: {
{"uri_template": "www.2713.foo/{id}"}}} "2713": {"put": {"uri_template": "www.2713.foo/{id}"}}
}), }
blob = new Blob([""]); }), blob = new Blob([""]);
Storage2713.prototype.putAttachment = function (id, Storage2713.prototype.putAttachment = function (id,
attachment_id, attachment) { attachment_id, attachment) {
...@@ -592,13 +633,15 @@ ...@@ -592,13 +633,15 @@
return [{"id": "13"}]; return [{"id": "13"}];
}; };
start();
jio.putAttachment("42", "2713", blob) jio.putAttachment("42", "2713", blob)
.push(function (result) { .push(function (result) {
equal(result, "2713"); equal(result, "2713");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -606,7 +649,6 @@ ...@@ -606,7 +649,6 @@
// mappingStorage.getAttachment // mappingStorage.getAttachment
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("mappingStorage.getAttachment"); module("mappingStorage.getAttachment");
test("getAttachment use sub_storage one's", function () { test("getAttachment use sub_storage one's", function () {
stop(); stop();
expect(3); expect(3);
...@@ -615,27 +657,30 @@ ...@@ -615,27 +657,30 @@
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
} }
}), }), blob = new Blob([""]);
blob = new Blob([""]);
Storage2713.prototype.getAttachment = function (doc_id, attachment) { Storage2713.prototype.getAttachment = function (doc_id, attachment) {
equal(doc_id, "42", "getAttachment 2713 called"); equal(doc_id, "42", "getAttachment 2713 called");
equal(attachment, "2713", "getAttachment 2713 called"); equal(attachment, "2713", "getAttachment 2713 called");
return blob; return blob;
}; };
start();
jio.getAttachment("42", "2713") jio.getAttachment("42", "2713")
.push(function (result) { .push(function (result) {
deepEqual(result, blob); deepEqual(result, blob);
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
test("getAttachment using UriTemplate", function () { test("getAttachment using UriTemplate", function () {
stop(); stop();
expect(3); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: { sub_storage: {
...@@ -644,37 +689,42 @@ ...@@ -644,37 +689,42 @@
attachment_mapping_dict: { attachment_mapping_dict: {
"2713": {"get": {"uri_template": "www.2713/{id}/ok.com"}} "2713": {"get": {"uri_template": "www.2713/{id}/ok.com"}}
} }
}), }), blob = new Blob([""]);
blob = new Blob([""]);
Storage2713.prototype.getAttachment = function (doc_id, attachment) { Storage2713.prototype.getAttachment = function (doc_id, attachment) {
equal(attachment, "www.2713/42/ok.com", "getAttachment 2713 called"); equal(attachment, "www.2713/42/ok.com", "getAttachment 2713 called");
equal(doc_id, "42", "getAttachment 2713 called"); equal(doc_id, "42", "getAttachment 2713 called");
return blob; return blob;
}; };
start();
jio.getAttachment("42", "2713") jio.getAttachment("42", "2713")
.push(function (result) { .push(function (result) {
deepEqual(result, blob); deepEqual(result, blob);
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
test("getAttachment with UriTemplate and id mapped", function () { test("getAttachment with UriTemplate and id mapped", function () {
stop(); stop();
expect(4); expect(4);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, },
mapping_dict: {"id": {"equal": "otherId"}}, mapping_dict: {
attachment_mapping_dict: {"2713": {"get": "id": {"equal": "otherId"}
{"uri_template": "www.2713.foo/{id}"}}} },
}), attachment_mapping_dict: {
blob = new Blob([""]); "2713": {"get": {"uri_template": "www.2713.foo/{id}"}}
}
}), blob = new Blob([""]);
Storage2713.prototype.getAttachment = function (id, Storage2713.prototype.getAttachment = function (id,
attachment_id) { attachment_id) {
...@@ -692,13 +742,15 @@ ...@@ -692,13 +742,15 @@
return [{"id": "13"}]; return [{"id": "13"}];
}; };
start();
jio.getAttachment("42", "2713") jio.getAttachment("42", "2713")
.push(function (result) { .push(function (result) {
deepEqual(result, blob); deepEqual(result, blob);
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -706,7 +758,6 @@ ...@@ -706,7 +758,6 @@
// mappingStorage.removeAttachment // mappingStorage.removeAttachment
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("mappingStorage.removeAttachment"); module("mappingStorage.removeAttachment");
test("removeAttachment use sub_storage one's", function () { test("removeAttachment use sub_storage one's", function () {
stop(); stop();
expect(3); expect(3);
...@@ -724,13 +775,15 @@ ...@@ -724,13 +775,15 @@
return doc_id; return doc_id;
}; };
start();
jio.removeAttachment("42", "2713") jio.removeAttachment("42", "2713")
.push(function (result) { .push(function (result) {
deepEqual(result, "2713"); deepEqual(result, "2713");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -743,8 +796,9 @@ ...@@ -743,8 +796,9 @@
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, },
attachment_mapping_dict: {"2713": attachment_mapping_dict: {
{"remove": {"uri_template": "www.2713/{id}.bar"}}} "2713": {"remove": {"uri_template": "www.2713/{id}.bar"}}
}
}); });
Storage2713.prototype.removeAttachment = function (doc_id, attachment) { Storage2713.prototype.removeAttachment = function (doc_id, attachment) {
...@@ -753,13 +807,15 @@ ...@@ -753,13 +807,15 @@
return doc_id; return doc_id;
}; };
start();
jio.removeAttachment("42", "2713") jio.removeAttachment("42", "2713")
.push(function (result) { .push(function (result) {
deepEqual(result, "2713"); deepEqual(result, "2713");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -773,8 +829,9 @@ ...@@ -773,8 +829,9 @@
type: "mappingstorage2713" type: "mappingstorage2713"
}, },
mapping_dict: {"id": {"equal": "otherId"}}, mapping_dict: {"id": {"equal": "otherId"}},
attachment_mapping_dict: {"2713": {"remove": attachment_mapping_dict: {
{"uri_template": "www.2713.foo/{id}"}}} "2713": {"remove": {"uri_template": "www.2713.foo/{id}"}}
}
}); });
Storage2713.prototype.removeAttachment = function (id, Storage2713.prototype.removeAttachment = function (id,
...@@ -793,13 +850,15 @@ ...@@ -793,13 +850,15 @@
return [{"id": "13"}]; return [{"id": "13"}];
}; };
start();
jio.removeAttachment("42", "2713") jio.removeAttachment("42", "2713")
.push(function (result) { .push(function (result) {
equal(result, "2713"); equal(result, "2713");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -825,12 +884,7 @@ ...@@ -825,12 +884,7 @@
map_all_property: true map_all_property: true
}); });
start(); jio.put("42", {"title": "foo", "smth": "bar"})
jio.put("42",
{
"title": "foo",
"smth": "bar"
})
.push(function () { .push(function () {
return jio.allDocs({ return jio.allDocs({
query: '(title: "foo") AND (smth: "bar")', query: '(title: "foo") AND (smth: "bar")',
...@@ -858,6 +912,9 @@ ...@@ -858,6 +912,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -883,7 +940,6 @@ ...@@ -883,7 +940,6 @@
} }
}); });
start();
jio.put("42", jio.put("42",
{ {
"title": "foo", "title": "foo",
...@@ -916,6 +972,9 @@ ...@@ -916,6 +972,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -940,7 +999,6 @@ ...@@ -940,7 +999,6 @@
} }
}); });
start();
jio.put("42", jio.put("42",
{ {
"title": "foo", "title": "foo",
...@@ -966,6 +1024,9 @@ ...@@ -966,6 +1024,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -991,7 +1052,6 @@ ...@@ -991,7 +1052,6 @@
map_all_property: true map_all_property: true
}); });
start();
jio.put("42", jio.put("42",
{ {
"title": "foo", "title": "foo",
...@@ -1020,6 +1080,9 @@ ...@@ -1020,6 +1080,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -1045,7 +1108,6 @@ ...@@ -1045,7 +1108,6 @@
} }
}); });
start();
jio.put("42", jio.put("42",
{ {
"title": "foo", "title": "foo",
...@@ -1074,6 +1136,9 @@ ...@@ -1074,6 +1136,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -1136,7 +1201,6 @@ ...@@ -1136,7 +1201,6 @@
]; ];
}; };
start();
jio.bulk([{ jio.bulk([{
method: "get", method: "get",
parameter_list: ["id1"] parameter_list: ["id1"]
...@@ -1161,6 +1225,9 @@ ...@@ -1161,6 +1225,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -1186,13 +1253,15 @@ ...@@ -1186,13 +1253,15 @@
return "foobar"; return "foobar";
}; };
start();
jio.repair(["foo", "bar"]) jio.repair(["foo", "bar"])
.push(function (result) { .push(function (result) {
equal(result, "foobar", "Check repair"); equal(result, "foobar", "Check repair");
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
......
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
<script src="queries/key-jiodate.tests.js"></script> <script src="queries/key-jiodate.tests.js"></script>
<!--script src="queries/key-localstorage.tests.js"></script--> <!--script src="queries/key-localstorage.tests.js"></script-->
<script src="jio.storage/memorystorage.tests.js"></script> <script src="jio.storage/memorystorage.tests.js"></script>
<script src="jio.storage/querystorage.tests.js"></script> <script src="jio.storage/querystorage.tests.js"></script>
<script src="jio.storage/localstorage.tests.js"></script> <script src="jio.storage/localstorage.tests.js"></script>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment