Commit 49dd5aa7 authored by Romain Courteaud's avatar Romain Courteaud

MemoryStorage: fix include_docs parameter handling.

Copy all documents/attachments set in the storage, to prevent unwanting external modifications
parent 506f646f
......@@ -5,7 +5,7 @@
*/
/*jslint nomen: true*/
/*global jIO*/
/*global jIO, RSVP*/
/**
* JIO Memory Storage. Type = 'memory'.
......@@ -20,7 +20,7 @@
* @class MemoryStorage
*/
(function (jIO) {
(function (jIO, JSON, RSVP) {
"use strict";
/**
......@@ -39,13 +39,13 @@
attachments: {}
};
}
this._database[id].doc = metadata;
this._database[id].doc = JSON.stringify(metadata);
return id;
};
MemoryStorage.prototype.get = function (id) {
try {
return this._database[id].doc;
return JSON.parse(this._database[id].doc);
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
......@@ -92,7 +92,7 @@
404
);
}
return result;
return jIO.util.dataURItoBlob(result);
} catch (error) {
if (error instanceof TypeError) {
throw new jIO.util.jIOError(
......@@ -114,7 +114,13 @@
}
throw error;
}
attachment_dict[name] = blob;
return new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsDataURL(blob);
})
.push(function (evt) {
attachment_dict[name] = evt.target.result;
});
};
MemoryStorage.prototype.removeAttachment = function (id, name) {
......@@ -145,7 +151,7 @@
rows.push({
id: i,
value: {},
doc: this._database[i]
doc: JSON.parse(this._database[i].doc)
});
} else {
rows.push({
......@@ -161,4 +167,4 @@
jIO.addStorage('memory', MemoryStorage);
}(jIO));
}(jIO, JSON, RSVP));
......@@ -58,9 +58,7 @@
equal(uuid, "put1");
deepEqual(test.jio.__storage._database.put1, {
attachments: {},
doc: {
"title": "myPut1"
}
doc: "{\"title\":\"myPut1\"}"
});
})
.fail(function (error) {
......@@ -88,9 +86,7 @@
deepEqual(test.jio.__storage._database.put1, {
"foo": "bar",
"attachments": {"foo": "bar"},
doc: {
"title": "myPut2"
}
doc: "{\"title\":\"myPut2\"}"
});
})
.fail(function (error) {
......@@ -133,7 +129,7 @@
test("get document", function () {
var id = "post1";
this.jio.__storage._database[id] = {
"doc": {title: "myPost1"}
"doc": "{\"title\":\"myPost1\"}"
};
stop();
expect(1);
......@@ -156,7 +152,7 @@
var id = "putattmt1";
this.jio.__storage._database[id] = {
"doc": {},
"doc": "{}",
"attachments": {
putattmt2: undefined
}
......@@ -370,14 +366,14 @@
this.jio.__storage._database[id] = {
"doc": JSON.stringify({}),
"attachments": {
"putattmt2": blob
"putattmt2": "data:x-application/foo;base64,dGVzdA=="
}
};
this.jio.getAttachment(id, attachment)
.then(function (result) {
ok(result instanceof Blob, "Data is Blob");
equal(result, blob);
deepEqual(result, blob);
})
.fail(function (error) {
ok(false, error);
......@@ -431,7 +427,8 @@
jio.putAttachment(id, "putattmt2", blob)
.then(function () {
equal(jio.__storage._database[id].attachments.putattmt2, blob);
equal(jio.__storage._database[id].attachments.putattmt2,
"data:x-application/foo;base64,dGVzdA==");
})
.fail(function (error) {
ok(false, error);
......@@ -555,8 +552,8 @@
});
test("list documents with include_docs", function () {
this.jio.__storage._database.foo2 = "bar2";
this.jio.__storage._database.foo1 = "bar1";
this.jio.__storage._database.foo2 = {doc: "{\"title\":\"bar2\"}"};
this.jio.__storage._database.foo1 = {doc: "{\"title\":\"bar1\"}"};
stop();
expect(1);
......@@ -569,12 +566,12 @@
{
"id": "foo2",
"value": {},
"doc": "bar2"
"doc": {title: "bar2"}
},
{
"id": "foo1",
"value": {},
"doc": "bar1"
"doc": {title: "bar1"}
}
],
"total_rows": 2
......
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