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