diff --git a/js/common/modules/org/arangodb/general-graph.js b/js/common/modules/org/arangodb/general-graph.js new file mode 100644 index 0000000000..238905aca2 --- /dev/null +++ b/js/common/modules/org/arangodb/general-graph.js @@ -0,0 +1,163 @@ +/*jslint indent: 2, nomen: true, maxlen: 100, sloppy: true, vars: true, white: true, plusplus: true */ +/*global require, exports */ + +//////////////////////////////////////////////////////////////////////////////// +/// @brief Graph functionality +/// +/// @file +/// +/// DISCLAIMER +/// +/// Copyright 2010-2014 triagens GmbH, Cologne, Germany +/// +/// Licensed under the Apache License, Version 2.0 (the "License"); +/// you may not use this file except in compliance with the License. +/// You may obtain a copy of the License at +/// +/// http://www.apache.org/licenses/LICENSE-2.0 +/// +/// Unless required by applicable law or agreed to in writing, software +/// distributed under the License is distributed on an "AS IS" BASIS, +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +/// See the License for the specific language governing permissions and +/// limitations under the License. +/// +/// Copyright holder is triAGENS GmbH, Cologne, Germany +/// +/// @author Florian Bartels +/// @author Copyright 2011-2014, triAGENS GmbH, Cologne, Germany +//////////////////////////////////////////////////////////////////////////////// + + +var collection = require("org/arangodb/arango-collection"); + + +// ----------------------------------------------------------------------------- +// --SECTION-- module "org/arangodb/general-graph" +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// --SECTION-- private functions +// ----------------------------------------------------------------------------- + +//////////////////////////////////////////////////////////////////////////////// +/// @brief transform a string into an array. +//////////////////////////////////////////////////////////////////////////////// + + +var stringToArray = function (x) { + + if (typeof x === "String") { + return [x]; + } + return x; +}; + +//////////////////////////////////////////////////////////////////////////////// +/// @brief checks if a parameter is not defined, an empty string or an empty +// array +//////////////////////////////////////////////////////////////////////////////// + + +var isValidCollectionsParameter = function (x) { + + if (!x) { + return false; + } + if (Array.isArray(x) && x.length === 0) { + return false; + } + if (typeof x !== "String" && !Array.isArray(x)) { + return false; + } + return true; +}; + + + +// ----------------------------------------------------------------------------- +// --SECTION-- module "org/arangodb/general-graph" +// ----------------------------------------------------------------------------- + +// ----------------------------------------------------------------------------- +// --SECTION-- public functions +// ----------------------------------------------------------------------------- + +//////////////////////////////////////////////////////////////////////////////// +/// @brief define an undirected relation. +//////////////////////////////////////////////////////////////////////////////// + + +var _undirectedRelationDefinition = function (relationName, vertexCollections) { + + if (_undirectedRelationDefinition.arguments.length < 2) { + throw "method _undirectedRelationDefinition expects 2 arguments"; + } + + if (typeof relationName !== "string" || relationName === "") { + throw " must be a not empty string"; + } + + if (!isValidCollectionsParameter(vertexCollections)) { + throw " must be a not empty string or array"; + } + + return { + collection: "relationName", + from: stringToArray(vertexCollections), + to: stringToArray(vertexCollections) + }; +}; + + +//////////////////////////////////////////////////////////////////////////////// +/// @brief define an directed relation. +//////////////////////////////////////////////////////////////////////////////// + + +var _directedRelationDefinition = function (relationName, fromVertexCollections, toVertexCollections) { + + if (_directedRelationDefinition.arguments.length < 3) { + throw "method _undirectedRelationDefinition expects 3 arguments"; + } + + if (typeof relationName !== "string" || relationName === "") { + throw " must be a not empty string"; + } + + if (!isValidCollectionsParameter(fromVertexCollections)) { + throw " must be a not empty string or array"; + } + + if (!isValidCollectionsParameter(toVertexCollections)) { + throw " must be a not empty string or array"; + } + + return { + collection: "relationName", + from: stringToArray(fromVertexCollections), + to: stringToArray(toVertexCollections) + }; +}; + + + + + +// ----------------------------------------------------------------------------- +// --SECTION-- MODULE EXPORTS +// ----------------------------------------------------------------------------- + +exports._undirectedRelationDefinition = _undirectedRelationDefinition; +exports._directedRelationDefinition = _directedRelationDefinition; + + + +// ----------------------------------------------------------------------------- +// --SECTION-- END-OF-FILE +// ----------------------------------------------------------------------------- + +// Local Variables: +// mode: outline-minor +// outline-regexp: "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)" +// End: diff --git a/js/common/tests/shell-general-graph.js b/js/common/tests/shell-general-graph.js new file mode 100644 index 0000000000..cb4a03ec98 --- /dev/null +++ b/js/common/tests/shell-general-graph.js @@ -0,0 +1,203 @@ +/*jslint indent: 2, nomen: true, maxlen: 80, sloppy: true */ +/*global require, assertEqual, assertTrue */ + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test the general-graph class +/// +/// @file +/// +/// DISCLAIMER +/// +/// Copyright 2010-2014 triagens GmbH, Cologne, Germany +/// +/// Licensed under the Apache License, Version 2.0 (the "License"); +/// you may not use this file except in compliance with the License. +/// You may obtain a copy of the License at +/// +/// http://www.apache.org/licenses/LICENSE-2.0 +/// +/// Unless required by applicable law or agreed to in writing, software +/// distributed under the License is distributed on an "AS IS" BASIS, +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +/// See the License for the specific language governing permissions and +/// limitations under the License. +/// +/// Copyright holder is triAGENS GmbH, Cologne, Germany +/// +/// @author Florian Bartels +/// @author Copyright 2014, triAGENS GmbH, Cologne, Germany +//////////////////////////////////////////////////////////////////////////////// + +var jsunity = require("jsunity"); + +var arangodb = require("org/arangodb"); +var console = require("console"); +var graph = require("org/arangodb/general-graph") + +var print = arangodb.print; + +// ----------------------------------------------------------------------------- +// --SECTION-- graph module +// ----------------------------------------------------------------------------- + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test suite: general-graph Creation and edge definition +//////////////////////////////////////////////////////////////////////////////// + +function GeneralGraphCreationSuite() { + + return { + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test: Graph Creation +//////////////////////////////////////////////////////////////////////////////// + + test_undirectedRelationDefinition : function () { + var r; + + try { + r = graph._undirectedRelationDefinition("relationName", ["vertexC1", "vertexC2"]); + } + catch (err) { + } + + assertEqual(r, { + collection: "relationName", + from: ["vertexC1", "vertexC2"], + to: ["vertexC1", "vertexC2"] + }); + + }, + + test_undirectedRelationDefinitionWithMissingName : function () { + var r, exception; + try { + r = graph._undirectedRelationDefinition("", ["vertexC1", "vertexC2"]); + } + catch (err) { + exception = err; + } + + assertEqual(exception, " must be a not empty string"); + + }, + + test_undirectedRelationDefinitionWithTooFewArgs : function () { + var r, exception; + try { + r = graph._undirectedRelationDefinition(["vertexC1", "vertexC2"]); + } + catch (err) { + exception = err; + } + + assertEqual(exception, "method _undirectedRelationDefinition expects 2 arguments"); + + }, + + test_undirectedRelationDefinitionWithInvalidSecondArg : function () { + var r, exception; + try { + r = graph._undirectedRelationDefinition("name", {"vertexC1" : "vertexC2"}); + } + catch (err) { + exception = err; + } + + assertEqual(exception, " must be a not empty string or array"); + + }, + + test_directedRelationDefinition : function () { + var r; + + try { + r = graph._directedRelationDefinition("relationName", + ["vertexC1", "vertexC2"], ["vertexC3", "vertexC4"]); + } + catch (err) { + } + + assertEqual(r, { + collection: "relationName", + from: ["vertexC1", "vertexC2"], + to: ["vertexC3", "vertexC4"] + }); + + }, + + test_directedRelationDefinitionWithMissingName : function () { + var r, exception; + try { + r = graph._directedRelationDefinition("", + ["vertexC1", "vertexC2"], ["vertexC3", "vertexC4"]); + } + catch (err) { + exception = err; + } + + assertEqual(exception, " must be a not empty string"); + + }, + + test_directedRelationDefinitionWithTooFewArgs : function () { + var r, exception; + try { + r = graph._directedRelationDefinition(["vertexC1", "vertexC2"], ["vertexC3", "vertexC4"]); + } + catch (err) { + exception = err; + } + + assertEqual(exception, "method _undirectedRelationDefinition expects 3 arguments"); + + }, + + test_directedRelationDefinitionWithInvalidSecondArg : function () { + var r, exception; + try { + r = graph._directedRelationDefinition("name", {"vertexC1" : "vertexC2"}, ""); + } + catch (err) { + exception = err; + } + + assertEqual(exception, " must be a not empty string or array"); + + }, + + test_directedRelationDefinitionWithInvalidThirdArg : function () { + var r, exception; + try { + r = graph._directedRelationDefinition("name", ["vertexC1", "vertexC2"], []); + } + catch (err) { + exception = err; + } + + assertEqual(exception, " must be a not empty string or array"); + + } + + + } + +} + + +// ----------------------------------------------------------------------------- +// --SECTION-- main +// ----------------------------------------------------------------------------- + +//////////////////////////////////////////////////////////////////////////////// +/// @brief executes the test suites +//////////////////////////////////////////////////////////////////////////////// + +jsunity.run(GeneralGraphCreationSuite); + +return jsunity.done(); + +// Local Variables: +// mode: outline-minor +// outline-regexp: "^\\(/// @brief\\|/// @addtogroup\\|// --SECTION--\\|/// @page\\|/// @}\\)" +// End: