1
0
Fork 0

Implemented a wrapper around collections in graph module. Old collection functionality is now preserved

This commit is contained in:
Michael Hackstein 2014-05-21 09:45:53 +02:00
parent d03a8fa11b
commit ed14083e6f
2 changed files with 65 additions and 10 deletions

View File

@ -131,6 +131,20 @@ var _getGraphCollection = function() {
return gCol;
};
////////////////////////////////////////////////////////////////////////////////
/// @brief internal function to wrap arango collections for overwrite
////////////////////////////////////////////////////////////////////////////////
var wrapCollection = function(col) {
var wrapper = {};
_.each(_.functions(col), function(func) {
wrapper[func] = function() {
return col[func].apply(col, arguments);
};
});
return wrapper;
};
// -----------------------------------------------------------------------------
@ -396,11 +410,10 @@ var Graph = function(graphName, edgeDefinitions, vertexCollections, edgeCollecti
this.__edgeCollections = edgeCollections;
this.__edgeDefinitions = edgeDefinitions;
_.each(vertexCollections, function(obj, key) {
self[key] = obj;
var old_remove = obj.remove.bind(obj);
obj.remove = function(vertexId, options) {
var wrap = wrapCollection(obj);
var old_remove = wrap.remove;
wrap.remove = function(vertexId, options) {
var myEdges = self._EDGES(vertexId);
myEdges.forEach(
function(edgeObj) {
@ -411,15 +424,15 @@ var Graph = function(graphName, edgeDefinitions, vertexCollections, edgeCollecti
}
}
);
return old_remove(vertexId, options);
};
self[key] = wrap;
});
_.each(edgeCollections, function(obj, key) {
self[key] = obj;
var old_save = obj.save.bind(obj);
obj.save = function(from, to, data) {
var wrap = wrapCollection(obj);
var old_save = wrap.save;
wrap.save = function(from, to, data) {
edgeDefinitions.forEach(
function(edgeDefinition) {
if (edgeDefinition.collection === key) {
@ -434,6 +447,7 @@ var Graph = function(graphName, edgeDefinitions, vertexCollections, edgeCollecti
);
return old_save(from, to, data);
};
self[key] = wrap;
});
};

View File

@ -403,6 +403,47 @@ function GeneralGraphCreationSuite() {
msg = e;
}
assertEqual(msg, "graph bla4 does not exists.");
},
test_creationOfGraphShouldNotAffectCollections: function() {
var en = "UnitTestsEdges";
var vn1 = "UnitTestsVertices";
var vn2 = "UnitTestsVertices2";
var gn = "UnitTestsGraph";
var edgeDef = [graph._directedRelationDefinition(en, vn1, vn2)];
var g = graph._create(gn, edgeDef);
var v1 = g[vn1].save({_key: "1"})._id;
var v2 = g[vn2].save({_key: "2"})._id;
var v3 = g[vn1].save({_key: "3"})._id;
g[en].save(v1, v2, {});
assertEqual(g[vn1].count(), 2);
assertEqual(g[vn2].count(), 1);
assertEqual(g[en].count(), 1);
try {
g[en].save(v2, v3, {});
fail();
} catch (e) {
// This should create an error
assertEqual(g[en].count(), 1);
}
try {
db[en].save(v2, v3, {});
} catch (e) {
// This should not create an error
fail();
}
assertEqual(g[en].count(), 2);
db[vn2].remove(v2);
// This should not remove edges
assertEqual(g[en].count(), 2);
g[vn1].remove(v1);
// This should remove edges
assertEqual(g[en].count(), 1);
graph._drop(gn, true);
}
};
@ -853,8 +894,8 @@ function EdgesAndVerticesSuite() {
test_vC_remove : function () {
var vertex = g.unitTestVertexCollection1.save({first_name: "Tim"});
var vertexId = vertex._id;
var vertex = g.unitTestVertexCollection1.remove(vertexId);
assertTrue(vertex);
var result = g.unitTestVertexCollection1.remove(vertexId);
assertTrue(result);
},
test_vC_removeWithEdge : function () {