From ae18204d1c47b77e06a26c5850231e6af327bee6 Mon Sep 17 00:00:00 2001 From: Frank Celler Date: Sat, 21 Apr 2012 18:39:36 +0200 Subject: [PATCH] fixed unit tests --- RestServer/module-graph.dox | 5 +++ js/common/modules/graph.js | 63 ++++++++++++++++++++++++++-------- js/common/tests/shell-graph.js | 39 +++++++++++++++------ 3 files changed, 82 insertions(+), 25 deletions(-) diff --git a/RestServer/module-graph.dox b/RestServer/module-graph.dox index 03028d84b2..7cc0d1cb04 100644 --- a/RestServer/module-graph.dox +++ b/RestServer/module-graph.dox @@ -63,6 +63,7 @@ ///
  • @ref JSModuleGraphGraphGetVertices "Graph.getVertices"
  • ///
  • @ref JSModuleGraphGraphRemoveEdge "Graph.removeEdge"
  • ///
  • @ref JSModuleGraphGraphRemoveVertex "Graph.removeVertex"
  • +///
  • @ref JSModuleGraphGraphDrop "Graph.drop"
  • /// /// ///
  • @ref JSModuleGraphVertex @@ -140,6 +141,10 @@ /// /// @anchor JSModuleGraphGraphRemoveEdge /// @copydetails JSF_Graph_prototype_removeEdge +///
    +/// +/// @anchor JSModuleGraphGraphDrop +/// @copydetails JSF_Graph_prototype_drop /// /// @section JSModuleGraphVertex Vertex Methods /////////////////////////////////////////////// diff --git a/js/common/modules/graph.js b/js/common/modules/graph.js index 75accbddeb..6ede4fc7da 100644 --- a/js/common/modules/graph.js +++ b/js/common/modules/graph.js @@ -307,6 +307,7 @@ Edge.prototype.properties = function () { //////////////////////////////////////////////////////////////////////////////// Edge.prototype._PRINT = function (seen, path, names) { + // Ignores the standard arguments seen = path = names = null; @@ -785,11 +786,10 @@ function Graph (name, vertices, edges) { var col; var props; - gdb = internal.db._collection(name); + gdb = internal.db._collection("_graph"); if (gdb === null) { - gdb = internal.db._create(name, - { waitForSync : true, isSystem : true }); + gdb = internal.db._create("_graph", { waitForSync : true, isSystem : true }); // gdb.ensureUniqueConstraint("name"); } @@ -797,12 +797,21 @@ function Graph (name, vertices, edges) { if (vertices === undefined && edges == undefined) { props = gdb.firstExample('name', name); - if (props == null) { + if (props === null) { throw "no graph named '" + name + "' found"; } vertices = internal.db._collection(props.vertices); + + if (vertices == null) { + throw "vertex collection '" + props.vertices + "' has vanished"; + } + edges = internal.edges._collection(props.edges); + + if (edges == null) { + throw "edge collection '" + props.edges + "' has vanished"; + } } else { @@ -814,6 +823,10 @@ function Graph (name, vertices, edges) { col = internal.db._create(vertices); } + if (col == null) { + throw "vertex collection '" + vertices + "' has vanished"; + } + // col.ensureUniqueConstraint("$id"); vertices = col; @@ -824,7 +837,11 @@ function Graph (name, vertices, edges) { col = internal.edges._collection(edges); if (col === null) { - col = internal.db._create(edges); + col = internal.edges._create(edges); + } + + if (col == null) { + throw "edge collection '" + edges + "' has vanished"; } // col.ensureUniqueConstraint("$id"); @@ -869,11 +886,11 @@ function Graph (name, vertices, edges) { } } - if (!vertices instanceof AvocadoCollection) { + if (! (vertices instanceof AvocadoCollection)) { throw " must be a document collection"; } - if (!edges instanceof AvocadoEdgesCollection) { + if (! (edges instanceof AvocadoEdgesCollection)) { throw " must be an edges collection"; } @@ -901,6 +918,25 @@ function Graph (name, vertices, edges) { /// @{ //////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +/// @brief drops the graph, the vertices, and the edges +/// +/// @FUN{@FA{graph}.drop()} +/// +/// Drops the graph, the vertices, and the edges. Handle with care. +//////////////////////////////////////////////////////////////////////////////// + +Graph.prototype.drop = function () { + var gdb; + + gdb = internal.db._collection("_graph"); + + gdb.remove(this._properties); + + this._vertices.drop(); + this._edges.drop(); +} + //////////////////////////////////////////////////////////////////////////////// /// @brief adds an edge to the graph /// @@ -1008,7 +1044,7 @@ Graph.prototype.addVertex = function (id, data) { /// /// @FUN{@FA{graph}.getVertex(@FA{id})} /// -/// Returns the vertex identified by @FA{id} or undefined. +/// Returns the vertex identified by @FA{id} or @LIT{null}. /// /// @EXAMPLES /// @@ -1019,13 +1055,13 @@ Graph.prototype.getVertex = function (id) { var ref, vertex; - ref = this._vertices.select({ $id : id }); + ref = this._vertices.firstExample('$id', id); - if (ref.count() === 1) { - vertex = this.constructVertex(ref.next()._id); + if (ref !== null) { + vertex = this.constructVertex(ref._id); } else { - vertex = undefined; + vertex = null; } return vertex; @@ -1271,6 +1307,5 @@ exports.Vertex = Vertex; // Local Variables: // mode: outline-minor -// outline-regexp: -// "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)" +// outline-regexp: "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)" // End: diff --git a/js/common/tests/shell-graph.js b/js/common/tests/shell-graph.js index e25db60acc..2e5526aa35 100644 --- a/js/common/tests/shell-graph.js +++ b/js/common/tests/shell-graph.js @@ -44,11 +44,11 @@ function graphBasicsSuite() { //var ERRORS = require("internal").errors; - var Graph = require("graph").Graph, - graph_name = "UnitTestsCollectionGraph", - vertex = "UnitTestsCollectionVertex", - edge = "UnitTestsCollectionEdge", - graph = null; + var Graph = require("graph").Graph; + var graph_name = "UnitTestsCollectionGraph"; + var vertex = "UnitTestsCollectionVertex"; + var edge = "UnitTestsCollectionEdge"; + var graph = null; return { @@ -57,10 +57,21 @@ //////////////////////////////////////////////////////////////////////////////// setUp : function () { - db._drop(vertex); - db._drop(edge); + try { + try { + graph = new Graph(graph_name); + print("FOUND: "); + PRINT_OBJECT(graph); + graph.drop(); + } + catch (err) { + } - graph = new Graph(graph_name, vertex, edge); + graph = new Graph(graph_name, vertex, edge); + } + catch (err) { + console.error("[FAILED] setup failed:" + err); + } }, //////////////////////////////////////////////////////////////////////////////// @@ -68,8 +79,14 @@ //////////////////////////////////////////////////////////////////////////////// tearDown : function () { - db._drop(vertex); - db._drop(edge); + try { + if (graph != null) { + graph.drop(); + } + } + catch (err) { + console.error("[FAILED] tear-down failed:" + err); + } }, //////////////////////////////////////////////////////////////////////////////// @@ -132,4 +149,4 @@ jsunity.run(graphBasicsSuite); jsunity.done(); -}()); \ No newline at end of file +}());