Commit 6e12e729 authored by Vincent Bechu's avatar Vincent Bechu

mappingstorage: add switchPropertyValue mapping function and tests

parent 70c3697f
...@@ -119,6 +119,23 @@ ...@@ -119,6 +119,23 @@
doc[property] = sub_doc[property]; doc[property] = sub_doc[property];
return property; return property;
} }
},
"switchPropertyValue": {
"mapToSubProperty": function (property, sub_doc, doc, args) {
sub_doc[args[0]] = args[1][doc[property]];
return args[0];
},
"mapToMainProperty": function (property, sub_doc, doc, args) {
var subvalue, value = sub_doc[args[0]];
for (subvalue in args[1]) {
if (args[1].hasOwnProperty(subvalue)) {
if (value === args[1][subvalue]) {
doc[property] = subvalue;
return property;
}
}
}
}
} }
}; };
/*jslint unparam: false*/ /*jslint unparam: false*/
......
...@@ -280,6 +280,42 @@ ...@@ -280,6 +280,42 @@
}); });
}); });
test("with switchPropertyValue", function () {
stop();
expect(2);
var jio = jIO.createJIO({
type: "mapping",
property: {
"title": ["switchPropertyValue", [
"subTitle",
{"mytitle": "title", "yourtitle": "othertitle"}
]],
"subTitle": ["ignore"]
},
sub_storage: {
type: "mappingstorage2713"
}
});
Storage2713.prototype.get = function (id) {
equal(id, "bar", "get 2713 called");
return {"subTitle": "title"};
};
jio.get("bar")
.push(function (result) {
deepEqual(result, {
"title": "mytitle"
});
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// mappingStorage.put // mappingStorage.put
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
...@@ -373,7 +409,7 @@ ...@@ -373,7 +409,7 @@
Storage2713.prototype.put = function (id, doc) { Storage2713.prototype.put = function (id, doc) {
deepEqual(doc, deepEqual(doc,
{"otherId": "42"}, "post 2713 called"); {"otherId": "42", "title": "bar"}, "post 2713 called");
equal(id, "bar"); equal(id, "bar");
return "bar"; return "bar";
}; };
...@@ -404,7 +440,7 @@ ...@@ -404,7 +440,7 @@
Storage2713.prototype.put = function (id, param) { Storage2713.prototype.put = function (id, param) {
equal(id, "2713", "put 2713 called"); equal(id, "2713", "put 2713 called");
deepEqual(param, {"foo": "bar"}, "put 2713 called"); deepEqual(param, {"foo": "bar", "title": "2713"}, "put 2713 called");
return id; return id;
}; };
...@@ -479,6 +515,39 @@ ...@@ -479,6 +515,39 @@
start(); start();
}); });
}); });
test("with switchPropertyValue", function () {
stop();
expect(3);
var jio = jIO.createJIO({
type: "mapping",
property: {
"title": ["switchPropertyValue", [
"subTitle",
{"mytitle": "title", "yourtitle": "othertitle"}
]]
},
sub_storage: {
type: "mappingstorage2713"
}
});
Storage2713.prototype.put = function (id, doc) {
equal(id, "bar", "put 2713 called");
deepEqual(doc, {"subTitle": "title"}, "put 2713 called");
return id;
};
jio.put("bar", {"title": "mytitle"})
.push(function (result) {
equal(result, "bar");
}).push(undefined, function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
///////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////
// mappingStorage.remove // mappingStorage.remove
...@@ -610,7 +679,7 @@ ...@@ -610,7 +679,7 @@
}); });
}); });
test("with equalSubId mapped and id in doc", function () { test("with equalSubId and id in doc", function () {
stop(); stop();
expect(3); expect(3);
...@@ -623,7 +692,7 @@ ...@@ -623,7 +692,7 @@
}); });
Storage2713.prototype.put = function (id, doc) { Storage2713.prototype.put = function (id, doc) {
deepEqual(doc, {"title": "foo"}, "put 2713 called"); deepEqual(doc, {"title": "foo", "otherId": "bar"}, "put 2713 called");
equal(id, "bar", "put 2713 called"); equal(id, "bar", "put 2713 called");
return "bar"; return "bar";
}; };
......
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