Commit da7179f8 authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: change configuration dict

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