Commit 32519b68 authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: add attachment_uri_template,query and tests

parent a44d2218
/*jslint indent:2, maxlen: 80, nomen: true */
/*global jIO, RSVP */
/*global jIO, RSVP, UriTemplate */
(function (jIO, RSVP) {
"use strict";
......@@ -8,11 +8,29 @@
this._default_dict = spec.default_dict || {};
this._sub_storage = jIO.createJIO(spec.sub_storage);
this._map_all_property = spec.map_all_property || false;
this._mapping_dict_attachment = spec.mapping_dict_attachment || undefined;
this._query = spec.query || {};
this._id_is_mapped = (this._mapping_dict.id !== undefined
&& this._mapping_dict.id.equal !== "id");
}
function getAttachmentId(storage, sub_id, attachment_id, method) {
var mapping_dict = storage._mapping_dict_attachment;
return new RSVP.Queue()
.push(function () {
if (mapping_dict !== undefined
&& mapping_dict[attachment_id] !== undefined
&& mapping_dict[attachment_id].uri_template !== undefined) {
return UriTemplate.parse(
mapping_dict[attachment_id][method].uri_template
).expand({id: sub_id});
}
return attachment_id;
});
}
function getSubStorageId(storage, index) {
var query;
return new RSVP.Queue()
......@@ -23,8 +41,8 @@
}
if (storage._mapping_dict.id.equal !== undefined) {
query = storage._mapping_dict.id.equal + ': "' + index + '"';
if (storage._mapping_dict.id.query_limit !== undefined) {
query += ' AND ' + storage._mapping_dict.id.query_limit;
if (storage._query.query !== undefined) {
query += ' AND ' + storage._query.query;
}
for (property in storage._default_dict) {
if (storage._default_dict.hasOwnProperty(property)) {
......@@ -32,7 +50,12 @@
+ storage._default_dict[property].equal + '"';
}
}
return storage._sub_storage.allDocs({"query": query})
return storage._sub_storage.allDocs({
"query": query,
"sort_on": storage._query.sort_on,
"select_list": storage._query.select_list,
"limit": storage._query.limit
})
.push(function (data) {
if (data.data.rows.length === 0) {
return undefined;
......@@ -185,32 +208,45 @@
});
};
MappingStorage.prototype.putAttachment = function (doc_id) {
var that = this, argument_list = arguments;
return getSubStorageId(this, doc_id)
.push(function (id) {
argument_list[0] = id;
return that._sub_storage.putAttachment.apply(that._sub_storage,
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);
});
};
MappingStorage.prototype.getAttachment = function (doc_id) {
var that = this, argument_list = arguments;
return getSubStorageId(this, doc_id)
.push(function (id) {
argument_list[0] = id;
return that._sub_storage.getAttachment.apply(that._sub_storage,
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.removeAttachment = function (doc_id) {
var that = this, argument_list = arguments;
return getSubStorageId(this, doc_id)
.push(function (id) {
argument_list[0] = id;
return that._sub_storage.removeAttachment.apply(that._sub_storage,
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);
});
};
......@@ -219,15 +255,27 @@
return this._sub_storage.hasCapacity.apply(this._sub_storage, arguments);
};
MappingStorage.prototype.bulk = function () {
var i, that = this, mapped_result = [];
return this._sub_storage.bulk.apply(arguments)
.push(function (result) {
for (i = 0; i < result.length; i += 1) {
mapped_result.push(mapDocument(that, result[i]), false);
}
return mapped_result;
});
};
MappingStorage.prototype.buildQuery = function (option) {
var that = this,
i,
query,
property,
select_list = [],
sort_on = [];
function mapQuery(one_query) {
var i, result = "", key;
var i, result = "(", key;
if (one_query.type === "complex") {
for (i = 0; i < one_query.query_list.length; i += 1) {
result += "(" + mapQuery(one_query.query_list[i]) + ")";
......@@ -235,6 +283,7 @@
result += " " + one_query.operator + " ";
}
}
result += ")";
return result;
}
if (that._mapping_dict.hasOwnProperty(one_query.key)) {
......@@ -251,19 +300,42 @@
if (option.sort_on !== undefined) {
for (i = 0; i < option.sort_on.length; i += 1) {
sort_on.push([this._mapping_dict[option.sort_on[i][0]].equal,
option.sort_on[i][1]]);
property = [this._mapping_dict[option.sort_on[i][0]].equal,
option.sort_on[i][1]];
if (sort_on.indexOf(property) < 0) {
sort_on.push(property);
}
}
}
if (this._query.sort_on !== undefined) {
for (i = 0; i < this._query.sort_on.length; i += 1) {
property = this._query.sort_on[i];
if (sort_on.indexOf(property) < 0) {
sort_on.push(property);
}
}
}
if (option.select_list !== undefined) {
for (i = 0; i < option.select_list.length; i += 1) {
property = false;
if (this._mapping_dict.hasOwnProperty(option.select_list[i])) {
select_list.push(this._mapping_dict[option.select_list[i]].equal);
property = this._mapping_dict[option.select_list[i]].equal;
} else {
if (this._map_all_property) {
select_list.push(option.select_list[i]);
property = option.select_list[i];
}
}
if (property && sort_on.indexOf(property) < 0) {
select_list.push(property);
}
}
}
if (this._query.select_list !== undefined) {
for (i = 0; i < this._query.select_list; i += 1) {
property = this._query.select_list[i];
if (select_list.indexOf(property) < 0) {
select_list.push(property);
}
}
}
if (this._id_is_mapped) {
......@@ -272,8 +344,14 @@
if (option.query !== undefined) {
query = mapQuery(jIO.QueryFactory.create(option.query));
}
if (this._mapping_dict.id.query_limit !== undefined) {
query += 'AND( ' + this._mapping_dict.id.query_limit + ' )';
if (this._query.query !== undefined) {
if (query !== undefined) {
query += ' AND ';
} else {
query = "";
}
query += this._query.query;
}
return this._sub_storage.allDocs(
{
......@@ -298,5 +376,4 @@
};
jIO.addStorage('mapping', MappingStorage);
}(jIO, RSVP));
\ No newline at end of file
......@@ -139,7 +139,7 @@
};
Storage2713.prototype.buildQuery = function (options) {
deepEqual(options, {query: 'otherId: "42"'}, "allDoc 2713 called");
equal(options.query, 'otherId: "42"', "allDoc 2713 called");
return [{id: "2713"}];
};
......@@ -161,7 +161,7 @@
});
});
test("get with id mapped and query_limit", function () {
test("get with id mapped and query", function () {
stop();
expect(3);
......@@ -172,8 +172,9 @@
},
mapping_dict: {
"title": {"equal": "otherTitle"},
"id": {"equal": "otherId", "query_limit": 'otherTitle: "foo"'}
}
"id": {"equal": "otherId"}
},
query: {"query": 'otherTitle: "foo"'}
});
Storage2713.prototype.hasCapacity = function () {
......@@ -181,9 +182,9 @@
};
Storage2713.prototype.buildQuery = function (options) {
deepEqual(
options,
{query: 'otherId: "42" AND otherTitle: "foo"'},
equal(
options.query,
'otherId: "42" AND otherTitle: "foo"',
"allDoc 2713 called"
);
return [{id: "2713"}];
......@@ -227,9 +228,9 @@
};
Storage2713.prototype.buildQuery = function (options) {
deepEqual(
options,
{query: 'otherId: "42"'},
equal(
options.query,
'otherId: "42"',
"allDoc 2713 called"
);
return [{id: "2713"}];
......@@ -342,7 +343,7 @@
};
Storage2713.prototype.buildQuery = function (option) {
deepEqual(option, {"query": 'otherId: "42"'}, "allDocs 2713 called");
equal(option.query, 'otherId: "42"', "allDocs 2713 called");
return [];
};
......@@ -440,7 +441,7 @@
};
Storage2713.prototype.buildQuery = function (options) {
deepEqual(options, {query: 'otherId: "42"'}, "allDoc 2713 called");
equal(options.query, 'otherId: "42"', "allDoc 2713 called");
return [{id: "2713"}];
};
......@@ -494,6 +495,36 @@
});
});
test("putAttachment with UriTemplate", function () {
stop();
expect(4);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict_attachment: {"2713": {"uri_template": "www.2713.foo/{id}"}}
}),
blob = new Blob([""]);
Storage2713.prototype.putAttachment = function (doc_id,
attachment_id, attachment) {
equal(doc_id, "42", "putAttachment 2713 called");
equal(attachment_id, "www.2713.foo/42", "putAttachment 2713 called");
deepEqual(attachment, blob, "putAttachment 2713 called");
return doc_id;
};
jio.putAttachment("42", "2713", blob)
.then(function (result) {
equal(result, "42");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// mappingStorage.getAttachment
/////////////////////////////////////////////////////////////////
......@@ -526,12 +557,42 @@
});
});
test("getAttachment using UriTemplate", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict_attachment: {
"2713": {"uri_template": "www.2713/{id}/ok.com"}
}
}),
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;
};
jio.getAttachment("42", "2713")
.then(function (result) {
deepEqual(result, blob);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// mappingStorage.removeAttachment
/////////////////////////////////////////////////////////////////
module("mappingStorage.removeAttachment");
test("putAttachment use sub_storage one's", function () {
test("removeAttachment use sub_storage one's", function () {
stop();
expect(3);
var jio = jIO.createJIO({
......@@ -557,6 +618,33 @@
});
});
test("removeAttachment use UriTemplate", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
mapping_dict_attachment: {"2713": {"uri_template": "www.2713/{id}.bar"}}
});
Storage2713.prototype.removeAttachment = function (doc_id, attachment) {
equal(doc_id, "42", "putAttachment 2713 called");
equal(attachment, "www.2713/42.bar", "getAttachment 2713 called");
return doc_id;
};
jio.removeAttachment("42", "2713")
.then(function (result) {
deepEqual(result, "42");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// mappingStorage.allDocs
/////////////////////////////////////////////////////////////////
......@@ -698,13 +786,12 @@
jio.put("42",
{
"title": "foo",
"smth": "bar",
"stmth2": "bar2"
"smth": "bar"
})
.push(function () {
return jio.allDocs({
query: 'title: "foo"',
select_list: ["title", "smth2", "smth"]
select_list: ["title", "smth"]
});
})
.push(function (result) {
......@@ -714,7 +801,7 @@
"rows": [
{
"id": "42",
"value": {"title": "foo", "smth": "bar", "smth2": "bar2"},
"value": {"title": "foo", "smth": "bar"},
"doc": {}
}
],
......@@ -729,4 +816,65 @@
start();
});
});
/////////////////////////////////////////////////////////////////
// mappingStorage.bulk
/////////////////////////////////////////////////////////////////
module("mappingstorage.bulk");
test("bulk with map_all_property", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
sub_storage: {
type: "mappingstorage2713"
},
map_all_doc: true,
mapping_dict: {
"title": {"equal": "otherTitle"},
"id": {"equal": "otherId"}
}
});
Storage2713.prototype.hasCapacity = function () {
return true;
};
Storage2713.prototype.bulk = function () {
deepEqual(arguments, ["some", "arguments"], "bulk 2713 called");
return [
{
"otherId": "foo",
"otherTitle": "bar",
"bar": "foo"
}, {
"otherId": "bar",
"otherTitle": "foo",
"foo": "bar"
}
];
};
jio.bulk("some", "arguments")
.push(function (result) {
deepEqual(
result,
[{
"id": "foo",
"title": "bar",
"bar": "foo"
}, {
"id": "bar",
"title": "foo",
"foo": "bar"
}],
"bulk test"
);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob));
\ No newline at end of file
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