1
0
Fork 0

Started implementing more elaborate tests for chaining. Initial vertex tests are implemented but will intentionally fail now, corresponding AQL implementation is yet missing

This commit is contained in:
Michael Hackstein 2014-05-22 14:03:58 +02:00
parent ec1ecaa107
commit 790046b177
2 changed files with 315 additions and 39 deletions

View File

@ -146,6 +146,19 @@ var wrapCollection = function(col) {
};
var transformExample = function(example) {
if (example === undefined) {
return {};
}
if (typeof example === "string") {
return {_id: example};
}
if (typeof example === "object") {
return example;
}
throw "Invalid example type. Has to be String, Array or Object";
};
// -----------------------------------------------------------------------------
// --SECTION-- module "org/arangodb/general-graph"
@ -155,9 +168,11 @@ var wrapCollection = function(col) {
// --SECTION-- Fluent AQL Interface
// -----------------------------------------------------------------------------
var AQLStatement = function(query, isEdgeQuery) {
var AQLStatement = function(query, type) {
this.query = query;
this.edgeQuery = isEdgeQuery || false;
if (type) {
this.type = type;
}
};
AQLStatement.prototype.printQuery = function() {
@ -165,7 +180,11 @@ AQLStatement.prototype.printQuery = function() {
};
AQLStatement.prototype.isEdgeQuery = function() {
return this.edgeQuery;
return this.type === "edge";
};
AQLStatement.prototype.isVertexQuery = function() {
return this.type === "vertex";
};
// -----------------------------------------------------------------------------
@ -179,7 +198,7 @@ var AQLGenerator = function(graph) {
};
this.graph = graph;
this.cursor = null;
this.lastEdgeVar = "";
this.lastVar = "";
};
AQLGenerator.prototype._clearCursor = function() {
@ -195,7 +214,7 @@ AQLGenerator.prototype._createCursor = function() {
}
};
AQLGenerator.prototype.edges = function(startVertex, direction) {
AQLGenerator.prototype._edges = function(startVertex, direction) {
this._clearCursor();
var edgeName = "edges_" + this.stack.length;
var query = "FOR " + edgeName
@ -203,17 +222,49 @@ AQLGenerator.prototype.edges = function(startVertex, direction) {
+ this.stack.length + ',"'
+ direction + '")';
this.bindVars["startVertex_" + this.stack.length] = startVertex;
var stmt = new AQLStatement(query, true);
var stmt = new AQLStatement(query, "edge");
this.stack.push(stmt);
this.lastEdgeVar = edgeName;
this.lastVar = edgeName;
return this;
};
AQLGenerator.prototype.getLastEdgeVar = function() {
if (this.lastEdgeVar === "") {
AQLGenerator.prototype.edges = function(example) {
return this._edges(example, "any");
};
AQLGenerator.prototype.outEdges = function(example) {
return this._edges(example, "outbound");
};
AQLGenerator.prototype.inEdges = function(example) {
return this._edges(example, "inbound");
};
AQLGenerator.prototype._vertices = function(example, direction) {
this._clearCursor();
var ex = transformExample(example);
var vertexName = "vertices_" + this.stack.length;
var query = "FOR " + vertexName
+ " IN GRAPH_VERTICIES(@graphName,@startEdge_"
+ this.stack.length + ','
+ '"' + direction + '")';
this.bindVars["startEdge_" + this.stack.length] = ex;
var stmt = new AQLStatement(query, "vertex");
this.stack.push(stmt);
this.lastVar = vertexName;
return this;
};
AQLGenerator.prototype.verticies = function(example) {
return this._verticies(example, "both");
};
AQLGenerator.prototype.getLastVar = function() {
if (this.lastVar === "") {
return false;
}
return this.lastEdgeVar;
return this.lastVar;
};
AQLGenerator.prototype.restrict = function(restrictions) {
@ -255,29 +306,11 @@ AQLGenerator.prototype.filter = function(example) {
} else {
ex = example;
}
var query = "FILTER MATCHES(" + this.getLastEdgeVar() + "," + JSON.stringify(ex) + ")";
var query = "FILTER MATCHES(" + this.getLastVar() + "," + JSON.stringify(ex) + ")";
this.stack.push(new AQLStatement(query));
return this;
};
/* Old Filter version, string replacements broxen
AQLGenerator.prototype.filter = function(condition) {
var con = condition.replace("e.", this.getLastEdgeVar() + ".");
var query = "FILTER " + con;
this.stack.push(new AQLStatement(query));
return this;
};
*/
/* Old LET version, string replacements broxen
AQLGenerator.prototype.let = function(assignment) {
var asg = assignment.replace("e.", this.getLastEdgeVar() + ".");
var query = "LET " + asg;
this.stack.push(new AQLStatement(query));
return this;
};
*/
AQLGenerator.prototype.printQuery = function() {
return this.stack.map(function(stmt) {
return stmt.printQuery();
@ -288,7 +321,7 @@ AQLGenerator.prototype.execute = function() {
this._clearCursor();
var query = this.printQuery();
var bindVars = this.bindVars;
query += " RETURN " + this.getLastEdgeVar();
query += " RETURN " + this.getLastVar();
return db._query(query, bindVars, {count: true});
};
@ -735,6 +768,19 @@ Graph.prototype._outEdges = function(vertexId) {
var AQLStmt = new AQLGenerator(this);
return AQLStmt.edges(vertexId, "outbound");
};
////////////////////////////////////////////////////////////////////////////////
/// @brief _vertices(edgeExample||edgeId).
////////////////////////////////////////////////////////////////////////////////
Graph.prototype._verticies = function(example) {
var AQLStmt = new AQLGenerator(this);
return AQLStmt.verticies(example);
};
////////////////////////////////////////////////////////////////////////////////
/// @brief get ingoing vertex of an edge.
////////////////////////////////////////////////////////////////////////////////

View File

@ -758,28 +758,257 @@ function GeneralGraphAQLQueriesSuite() {
function ChainedFluentAQLResultsSuite() {
var gn = "UnitTestGraph";
var user = "UnitTestUsers";
var product = "UnitTestProducts";
var isFriend = "UnitTestIsFriend";
var hasBought = "UnitTestHasBought";
var uaName = "Alice";
var ubName = "Bob";
var ucName = "Charly";
var udName = "Diana";
var p1Name = "HiFi";
var p2Name = "Shirt";
var p3Name = "TV";
var pTypeElec = "Electro";
var pTypeCloth = "Cloth";
var ud1 = 2000;
var ud2 = 2001;
var ud3 = 2002;
var ud4 = 2003;
var d1 = 2004;
var d2 = 2005;
var d3 = 2006;
var d4 = 2007;
var d5 = 2008;
var g;
var edgeDef = [];
var createTestData = function() {
g = graph._create(gn, edgeDef);
};
edgeDef.push(graph._undirectedRelationDefinition(isFriend, user));
edgeDef.push(graph._directedRelationDefinition(hasBought, user, product));
var dropData = function() {
try {
graph._drop(gn);
} catch(e) {
}
};
var createTestData = function() {
dropData();
g = graph._create(gn, edgeDef);
var ua = g[user].save({name: uaName})._id;
var ub = g[user].save({name: ubName})._id;
var uc = g[user].save({name: ucName})._id;
var ud = g[user].save({name: udName})._id;
var p1 = g[product].save({name: p1Name, type: pTypeElec})._id;
var p2 = g[product].save({name: p2Name, type: pTypeCloth})._id;
var p3 = g[product].save({name: p3Name, type: pTypeElec})._id;
g[isFriend].save(ua, ub, {
since: ud1
});
g[isFriend].save(ua, uc, {
since: ud2
});
g[isFriend].save(ub, ud, {
since: ud3
});
g[isFriend].save(uc, ud, {
since: ud4
});
g[hasBought].save(ua, p1, {
date: d1
});
g[hasBought].save(ub, p1, {
date: d2
});
g[hasBought].save(ub, p3, {
date: d3
});
g[hasBought].save(ud, p1, {
date: d4
});
g[hasBought].save(ud, p2, {
date: d5
});
};
return {
setUp: function() {
setUp: createTestData,
tearDown: dropData,
test_getAllVerticies: function() {
fail("Not yet testable");
var result = g._verticies().toArray();
assertEqual(result.length, 7);
var sorted = _.sort(result, "name");
assertEqual(sorted[0].name, uaName);
assertEqual(sorted[1].name, ubName);
assertEqual(sorted[2].name, ucName);
assertEqual(sorted[3].name, udName);
assertEqual(sorted[4].name, p1Name);
assertEqual(sorted[5].name, p2Name);
assertEqual(sorted[6].name, p3Name);
},
tearDown: dropData
test_getVertexById: function() {
fail("Not yet testable");
var a_id = g[user].byExample({name: uaName})._id;
var result = g._verticies(a_id).toArray();
assertEqual(result.length, 1);
assertEqual(result[0].name, uaName);
},
test_getVerticiesById: function() {
fail("Not yet testable");
var a_id = g[user].byExample({name: uaName})._id;
var b_id = g[user].byExample({name: ubName})._id;
var result = g._verticies([a_id, b_id]).toArray();
assertEqual(result.length, 2);
var sorted = _.sort(result, "name");
assertEqual(sorted[0].name, uaName);
assertEqual(sorted[1].name, ubName);
},
test_getVertexByExample: function() {
fail("Not yet testable");
var result = g._verticies({
name: uaName
}).toArray();
assertEqual(result.length, 1);
assertEqual(result[0].name, uaName);
},
test_getVerticiesByExample: function() {
fail("Not yet testable");
var result = g._verticies([{
name: uaName
},{
name: p1Name
}]).toArray();
assertEqual(result.length, 2);
var sorted = _.sort(result, "name");
assertEqual(sorted[0].name, uaName);
assertEqual(sorted[1].name, p1Name);
},
test_getVerticiesByExampleAndIdMix: function() {
fail("Not yet testable");
var b_id = g[user].byExample({name: ubName})._id;
var result = g._verticies([{
name: uaName
},
b_id,
{
name: ucName
}]).toArray();
assertEqual(result.length, 3);
var sorted = _.sort(result, "name");
assertEqual(sorted[0].name, uaName);
assertEqual(sorted[1].name, ubName);
assertEqual(sorted[2].name, ucName);
},
test_getAllEdges: function() {
fail("Not yet testable");
var result = g.edges().toArray();
assertEqual(result.length, 9);
var friends = _.sort(
_.filter(result, function(e) {
return e._id.split("/")[0] === isFriend;
}),
"since"
);
var bought = _.sort(
_.filter(result, function(e) {
return e._id.split("/")[0] === hasBought;
}),
"date"
);
assertEqual(friends[0].since, ud1);
assertEqual(friends[1].since, ud2);
assertEqual(friends[2].since, ud3);
assertEqual(friends[3].since, ud4);
assertEqual(bought[0].date, d1);
assertEqual(bought[1].date, d2);
assertEqual(bought[2].date, d3);
assertEqual(bought[3].date, d4);
assertEqual(bought[4].date, d5);
},
test_getEdgeById: function() {
fail("Not yet testable");
var a_id = g[hasBought].byExample({date: d1})._id;
var result = g.edges(a_id).toArray();
assertEqual(result.length, 1);
assertEqual(result[0].date, d1);
},
test_getEdgesById: function() {
fail("Not yet testable");
var a_id = g[hasBought].byExample({date: d1})._id;
var b_id = g[isFriend].byExample({since: ud2})._id;
var result = g._verticies([a_id, b_id]).toArray();
assertEqual(result.length, 2);
if (result[0].date) {
assertEqual(result[0].date, d1);
assertEqual(result[1].since, ud2);
} else {
assertEqual(result[0].since, ud2);
assertEqual(result[1].date, d1);
}
},
/*
test_getVertexByExample: function() {
fail("Not yet testable");
var result = g._verticies({
name: uaName
}).toArray();
assertEqual(result.length, 1);
assertEqual(result[0].name, uaName);
},
test_getVerticiesByExample: function() {
fail("Not yet testable");
var result = g._verticies([{
name: uaName
},{
name: p1Name
}]).toArray();
assertEqual(result.length, 2);
var sorted = _.sort(result, "name");
assertEqual(sorted[0].name, uaName);
assertEqual(sorted[1].name, p1Name);
},
test_getVerticiesByExampleAndIdMix: function() {
fail("Not yet testable");
var b_id = g[user].byExample({name: ubName})._id;
var result = g._verticies([{
name: uaName
},
b_id,
{
name: ucName
}]).toArray();
assertEqual(result.length, 3);
var sorted = _.sort(result, "name");
assertEqual(sorted[0].name, uaName);
assertEqual(sorted[1].name, ubName);
assertEqual(sorted[2].name, ucName);
}
*/
};
@ -1072,6 +1301,7 @@ function EdgesAndVerticesSuite() {
jsunity.run(EdgesAndVerticesSuite);
jsunity.run(GeneralGraphCreationSuite);
jsunity.run(GeneralGraphAQLQueriesSuite);
jsunity.run(ChainedFluentAQLResultsSuite);
return jsunity.done();