<commit_before>/*jslint indent: 2, nomen: true, maxlen: 100, sloppy: true */
/*global require, assertEqual, assertTrue, assertFalse, fail */

////////////////////////////////////////////////////////////////////////////////
/// @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 <NAME>, <NAME>
/// @author Copyright 2014, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

var jsunity = require("jsunity");
var arangodb = require("org/arangodb");
var db = arangodb.db;
var graph = require("org/arangodb/general-graph");
var ERRORS = arangodb.errors;

var _ = require("underscore");

// -----------------------------------------------------------------------------
// --SECTION--                                                      graph module
// -----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
/// @brief test suite: general-graph Creation and edge definition
////////////////////////////////////////////////////////////////////////////////

function GeneralGraphCreationSuite() {

  var rn = "UnitTestRelationName";
  var rn1 = "UnitTestRelationName1";
  var vn1 = "UnitTestVerticies1";
  var vn2 = "UnitTestVerticies2";
  var vn3 = "UnitTestVerticies3";
  var vn4 = "UnitTestVerticies4";
  var gn = "UnitTestGraph";
  var edgeDef = graph._edgeDefinitions(
    graph._undirectedRelationDefinition(rn, vn1),
    graph._directedRelationDefinition(rn1,
      [vn1, vn2], [vn3, vn4]
    )
  );

  return {

////////////////////////////////////////////////////////////////////////////////
/// @brief test: Graph Creation
////////////////////////////////////////////////////////////////////////////////

    test_undirectedRelationDefinition : function () {
      var r = graph._undirectedRelationDefinition(rn, [vn1, vn2]);

      assertEqual(r, {
        collection: rn,
        from: [vn1, vn2],
        to: [vn1, vn2]
      });

    },

    test_undirectedRelationDefinitionWithSingleCollection : function () {
      var r = graph._undirectedRelationDefinition(rn, vn1);

      assertEqual(r, {
        collection: rn,
        from: [vn1],
        to: [vn1]
      });

    },

    test_undirectedRelationDefinitionWithMissingName : function () {
      try {
        graph._undirectedRelationDefinition("", [vn1, vn2]);
        fail();
      }
      catch (err) {
        assertEqual(err, "<relationName> must be a not empty string");
      }
    },

    test_undirectedRelationDefinitionWithTooFewArgs : function () {
      try {
        graph._undirectedRelationDefinition([vn1, vn2]);
        fail();
      }
      catch (err) {
        assertEqual(err, "method _undirectedRelationDefinition expects 2 arguments");
      }
    },

    test_undirectedRelationDefinitionWithInvalidSecondArg : function () {
      try {
        var param = {};
        param[vn1] = vn2;
        graph._undirectedRelationDefinition(rn, param);
        fail();
      }
      catch (err) {
        assertEqual(err, "<vertexCollections> must be a not empty string or array");
      }
    },

    test_directedRelationDefinition : function () {
      var r = graph._directedRelationDefinition(rn,
          [vn1, vn2], [vn3, vn4]);

      assertEqual(r, {
        collection: rn,
        from: [vn1, vn2],
        to: [vn3, vn4]
      });

    },

    test_directedRelationDefinitionWithMissingName : function () {
      try {
        graph._directedRelationDefinition("",
          [vn1, vn2], [vn3, vn4]);
        fail();
      }
      catch (err) {
        assertEqual(err, "<relationName> must be a not empty string");
      }
    },

    test_directedRelationDefinitionWithTooFewArgs : function () {
      try {
        graph._directedRelationDefinition([vn1, vn2], [vn3, vn4]);
        fail();
      }
      catch (err) {
        assertEqual(err, "method _directedRelationDefinition expects 3 arguments");
      }
    },

    test_directedRelationDefinitionWithInvalidSecondArg : function () {
      try {
        var param = {};
        param[vn1] = vn2;
        graph._directedRelationDefinition(rn, param, vn3);
        fail();
      }
      catch (err) {
        assertEqual(err, "<fromVertexCollections> must be a not empty string or array");
      }


    },

    test_directedRelationDefinitionWithInvalidThirdArg : function () {
      try {
        var param = {};
        param[vn1] = vn2;
        graph._directedRelationDefinition(rn, vn3, param);
        fail();
      }
      catch (err) {
        assertEqual(err, "<toVertexCollections> must be a not empty string or array");
      }
    },

    testEdgeDefinitions : function () {

      //with empty args
      assertEqual(graph._edgeDefinitions(), []);

      //with args
      assertEqual(graph._edgeDefinitions(
        graph._undirectedRelationDefinition(rn, vn1),
        graph._directedRelationDefinition(rn1,
          [vn1, vn2], [vn3, vn4])
      ), [
        {
          collection: rn,
          from: [vn1],
          to: [vn1]
        },
        {
          collection: rn1,
          from: [vn1, vn2],
          to: [vn3, vn4]
        }
      ]);
    },

    test_create : function () {
      if (db._collection("_graphs").exists(gn)) {
        db._collection("_graphs").remove(gn);
      }
      var a = graph._create(
        gn,
        graph._edgeDefinitions(
          graph._undirectedRelationDefinition(rn, vn1),
          graph._directedRelationDefinition(rn1, [vn1, vn2], [vn3, vn4])
        )
      );
      assertTrue(a.__vertexCollections.hasOwnProperty(vn1));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn2));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn3));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn4));
      assertTrue(a.__edgeCollections.hasOwnProperty(rn));
      assertTrue(a.__edgeCollections.hasOwnProperty(rn1));
      assertEqual(a.__edgeDefinitions, [
        {
          "collection" : rn,
          "from" : [
            vn1
          ],
          "to" : [
            vn1
          ]
        },
        {
          "collection" : rn1,
          "from" : [
            vn1,
            vn2
          ],
          "to" : [
            vn3,
            vn4
          ]
        }
      ]
      );
    },

    test_create_WithOut_EdgeDefiniton : function () {
      if (db._collection("_graphs").exists(gn)) {
        db._collection("_graphs").remove(gn);
      }
      try {
        graph._create(
          gn,
          []
        );
        fail();
      } catch (err) {
        assertEqual(err, "at least one edge definition is required to create a graph.");
      }
    },

    test_create_WithOut_Name : function () {
      if (db._collection("_graphs").exists(gn)) {
        db._collection("_graphs").remove(gn);
      }
      try {
        graph._create(
          "",
          edgeDef
        );
        fail();
      } catch (err) {
        assertEqual(err, "a graph name is required to create a graph.");
      }
    },

    test_create_With_Already_Existing_Graph : function () {
      if (db._collection("_graphs").exists(gn)) {
        db._collection("_graphs").remove(gn);
      }
      graph._create(gn, edgeDef);
      try {
        graph._create(gn, edgeDef);
      } catch (err) {
        assertEqual(err, "graph " + gn + " already exists.");
      }
    },

    test_get_graph : function () {
      if (db._collection("_graphs").exists(gn)) {
        db._collection("_graphs").remove(gn);
      }
      graph._create(gn, edgeDef);
      var a = graph._graph(gn);

      assertTrue(a.__vertexCollections.hasOwnProperty(vn1));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn2));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn3));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn4));
      assertTrue(a.__edgeCollections.hasOwnProperty(rn));
      assertTrue(a.__edgeCollections.hasOwnProperty(rn1));
      assertEqual(a.__edgeDefinitions, [
        {
          "collection" : rn,
          "from" : [
            vn1
          ],
          "to" : [
            vn1
          ]
        },
        {
          "collection" : rn1,
          "from" : [
            vn1,
            vn2
          ],
          "to" : [
            vn3,
            vn4
          ]
        }
      ]
      );
    },

    test_get_graph_without_hit : function () {
      var msg;
      try {
        var a = graph._graph("bla4");
      } catch (e) {
        msg = e;
      }
      assertEqual(msg, "graph bla4 does not exists.");
    },

    test_creationOfGraphShouldNotAffectCollections: function() {
      var en = "UnitTestsEdges";
      var vn1 = "UnitTestsVertices";
      var vn2 = "UnitTestsVertices2";
      var gn = "UnitTestsGraph";
      if(graph._exists(gn)) {
        graph._drop(gn);
      }
      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);
    }

  };

}

