Commit 5afa525b authored by Vincent Bechu's avatar Vincent Bechu Committed by Aurel

mappingstorage: change configuration dict

parent a7d9f005
/*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 initializeQueryAndDefaultMapping(storage) {
this._mapping_dict = spec.mapping_dict || {};
this._sub_storage = jIO.createJIO(spec.sub_storage);
this._map_all_property = spec.map_all_property !== undefined ?
spec.map_all_property : true;
this._attachment_mapping_dict = spec.attachment_mapping_dict || {};
this._query = spec.query || {};
if (this._query.query !== undefined) {
this._query.query = QueryFactory.create(this._query.query);
}
this._default_mapping = {};
this._id_is_mapped = (this._mapping_dict.id !== undefined
&& this._mapping_dict.id.equal !== "id");
var property, query_list = []; var property, query_list = [];
for (property in storage._mapping_dict) {
// handle default_value. if (storage._mapping_dict.hasOwnProperty(property)) {
for (property in this._mapping_dict) { if (storage._mapping_dict[property][0] === "equalValue") {
if (this._mapping_dict.hasOwnProperty(property)) { if (storage._mapping_dict[property][1] === undefined) {
if (this._mapping_dict[property].default_value !== undefined) { throw new jIO.util.jIOError("equalValue has not parameter", 400);
this._default_mapping[property] = }
this._mapping_dict[property].default_value; storage._default_mapping[property] =
storage._mapping_dict[property][1];
query_list.push(new SimpleQuery({ query_list.push(new SimpleQuery({
key: property, key: property,
value: this._mapping_dict[property].default_value, value: storage._mapping_dict[property][1],
type: "simple" type: "simple"
})); }));
} }
if (storage._mapping_dict[property][0] === "equalSubId") {
if (storage._property_for_sub_id !== undefined) {
throw new jIO.util.jIOError(
"equalSubId can be defined one time",
400
);
}
storage._property_for_sub_id = property;
}
} }
} }
if (storage._query.query !== undefined) {
if (this._query.query !== undefined) { query_list.push(QueryFactory.create(storage._query.query));
query_list.push(QueryFactory.create(this._query.query));
} }
if (query_list.length > 1) { if (query_list.length > 1) {
this._query.query = new ComplexQuery({ storage._query.query = new ComplexQuery({
type: "complex", type: "complex",
query_list: query_list, query_list: query_list,
operator: "AND" operator: "AND"
}); });
} else if (query_list.length === 1) { } else if (query_list.length === 1) {
this._query.query = query_list[0]; storage._query.query = query_list[0];
} }
} }
function MappingStorage(spec) {
this._mapping_dict = spec.mapping_dict || {};
this._sub_storage = jIO.createJIO(spec.sub_storage);
this._map_all_property = spec.map_all_property !== undefined ?
spec.map_all_property : true;
this._attachment_mapping_dict = spec.attachment_mapping_dict || {};
this._query = spec.query || {};
this._map_id = spec.map_id;
this._id_mapped = (spec.map_id !== undefined) ? spec.map_id[1] : false;
if (this._query.query !== undefined) {
this._query.query = QueryFactory.create(this._query.query);
}
this._default_mapping = {};
initializeQueryAndDefaultMapping(this);
}
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() if (mapping_dict !== undefined
.push(function () { && mapping_dict[attachment_id] !== undefined
if (mapping_dict !== undefined && mapping_dict[attachment_id][method] !== undefined
&& mapping_dict[attachment_id] !== undefined && mapping_dict[attachment_id][method].uri_template !== undefined) {
&& mapping_dict[attachment_id][method].uri_template !== undefined) { return UriTemplate.parse(
return UriTemplate.parse( mapping_dict[attachment_id][method].uri_template
mapping_dict[attachment_id][method].uri_template ).expand({id: sub_id});
).expand({id: sub_id}); }
} return attachment_id;
return attachment_id;
});
} }
function getSubStorageId(storage, id) { function getSubStorageId(storage, id, doc) {
var query; var query;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
if (!storage._id_is_mapped || id === undefined) { if (storage._property_for_sub_id !== undefined &&
doc !== undefined &&
doc[storage._property_for_sub_id] !== undefined) {
return doc[storage._property_for_sub_id];
}
if (!storage._id_mapped) {
return id; return id;
} }
if (storage._mapping_dict.id.equal !== undefined) { if (storage._map_id[0] === "equalSubProperty") {
query = new SimpleQuery({ query = new SimpleQuery({
key: storage._mapping_dict.id.equal, key: storage._map_id[1],
value: id, value: id,
type: "simple" type: "simple"
}); });
...@@ -94,7 +111,10 @@ ...@@ -94,7 +111,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
...@@ -111,15 +131,24 @@ ...@@ -111,15 +131,24 @@
} }
function mapToSubProperty(storage, property, sub_doc, doc) { function mapToSubProperty(storage, property, sub_doc, doc) {
var mapping_function, parameter;
if (storage._mapping_dict[property] !== undefined) { if (storage._mapping_dict[property] !== undefined) {
if (storage._mapping_dict[property].equal !== undefined) { mapping_function = storage._mapping_dict[property][0];
sub_doc[storage._mapping_dict[property].equal] = doc[property]; parameter = storage._mapping_dict[property][1];
return storage._mapping_dict[property].equal; if (mapping_function === "equalSubProperty") {
sub_doc[parameter] = doc[property];
return parameter;
} }
if (storage._mapping_dict[property].default_value !== undefined) { if (mapping_function === "equalValue") {
sub_doc[property] = storage._mapping_dict[property].default_value; sub_doc[property] = parameter;
return property; return property;
} }
if (mapping_function === "ignore" || mapping_function === "equalSubId") {
return false;
}
}
if (!storage._map_all_property) {
return false;
} }
if (storage._map_all_property) { if (storage._map_all_property) {
sub_doc[property] = doc[property]; sub_doc[property] = doc[property];
...@@ -132,14 +161,20 @@ ...@@ -132,14 +161,20 @@
} }
function mapToMainProperty(storage, property, sub_doc, doc) { function mapToMainProperty(storage, property, sub_doc, doc) {
var mapping_function, parameter;
if (storage._mapping_dict[property] !== undefined) { if (storage._mapping_dict[property] !== undefined) {
if (storage._mapping_dict[property].equal !== undefined) { mapping_function = storage._mapping_dict[property][0];
if (sub_doc.hasOwnProperty(storage._mapping_dict[property].equal)) { parameter = storage._mapping_dict[property][1];
doc[property] = sub_doc[storage._mapping_dict[property].equal]; if (mapping_function === "equalSubProperty") {
if (sub_doc.hasOwnProperty(parameter)) {
doc[property] = sub_doc[parameter];
} }
return storage._mapping_dict[property].equal; return parameter;
}
if (mapping_function === "equalValue") {
return property;
} }
if (storage._mapping_dict[property].default_value !== undefined) { if (mapping_function === "ignore") {
return property; return property;
} }
} }
...@@ -152,10 +187,10 @@ ...@@ -152,10 +187,10 @@
return false; return false;
} }
function mapToMainDocument(storage, sub_doc, delete_id_from_doc) { function mapToMainDocument(storage, sub_doc, sub_id) {
var doc = {}, var doc = {},
property, property,
property_list = []; property_list = [storage._id_mapped];
for (property in storage._mapping_dict) { for (property in storage._mapping_dict) {
if (storage._mapping_dict.hasOwnProperty(property)) { if (storage._mapping_dict.hasOwnProperty(property)) {
property_list.push(mapToMainProperty(storage, property, sub_doc, doc)); property_list.push(mapToMainProperty(storage, property, sub_doc, doc));
...@@ -170,13 +205,14 @@ ...@@ -170,13 +205,14 @@
} }
} }
} }
if (delete_id_from_doc) { if (storage._property_for_sub_id !== undefined &&
delete doc.id; sub_id !== undefined) {
doc[storage._property_for_sub_id] = sub_id;
} }
return doc; return doc;
} }
function mapToSubstorageDocument(storage, doc) { function mapToSubstorageDocument(storage, doc, id) {
var sub_doc = {}, property; var sub_doc = {}, property;
for (property in doc) { for (property in doc) {
...@@ -189,28 +225,46 @@ ...@@ -189,28 +225,46 @@
sub_doc[property] = storage._default_mapping[property]; sub_doc[property] = storage._default_mapping[property];
} }
} }
delete sub_doc.id; if (storage._id_mapped && id !== undefined) {
sub_doc[storage._id_mapped] = id;
}
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)
.push(function (sub_id) { .push(function (sub_id) {
return context._sub_storage.get(sub_id); return context._sub_storage.get(sub_id)
}) .push(function (sub_doc) {
.push(function (sub_doc) { return mapToMainDocument(context, sub_doc, sub_id);
return mapToMainDocument(context, sub_doc, true); });
})
.push(undefined, function (error) {
throw new jIO.util.jIOError("Cannot find document " + id
+ ", cause: " + error.message, 404);
}); });
}; };
MappingStorage.prototype.post = function (doc) { MappingStorage.prototype.post = function (doc) {
if (!this._id_is_mapped) { if (!this._id_is_mapped) {
return this._sub_storage.post(mapToSubstorageDocument(this, doc)); return this._sub_storage.post(mapToSubstorageDocument(
this,
doc
));
} }
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
"post is not supported with id mapped", "post is not supported with id mapped",
...@@ -220,19 +274,16 @@ ...@@ -220,19 +274,16 @@
MappingStorage.prototype.put = function (id, doc) { MappingStorage.prototype.put = function (id, doc) {
var context = this, var context = this,
sub_doc = mapToSubstorageDocument(this, doc); sub_doc = mapToSubstorageDocument(this, doc, id);
return getSubStorageId(this, id) return getSubStorageId(this, id, doc)
.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) {
return context._sub_storage.post(sub_doc); if (error instanceof jIO.util.jIOError && error.status_code === 404) {
return context._sub_storage.post(sub_doc);
}
throw error;
}) })
.push(function () { .push(function () {
return id; return id;
...@@ -251,55 +302,55 @@ ...@@ -251,55 +302,55 @@
}; };
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;
}); });
}; };
MappingStorage.prototype.hasCapacity = function () { MappingStorage.prototype.allAttachments = function (id) {
return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments); var context = this, sub_id;
return getSubStorageId(context, id)
.push(function (sub_id_result) {
sub_id = sub_id_result;
return context._sub_storage.allAttachments(sub_id);
})
.push(function (result) {
var attachment_id,
attachments = {},
mapping_dict = {};
for (attachment_id in context._attachment_mapping_dict) {
if (context._attachment_mapping_dict.hasOwnProperty(attachment_id)) {
mapping_dict[getAttachmentId(context, sub_id, attachment_id, "get")]
= attachment_id;
}
}
for (attachment_id in result) {
if (result.hasOwnProperty(attachment_id)) {
if (mapping_dict.hasOwnProperty(attachment_id)) {
attachments[mapping_dict[attachment_id]] = {};
} else {
attachments[attachment_id] = {};
}
}
}
return attachments;
});
};
MappingStorage.prototype.hasCapacity = function (name) {
return this._sub_storage.hasCapacity(name);
}; };
MappingStorage.prototype.repair = function () { MappingStorage.prototype.repair = function () {
...@@ -307,26 +358,30 @@ ...@@ -307,26 +358,30 @@
}; };
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]
));
} }
return mapped_result; return mapped_result;
}); });
...@@ -341,31 +396,38 @@ ...@@ -341,31 +396,38 @@
sort_on = []; sort_on = [];
function mapQuery(one_query) { function mapQuery(one_query) {
var i, query_list = []; var j, query_list = [], key, sub_query;
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])); sub_query = mapQuery(one_query.query_list[j]);
if (sub_query) {
query_list.push(sub_query);
}
} }
one_query.query_list = query_list; one_query.query_list = query_list;
return one_query; return one_query;
} }
one_query.key = mapToMainProperty(context, one_query.key, {}, {}); key = mapToMainProperty(context, one_query.key, {}, {});
return one_query; if (key) {
one_query.key = key;
return one_query;
}
return false;
} }
if (option.sort_on !== undefined) { if (option.sort_on !== undefined) {
for (i = 0; i < option.sort_on.length; i += 1) { for (i = 0; i < option.sort_on.length; i += 1) {
property = mapToMainProperty(this, option.sort_on[i][0], {}, {}); property = mapToMainProperty(this, option.sort_on[i][0], {}, {});
if (property && sort_on.indexOf(property) < 0) { if (property && sort_on.indexOf(property) < 0) {
select_list.push([property, option.sort_on[i][1]]); sort_on.push([property, option.sort_on[i][1]]);
} }
} }
} }
if (this._query.sort_on !== undefined) { if (this._query.sort_on !== undefined) {
for (i = 0; i < this._query.sort_on.length; i += 1) { for (i = 0; i < this._query.sort_on.length; i += 1) {
property = this._query.sort_on[i]; property = mapToMainProperty(this, this._query.sort_on[i], {}, {});
if (sort_on.indexOf(property) < 0) { if (sort_on.indexOf(property) < 0) {
sort_on.push(property); sort_on.push([property, option.sort_on[i][1]]);
} }
} }
} }
...@@ -385,8 +447,9 @@ ...@@ -385,8 +447,9 @@
} }
} }
} }
if (this._id_is_mapped) { if (this._id_mapped) {
select_list.push(this._mapping_dict.id.equal); // modify here for future way to map id
select_list.push(this._id_mapped);
} }
if (option.query !== undefined) { if (option.query !== undefined) {
query = mapQuery(QueryFactory.create(option.query)); query = mapQuery(QueryFactory.create(option.query));
...@@ -415,13 +478,16 @@ ...@@ -415,13 +478,16 @@
} }
) )
.push(function (result) { .push(function (result) {
var doc;
for (i = 0; i < result.data.total_rows; i += 1) { for (i = 0; i < result.data.total_rows; i += 1) {
doc = result.data.rows[i].value;
result.data.rows[i].value = result.data.rows[i].value =
mapToMainDocument(context, result.data.rows[i].value, false); mapToMainDocument(
if (result.data.rows[i].id !== undefined && context._id_is_mapped) { context,
result.data.rows[i].id = doc
result.data.rows[i].value.id; );
delete result.data.rows[i].value.id; if (context._id_mapped) {
result.data.rows[i].id = doc[context._id_mapped];
} }
} }
return result.data.rows; return result.data.rows;
...@@ -429,4 +495,4 @@ ...@@ -429,4 +495,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,47 +33,54 @@ ...@@ -34,47 +33,54 @@
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._mapping_dict_attachment, {}); 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 () {
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict: { "bar": {"equal": "foo"}},
map_all_property: false, map_all_property: false,
query: {"query": 'foo: "bar"'}, query: {"query": 'foo: "bar"'},
mapping_dict_attachment: {"foo": {"get": "bar"}} attachment_mapping_dict: {"foo": {"get": "bar"}},
mapping_dict: { "bar": ["equalSubProperty", "foo"]},
map_id: ["equalSubProperty", "otherId"],
sub_storage: {
type: "mappingstorage2713"
}
}); });
deepEqual(jio.__storage._mapping_dict, {"bar": {"equal": "foo"}}); deepEqual(
jio.__storage._mapping_dict,
{"bar": ["equalSubProperty", "foo"]}
);
deepEqual(jio.__storage._map_id, ["equalSubProperty", "otherId"]);
equal(jio.__storage._query.query.key, "foo"); equal(jio.__storage._query.query.key, "foo");
equal(jio.__storage._query.query.value, "bar"); equal(jio.__storage._query.query.value, "bar");
equal(jio.__storage._query.query.type, "simple"); equal(jio.__storage._query.query.type, "simple");
deepEqual(jio.__storage._mapping_dict_attachment, {"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);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
mapping_dict: {"title": ["equalSubProperty", "title"]},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
mapping_dict: {"title": {"equal": "title"}}
}); });
Storage2713.prototype.get = function (id) { Storage2713.prototype.get = function (id) {
...@@ -82,7 +88,6 @@ ...@@ -82,7 +88,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,34 +96,39 @@ ...@@ -91,34 +96,39 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
test("get with props mapped", function () { test("get with id mapped", function () {
stop(); stop();
expect(2); expect(2);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
mapping_dict: {"title": ["equalSubId"]},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
mapping_dict: {"title": {"equal": "otherTitle"}}
}); });
Storage2713.prototype.get = function (id) { Storage2713.prototype.get = function (id) {
equal(id, "bar", "get 2713 called"); equal(id, "bar", "get 2713 called");
return {otherTitle: "foo"}; return {};
}; };
start();
jio.get("bar") jio.get("bar")
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
"title": "foo" "title": "bar"
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -128,12 +138,12 @@ ...@@ -128,12 +138,12 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
map_id: ["equalSubProperty", "otherId"],
mapping_dict: {
"title": ["equalSubProperty", "otherTitle"]
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
},
mapping_dict: {
"title": {"equal": "otherTitle"},
"id": {"equal": "otherId"}
} }
}); });
...@@ -151,7 +161,6 @@ ...@@ -151,7 +161,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 +168,9 @@ ...@@ -159,6 +168,9 @@
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -168,14 +180,14 @@ ...@@ -168,14 +180,14 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
sub_storage: { query: {"query": 'otherTitle: "foo"'},
type: "mappingstorage2713" map_id: ["equalSubProperty", "otherId"],
},
mapping_dict: { mapping_dict: {
"title": {"equal": "otherTitle"}, "title": ["equalSubProperty", "otherTitle"]
"id": {"equal": "otherId"}
}, },
query: {"query": 'otherTitle: "foo"'} sub_storage: {
type: "mappingstorage2713"
}
}); });
Storage2713.prototype.hasCapacity = function () { Storage2713.prototype.hasCapacity = function () {
...@@ -196,7 +208,6 @@ ...@@ -196,7 +208,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,20 +215,25 @@ ...@@ -204,20 +215,25 @@
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
test("get with map_all_property", function () { test("get with not map_all_property", function () {
stop(); stop();
expect(3); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
map_all_property: false,
map_id: ["equalSubProperty", "otherId"],
mapping_dict: {
"title": ["equalSubProperty", "otherTitle"]
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
},
mapping_dict: {
"id": {"equal": "otherId"}
} }
}); });
...@@ -236,10 +252,9 @@ ...@@ -236,10 +252,9 @@
Storage2713.prototype.get = function (id) { Storage2713.prototype.get = function (id) {
equal(id, "2713", "get 2713 called"); equal(id, "2713", "get 2713 called");
return {"title": "foo"}; return {"otherTitle": "foo", "foo": "bar"};
}; };
start();
jio.get("42") jio.get("42")
.push(function (result) { .push(function (result) {
deepEqual(result, { deepEqual(result, {
...@@ -247,6 +262,73 @@ ...@@ -247,6 +262,73 @@
}); });
}).push(undefined, function (error) { }).push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
});
});
test("get with props equal", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
mapping_dict: {
"title": ["equalSubProperty", "otherTitle"]
},
sub_storage: {
type: "mappingstorage2713"
}
});
Storage2713.prototype.get = function (id) {
equal(id, "bar", "get 2713 called");
return {otherTitle: "foo"};
};
jio.get("bar")
.push(function (result) {
deepEqual(result, {
"title": "foo"
});
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get with ignore", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
mapping_dict: {
"title": ["ignore"]
},
sub_storage: {
type: "mappingstorage2713"
}
});
Storage2713.prototype.get = function (id) {
equal(id, "bar", "get 2713 called");
return {"title": "foo", "foo": "bar"};
};
jio.get("bar")
.push(function (result) {
deepEqual(result, {
"foo": "bar"
});
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -261,10 +343,12 @@ ...@@ -261,10 +343,12 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
mapping_dict: {
"title": ["equalSubProperty", "title"]
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
mapping_dict: {"title": {"equal": "title"}}
}); });
Storage2713.prototype.put = function (id, param) { Storage2713.prototype.put = function (id, param) {
...@@ -273,25 +357,28 @@ ...@@ -273,25 +357,28 @@
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",
mapping_dict: {"title": ["equalValue", "foobar"]},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
mapping_dict: {"title": {"default_value": "foobar"}}
}); });
Storage2713.prototype.put = function (id, param) { Storage2713.prototype.put = function (id, param) {
...@@ -300,27 +387,30 @@ ...@@ -300,27 +387,30 @@
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",
map_id: ["equalSubProperty", "otherId"],
mapping_dict: {
"title": ["equalSubProperty", "otherTitle"]
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
},
mapping_dict: {
"title": {"equal": "otherTitle"},
"id": {"equal": "otherId"}
} }
}); });
...@@ -330,53 +420,92 @@ ...@@ -330,53 +420,92 @@
return "bar"; return "bar";
}; };
Storage2713.prototype.hasCapacity = function () {
return true;
};
Storage2713.prototype.buildQuery = function (option) { Storage2713.prototype.buildQuery = function (option) {
equal(option.query, 'otherId: "42"', "allDocs 2713 called"); equal(option.query, 'otherId: "42"', "allDocs 2713 called");
return []; return [];
}; };
start(); Storage2713.prototype.hasCapacity = function () {
return true;
};
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 id mapped", function () {
stop(); stop();
expect(3); expect(3);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
map_id: ["equalSubProperty", "otherId"],
mapping_dict: {
"title": ["equalSubId"]
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
});
Storage2713.prototype.put = function (id, doc) {
deepEqual(doc,
{"otherId": "42"}, "post 2713 called");
equal(id, "bar");
return "bar";
};
jio.put("42", {"title": "bar"})
.push(function (result) {
equal(result, "42");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("put with no map_all_property", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
map_all_property: false,
mapping_dict: { mapping_dict: {
"id": {"equal": "id"} "title": ["equalSubProperty", "title"]
}, },
map_all_property: true sub_storage: {
type: "mappingstorage2713"
}
}); });
Storage2713.prototype.put = function (id, doc) { Storage2713.prototype.put = function (id, doc) {
deepEqual(doc, deepEqual(doc,
{"title": "foo", "smth": "bar", "smth2": "bar2"}, "post 2713 called"); {"title": "foo"}, "post 2713 called");
equal(id, "42", "put 2713 called"); equal(id, "42", "put 2713 called");
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 +517,28 @@ ...@@ -388,24 +517,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();
}); });
}); });
...@@ -415,11 +548,9 @@ ...@@ -415,11 +548,9 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
map_id: ["equalSubProperty", "otherId"],
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
},
mapping_dict: {
"id": {"equal": "otherId"}
} }
}); });
...@@ -437,12 +568,14 @@ ...@@ -437,12 +568,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,48 +587,57 @@ ...@@ -454,48 +587,57 @@
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",
mapping_dict: {
"title": ["equalSubProperty", "otherTitle"]
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
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",
map_id: ["equalSubProperty", "otherId"],
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
mapping_dict: {"id": {"equal": "otherId"}}
}); });
Storage2713.prototype.post = function () { Storage2713.prototype.post = function () {
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 +653,8 @@ ...@@ -511,8 +653,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 +663,32 @@ ...@@ -521,28 +663,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",
attachment_mapping_dict: {
"2713": {"put": {"uri_template": "www.2713.foo/{id}"}}
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
mapping_dict_attachment: {"2713": {"put": }), blob = new Blob([""]);
{"uri_template": "www.2713.foo/{id}"}}}
}),
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 +697,15 @@ ...@@ -551,13 +697,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();
}); });
}); });
...@@ -566,14 +714,14 @@ ...@@ -566,14 +714,14 @@
expect(5); expect(5);
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
map_id: ["equalSubProperty", "otherId"],
attachment_mapping_dict: {
"2713": {"put": {"uri_template": "www.2713.foo/{id}"}}
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
mapping_dict: {"id": {"equal": "otherId"}}, }), blob = new Blob([""]);
mapping_dict_attachment: {"2713": {"put":
{"uri_template": "www.2713.foo/{id}"}}}
}),
blob = new Blob([""]);
Storage2713.prototype.putAttachment = function (id, Storage2713.prototype.putAttachment = function (id,
attachment_id, attachment) { attachment_id, attachment) {
...@@ -592,13 +740,15 @@ ...@@ -592,13 +740,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 +756,6 @@ ...@@ -606,7 +756,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,66 +764,72 @@ ...@@ -615,66 +764,72 @@
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",
attachment_mapping_dict: {
"2713": {"get": {"uri_template": "www.2713/{id}/ok.com"}}
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
},
mapping_dict_attachment: {
"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",
map_id: ["equalSubProperty", "otherId"],
attachment_mapping_dict: {
"2713": {"get": {"uri_template": "www.2713.foo/{id}"}}
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
mapping_dict: {"id": {"equal": "otherId"}}, }), blob = new Blob([""]);
mapping_dict_attachment: {"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 +847,15 @@ ...@@ -692,13 +847,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 +863,6 @@ ...@@ -706,7 +863,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 +880,15 @@ ...@@ -724,13 +880,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();
}); });
}); });
...@@ -740,11 +898,12 @@ ...@@ -740,11 +898,12 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
attachment_mapping_dict: {
"2713": {"remove": {"uri_template": "www.2713/{id}.bar"}}
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
mapping_dict_attachment: {"2713":
{"remove": {"uri_template": "www.2713/{id}.bar"}}}
}); });
Storage2713.prototype.removeAttachment = function (doc_id, attachment) { Storage2713.prototype.removeAttachment = function (doc_id, attachment) {
...@@ -753,13 +912,15 @@ ...@@ -753,13 +912,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();
}); });
}); });
...@@ -769,12 +930,13 @@ ...@@ -769,12 +930,13 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
map_id: ["equalSubProperty", "otherId"],
attachment_mapping_dict: {
"2713": {"remove": {"uri_template": "www.2713.foo/{id}"}}
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, }
mapping_dict: {"id": {"equal": "otherId"}},
mapping_dict_attachment: {"2713": {"remove":
{"uri_template": "www.2713.foo/{id}"}}}
}); });
Storage2713.prototype.removeAttachment = function (id, Storage2713.prototype.removeAttachment = function (id,
...@@ -793,13 +955,78 @@ ...@@ -793,13 +955,78 @@
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();
});
});
/////////////////////////////////////////////////////////////////
// mappingStorage.allAttachments
/////////////////////////////////////////////////////////////////
module("mappingStorage.allAttachments");
test("allAttachments use sub_storage one's", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
}
});
Storage2713.prototype.allAttachments = function (doc_id) {
equal(doc_id, "42", "allAttachments 2713 called");
return {};
};
jio.allAttachments("42")
.push(function (result) {
deepEqual(result, {});
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("allAttachments use UriTemplate", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
attachment_mapping_dict: {
"2713": {"get": {"uri_template": "www.2713.bar"}}
},
sub_storage: {
type: "mappingstorage2713"
}
});
Storage2713.prototype.allAttachments = function (doc_id) {
equal(doc_id, "42", "allAttachments 2713 called");
return {"www.2713.bar": {}};
};
jio.allAttachments("42")
.push(function (result) {
deepEqual(result, {"2713": {}});
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -821,17 +1048,11 @@ ...@@ -821,17 +1048,11 @@
type: "memory" type: "memory"
} }
} }
}, }
map_all_property: true
}); });
start(); jio.put("42", {"title": "foo", "smth": "bar"})
jio.put("42", .push(function () {
{
"title": "foo",
"smth": "bar"
})
.push(function () {
return jio.allDocs({ return jio.allDocs({
query: '(title: "foo") AND (smth: "bar")', query: '(title: "foo") AND (smth: "bar")',
select_list: ["title", "smth"], select_list: ["title", "smth"],
...@@ -858,6 +1079,9 @@ ...@@ -858,6 +1079,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -867,6 +1091,11 @@ ...@@ -867,6 +1091,11 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
map_id: ["equalSubProperty", "otherId"],
mapping_dict: {
"title": ["equalSubProperty", "otherTitle"],
"smth": ["equalSubProperty", "otherSmth"]
},
sub_storage: { sub_storage: {
type: "query", type: "query",
sub_storage: { sub_storage: {
...@@ -875,28 +1104,31 @@ ...@@ -875,28 +1104,31 @@
type: "memory" type: "memory"
} }
} }
},
mapping_dict: {
"id": {"equal": "otherId"},
"title": {"equal": "otherTitle"},
"smth": {"equal": "otherSmth"}
} }
}); });
start();
jio.put("42", jio.put("42",
{ {
"title": "foo", "title": "foo",
"smth": "bar" "smth": "bar"
}) })
.push(function () { .push(function () {
jio.put(
"2713",
{
"title": "bar",
"smth": "foo"
}
);
})
.push(function () {
return jio.allDocs({ return jio.allDocs({
query: '(title: "foo") AND (smth: "bar")', query: '(title: "foo") OR (title: "bar")',
select_list: ["title", "smth"], select_list: ["title", "smth"],
sort_on: [["title", "descending"]] sort_on: [["title", "descending"]]
}); });
}) })
.push(function (result) { .push(function (result) {
deepEqual(result, deepEqual(result,
{ {
"data": { "data": {
...@@ -908,6 +1140,14 @@ ...@@ -908,6 +1140,14 @@
"smth": "bar" "smth": "bar"
}, },
"doc": {} "doc": {}
},
{
"id": "2713",
"value": {
"title": "bar",
"smth": "foo"
},
"doc": {}
} }
], ],
"total_rows": 1 "total_rows": 1
...@@ -916,6 +1156,9 @@ ...@@ -916,6 +1156,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -925,6 +1168,10 @@ ...@@ -925,6 +1168,10 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
map_id: ["equalSubProperty", "otherId"],
mapping_dict: {
"title": ["equalSubProperty", "otherTitle"]
},
sub_storage: { sub_storage: {
type: "query", type: "query",
sub_storage: { sub_storage: {
...@@ -933,23 +1180,18 @@ ...@@ -933,23 +1180,18 @@
type: "memory" type: "memory"
} }
} }
},
mapping_dict: {
"id": {"equal": "otherId"},
"title": {"equal": "otherTitle"}
} }
}); });
start();
jio.put("42", jio.put("42",
{ {
"title": "foo", "title": "foo",
"smth": "bar" "smth": "bar"
}) })
.push(function () { .push(function () {
return jio.allDocs(); return jio.allDocs();
}) })
.push(function (result) { .push(function (result) {
deepEqual(result, deepEqual(result,
{ {
"data": { "data": {
...@@ -966,6 +1208,9 @@ ...@@ -966,6 +1208,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -975,6 +1220,10 @@ ...@@ -975,6 +1220,10 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
map_id: ["equalSubProperty", "otherId"],
mapping_dict: {
"title": ["equalSubProperty", "otherTitle"]
},
sub_storage: { sub_storage: {
type: "query", type: "query",
sub_storage: { sub_storage: {
...@@ -983,27 +1232,21 @@ ...@@ -983,27 +1232,21 @@
type: "memory" type: "memory"
} }
} }
}, }
mapping_dict: {
"id": {"equal": "otherId"},
"title": {"equal": "otherTitle"}
},
map_all_property: true
}); });
start();
jio.put("42", jio.put("42",
{ {
"title": "foo", "title": "foo",
"smth": "bar" "smth": "bar"
}) })
.push(function () { .push(function () {
return jio.allDocs({ return jio.allDocs({
query: 'title: "foo"', query: 'title: "foo"',
select_list: ["title", "smth"] select_list: ["title", "smth"]
}); });
}) })
.push(function (result) { .push(function (result) {
deepEqual(result, deepEqual(result,
{ {
"data": { "data": {
...@@ -1020,6 +1263,9 @@ ...@@ -1020,6 +1263,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -1030,9 +1276,9 @@ ...@@ -1030,9 +1276,9 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
query: {"query": 'otherId: "42"'}, query: {"query": 'otherId: "42"'},
map_id: ["equalSubProperty", "otherId"],
mapping_dict: { mapping_dict: {
"id": {"equal": "otherId"}, "title": ["equalSubProperty", "otherTitle"]
"title": {"equal": "otherTitle"}
}, },
sub_storage: { sub_storage: {
type: "query", type: "query",
...@@ -1045,19 +1291,18 @@ ...@@ -1045,19 +1291,18 @@
} }
}); });
start();
jio.put("42", jio.put("42",
{ {
"title": "foo", "title": "foo",
"smth": "bar" "smth": "bar"
}) })
.push(function () { .push(function () {
return jio.allDocs({ return jio.allDocs({
query: 'title: "foo"', query: 'title: "foo"',
select_list: ["title"] select_list: ["title"]
}); });
}) })
.push(function (result) { .push(function (result) {
deepEqual(result, deepEqual(result,
{ {
"data": { "data": {
...@@ -1074,6 +1319,9 @@ ...@@ -1074,6 +1319,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -1087,13 +1335,12 @@ ...@@ -1087,13 +1335,12 @@
var jio = jIO.createJIO({ var jio = jIO.createJIO({
type: "mapping", type: "mapping",
map_id: ["equalSubProperty", "otherId"],
mapping_dict: {
"title": ["equalSubProperty", "otherTitle"]
},
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
},
map_all_property: true,
mapping_dict: {
"title": {"equal": "otherTitle"},
"id": {"equal": "otherId"}
} }
}); });
...@@ -1136,7 +1383,6 @@ ...@@ -1136,7 +1383,6 @@
]; ];
}; };
start();
jio.bulk([{ jio.bulk([{
method: "get", method: "get",
parameter_list: ["id1"] parameter_list: ["id1"]
...@@ -1148,11 +1394,9 @@ ...@@ -1148,11 +1394,9 @@
deepEqual( deepEqual(
result, result,
[{ [{
"id": "foo",
"title": "bar", "title": "bar",
"bar": "foo" "bar": "foo"
}, { }, {
"id": "bar",
"title": "foo", "title": "foo",
"foo": "bar" "foo": "bar"
}], }],
...@@ -1161,6 +1405,9 @@ ...@@ -1161,6 +1405,9 @@
}) })
.push(undefined, function (error) { .push(undefined, function (error) {
ok(false, error); ok(false, error);
})
.always(function () {
start();
}); });
}); });
...@@ -1178,7 +1425,9 @@ ...@@ -1178,7 +1425,9 @@
sub_storage: { sub_storage: {
type: "mappingstorage2713" type: "mappingstorage2713"
}, },
mapping_dict: {"title": {"equal": "title"}} mapping_dict: {
"title": ["equalSubProperty", "title"]
}
}); });
Storage2713.prototype.repair = function (id_list) { Storage2713.prototype.repair = function (id_list) {
...@@ -1186,13 +1435,15 @@ ...@@ -1186,13 +1435,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();
}); });
}); });
......
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