Commit 1ddbf450 authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: fix post and add tests

parent 46b38ffa
...@@ -260,11 +260,16 @@ ...@@ -260,11 +260,16 @@
}; };
MappingStorage.prototype.post = function (doc) { MappingStorage.prototype.post = function (doc) {
if (!this._id_is_mapped) { var sub_doc = mapToSubstorageDocument(
return this._sub_storage.post(mapToSubstorageDocument( this,
this, doc
doc ),
)); id = doc[this._property_for_sub_id];
if (this._property_for_sub_id && id !== undefined) {
return this._sub_storage.put(id, sub_doc);
}
if (!this._id_mapped || doc[this._id_mapped] !== undefined) {
return this._sub_storage.post(sub_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",
......
...@@ -421,7 +421,7 @@ ...@@ -421,7 +421,7 @@
}; };
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 [];
}; };
...@@ -580,7 +580,7 @@ ...@@ -580,7 +580,7 @@
}); });
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// mappingStorage.remove // mappingStorage.post
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
module("mappingStorage.post"); module("mappingStorage.post");
...@@ -615,7 +615,7 @@ ...@@ -615,7 +615,7 @@
}); });
}); });
test("post with id mapped", function () { test("post with id mapped, no id in doc", function () {
stop(); stop();
expect(2); expect(2);
...@@ -640,6 +640,65 @@ ...@@ -640,6 +640,65 @@
start(); start();
}); });
}); });
test("post with id mapped and in doc", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
map_id: ["equalSubProperty", "otherId"],
sub_storage: {
type: "mappingstorage2713"
}
});
Storage2713.prototype.post = function (doc) {
deepEqual(doc, {"title": "foo", "otherId": "bar"}, "post 2713 called");
return "42";
};
jio.post({"title": "foo", "otherId": "bar"})
.push(function (result) {
equal(result, "42");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("post with sub_id mapped and in doc", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
mapping_dict: {"otherId": ["equalSubId"]},
sub_storage: {
type: "mappingstorage2713"
}
});
Storage2713.prototype.put = function (id, doc) {
deepEqual(doc, {"title": "foo"}, "put 2713 called");
equal(id, "bar", "put 2713 called");
return "bar";
};
jio.post({"title": "foo", "otherId": "bar"})
.push(function (result) {
equal(result, "bar");
})
.push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// mappingStorage.putAttachment // mappingStorage.putAttachment
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -1150,7 +1209,7 @@ ...@@ -1150,7 +1209,7 @@
"doc": {} "doc": {}
} }
], ],
"total_rows": 1 "total_rows": 2
} }
}, "allDocs check"); }, "allDocs check");
}) })
......
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