// -----------------------------------------------------------------------------
// --SECTION--                                                    Simple Queries
// -----------------------------------------------------------------------------
<commit_msg>Further refactoring, another suite now based on variables and prefixed with UnitTests<commit_after>/*jslint indent: 2, nomen: true, maxlen: 100, sloppy: true */
/*global require, assertEqual, assertTrue, assertFalse, fail */

////////////////////////////////////////////////////////////////////////////////
/// @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 <NAME>, <NAME>
/// @author Copyright 2014, triAGENS GmbH, Cologne, Germany
////////////////////////////////////////////////////////////////////////////////

var jsunity = require("jsunity");
var arangodb = require("org/arangodb");
var db = arangodb.db;
var graph = require("org/arangodb/general-graph");
var ERRORS = arangodb.errors;

var _ = require("underscore");

// -----------------------------------------------------------------------------
// --SECTION--                                                      graph module
// -----------------------------------------------------------------------------

////////////////////////////////////////////////////////////////////////////////
/// @brief test suite: general-graph Creation and edge definition
////////////////////////////////////////////////////////////////////////////////

function GeneralGraphCreationSuite() {

  var rn = "UnitTestRelationName";
  var rn1 = "UnitTestRelationName1";
  var vn1 = "UnitTestVerticies1";
  var vn2 = "UnitTestVerticies2";
  var vn3 = "UnitTestVerticies3";
  var vn4 = "UnitTestVerticies4";
  var gn = "UnitTestGraph";
  var edgeDef = graph._edgeDefinitions(
    graph._undirectedRelationDefinition(rn, vn1),
    graph._directedRelationDefinition(rn1,
      [vn1, vn2], [vn3, vn4]
    )
  );

  return {

////////////////////////////////////////////////////////////////////////////////
/// @brief test: Graph Creation
////////////////////////////////////////////////////////////////////////////////

    test_undirectedRelationDefinition : function () {
      var r = graph._undirectedRelationDefinition(rn, [vn1, vn2]);

      assertEqual(r, {
        collection: rn,
        from: [vn1, vn2],
        to: [vn1, vn2]
      });

    },

    test_undirectedRelationDefinitionWithSingleCollection : function () {
      var r = graph._undirectedRelationDefinition(rn, vn1);

      assertEqual(r, {
        collection: rn,
        from: [vn1],
        to: [vn1]
      });

    },

    test_undirectedRelationDefinitionWithMissingName : function () {
      try {
        graph._undirectedRelationDefinition("", [vn1, vn2]);
        fail();
      }
      catch (err) {
        assertEqual(err, "<relationName> must be a not empty string");
      }
    },

    test_undirectedRelationDefinitionWithTooFewArgs : function () {
      try {
        graph._undirectedRelationDefinition([vn1, vn2]);
        fail();
      }
      catch (err) {
        assertEqual(err, "method _undirectedRelationDefinition expects 2 arguments");
      }
    },

    test_undirectedRelationDefinitionWithInvalidSecondArg : function () {
      try {
        var param = {};
        param[vn1] = vn2;
        graph._undirectedRelationDefinition(rn, param);
        fail();
      }
      catch (err) {
        assertEqual(err, "<vertexCollections> must be a not empty string or array");
      }
    },

    test_directedRelationDefinition : function () {
      var r = graph._directedRelationDefinition(rn,
          [vn1, vn2], [vn3, vn4]);

      assertEqual(r, {
        collection: rn,
        from: [vn1, vn2],
        to: [vn3, vn4]
      });

    },

    test_directedRelationDefinitionWithMissingName : function () {
      try {
        graph._directedRelationDefinition("",
          [vn1, vn2], [vn3, vn4]);
        fail();
      }
      catch (err) {
        assertEqual(err, "<relationName> must be a not empty string");
      }
    },

    test_directedRelationDefinitionWithTooFewArgs : function () {
      try {
        graph._directedRelationDefinition([vn1, vn2], [vn3, vn4]);
        fail();
      }
      catch (err) {
        assertEqual(err, "method _directedRelationDefinition expects 3 arguments");
      }
    },

    test_directedRelationDefinitionWithInvalidSecondArg : function () {
      try {
        var param = {};
        param[vn1] = vn2;
        graph._directedRelationDefinition(rn, param, vn3);
        fail();
      }
      catch (err) {
        assertEqual(err, "<fromVertexCollections> must be a not empty string or array");
      }


    },

    test_directedRelationDefinitionWithInvalidThirdArg : function () {
      try {
        var param = {};
        param[vn1] = vn2;
        graph._directedRelationDefinition(rn, vn3, param);
        fail();
      }
      catch (err) {
        assertEqual(err, "<toVertexCollections> must be a not empty string or array");
      }
    },

    testEdgeDefinitions : function () {

      //with empty args
      assertEqual(graph._edgeDefinitions(), []);

      //with args
      assertEqual(graph._edgeDefinitions(
        graph._undirectedRelationDefinition(rn, vn1),
        graph._directedRelationDefinition(rn1,
          [vn1, vn2], [vn3, vn4])
      ), [
        {
          collection: rn,
          from: [vn1],
          to: [vn1]
        },
        {
          collection: rn1,
          from: [vn1, vn2],
          to: [vn3, vn4]
        }
      ]);
    },

    test_create : function () {
      if (db._collection("_graphs").exists(gn)) {
        db._collection("_graphs").remove(gn);
      }
      var a = graph._create(
        gn,
        graph._edgeDefinitions(
          graph._undirectedRelationDefinition(rn, vn1),
          graph._directedRelationDefinition(rn1, [vn1, vn2], [vn3, vn4])
        )
      );
      assertTrue(a.__vertexCollections.hasOwnProperty(vn1));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn2));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn3));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn4));
      assertTrue(a.__edgeCollections.hasOwnProperty(rn));
      assertTrue(a.__edgeCollections.hasOwnProperty(rn1));
      assertEqual(a.__edgeDefinitions, [
        {
          "collection" : rn,
          "from" : [
            vn1
          ],
          "to" : [
            vn1
          ]
        },
        {
          "collection" : rn1,
          "from" : [
            vn1,
            vn2
          ],
          "to" : [
            vn3,
            vn4
          ]
        }
      ]
      );
    },

    test_create_WithOut_EdgeDefiniton : function () {
      if (db._collection("_graphs").exists(gn)) {
        db._collection("_graphs").remove(gn);
      }
      try {
        graph._create(
          gn,
          []
        );
        fail();
      } catch (err) {
        assertEqual(err, "at least one edge definition is required to create a graph.");
      }
    },

    test_create_WithOut_Name : function () {
      if (db._collection("_graphs").exists(gn)) {
        db._collection("_graphs").remove(gn);
      }
      try {
        graph._create(
          "",
          edgeDef
        );
        fail();
      } catch (err) {
        assertEqual(err, "a graph name is required to create a graph.");
      }
    },

    test_create_With_Already_Existing_Graph : function () {
      if (db._collection("_graphs").exists(gn)) {
        db._collection("_graphs").remove(gn);
      }
      graph._create(gn, edgeDef);
      try {
        graph._create(gn, edgeDef);
      } catch (err) {
        assertEqual(err, "graph " + gn + " already exists.");
      }
    },

    test_get_graph : function () {
      if (db._collection("_graphs").exists(gn)) {
        db._collection("_graphs").remove(gn);
      }
      graph._create(gn, edgeDef);
      var a = graph._graph(gn);

      assertTrue(a.__vertexCollections.hasOwnProperty(vn1));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn2));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn3));
      assertTrue(a.__vertexCollections.hasOwnProperty(vn4));
      assertTrue(a.__edgeCollections.hasOwnProperty(rn));
      assertTrue(a.__edgeCollections.hasOwnProperty(rn1));
      assertEqual(a.__edgeDefinitions, [
        {
          "collection" : rn,
          "from" : [
            vn1
          ],
          "to" : [
            vn1
          ]
        },
        {
          "collection" : rn1,
          "from" : [
            vn1,
            vn2
          ],
          "to" : [
            vn3,
            vn4
          ]
        }
      ]
      );
    },

    test_get_graph_without_hit : function () {
      try {
        graph._graph(gn + "UnknownExtension");
        fail();
      } catch (e) {
        assertEqual(e, "graph " + gn + "UnknownExtension" + " does not exists.");
      }
    },

    test_creationOfGraphShouldNotAffectCollections: function() {
      if(graph._exists(gn)) {
        graph._drop(gn);
      }
      var edgeDef2 = [graph._directedRelationDefinition(rn, vn1, vn2)];
      var g = graph._create(gn, edgeDef2);
      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[rn].save(v1, v2, {});
      assertEqual(g[vn1].count(), 2);
      assertEqual(g[vn2].count(), 1);
      assertEqual(g[rn].count(), 1);
      try {
        g[rn].save(v2, v3, {});
        fail();
      } catch (e) {
        // This should create an error
        assertEqual(g[rn].count(), 1);
      }

      try {
        db[rn].save(v2, v3, {});
      } catch (e) {
        // This should not create an error
        fail();
      }
      assertEqual(g[rn].count(), 2);

      db[vn2].remove(v2);
      // This should not remove edges
      assertEqual(g[rn].count(), 2);

      g[vn1].remove(v1);
      // This should remove edges
      assertEqual(g[rn].count(), 1);
      graph._drop(gn, true);
    }

  };

}

// -----------------------------------------------------------------------------
// --SECTION--                                                    Simple Queries
// -----------------------------------------------------------------------------

{'fraction_non_alphanumeric': 0.19627167495699874, 'fraction_numerical': 0.01408581655896983, 'mean_word_length': 1.6141694008992586, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4808534', 'n_tokens_mistral': 6376, 'n_tokens_neox': 5996, 'n_words': 1276}