Commit 41b69b80 authored by Romain Courteaud's avatar Romain Courteaud

Add memorystorage in the build.

Add tests for memorystorage.
Drop copy/paste code in jio.post. Should be added in a standalone storage if needed.
parent e562fe8a
......@@ -171,6 +171,7 @@ module.exports = function (grunt) {
'src/jio.js',
'src/jio.storage/memorystorage.js',
'src/jio.storage/localstorage.js',
'src/jio.storage/davstorage.js',
'src/jio.storage/unionstorage.js',
......
......@@ -4,8 +4,8 @@
* http://www.gnu.org/licenses/lgpl.html
*/
/*jslint nomen: true, maxlen: 200*/
/*global jIO, window, Blob, Uint8Array, RSVP, console */
/*jslint nomen: true*/
/*global jIO*/
/**
* JIO Memory Storage. Type = 'memory'.
......@@ -34,19 +34,6 @@
}
MemoryStorage.prototype.post = function (metadata) {
var doc_id = metadata._id;
if (doc_id === undefined) {
doc_id = jIO.util.generateUuid();
}
if (this._database.hasOwnProperty(doc_id)) {
// the document already exists
throw new jIO.util.jIOError("Cannot create a new document", 409);
}
metadata._id = doc_id;
return this.put(metadata);
};
MemoryStorage.prototype.put = function (metadata) {
if (!this._database.hasOwnProperty(metadata._id)) {
this._database[metadata._id] = {
......@@ -58,24 +45,35 @@
};
MemoryStorage.prototype.get = function (param) {
if (!this._database.hasOwnProperty(param._id)) {
throw new jIO.util.jIOError("Cannot find document", 404);
}
var doc = JSON.parse(this._database[param._id].doc),
key,
found = false,
attachments = {};
for (key in this._database[param._id].attachments) {
if (this._database[param._id].attachments.hasOwnProperty(key)) {
found = true;
attachments[key] = {};
try {
return JSON.parse(this._database[param._id].doc);
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
"Cannot find document: " + param._id,
404
);
}
throw error;
}
if (found) {
doc._attachments = attachments;
}
return doc;
// XXX NotImplemented: list all attachments
// var doc = JSON.parse(this._database[param._id].doc),
// key,
// found = false,
// attachments = {};
//
// for (key in this._database[param._id].attachments) {
// if (this._database[param._id].attachments.hasOwnProperty(key)) {
// found = true;
// attachments[key] = {};
// }
// }
// if (found) {
// doc._attachments = attachments;
// }
// return doc;
};
MemoryStorage.prototype.remove = function (param) {
......@@ -84,24 +82,47 @@
};
MemoryStorage.prototype.getAttachment = function (param) {
if (!((this._database.hasOwnProperty(param._id)) &&
(this._database[param._id].attachments.hasOwnProperty(param._attachments)))) {
throw new jIO.util.jIOError("Cannot find attachment", 404);
try {
return this._database[param._id].attachments[param._attachment];
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
"Cannot find attachment: " + param._id + " , " + param._attachment,
404
);
}
throw error;
}
return this._database[param._id].attachments[param._attachment];
};
MemoryStorage.prototype.putAttachment = function (param) {
this._database[param._id].attachments[param._attachment] =
param._blob;
var attachment_dict;
try {
attachment_dict = this._database[param._id].attachments;
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError("Cannot find document: " + param._id, 404);
}
throw error;
}
attachment_dict[param._attachment] = param._blob;
};
MemoryStorage.prototype.removeAttachment = function (param) {
delete this._database[param._id].attachments[param._attachment];
try {
delete this._database[param._id].attachments[param._attachment];
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
"Cannot find document: " + param._id,
404
);
}
throw error;
}
};
MemoryStorage.prototype.hasCapacity = function (name) {
return (name === "list");
};
......
/*jslint nomen: true */
/*global Blob*/
(function (jIO, QUnit, Blob) {
"use strict";
var test = QUnit.test,
stop = QUnit.stop,
start = QUnit.start,
ok = QUnit.ok,
expect = QUnit.expect,
deepEqual = QUnit.deepEqual,
equal = QUnit.equal,
module = QUnit.module;
/////////////////////////////////////////////////////////////////
// memoryStorage constructor
/////////////////////////////////////////////////////////////////
module("memoryStorage.constructor");
test("Storage has a memory database", function () {
var jio = jIO.createJIO({
"type": "memory"
});
equal(jio.__type, "memory");
deepEqual(jio.__storage._database, {});
});
test("Storage's memory database is not shared", function () {
var jio = jIO.createJIO({
"type": "memory"
}),
jio2 = jIO.createJIO({
"type": "memory"
});
ok(jio.__storage._database !== jio2.__storage._database,
"Database is not shared");
});
/////////////////////////////////////////////////////////////////
// memoryStorage.put
/////////////////////////////////////////////////////////////////
module("memoryStorage.put", {
setup: function () {
this.jio = jIO.createJIO({
"type": "memory"
});
}
});
test("put non empty document", function () {
expect(2);
stop();
var test = this;
this.jio.put({"_id": "put1", "title": "myPut1"})
.then(function (uuid) {
equal(uuid, "put1");
deepEqual(test.jio.__storage._database.put1, {
attachments: {},
doc: JSON.stringify({
"_id": "put1",
"title": "myPut1"
})
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("put when document already exists", function () {
var id = "put1",
test = this;
this.jio.__storage._database[id] = {
"foo": "bar",
"attachments": {"foo": "bar"},
"doc": "foobar"
};
expect(2);
stop();
this.jio.put({"_id": id, "title": "myPut2"})
.then(function (uuid) {
equal(uuid, "put1");
deepEqual(test.jio.__storage._database.put1, {
"foo": "bar",
"attachments": {"foo": "bar"},
doc: JSON.stringify({
"_id": "put1",
"title": "myPut2"
})
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// memoryStorage.get
/////////////////////////////////////////////////////////////////
module("memoryStorage.get", {
setup: function () {
this.jio = jIO.createJIO({
"type": "memory"
});
}
});
test("get inexistent document", function () {
stop();
expect(3);
this.jio.get({"_id": "inexistent"})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError, error);
equal(error.message, "Cannot find document: inexistent");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get document", function () {
var id = "post1";
this.jio.__storage._database[id] = {
"doc": JSON.stringify({title: "myPost1"})
};
stop();
expect(1);
this.jio.get({"_id": id})
.then(function (result) {
deepEqual(result, {
"_id": id,
"title": "myPost1"
}, "Check document");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
// test("get document with attachment", function () {
// var id = "putattmt1";
//
// this.jio.__storage._database[id] = {
// "doc": JSON.stringify({}),
// "attachments": {
// putattmt2: undefined
// }
// };
//
// stop();
// expect(1);
//
// this.jio.get({"_id": id})
// .then(function (result) {
// deepEqual(result, {
// "_id": id,
// "_attachment": {},
// "title": "myPost1"
// }, "Check document");
// })
// .fail(function (error) {
// ok(false, error);
// })
// .always(function () {
// start();
// });
// });
/////////////////////////////////////////////////////////////////
// memoryStorage.remove
/////////////////////////////////////////////////////////////////
module("memoryStorage.remove", {
setup: function () {
this.jio = jIO.createJIO({
"type": "memory"
});
}
});
test("remove document", function () {
var id = "foo";
this.jio.__storage._database[id] = {
"doc": JSON.stringify({title: "myPost1"})
};
stop();
expect(1);
this.jio.remove({"_id": "foo"})
.then(function (result) {
equal(result, "foo");
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// memoryStorage.getAttachment
/////////////////////////////////////////////////////////////////
module("memoryStorage.getAttachment", {
setup: function () {
this.jio = jIO.createJIO({
"type": "memory"
});
}
});
test("get attachment from inexistent document", function () {
stop();
expect(3);
this.jio.getAttachment({
"_id": "inexistent",
"_attachment": "a"
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find attachment: inexistent , a");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get inexistent attachment from document", function () {
var id = "b";
stop();
expect(3);
this.jio.__storage._database[id] = {
"doc": JSON.stringify({})
};
this.jio.getAttachment({
"_id": id,
"_attachment": "inexistent"
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError);
equal(error.message, "Cannot find attachment: b , inexistent");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get attachment from document", function () {
var id = "putattmt1",
attachment = "putattmt2",
blob = new Blob(["test"], {"type": "x-application/foo"});
stop();
expect(2);
this.jio.__storage._database[id] = {
"doc": JSON.stringify({}),
"attachments": {
"putattmt2": blob
}
};
this.jio.getAttachment({
"_id": id,
"_attachment": attachment
})
.then(function (result) {
ok(result.data instanceof Blob, "Data is Blob");
equal(result.data, blob);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// memoryStorage.putAttachment
/////////////////////////////////////////////////////////////////
module("memoryStorage.putAttachment", {
setup: function () {
this.jio = jIO.createJIO({
"type": "memory"
});
}
});
test("put an attachment to an inexistent document", function () {
stop();
expect(3);
this.jio.putAttachment({
"_id": "inexistent",
"_attachment": "putattmt2",
"_data": ""
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError, error);
equal(error.message, "Cannot find document: inexistent");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("put an attachment to a document", function () {
var id = "putattmt1",
blob = new Blob(["test"], {"type": "x-application/foo"}),
jio = this.jio;
jio.__storage._database[id] = {
"doc": JSON.stringify({"foo": "bar"}),
"attachments": {}
};
stop();
expect(1);
jio.putAttachment({
"_id": id,
"_attachment": "putattmt2",
"_blob": blob
})
.then(function () {
equal(jio.__storage._database[id].attachments.putattmt2, blob);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// memoryStorage.removeAttachment
/////////////////////////////////////////////////////////////////
module("memoryStorage.removeAttachment", {
setup: function () {
this.jio = jIO.createJIO({
"type": "memory"
});
}
});
test("remove an attachment to an inexistent document", function () {
stop();
expect(3);
this.jio.removeAttachment({
"_id": "inexistent",
"_attachment": "removeattmt2"
})
.fail(function (error) {
ok(error instanceof jIO.util.jIOError, error);
equal(error.message, "Cannot find document: inexistent");
equal(error.status_code, 404);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("remove an attachment to a document", function () {
var id = "removeattmt1",
jio = this.jio;
jio.__storage._database[id] = {
"doc": JSON.stringify({"foo": "bar"}),
"attachments": {"removeattmt2": "bar"}
};
stop();
expect(1);
jio.removeAttachment({
"_id": id,
"_attachment": "removeattmt2"
})
.then(function () {
deepEqual(jio.__storage._database[id].attachments, {});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
/////////////////////////////////////////////////////////////////
// memoryStorage.hasCapacity
/////////////////////////////////////////////////////////////////
module("memoryStorage.hasCapacity", {
setup: function () {
this.jio = jIO.createJIO({
"type": "memory"
});
}
});
test("can list documents", function () {
ok(this.jio.hasCapacity("list"));
});
/////////////////////////////////////////////////////////////////
// memoryStorage.buildQuery
/////////////////////////////////////////////////////////////////
module("memoryStorage.buildQuery", {
setup: function () {
this.jio = jIO.createJIO({
"type": "memory"
});
}
});
test("list documents", function () {
this.jio.__storage._database.foo2 = "bar2";
this.jio.__storage._database.foo1 = "bar1";
stop();
expect(1);
this.jio.allDocs()
.then(function (result) {
deepEqual(result, {
"data": {
"rows": [
{
"id": "foo2",
"value": {}
},
{
"id": "foo1",
"value": {}
}
],
"total_rows": 2
}
});
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
}(jIO, QUnit, Blob));
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