예제 #1
0
 def setUp(self):
     self.arango = Arango()
     self.db_name = get_next_db_name(self.arango)
     self.db = self.arango.create_database(self.db_name)
     self.col_name = get_next_col_name(self.db)
     self.col = self.db.create_collection(self.col_name)
     # Create the vertex collection
     self.vertex_col_name = get_next_col_name(self.db)
     self.vertex_col = self.db.create_collection(self.vertex_col_name)
     # Create the edge collection
     self.edge_col_name = get_next_col_name(self.db)
     self.edge_col = self.db.create_collection(
         self.edge_col_name, is_edge=True
     )
     # Create the graph
     self.graph_name = get_next_graph_name(self.db)
     self.graph = self.db.create_graph(
         name=self.graph_name,
         edge_definitions=[{
             "collection": self.edge_col_name,
             "from": [self.vertex_col_name],
             "to": [self.vertex_col_name]
         }],
     )
     # Test database cleaup
     self.addCleanup(self.arango.delete_database,
                     name=self.db_name, safe_delete=True)
예제 #2
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)
예제 #3
0
class BatchRequestTest(unittest.TestCase):

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.add_database(self.db_name)
        self.col_name01 = get_next_col_name(self.db)
        self.col01 = self.db.add_collection(self.col_name01)
        self.col_name02 = get_next_col_name(self.db)
        self.col02 = self.db.add_collection(self.col_name02)

    def tearDown(self):
        self.arango.remove_database(self.db_name)

    def test_execute_transaction(self):
        action = """
            function () {
                var db = require('internal').db;
                db.%s.save({ _key: 'doc01'});
                db.%s.save({ _key: 'doc02'});
                return 'success!';
            }
        """ % (self.col_name01, self.col_name02)

        res = self.db.execute_transaction(
            action=action,
            read_collections=[self.col_name01, self.col_name02],
            write_collections=[self.col_name01, self.col_name02],
            wait_for_sync=True,
            lock_timeout=10000
        )
        self.assertEqual(res, "success!")
        self.assertIn("doc01", self.col01)
        self.assertIn("doc02", self.col02)
예제 #4
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)

        # Test database cleaup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)
예제 #5
0
    def setUp(self):
        self.arango = Arango()
        self.username = generate_user_name(self.arango)

        # Test user cleanup
        self.addCleanup(self.arango.delete_user,
                        username=self.username,
                        safe_delete=True)
예제 #6
0
	def __init__(self):
		conn = Arango(host="localhost", port=8529)
		ilanlar=conn.db("ilanlar")
		collections=ilanlar.collections
		self.jobtitles = ilanlar.collection("jobtitles")
		self.ads = ilanlar.collection("ads")
		self.sites = ilanlar.collection("sites")
		self.my_graph = ilanlar.graph("my_graph")
		"""self.my_graph.create_vertex_collection("jobtitles")
예제 #7
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = generate_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)
예제 #8
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name01 = get_next_col_name(self.db)
        self.col01 = self.db.create_collection(self.col_name01)
        self.col_name02 = get_next_col_name(self.db)
        self.col02 = self.db.create_collection(self.col_name02)

        # Test database cleaup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)
예제 #9
0
class AQLFunctionManagementTest(unittest.TestCase):

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.add_database(self.db_name)

    def tearDown(self):
        self.arango.remove_database(self.db_name)

    def test_add_valid_aql_function(self):
        self.db.add_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { return celsius * 1.8 + 32; }"
        )
        self.assertEqual(
            self.db.aql_functions,
            {
                "myfunctions::temperature::celsiustofahrenheit": (
                    "function (celsius) { return celsius * 1.8 + 32; }"
                )
            }
        )

    def test_add_invalid_aql_function(self):
        self.assertRaises(
            AQLFunctionAddError,
            self.db.add_aql_function,
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { invalid syntax }"
        )

    def test_remove_aql_function(self):
        self.db.add_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { return celsius * 1.8 + 32; }"
        )
        self.db.remove_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
        )
        self.assertEqual(self.db.aql_functions, {})

    # TODO create functions within function
    def test_remove_aql_functions_by_group(self):
        self.db.add_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { return celsius * 1.8 + 32; }"
        )
        self.db.remove_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            group=True
        )
        self.assertEqual(self.db.aql_functions, {})
예제 #10
0
class MonitoringTest(unittest.TestCase):

    def setUp(self):
        self.arango = Arango()

    def test_get_log(self):
        self.arango.get_log()

    def test_get_statistics(self):
        self.arango.statistics
        self.arango.statistics_description

    def test_get_server_role(self):
        self.arango.server_role
예제 #11
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = generate_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)
        self.col.create_geo_index(["coord"])
        self.col.create_skiplist_index(["value"])
        self.col.create_fulltext_index(["text"])

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)
예제 #12
0
    def setUp(self):
        # Create the test database
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        # Create the test vertex collection
        self.vertex_col_name = get_next_col_name(self.db)
        self.vertex_col = self.db.create_collection(self.vertex_col_name)
        # Create the test edge collection
        self.edge_col_name = get_next_col_name(self.db)
        self.edge_col = self.db.create_collection(
            self.edge_col_name, is_edge=True
        )
        # Create the test graph
        self.graph_name = get_next_graph_name(self.db)
        self.graph = self.db.create_graph(
            name=self.graph_name,
            edge_definitions=[{
                "collection": self.edge_col_name,
                "from": [self.vertex_col_name],
                "to": [self.vertex_col_name]
            }],
        )
        # Create a few test vertices
        self.graph.create_vertex(
            self.vertex_col_name,
            data={
                "_key": "vertex01",
                "value": 1
            }
        )
        self.graph.create_vertex(
            self.vertex_col_name,
            data={
                "_key": "vertex02",
                "value": 1
            }
        )
        self.graph.create_vertex(
            self.vertex_col_name,
            data={
                "_key": "vertex03",
                "value": 1
            }
        )

        # Test database cleaup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)
예제 #13
0
def connect_to_arango():
    a = Arango(host=ARANGO_HOSTNAME,
               port=ARANGO_PORT,
               username='******',
               password='******')
    try:
        user_info = dict()
        user_info['username'] = '******'
        user_info['passwd'] = 'joker'
        user_info['active'] = True
        db = a.create_database(ARANGODB_NAME, users=[user_info])
        return db
    except:
        db = a.database(ARANGODB_NAME)
        return db
예제 #14
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)

        # Test database cleaup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)
예제 #15
0
    def setUp(self):
        self.arango = Arango()
        self.username = generate_user_name(self.arango)

        # Test user cleanup
        self.addCleanup(self.arango.delete_user,
                        username=self.username, safe_delete=True)
예제 #16
0
 def setUp(self):
     self.arango = Arango()
     self.db_name = get_next_db_name(self.arango)
     self.db = self.arango.add_database(self.db_name)
     self.col_name01 = get_next_col_name(self.db)
     self.col01 = self.db.add_collection(self.col_name01)
     self.col_name02 = get_next_col_name(self.db)
     self.col02 = self.db.add_collection(self.col_name02)
예제 #17
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)
예제 #18
0
class DatabaseManagementTest(unittest.TestCase):
    """Tests for managing ArangoDB databases."""

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)

        # Test database cleaup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)

    def test_database_create_and_delete(self):
        self.arango.create_database(self.db_name)
        self.assertIn(self.db_name, self.arango.databases["all"])

        # Check the properties of the new database
        self.assertEqual(self.arango.database(self.db_name).name,
                         self.db_name)
        self.assertEqual(self.arango.database(self.db_name).is_system, False)

        # Delete the test database
        self.arango.delete_database(self.db_name)
        self.assertNotIn(self.db_name, self.arango.databases["all"])

    def test_database_properties(self):
        db = self.arango.database("_system")
        self.assertEqual(db.name, "_system")
        self.assertTrue(isinstance(db.properties, dict))
        self.assertTrue(is_string(db.id))
        self.assertTrue(is_string(db.path))
        self.assertEqual(db.is_system, True)
예제 #19
0
class DatabaseManagementTest(unittest.TestCase):
    """Tests for managing ArangoDB databases."""

    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)

    def test_database_create_and_delete(self):
        self.arango.create_database(self.db_name)
        self.assertIn(self.db_name, self.arango.databases["all"])

        # Check the properties of the new database
        self.assertEqual(self.arango.database(self.db_name).name,
                         self.db_name)
        self.assertEqual(self.arango.database(self.db_name).is_system, False)

        # Delete the test database
        self.arango.delete_database(self.db_name)
        self.assertNotIn(self.db_name, self.arango.databases["all"])

    def test_database_properties(self):
        db = self.arango.database("_system")
        self.assertEqual(db.name, "_system")
        self.assertTrue(isinstance(db.properties, dict))
        self.assertTrue(is_string(db.id))
        self.assertTrue(is_string(db.file_path))
        self.assertEqual(db.is_system, True)
예제 #20
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = get_next_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)

        # Test database cleaup
        self.addCleanup(self.arango.delete_database, name=self.db_name, safe_delete=True)
예제 #21
0
 def setUp(self):
     self.arango = Arango()
     self.db_name = get_next_db_name(self.arango)
     self.db = self.arango.add_database(self.db_name)
     self.col_name = get_next_col_name(self.db)
     self.col = self.db.add_collection(self.col_name)
     self.col.add_geo_index(["coord"])
     self.col.add_skiplist_index(["value"])
     self.col.add_fulltext_index(["text"])
예제 #22
0
class AQLFunctionManagementTest(unittest.TestCase):
    """Tests for ArangoDB AQL functions."""

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)

        # Test database cleaup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)

    def test_create_valid_aql_function(self):
        self.db.create_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { return celsius * 1.8 + 32; }"
        )
        self.assertEqual(
            self.db.aql_functions,
            {
                "myfunctions::temperature::celsiustofahrenheit": (
                    "function (celsius) { return celsius * 1.8 + 32; }"
                )
            }
        )

    def test_create_invalid_aql_function(self):
        self.assertRaises(
            AQLFunctionCreateError,
            self.db.create_aql_function,
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { invalid syntax }"
        )

    def test_delete_aql_function(self):
        self.db.create_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { return celsius * 1.8 + 32; }"
        )
        self.db.delete_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
        )
        self.assertEqual(self.db.aql_functions, {})

    # TODO create functions within function
    def test_delete_aql_functions_by_group(self):
        self.db.create_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { return celsius * 1.8 + 32; }"
        )
        self.db.delete_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            group=True
        )
        self.assertEqual(self.db.aql_functions, {})
예제 #23
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name01 = generate_col_name(self.db)
        self.col01 = self.db.create_collection(self.col_name01)
        self.col_name02 = generate_col_name(self.db)
        self.col02 = self.db.create_collection(self.col_name02)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)
예제 #24
0
class DatabaseManagementTest(unittest.TestCase):

    def setUp(self):
        self.arango = Arango()

    def test_database_add_and_remove(self):
        db_name = get_next_db_name(self.arango)
        self.arango.add_database(db_name)
        self.assertIn(db_name, self.arango.databases["all"])

        # Check the properties of the new database
        self.assertEqual(self.arango.db(db_name).name, db_name)
        self.assertEqual(self.arango.db(db_name).is_system, False)

        # Remove the test database
        self.arango.remove_database(db_name)
        self.assertNotIn(db_name, self.arango.databases["all"])

    def test_database_properties(self):
        db = self.arango.database("_system")
        self.assertEqual(db.name, "_system")
        self.assertTrue(isinstance(db.properties, dict))
        self.assertTrue(is_string(db.id))
        self.assertTrue(is_string(db.path))
        self.assertEqual(db.is_system, True)
예제 #25
0
def load_into_arango(author_file="/media/sf_Data/AMiner/AMiner-Author.txt", coauthor_file="/media/sf_Data/AMiner/AMiner-Coauthor.txt"):
    print "Connecting to arango"
    a = Arango(host="localhost", port=8529, username='******', password='******')
    try:
        user_info = dict()
        user_info['username'] = '******'
        user_info['passwd'] = 'joker'
        user_info['active'] = True
        db = a.create_database("aminer", users=[user_info])

    except:
        db = a.database("aminer")

    try:
        graph = db.create_graph("aminer_coauthors")
    except:
        graph = db.graph("aminer_coauthors")

    try:
        db.create_collection("authors")
        graph.create_vertex_collection("authors")
        db.create_collection("coauthors", is_edge=True)
        graph.create_edge_definition(edge_collection="coauthors",
                                from_vertex_collections=["authors"],
                                 to_vertex_collections=["authors"])
    except:
        pass

    print "Reading AMiner Data"
    authors, coauthor_relations = get_author_coauthors(author_file, coauthor_file)

    print "Loading authors into arango"
    for key in authors:
        graph.create_vertex("authors", authors[key])
    print "Building coauthor relations"
    for author in coauthor_relations.keys():
        for rel in coauthor_relations[author]:
            graph.create_edge("coauthors", {"_from": "authors/" + unicode(author),
                                            "_to": "authors/"+ unicode(rel[0]),
                                            "w": rel[1]})
예제 #26
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = generate_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)
        self.col.create_geo_index(["coord"])
        self.col.create_skiplist_index(["value"])
        self.col.create_fulltext_index(["text"])

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)
예제 #27
0
class AQLFunctionManagementTest(unittest.TestCase):
    """Tests for ArangoDB AQL functions."""
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)

    def test_create_valid_aql_function(self):
        self.db.create_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { return celsius * 1.8 + 32; }")
        self.assertEqual(
            self.db.aql_functions, {
                "myfunctions::temperature::celsiustofahrenheit":
                ("function (celsius) { return celsius * 1.8 + 32; }")
            })

    def test_create_invalid_aql_function(self):
        self.assertRaises(AQLFunctionCreateError, self.db.create_aql_function,
                          "myfunctions::temperature::celsiustofahrenheit",
                          "function (celsius) { invalid syntax }")

    def test_delete_aql_function(self):
        self.db.create_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { return celsius * 1.8 + 32; }")
        self.db.delete_aql_function(
            "myfunctions::temperature::celsiustofahrenheit", )
        self.assertEqual(self.db.aql_functions, {})

    # TODO create functions within function
    def test_delete_aql_functions_by_group(self):
        self.db.create_aql_function(
            "myfunctions::temperature::celsiustofahrenheit",
            "function (celsius) { return celsius * 1.8 + 32; }")
        self.db.delete_aql_function(
            "myfunctions::temperature::celsiustofahrenheit", group=True)
        self.assertEqual(self.db.aql_functions, {})
예제 #28
0
    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.add_database(self.db_name)
        self.col_name = get_next_col_name(self.db)
        self.col = self.db.add_collection(self.col_name)

        # Create the vertex collection
        self.vertex_col_name = get_next_col_name(self.db)
        self.vertex_col = self.db.add_collection(self.vertex_col_name)
        # Create the edge collection
        self.edge_col_name = get_next_col_name(self.db)
        self.edge_col = self.db.add_collection(self.edge_col_name, is_edge=True)
        # Create the graph
        self.graph_name = get_next_graph_name(self.db)
        self.graph = self.db.add_graph(
            name=self.graph_name,
            edge_definitions=[{
                "collection": self.edge_col_name,
                "from": [self.vertex_col_name],
                "to": [self.vertex_col_name]
            }],
        )
예제 #29
0
class DocumentManagementTest(unittest.TestCase):
    """Tests for ArangoDB document management."""
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = generate_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)

    def test_create_document(self):
        self.assertEqual(len(self.col), 0)
        self.col.create_document({"_key": "test_doc"})
        self.assertEqual(len(self.col), 1)
        self.assertIn("test_doc", self.col)

    def test_delete_document(self):
        rev = self.col.create_document({"_key": "test_doc"})["_rev"]
        self.assertEqual(len(self.col), 1)
        self.assertRaises(DocumentDeleteError,
                          self.col.delete_document,
                          "test_doc",
                          rev="wrong_revision")
        self.col.delete_document("test_doc", rev=rev)
        self.assertEqual(len(self.col), 0)
        self.assertNotIn("test_doc", self.col)

    def test_replace_document(self):
        rev = self.col.create_document({
            "_key": "test_doc",
            "value": 1,
            "value2": 2,
        })["_rev"]
        self.assertRaises(
            DocumentReplaceError,
            self.col.replace_document,
            "test_doc",
            {
                "_rev": "wrong_revision",
                "value": 2
            },
        )
        self.col.replace_document("test_doc", {"_rev": rev, "value": 2})
        self.assertEqual(self.col["test_doc"]["value"], 2)
        self.assertNotIn("value2", self.col["test_doc"])

    def test_update_document(self):
        rev = self.col.create_document({
            "_key": "test_doc",
            "value": 1,
            "value2": 2,
        })["_rev"]
        self.assertRaises(
            DocumentUpdateError,
            self.col.update_document,
            "test_doc",
            {
                "_rev": "wrong_revision",
                "value": 2
            },
        )
        self.col.update_document("test_doc", {"_rev": rev, "new_value": 2})
        self.assertEqual(self.col["test_doc"]["value"], 1)
        self.assertEqual(self.col["test_doc"]["new_value"], 2)

    def test_truncate(self):
        self.col.create_document({"_key": "test_doc_01"})
        self.col.create_document({"_key": "test_doc_02"})
        self.col.create_document({"_key": "test_doc_03"})
        self.assertEqual(len(self.col), 3)
        self.col.truncate()
        self.assertEqual(len(self.col), 0)

    def test_import_documents(self):
        documents = [
            {
                "_key": "test_doc_01"
            },
            {
                "_key": "test_doc_02"
            },
            {
                "_key": 1
            }  # invalid key
        ]
        # This should succeed partially
        res = self.col.import_documents(documents, complete=False)
        self.assertEqual(len(self.col), 2)
        self.assertIn("test_doc_01", self.col)
        self.assertIn("test_doc_01", self.col)
        self.assertEqual(res["errors"], 1)
        self.assertEqual(res["created"], 2)
        # This should fail because of the last document
        self.col.truncate()
        self.assertRaises(DocumentsImportError,
                          self.col.import_documents,
                          documents,
                          complete=True)
        self.assertEqual(len(self.col), 0)
        # This should succeed completely since all documents are valid
        self.col.truncate()
        res = self.col.import_documents(documents[:2], complete=True)
        self.assertEqual(len(self.col), 2)
        self.assertEqual(res["errors"], 0)
        self.assertEqual(res["created"], 2)

    def test_export_documents(self):
        pass
예제 #30
0
class EdgeManagementTest(unittest.TestCase):
    """Tests for ArangoDB graph traversals."""

    def setUp(self):
        # Create the test database
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        # Create the test vertex "from" collection
        self.from_col_name = generate_col_name(self.db)
        self.from_col = self.db.create_collection(self.from_col_name)
        # Create the test vertex "to" collection
        self.to_col_name = generate_col_name(self.db)
        self.to_col = self.db.create_collection(self.to_col_name)
        # Create the test edge collection
        self.edge_col_name = generate_col_name(self.db)
        self.edge_col = self.db.create_collection(
            self.edge_col_name, is_edge=True
        )
        # Create the test graph
        self.graph_name = generate_graph_name(self.db)
        self.graph = self.db.create_graph(
            name=self.graph_name,
            edge_definitions=[{
                "collection": self.edge_col_name,
                "from": [self.from_col_name],
                "to": [self.to_col_name]
            }],
        )
        # Create a few test "from" vertices
        self.graph.create_vertex(
            self.from_col_name,
            data={"_key": "from01", "value": 1}
        )
        self.graph.create_vertex(
            self.from_col_name,
            data={"_key": "from02", "value": 2}
        )
        # Create a few test "to" vertices
        self.graph.create_vertex(
            self.to_col_name,
            data={"_key": "to01", "value": 1}
        )
        self.graph.create_vertex(
            self.to_col_name,
            data={"_key": "to02", "value": 2}
        )
        self.graph.create_vertex(
            self.to_col_name,
            data={"_key": "to03", "value": 3}
        )

        # Create a few test edges
        self.graph.create_edge(
            self.edge_col_name,
            {
                "_from": "{}/{}".format(self.from_col_name, "from01"),
                "_to": "{}/{}".format(self.to_col_name, "to01"),
            }
        )
        self.graph.create_edge(
            self.edge_col_name,
            {
                "_from": "{}/{}".format(self.from_col_name, "from02"),
                "_to": "{}/{}".format(self.to_col_name, "to02"),
            }
        )
        self.graph.create_edge(
            self.edge_col_name,
            {
                "_from": "{}/{}".format(self.from_col_name, "from02"),
                "_to": "{}/{}".format(self.to_col_name, "to03"),
            }

        )
        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)

    def test_basic_traversal(self):
        visited = self.graph.execute_traversal(
            "{}/{}".format(self.from_col_name, "from01"),
            direction="outbound"
        )["visited"]
        self.assertEqual(len(visited["paths"]), 2)
        self.assertEqual(
            [vertex["_id"] for vertex in visited["vertices"]],
            [
                "{}/{}".format(self.from_col_name, "from01"),
                "{}/{}".format(self.to_col_name, "to01"),
            ]
        )
예제 #31
0
#! /usr/bin/env python

import ujson
import fileinput
import arango
from arango import Arango
from pprint import pprint

dbcnx = Arango(host="localhost")
try:
    dbcnx.delete_database("eris0")
except arango.exceptions.DatabaseDeleteError:
    pass
dbcnx.create_database("eris0")
db = dbcnx.database("eris0")
db.create_collection("events")
col = db.collection("events")
col.wait_for_sync = False


def main():
    for line in fileinput.input():
        line = line.strip()
        event = None
        try:
            event = ujson.loads(line)
        except ValueError:
            continue
        event["_key"] = event["id"]
        del event["id"]
        col.create_document(event)
예제 #32
0
class VertexManagementTest(unittest.TestCase):

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.add_database(self.db_name)
        self.col_name = get_next_col_name(self.db)
        self.col = self.db.add_collection(self.col_name)
        # Create the vertex collection
        self.vertex_col_name = get_next_col_name(self.db)
        self.vertex_col = self.db.add_collection(self.vertex_col_name)
        # Create the edge collection
        self.edge_col_name = get_next_col_name(self.db)
        self.edge_col = self.db.add_collection(
            self.edge_col_name, is_edge=True
        )
        # Create the graph
        self.graph_name = get_next_graph_name(self.db)
        self.graph = self.db.add_graph(
            name=self.graph_name,
            edge_definitions=[{
                "collection": self.edge_col_name,
                "from": [self.vertex_col_name],
                "to": [self.vertex_col_name]
            }],
        )

    def tearDown(self):
        self.arango.remove_database(self.db_name)

    def test_add_vertex(self):
        self.graph.add_vertex(
            self.vertex_col_name,
            data={"_key": "vertex01", "value": 10}
        )
        self.assertEqual(self.vertex_col.count, 1)
        self.assertEqual(
            self.graph.get_vertex(
                "{}/{}".format(self.vertex_col_name, "vertex01")
            )["value"],
            10
        )

    def test_update_vertex(self):
        self.graph.add_vertex(
            self.vertex_col_name,
            data={"_key": "vertex01", "value": 10}
        )
        self.graph.update_vertex(
            "{}/{}".format(self.vertex_col_name, "vertex01"),
            data={"value": 20, "new_value": 30}
        )
        self.assertEqual(
            self.graph.get_vertex(
                "{}/{}".format(self.vertex_col_name, "vertex01")
            )["value"],
            20
        )
        self.assertEqual(
            self.graph.get_vertex(
                "{}/{}".format(self.vertex_col_name, "vertex01")
            )["new_value"],
            30
        )

    def test_replace_vertex(self):
        self.graph.add_vertex(
            self.vertex_col_name,
            data={"_key": "vertex01", "value": 10}
        )
        self.graph.replace_vertex(
            "{}/{}".format(self.vertex_col_name, "vertex01"),
            data={"new_value": 30}
        )
        self.assertNotIn(
            "value",
            self.graph.get_vertex(
                "{}/{}".format(self.vertex_col_name, "vertex01")
            )
        )
        self.assertEqual(
            self.graph.get_vertex(
                "{}/{}".format(self.vertex_col_name, "vertex01")
            )["new_value"],
            30
        )

    def test_remove_vertex(self):
        self.graph.add_vertex(
            self.vertex_col_name,
            data={"_key": "vertex01", "value": 10}
        )
        self.graph.remove_vertex(
            "{}/{}".format(self.vertex_col_name, "vertex01")
        )
        self.assertNotIn("vertex01", self.vertex_col)
        self.assertEqual(len(self.vertex_col), 0)
예제 #33
0
from arango import Arango

a = Arango(host="localhost", port=8529, username='******', password='******')
db = a.database("technodeminer")
예제 #34
0
class DocumentManagementTest(unittest.TestCase):
    """Tests for ArangoDB document management."""

    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = generate_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)

    def test_create_document(self):
        self.assertEqual(len(self.col), 0)
        self.col.create_document({"_key": "test_doc"})
        self.assertEqual(len(self.col), 1)
        self.assertIn("test_doc", self.col)

    def test_delete_document(self):
        rev = self.col.create_document({"_key": "test_doc"})["_rev"]
        self.assertEqual(len(self.col), 1)
        self.assertRaises(
            DocumentDeleteError,
            self.col.delete_document,
            "test_doc",
            rev="wrong_revision"
        )
        self.col.delete_document("test_doc", rev=rev)
        self.assertEqual(len(self.col), 0)
        self.assertNotIn("test_doc", self.col)

    def test_replace_document(self):
        rev = self.col.create_document({
            "_key": "test_doc",
            "value": 1,
            "value2": 2,
        })["_rev"]
        self.assertRaises(
            DocumentReplaceError,
            self.col.replace_document,
            "test_doc",
            {"_rev": "wrong_revision", "value": 2},
        )
        self.col.replace_document(
            "test_doc",
            {"_rev": rev, "value": 2}
        )
        self.assertEqual(self.col["test_doc"]["value"], 2)
        self.assertNotIn("value2", self.col["test_doc"])

    def test_update_document(self):
        rev = self.col.create_document({
            "_key": "test_doc",
            "value": 1,
            "value2": 2,
        })["_rev"]
        self.assertRaises(
            DocumentUpdateError,
            self.col.update_document,
            "test_doc",
            {"_rev": "wrong_revision", "value": 2},
        )
        self.col.update_document(
            "test_doc",
            {"_rev": rev, "new_value": 2}
        )
        self.assertEqual(self.col["test_doc"]["value"], 1)
        self.assertEqual(self.col["test_doc"]["new_value"], 2)

    def test_truncate(self):
        self.col.create_document({"_key": "test_doc_01"})
        self.col.create_document({"_key": "test_doc_02"})
        self.col.create_document({"_key": "test_doc_03"})
        self.assertEqual(len(self.col), 3)
        self.col.truncate()
        self.assertEqual(len(self.col), 0)

    def test_import_documents(self):
        documents = [
            {"_key": "test_doc_01"},
            {"_key": "test_doc_02"},
            {"_key": 1}  # invalid key
        ]
        # This should succeed partially
        res = self.col.import_documents(documents, complete=False)
        self.assertEqual(len(self.col), 2)
        self.assertIn("test_doc_01", self.col)
        self.assertIn("test_doc_01", self.col)
        self.assertEqual(res["errors"], 1)
        self.assertEqual(res["created"], 2)
        # This should fail because of the last document
        self.col.truncate()
        self.assertRaises(
            DocumentsImportError,
            self.col.import_documents,
            documents,
            complete=True
        )
        self.assertEqual(len(self.col), 0)
        # This should succeed completely since all documents are valid
        self.col.truncate()
        res = self.col.import_documents(documents[:2], complete=True)
        self.assertEqual(len(self.col), 2)
        self.assertEqual(res["errors"], 0)
        self.assertEqual(res["created"], 2)

    def test_export_documents(self):
        pass
예제 #35
0
import os
import sys
import uuid
from arango import Arango

db_name = 'fstest'
col_name = 'filesystem'
arango = Arango(host="localhost", port=8529)

try:
    arango.delete_database(db_name)
except:
    pass

db = arango.create_database(db_name)
graph = db.create_graph('filesystem')
fsnodes = db.create_collection('fsnodes')
graph.create_vertex_collection('fsnodes')

db.create_collection('contains', is_edge=True)

graph.create_edge_definition(edge_collection='contains',
                             from_vertex_collections=['fsnodes'],
                             to_vertex_collections=['fsnodes'])

for dirname, dirnames, filenames in os.walk(sys.argv[1]):
    key = dirname.replace('/', '_')
    d = dict(type='dir', dirname=dirname, _key=key)
    graph.create_vertex('fsnodes', d)
    for fname in filenames:
        full_filename = filename = os.path.join(dirname, fname)
예제 #36
0
class EdgeManagementTest(unittest.TestCase):
    """Tests for ArangoDB graph traversals."""
    def setUp(self):
        # Create the test database
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        # Create the test vertex "from" collection
        self.from_col_name = generate_col_name(self.db)
        self.from_col = self.db.create_collection(self.from_col_name)
        # Create the test vertex "to" collection
        self.to_col_name = generate_col_name(self.db)
        self.to_col = self.db.create_collection(self.to_col_name)
        # Create the test edge collection
        self.edge_col_name = generate_col_name(self.db)
        self.edge_col = self.db.create_collection(self.edge_col_name,
                                                  is_edge=True)
        # Create the test graph
        self.graph_name = generate_graph_name(self.db)
        self.graph = self.db.create_graph(
            name=self.graph_name,
            edge_definitions=[{
                "collection": self.edge_col_name,
                "from": [self.from_col_name],
                "to": [self.to_col_name]
            }],
        )
        # Create a few test "from" vertices
        self.graph.create_vertex(self.from_col_name,
                                 data={
                                     "_key": "from01",
                                     "value": 1
                                 })
        self.graph.create_vertex(self.from_col_name,
                                 data={
                                     "_key": "from02",
                                     "value": 2
                                 })
        # Create a few test "to" vertices
        self.graph.create_vertex(self.to_col_name,
                                 data={
                                     "_key": "to01",
                                     "value": 1
                                 })
        self.graph.create_vertex(self.to_col_name,
                                 data={
                                     "_key": "to02",
                                     "value": 2
                                 })
        self.graph.create_vertex(self.to_col_name,
                                 data={
                                     "_key": "to03",
                                     "value": 3
                                 })

        # Create a few test edges
        self.graph.create_edge(
            self.edge_col_name, {
                "_from": "{}/{}".format(self.from_col_name, "from01"),
                "_to": "{}/{}".format(self.to_col_name, "to01"),
            })
        self.graph.create_edge(
            self.edge_col_name, {
                "_from": "{}/{}".format(self.from_col_name, "from02"),
                "_to": "{}/{}".format(self.to_col_name, "to02"),
            })
        self.graph.create_edge(
            self.edge_col_name, {
                "_from": "{}/{}".format(self.from_col_name, "from02"),
                "_to": "{}/{}".format(self.to_col_name, "to03"),
            })
        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)

    def test_basic_traversal(self):
        visited = self.graph.execute_traversal("{}/{}".format(
            self.from_col_name, "from01"),
                                               direction="outbound")["visited"]
        self.assertEqual(len(visited["paths"]), 2)
        self.assertEqual([vertex["_id"] for vertex in visited["vertices"]], [
            "{}/{}".format(self.from_col_name, "from01"),
            "{}/{}".format(self.to_col_name, "to01"),
        ])
예제 #37
0
    def setUp(self):
        # Create the test database
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        # Create the test vertex "from" collection
        self.from_col_name = generate_col_name(self.db)
        self.from_col = self.db.create_collection(self.from_col_name)
        # Create the test vertex "to" collection
        self.to_col_name = generate_col_name(self.db)
        self.to_col = self.db.create_collection(self.to_col_name)
        # Create the test edge collection
        self.edge_col_name = generate_col_name(self.db)
        self.edge_col = self.db.create_collection(self.edge_col_name,
                                                  is_edge=True)
        # Create the test graph
        self.graph_name = generate_graph_name(self.db)
        self.graph = self.db.create_graph(
            name=self.graph_name,
            edge_definitions=[{
                "collection": self.edge_col_name,
                "from": [self.from_col_name],
                "to": [self.to_col_name]
            }],
        )
        # Create a few test "from" vertices
        self.graph.create_vertex(self.from_col_name,
                                 data={
                                     "_key": "from01",
                                     "value": 1
                                 })
        self.graph.create_vertex(self.from_col_name,
                                 data={
                                     "_key": "from02",
                                     "value": 2
                                 })
        # Create a few test "to" vertices
        self.graph.create_vertex(self.to_col_name,
                                 data={
                                     "_key": "to01",
                                     "value": 1
                                 })
        self.graph.create_vertex(self.to_col_name,
                                 data={
                                     "_key": "to02",
                                     "value": 2
                                 })
        self.graph.create_vertex(self.to_col_name,
                                 data={
                                     "_key": "to03",
                                     "value": 3
                                 })

        # Create a few test edges
        self.graph.create_edge(
            self.edge_col_name, {
                "_from": "{}/{}".format(self.from_col_name, "from01"),
                "_to": "{}/{}".format(self.to_col_name, "to01"),
            })
        self.graph.create_edge(
            self.edge_col_name, {
                "_from": "{}/{}".format(self.from_col_name, "from02"),
                "_to": "{}/{}".format(self.to_col_name, "to02"),
            })
        self.graph.create_edge(
            self.edge_col_name, {
                "_from": "{}/{}".format(self.from_col_name, "from02"),
                "_to": "{}/{}".format(self.to_col_name, "to03"),
            })
        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)
예제 #38
0
class BatchRequestTest(unittest.TestCase):
    """Tests for ArangoDB transactions."""
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name01 = generate_col_name(self.db)
        self.col01 = self.db.create_collection(self.col_name01)
        self.col_name02 = generate_col_name(self.db)
        self.col02 = self.db.create_collection(self.col_name02)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)

    def test_execute_transaction(self):
        action = """
            function () {
                var db = require('internal').db;
                db.%s.save({ _key: 'doc01'});
                db.%s.save({ _key: 'doc02'});
                return 'success!';
            }
        """ % (self.col_name01, self.col_name02)

        res = self.db.execute_transaction(
            action=action,
            read_collections=[self.col_name01, self.col_name02],
            write_collections=[self.col_name01, self.col_name02],
            wait_for_sync=True,
            lock_timeout=10000)
        self.assertEqual(res, "success!")
        self.assertIn("doc01", self.col01)
        self.assertIn("doc02", self.col02)

    def test_execute_transaction_with_params(self):
        action = """
            function (params) {
                var db = require('internal').db;
                db.%s.save({ _key: 'doc11', val: params.val1 });
                db.%s.save({ _key: 'doc12', val: params.val2 });
                return 'success!';
            }
        """ % (self.col_name01, self.col_name02)

        params = {"val1": 1, "val2": 2}

        res = self.db.execute_transaction(
            action=action,
            read_collections=[self.col_name01, self.col_name02],
            write_collections=[self.col_name01, self.col_name02],
            params=params,
            wait_for_sync=True,
            lock_timeout=10000)

        self.assertEqual(res, "success!")
        self.assertIn("doc11", self.col01)
        self.assertIn("doc12", self.col02)
        self.assertEqual(self.col01["doc11"]["val"], params["val1"])
        self.assertEqual(self.col02["doc12"]["val"], params["val2"])
예제 #39
0
class UserManagementTest(unittest.TestCase):
    def setUp(self):
        self.arango = Arango()
        self.username = generate_user_name(self.arango)

        # Test user cleanup
        self.addCleanup(self.arango.delete_user,
                        username=self.username,
                        safe_delete=True)

    def test_get_user(self):
        self.assertEqual(self.arango.create_user(self.username, "password"), {
            "active": True,
            "change_password": False,
            "extra": {}
        })
        self.assertEqual(self.arango.user(self.username), {
            "active": True,
            "change_password": False,
            "extra": {}
        })
        # Retrieving a non-existing user should fail
        self.assertRaises(UserNotFoundError,
                          self.arango.user,
                          username=generate_user_name(self.arango))

    def test_create_user(self):
        self.assertEqual(
            self.arango.create_user(username=self.username,
                                    password="******",
                                    change_password=True,
                                    extra={"key": "val"}), {
                                        "active": True,
                                        "change_password": True,
                                        "extra": {
                                            "key": "val"
                                        }
                                    })
        self.assertIn(self.username, self.arango.users)
        self.assertEqual(self.arango.user(username=self.username), {
            "active": True,
            "change_password": True,
            "extra": {
                "key": "val"
            }
        })
        # Creating duplicate user should fail
        self.assertRaises(UserCreateError,
                          self.arango.create_user,
                          username=self.username,
                          password="******")

    def test_update_user(self):
        self.assertEqual(self.arango.create_user(self.username, "password"), {
            "active": True,
            "change_password": False,
            "extra": {}
        })
        self.assertEqual(
            self.arango.update_user(username=self.username,
                                    password="******",
                                    change_password=True,
                                    extra={"key": "val"}), {
                                        "active": True,
                                        "change_password": True,
                                        "extra": {
                                            "key": "val"
                                        }
                                    })
        self.assertEqual(self.arango.user(username=self.username), {
            "active": True,
            "change_password": True,
            "extra": {
                "key": "val"
            }
        })
        # Updating a non-existing user should fail
        self.assertRaises(UserUpdateError,
                          self.arango.update_user,
                          username=generate_user_name(self.arango),
                          password="******")

    def test_replace_user(self):
        self.arango.create_user(username=self.username,
                                password="******",
                                change_password=True,
                                extra={"key": "val"}),
        self.assertEqual(
            self.arango.replace_user(
                username=self.username,
                password="******",
            ), {
                "active": True,
                "change_password": False,
                "extra": {}
            })
        self.assertEqual(self.arango.user(username=self.username), {
            "active": True,
            "change_password": False,
            "extra": {}
        })

        # Updating non-existing user should fail
        self.assertRaises(UserReplaceError,
                          self.arango.replace_user,
                          username=generate_user_name(self.arango),
                          password="******")

    def test_delete_user(self):
        self.assertEqual(self.arango.create_user(self.username, "password"), {
            "active": True,
            "change_password": False,
            "extra": {}
        })
        self.assertIn(self.username, self.arango.users)
        self.arango.delete_user(self.username)
        self.assertNotIn(self.username, self.arango.users)

        # Deleting non-existing user should fail
        self.assertRaises(UserDeleteError,
                          self.arango.delete_user,
                          username=generate_user_name(self.arango))
예제 #40
0
class GraphManagementTest(unittest.TestCase):

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.add_database(self.db_name)

    def tearDown(self):
        self.arango.remove_database(self.db_name)

    def test_add_graph(self):
        graph_name = get_next_graph_name(self.db)
        self.db.add_graph(graph_name)
        self.assertIn(graph_name, self.db.graphs)

    def test_remove_graph(self):
        # Add a new collection
        graph_name = get_next_graph_name(self.db)
        self.db.add_graph(graph_name)
        self.assertIn(graph_name, self.db.graphs)
        # Remove the collection and ensure that it's gone
        self.db.remove_graph(graph_name)
        self.assertNotIn(graph_name, self.db.graphs)

    def test_add_graph_with_defined_cols(self):
        # Create the orphan collection
        orphan_col_name = get_next_col_name(self.db)
        self.db.add_collection(orphan_col_name)
        # Create the vertex collection
        vertex_col_name = get_next_col_name(self.db)
        self.db.add_collection(vertex_col_name)
        # Create the edge collection
        edge_col_name = get_next_col_name(self.db)
        self.db.add_collection(edge_col_name, is_edge=True)
        # Create the graph
        graph_name = get_next_graph_name(self.db)
        graph = self.db.add_graph(
            name=graph_name,
            edge_definitions=[{
                "collection": edge_col_name,
                "from": [vertex_col_name],
                "to": [vertex_col_name]
            }],
            orphan_collections=[orphan_col_name]
        )
        self.assertIn(graph_name, self.db.graphs)
        self.assertEqual(
            graph.orphan_collections,
            [orphan_col_name]
        )
        self.assertEqual(
            graph.edge_definitions,
            [{
                "collection": edge_col_name,
                "from": [vertex_col_name],
                "to": [vertex_col_name]
            }]
        )
        self.assertEqual(
            sorted(graph.vertex_collections),
            sorted([orphan_col_name, vertex_col_name])
        )
        properties = graph.properties
        del properties["_rev"]
        del properties["_id"]
        self.assertEqual(
            properties,
            {
                "name": graph_name,
                "edge_definitions": [
                    {
                        "collection": edge_col_name,
                        "from": [vertex_col_name],
                        "to": [vertex_col_name]
                    }
                ],
                "orphan_collections": [orphan_col_name]
            }
        )

    def test_add_and_remove_vertex_collection(self):
        # Create the vertex collection
        vertex_col_name = get_next_col_name(self.db)
        self.db.add_collection(vertex_col_name)
        # Create the graph
        graph_name = get_next_graph_name(self.db)
        graph = self.db.add_graph(graph_name)
        self.assertIn(graph_name, self.db.graphs)
        self.assertEqual(graph.vertex_collections, [])
        # Add the vertex collection to the graph
        graph.add_vertex_collection(vertex_col_name)
        self.assertEqual(
            graph.vertex_collections,
            [vertex_col_name]
        )
        # Remove the vertex collection (completely)
        graph.remove_vertex_collection(
            vertex_col_name,
            drop_collection=True
        )
        self.assertEqual(graph.vertex_collections, [])
        self.assertNotIn(vertex_col_name, self.db.collections["all"])

    def test_add_and_remove_edge_definition(self):
        # Create the edge and vertex collections
        vertex_col_name = get_next_col_name(self.db)
        self.db.add_collection(vertex_col_name)
        edge_col_name = get_next_col_name(self.db)
        self.db.add_collection(edge_col_name, is_edge=True)
        # Create the graph
        graph_name = get_next_graph_name(self.db)
        graph = self.db.add_graph(graph_name)
        # Add the edge definition to the graph
        edge_definition = {
            "collection": edge_col_name,
            "from": [vertex_col_name],
            "to": [vertex_col_name]
        }
        graph.add_edge_definition(
            edge_col_name,
            [vertex_col_name],
            [vertex_col_name]
        )
        self.assertEqual(
            graph.edge_definitions,
            [edge_definition]
        )
        graph.remove_edge_definition(
            edge_col_name,
            drop_collection=True
        )
        self.assertEqual(graph.edge_definitions, [])
        self.assertNotIn(edge_col_name, self.db.collections["all"])

    def test_replace_edge_definition(self):
        # Create edge and vertex collection set 1
        vertex_col_name = get_next_col_name(self.db)
        self.db.add_collection(vertex_col_name)
        edge_col_name = get_next_col_name(self.db)
        self.db.add_collection(edge_col_name, is_edge=True)

        # Create edge and vertex collection set 2
        vertex_col_name_2 = get_next_col_name(self.db)
        self.db.add_collection(vertex_col_name_2)
        edge_col_name_2 = get_next_col_name(self.db)
        self.db.add_collection(edge_col_name_2, is_edge=True)

        # Create the graph
        graph_name = get_next_graph_name(self.db)
        graph = self.db.add_graph(graph_name)

        # Add the edge definition to the graph
        edge_definition = {
            "collection": edge_col_name,
            "from": [vertex_col_name],
            "to": [vertex_col_name]
        }
        graph.add_edge_definition(
            edge_col_name,
            [vertex_col_name],
            [vertex_col_name]
        )
        self.assertEqual(
            graph.edge_definitions,
            [edge_definition]
        )

        # Replace the edge definition 1 with 2
        edge_definition_2 = {
            "collection": edge_col_name,
            "from": [vertex_col_name_2],
            "to": [vertex_col_name_2]
        }
        graph.replace_edge_definition(
            edge_col_name,
            [vertex_col_name_2],
            [vertex_col_name_2]
        )
        self.assertEqual(
            graph.edge_definitions,
            [edge_definition_2]
        )
예제 #41
0
class SimpleQueriesTest(unittest.TestCase):
    """Tests for managing ArangoDB documents."""

    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = generate_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)
        self.col.create_geo_index(["coord"])
        self.col.create_skiplist_index(["value"])
        self.col.create_fulltext_index(["text"])

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)

    def test_first(self):
        self.assertEqual(strip_system_keys(self.col.first(1)), [])
        self.col.import_documents([
            {"name": "test_doc_01"},
            {"name": "test_doc_02"},
            {"name": "test_doc_03"}
        ])
        self.assertEqual(len(self.col), 3)
        self.assertEqual(
            strip_system_keys(self.col.first(1)),
            [{"name": "test_doc_01"}]
        )
        self.assertEqual(
            strip_system_keys(self.col.first(2)),
            [{"name": "test_doc_01"}, {"name": "test_doc_02"}]
        )

    def test_last(self):
        self.assertEqual(strip_system_keys(self.col.last(1)), [])
        self.col.import_documents([
            {"name": "test_doc_01"},
            {"name": "test_doc_02"},
            {"name": "test_doc_03"}
        ])
        self.assertEqual(len(self.col), 3)
        self.assertEqual(
            strip_system_keys(self.col.last(1)),
            [{"name": "test_doc_03"}]
        )
        docs = strip_system_keys(self.col.last(2))
        self.assertIn({"name": "test_doc_03"}, docs)
        self.assertIn({"name": "test_doc_02"}, docs)

    def test_all(self):
        self.assertEqual(list(self.col.all()), [])
        self.col.import_documents([
            {"name": "test_doc_01"},
            {"name": "test_doc_02"},
            {"name": "test_doc_03"}
        ])
        self.assertEqual(len(self.col), 3)

        docs = strip_system_keys(self.col.all())
        self.assertIn({"name": "test_doc_01"}, docs)
        self.assertIn({"name": "test_doc_02"}, docs)
        self.assertIn({"name": "test_doc_03"}, docs)

    def test_any(self):
        self.assertEqual(strip_system_keys(self.col.all()), [])
        self.col.import_documents([
            {"name": "test_doc_01"},
            {"name": "test_doc_02"},
            {"name": "test_doc_03"}
        ])
        self.assertIn(
            strip_system_keys(self.col.any()),
            [
                {"name": "test_doc_01"},
                {"name": "test_doc_02"},
                {"name": "test_doc_03"}
            ]
        )

    def test_get_first_example(self):
        self.assertEqual(
            self.col.get_first_example({"value": 1}), None
        )
        self.col.import_documents([
            {"name": "test_doc_01", "value": 1},
            {"name": "test_doc_02", "value": 1},
            {"name": "test_doc_03", "value": 3}
        ])
        self.assertIn(
            strip_system_keys(self.col.get_first_example({"value": 1})),
            [
                {"name": "test_doc_01", "value": 1},
                {"name": "test_doc_02", "value": 1}
            ]
        )

    def test_get_by_example(self):
        self.col.import_documents([
            {"name": "test_doc_01", "value": 1},
            {"name": "test_doc_02", "value": 1},
            {"name": "test_doc_03", "value": 3}
        ])
        docs = strip_system_keys(self.col.get_by_example({"value": 1}))
        self.assertIn({"name": "test_doc_01", "value": 1}, docs)
        self.assertIn({"name": "test_doc_02", "value": 1}, docs)
        self.assertEqual(
            strip_system_keys(self.col.get_by_example({"value": 2})), []
        )
        self.assertTrue(
            len(list(self.col.get_by_example({"value": 1}, limit=1))), 1
        )

    def test_update_by_example(self):
        self.col.import_documents([
            {"name": "test_doc_01", "value": 1},
            {"name": "test_doc_02", "value": 1},
            {"name": "test_doc_03", "value": 3}
        ])
        self.col.update_by_example({"value": 1}, {"value": 2})
        docs = strip_system_keys(self.col.all())
        self.assertIn({"name": "test_doc_01", "value": 2}, docs)
        self.assertIn({"name": "test_doc_02", "value": 2}, docs)
        self.assertIn({"name": "test_doc_03", "value": 3}, docs)

    def test_replace_by_example(self):
        self.col.import_documents([
            {"name": "test_doc_01", "value": 1},
            {"name": "test_doc_02", "value": 1},
            {"name": "test_doc_03", "value": 3}
        ])
        self.col.replace_by_example({"value": 1}, {"foo": "bar"})

        docs = strip_system_keys(self.col.all())
        self.assertIn({"foo": "bar"}, docs)
        self.assertIn({"name": "test_doc_03", "value": 3}, docs)

    def test_remove_by_example(self):
        self.col.import_documents([
            {"name": "test_doc_01", "value": 1},
            {"name": "test_doc_02", "value": 1},
            {"name": "test_doc_03", "value": 3}
        ])
        self.col.remove_by_example({"value": 1})
        self.col.remove_by_example({"value": 2})
        self.assertEqual(
            strip_system_keys(self.col.all()),
            [{"name": "test_doc_03", "value": 3}]
        )

    def test_range(self):
        self.col.import_documents([
            {"name": "test_doc_01", "value": 1},
            {"name": "test_doc_02", "value": 2},
            {"name": "test_doc_03", "value": 3},
            {"name": "test_doc_04", "value": 4},
            {"name": "test_doc_05", "value": 5}
        ])
        self.assertEqual(
            strip_system_keys(
                self.col.range(
                    attribute="value",
                    left=2,
                    right=5,
                    closed=True,
                    skip=1,
                    limit=2
                )
            ),
            [
                {"name": "test_doc_03", "value": 3},
                {"name": "test_doc_04", "value": 4},
            ]
        )

    def test_near(self):
        self.col.import_documents([
            {"name": "test_doc_01", "coord": [1, 1]},
            {"name": "test_doc_02", "coord": [1, 4]},
            {"name": "test_doc_03", "coord": [4, 1]},
            {"name": "test_doc_03", "coord": [4, 4]},
        ])
        self.assertEqual(
            strip_system_keys(
                self.col.near(
                    latitude=1,
                    longitude=1,
                    limit=1,
                )
            ),
            [
                {"name": "test_doc_01", "coord": [1, 1]}
            ]
        )

    def test_fulltext(self):
        self.col.import_documents([
            {"name": "test_doc_01", "text": "Hello World!"},
            {"name": "test_doc_02", "text": "foo"},
            {"name": "test_doc_03", "text": "bar"},
            {"name": "test_doc_03", "text": "baz"},
        ])
        self.assertEqual(
            strip_system_keys(self.col.fulltext("text", "foo,|bar")),
            [
                {"name": "test_doc_02", "text": "foo"},
                {"name": "test_doc_03", "text": "bar"},
            ]
        )

    def test_lookup_by_keys(self):
        self.col.import_documents([
            {"_key": "key01", "value": 1},
            {"_key": "key02", "value": 2},
            {"_key": "key03", "value": 3},
            {"_key": "key04", "value": 4},
            {"_key": "key05", "value": 5},
            {"_key": "key06", "value": 6},
        ])
        self.assertEqual(
            strip_system_keys(
                self.col.lookup_by_keys(["key01", "key03", "key06"])
            ),
            [
                {"value": 1},
                {"value": 3},
                {"value": 6},
            ]
        )
        self.assertEqual(len(self.col), 6)

    def test_remove_by_keys(self):
        self.col.import_documents([
            {"_key": "key01", "value": 1},
            {"_key": "key02", "value": 2},
            {"_key": "key03", "value": 3},
            {"_key": "key04", "value": 4},
            {"_key": "key05", "value": 5},
            {"_key": "key06", "value": 6},
        ])
        self.assertEqual(
            self.col.remove_by_keys(["key01", "key03", "key06"]),
            {"removed": 3, "ignored": 0}
        )
        leftover = strip_system_keys(self.col.all())
        self.assertEqual(len(leftover), 3)
        self.assertIn({"value": 2}, leftover)
        self.assertIn({"value": 4}, leftover)
        self.assertIn({"value": 5}, leftover)
예제 #42
0
 def setUp(self):
     self.arango = Arango()
     self.db_name = get_next_db_name(self.arango)
     self.db = self.arango.add_database(self.db_name)
예제 #43
0
class GraphManagementTest(unittest.TestCase):
    """Tests for managing ArangoDB graphs."""
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)

    def test_create_graph(self):
        graph_name = generate_graph_name(self.db)
        self.db.create_graph(graph_name)
        self.assertIn(graph_name, self.db.graphs)

    def test_delete_graph(self):
        # Create a new collection
        graph_name = generate_graph_name(self.db)
        self.db.create_graph(graph_name)
        self.assertIn(graph_name, self.db.graphs)
        # Delete the collection and ensure that it's gone
        self.db.delete_graph(graph_name)
        self.assertNotIn(graph_name, self.db.graphs)

    def test_create_graph_with_defined_cols(self):
        # Create the orphan collection
        orphan_col_name = generate_col_name(self.db)
        self.db.create_collection(orphan_col_name)
        # Create the vertex collection
        vertex_col_name = generate_col_name(self.db)
        self.db.create_collection(vertex_col_name)
        # Create the edge collection
        edge_col_name = generate_col_name(self.db)
        self.db.create_collection(edge_col_name, is_edge=True)
        # Create the graph
        graph_name = generate_graph_name(self.db)
        graph = self.db.create_graph(name=graph_name,
                                     edge_definitions=[{
                                         "collection":
                                         edge_col_name,
                                         "from": [vertex_col_name],
                                         "to": [vertex_col_name]
                                     }],
                                     orphan_collections=[orphan_col_name])
        self.assertIn(graph_name, self.db.graphs)
        self.assertEqual(graph.orphan_collections, [orphan_col_name])
        self.assertEqual(graph.edge_definitions, [{
            "collection": edge_col_name,
            "from": [vertex_col_name],
            "to": [vertex_col_name]
        }])
        self.assertEqual(sorted(graph.vertex_collections),
                         sorted([orphan_col_name, vertex_col_name]))
        properties = graph.properties
        del properties["_rev"]
        del properties["_id"]
        self.assertEqual(
            properties, {
                "name":
                graph_name,
                "edge_definitions": [{
                    "collection": edge_col_name,
                    "from": [vertex_col_name],
                    "to": [vertex_col_name]
                }],
                "orphan_collections": [orphan_col_name]
            })

    def test_create_and_delete_vertex_collection(self):
        # Create the vertex collection
        vertex_col_name = generate_col_name(self.db)
        self.db.create_collection(vertex_col_name)
        # Create the graph
        graph_name = generate_graph_name(self.db)
        graph = self.db.create_graph(graph_name)
        self.assertIn(graph_name, self.db.graphs)
        self.assertEqual(graph.vertex_collections, [])
        # Create the vertex collection to the graph
        graph.create_vertex_collection(vertex_col_name)
        self.assertEqual(graph.vertex_collections, [vertex_col_name])
        # Delete the vertex collection (completely)
        graph.delete_vertex_collection(vertex_col_name, drop_collection=True)
        self.assertEqual(graph.vertex_collections, [])
        self.assertNotIn(vertex_col_name, self.db.collections["all"])

    def test_create_and_delete_edge_definition(self):
        # Create the edge and vertex collections
        vertex_col_name = generate_col_name(self.db)
        self.db.create_collection(vertex_col_name)
        edge_col_name = generate_col_name(self.db)
        self.db.create_collection(edge_col_name, is_edge=True)
        # Create the graph
        graph_name = generate_graph_name(self.db)
        graph = self.db.create_graph(graph_name)
        # Create the edge definition to the graph
        edge_definition = {
            "collection": edge_col_name,
            "from": [vertex_col_name],
            "to": [vertex_col_name]
        }
        graph.create_edge_definition(edge_col_name, [vertex_col_name],
                                     [vertex_col_name])
        self.assertEqual(graph.edge_definitions, [edge_definition])
        graph.delete_edge_definition(edge_col_name, drop_collection=True)
        self.assertEqual(graph.edge_definitions, [])
        self.assertNotIn(edge_col_name, self.db.collections["all"])

    def test_replace_edge_definition(self):
        # Create edge and vertex collection set 1
        vertex_col_name = generate_col_name(self.db)
        self.db.create_collection(vertex_col_name)
        edge_col_name = generate_col_name(self.db)
        self.db.create_collection(edge_col_name, is_edge=True)

        # Create edge and vertex collection set 2
        vertex_col_name_2 = generate_col_name(self.db)
        self.db.create_collection(vertex_col_name_2)
        edge_col_name_2 = generate_col_name(self.db)
        self.db.create_collection(edge_col_name_2, is_edge=True)

        # Create the graph
        graph_name = generate_graph_name(self.db)
        graph = self.db.create_graph(graph_name)

        # Create the edge definition to the graph
        edge_definition = {
            "collection": edge_col_name,
            "from": [vertex_col_name],
            "to": [vertex_col_name]
        }
        graph.create_edge_definition(edge_col_name, [vertex_col_name],
                                     [vertex_col_name])
        self.assertEqual(graph.edge_definitions, [edge_definition])

        # Replace the edge definition 1 with 2
        edge_definition_2 = {
            "collection": edge_col_name,
            "from": [vertex_col_name_2],
            "to": [vertex_col_name_2]
        }
        graph.replace_edge_definition(edge_col_name, [vertex_col_name_2],
                                      [vertex_col_name_2])
        self.assertEqual(graph.edge_definitions, [edge_definition_2])
예제 #44
0
class ArangoDBQueryTest(unittest.TestCase):
    """Tests for ArangoDB AQL queries."""
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = generate_col_name(self.db)
        self.db.create_collection(self.col_name)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)

    def test_explain_query(self):
        self.assertRaises(AQLQueryValidateError, self.db.validate_query,
                          "THIS IS AN INVALID QUERY")
        plans = self.db.explain_query(
            "FOR d IN {} RETURN d".format(self.col_name),
            all_plans=True,
            optimizer_rules=["-all", "+use-index-range"])
        for plan in plans:
            self.assertGreaterEqual(
                set(plan), {
                    "collections", "estimated_cost", "estimated_nr_items",
                    "nodes", "rules", "variables"
                })

    def test_validate_query(self):
        self.assertRaises(AQLQueryValidateError, self.db.validate_query,
                          "THIS IS AN INVALID QUERY")
        self.assertEqual(
            None,
            self.db.validate_query("FOR d IN {} RETURN d".format(
                self.col_name)),
        )

    def test_execute_query(self):
        collection = self.db.collection(self.col_name)
        collection.import_documents([
            {
                "_key": "doc01"
            },
            {
                "_key": "doc02"
            },
            {
                "_key": "doc03"
            },
        ])
        res = self.db.execute_query("FOR d IN {} RETURN d".format(
            self.col_name),
                                    count=True,
                                    batch_size=1,
                                    ttl=10,
                                    optimizer_rules=["+all"])
        self.assertEqual(sorted([doc["_key"] for doc in list(res)]),
                         ["doc01", "doc02", "doc03"])

    def test_execute_query_2(self):
        collection = self.db.collection(self.col_name)
        collection.import_documents([
            {
                "_key": "doc01",
                "value": 1
            },
            {
                "_key": "doc02",
                "value": 2
            },
            {
                "_key": "doc03",
                "value": 3
            },
        ])
        res = self.db.execute_query(
            "FOR d IN {} FILTER d.value == @value RETURN d".format(
                self.col_name),
            bind_vars={"value": 1})
        self.assertEqual(sorted([doc["_key"] for doc in list(res)]), ["doc01"])
예제 #45
0
class BatchRequestTest(unittest.TestCase):
    """Tests for ArangoDB batch requests."""

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = get_next_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)

        # Create the vertex collection
        self.vertex_col_name = get_next_col_name(self.db)
        self.vertex_col = self.db.create_collection(self.vertex_col_name)
        # Create the edge collection
        self.edge_col_name = get_next_col_name(self.db)
        self.edge_col = self.db.create_collection(self.edge_col_name, is_edge=True)
        # Create the graph
        self.graph_name = get_next_graph_name(self.db)
        self.graph = self.db.create_graph(
            name=self.graph_name,
            edge_definitions=[
                {"collection": self.edge_col_name, "from": [self.vertex_col_name], "to": [self.vertex_col_name]}
            ],
        )

        # Test database cleaup
        self.addCleanup(self.arango.delete_database, name=self.db_name, safe_delete=True)

    def test_batch_document_create(self):
        self.db.execute_batch(
            [
                (self.col.create_document, [{"_key": "doc01", "value": 1}], {}),
                (self.col.create_document, [{"_key": "doc02", "value": 2}], {}),
                (self.col.create_document, [{"_key": "doc03", "value": 3}], {}),
            ]
        )
        self.assertEqual(len(self.col), 3)
        self.assertEqual(self.col.document("doc01")["value"], 1)
        self.assertEqual(self.col.document("doc02")["value"], 2)
        self.assertEqual(self.col.document("doc03")["value"], 3)

    def test_batch_document_replace(self):
        self.col.import_documents(
            [{"_key": "doc01", "value": 1}, {"_key": "doc02", "value": 1}, {"_key": "doc03", "value": 1}]
        )
        self.db.execute_batch(
            [
                (self.col.replace_document, ["doc01", {"value": 2}], {}),
                (self.col.replace_document, ["doc02", {"value": 2}], {}),
                (self.col.replace_document, ["doc03", {"value": 2}], {}),
            ]
        )
        self.assertEqual(self.col.document("doc01")["value"], 2)
        self.assertEqual(self.col.document("doc02")["value"], 2)
        self.assertEqual(self.col.document("doc03")["value"], 2)

    def test_batch_document_update(self):
        self.col.import_documents(
            [{"_key": "doc01", "value": 1}, {"_key": "doc02", "value": 1}, {"_key": "doc03", "value": 1}]
        )
        self.db.execute_batch(
            [
                (self.col.update_document, ["doc01", {"value": 2}], {"wait_for_sync": True}),
                (self.col.update_document, ["doc02", {"value": 2}], {"wait_for_sync": True}),
                (self.col.update_document, ["doc03", {"value": 2}], {"wait_for_sync": True}),
            ]
        )
        self.assertEqual(self.col.document("doc01")["value"], 2)
        self.assertEqual(self.col.document("doc02")["value"], 2)
        self.assertEqual(self.col.document("doc03")["value"], 2)

    def test_batch_document_Delete(self):
        self.col.import_documents(
            [{"_key": "doc01", "value": 1}, {"_key": "doc02", "value": 1}, {"_key": "doc03", "value": 1}]
        )
        self.db.execute_batch(
            [
                (self.col.delete_document, ["doc01"], {}),
                (self.col.delete_document, ["doc02"], {}),
                (self.col.delete_document, ["doc03"], {}),
            ]
        )
        self.assertEqual(len(self.col), 0)

    def test_batch_document_mixed(self):
        self.col.import_documents(
            [{"_key": "doc01", "value": 0}, {"_key": "doc02", "value": 0}, {"_key": "doc03", "value": 0}]
        )
        self.db.execute_batch(
            [
                (self.col.create_document, [{"_key": "doc04", "value": 1}], {"wait_for_sync": True}),
                (self.col.update_document, ["doc01", {"value": 2}], {"wait_for_sync": True}),
                (self.col.replace_document, ["doc02", {"new_value": 3}], {"wait_for_sync": True}),
                (self.col.delete_document, ["doc03"], {"wait_for_sync": True}),
                (self.col.create_document, [{"_key": "doc05", "value": 5}], {"wait_for_sync": True}),
            ]
        )
        self.assertEqual(len(self.col), 4)
        self.assertEqual(self.col.document("doc01")["value"], 2)
        self.assertEqual(self.col.document("doc02")["new_value"], 3)
        self.assertNotIn("doc03", self.col)
        self.assertEqual(self.col.document("doc04")["value"], 1)
        self.assertEqual(self.col.document("doc05")["value"], 5)

    def test_batch_vertex_create(self):
        self.db.execute_batch(
            [
                (
                    self.graph.create_vertex,
                    [self.vertex_col_name, {"_key": "v01", "value": 1}],
                    {"wait_for_sync": True},
                ),
                (
                    self.graph.create_vertex,
                    [self.vertex_col_name, {"_key": "v02", "value": 2}],
                    {"wait_for_sync": True},
                ),
                (
                    self.graph.create_vertex,
                    [self.vertex_col_name, {"_key": "v03", "value": 3}],
                    {"wait_for_sync": True},
                ),
            ]
        )
        self.assertEqual(self.vertex_col.document("v01")["value"], 1)
        self.assertEqual(self.vertex_col.document("v02")["value"], 2)
        self.assertEqual(self.vertex_col.document("v03")["value"], 3)

    def test_batch_vertex_replace(self):
        self.vertex_col.import_documents(
            [{"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1}]
        )
        self.db.execute_batch(
            [
                (
                    self.graph.replace_vertex,
                    ["{}/{}".format(self.vertex_col_name, "v01"), {"new_val": 2}],
                    {"wait_for_sync": True},
                ),
                (
                    self.graph.replace_vertex,
                    ["{}/{}".format(self.vertex_col_name, "v02"), {"new_val": 3}],
                    {"wait_for_sync": True},
                ),
                (
                    self.graph.replace_vertex,
                    ["{}/{}".format(self.vertex_col_name, "v03"), {"new_val": 4}],
                    {"wait_for_sync": True},
                ),
            ]
        )
        self.assertEqual(self.vertex_col.document("v01")["new_val"], 2)
        self.assertEqual(self.vertex_col.document("v02")["new_val"], 3)
        self.assertEqual(self.vertex_col.document("v03")["new_val"], 4)

    def test_batch_vertex_update(self):
        self.vertex_col.import_documents(
            [{"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1}]
        )
        self.db.execute_batch(
            [
                (
                    self.graph.update_vertex,
                    ["{}/{}".format(self.vertex_col_name, "v01"), {"value": 2}],
                    {"wait_for_sync": True},
                ),
                (
                    self.graph.update_vertex,
                    ["{}/{}".format(self.vertex_col_name, "v02"), {"value": 3}],
                    {"wait_for_sync": True},
                ),
                (
                    self.graph.update_vertex,
                    ["{}/{}".format(self.vertex_col_name, "v03"), {"value": 4}],
                    {"wait_for_sync": True},
                ),
            ]
        )
        self.assertEqual(self.vertex_col.document("v01")["value"], 2)
        self.assertEqual(self.vertex_col.document("v02")["value"], 3)
        self.assertEqual(self.vertex_col.document("v03")["value"], 4)

    def test_batch_vertex_Delete(self):
        self.vertex_col.import_documents(
            [{"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1}]
        )
        self.db.execute_batch(
            [
                (self.graph.delete_vertex, ["{}/{}".format(self.vertex_col_name, "v01")], {"wait_for_sync": True}),
                (self.graph.delete_vertex, ["{}/{}".format(self.vertex_col_name, "v02")], {"wait_for_sync": True}),
                (self.graph.delete_vertex, ["{}/{}".format(self.vertex_col_name, "v03")], {"wait_for_sync": True}),
            ]
        )
        self.assertEqual(len(self.vertex_col), 0)

    def test_batch_edge_create(self):
        self.vertex_col.import_documents(
            [{"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1}]
        )
        self.db.execute_batch(
            [
                (
                    self.graph.create_edge,
                    [self.edge_col_name],
                    {
                        "data": {
                            "_key": "e01",
                            "_from": "{}/{}".format(self.vertex_col_name, "v01"),
                            "_to": "{}/{}".format(self.vertex_col_name, "v02"),
                            "value": 4,
                        }
                    },
                ),
                (
                    self.graph.create_edge,
                    [self.edge_col_name],
                    {
                        "data": {
                            "_key": "e02",
                            "_from": "{}/{}".format(self.vertex_col_name, "v02"),
                            "_to": "{}/{}".format(self.vertex_col_name, "v03"),
                            "value": 5,
                        }
                    },
                ),
            ]
        )
        self.assertEqual(self.edge_col.document("e01")["value"], 4)
        self.assertEqual(self.edge_col.document("e02")["value"], 5)

    def test_batch_edge_replace(self):
        self.vertex_col.import_documents(
            [{"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1}]
        )
        self.db.execute_batch(
            [
                (
                    self.graph.create_edge,
                    [self.edge_col_name],
                    {
                        "data": {
                            "_key": "e01",
                            "_from": "{}/{}".format(self.vertex_col_name, "v01"),
                            "_to": "{}/{}".format(self.vertex_col_name, "v02"),
                            "value": 4,
                        }
                    },
                ),
                (
                    self.graph.create_edge,
                    [self.edge_col_name],
                    {
                        "data": {
                            "_key": "e02",
                            "_from": "{}/{}".format(self.vertex_col_name, "v02"),
                            "_to": "{}/{}".format(self.vertex_col_name, "v03"),
                            "value": 5,
                        }
                    },
                ),
            ]
        )
        self.db.execute_batch(
            [
                (self.graph.replace_edge, ["{}/{}".format(self.edge_col_name, "e01")], {"data": {"new_val": 1}}),
                (self.graph.replace_edge, ["{}/{}".format(self.edge_col_name, "e02")], {"data": {"new_val": 2}}),
            ]
        )
        self.assertEqual(self.edge_col.document("e01")["new_val"], 1)
        self.assertEqual(self.edge_col.document("e02")["new_val"], 2)

    def test_batch_edge_update(self):
        self.vertex_col.import_documents(
            [{"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1}]
        )
        self.db.execute_batch(
            [
                (
                    self.graph.create_edge,
                    [self.edge_col_name],
                    {
                        "data": {
                            "_key": "e01",
                            "_from": "{}/{}".format(self.vertex_col_name, "v01"),
                            "_to": "{}/{}".format(self.vertex_col_name, "v02"),
                            "value": 4,
                        }
                    },
                ),
                (
                    self.graph.create_edge,
                    [self.edge_col_name],
                    {
                        "data": {
                            "_key": "e02",
                            "_from": "{}/{}".format(self.vertex_col_name, "v02"),
                            "_to": "{}/{}".format(self.vertex_col_name, "v03"),
                            "value": 5,
                        }
                    },
                ),
            ]
        )
        self.db.execute_batch(
            [
                (self.graph.replace_edge, ["{}/{}".format(self.edge_col_name, "e01")], {"data": {"value": 1}}),
                (self.graph.replace_edge, ["{}/{}".format(self.edge_col_name, "e02")], {"data": {"value": 2}}),
            ]
        )
        self.assertEqual(self.edge_col.document("e01")["value"], 1)
        self.assertEqual(self.edge_col.document("e02")["value"], 2)

    def test_batch_edge_Delete(self):
        self.vertex_col.import_documents(
            [{"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1}]
        )
        self.db.execute_batch(
            [
                (
                    self.graph.create_edge,
                    [self.edge_col_name],
                    {
                        "data": {
                            "_key": "e01",
                            "_from": "{}/{}".format(self.vertex_col_name, "v01"),
                            "_to": "{}/{}".format(self.vertex_col_name, "v02"),
                            "value": 4,
                        }
                    },
                ),
                (
                    self.graph.create_edge,
                    [self.edge_col_name],
                    {
                        "data": {
                            "_key": "e02",
                            "_from": "{}/{}".format(self.vertex_col_name, "v02"),
                            "_to": "{}/{}".format(self.vertex_col_name, "v03"),
                            "value": 5,
                        }
                    },
                ),
            ]
        )
        self.assertEqual(len(self.edge_col), 2)
        self.db.execute_batch(
            [
                (self.graph.delete_edge, ["{}/{}".format(self.edge_col_name, "e01")], {}),
                (self.graph.delete_edge, ["{}/{}".format(self.edge_col_name, "e02")], {}),
            ]
        )
        self.assertEqual(len(self.edge_col), 0)
예제 #46
0
class BatchRequestTest(unittest.TestCase):
    """Tests for ArangoDB batch requests."""

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = get_next_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)

        # Create the vertex collection
        self.vertex_col_name = get_next_col_name(self.db)
        self.vertex_col = self.db.create_collection(self.vertex_col_name)
        # Create the edge collection
        self.edge_col_name = get_next_col_name(self.db)
        self.edge_col = self.db.create_collection(
            self.edge_col_name, is_edge=True
        )
        # Create the graph
        self.graph_name = get_next_graph_name(self.db)
        self.graph = self.db.create_graph(
            name=self.graph_name,
            edge_definitions=[{
                "collection": self.edge_col_name,
                "from": [self.vertex_col_name],
                "to": [self.vertex_col_name]
            }],
        )

        # Test database cleaup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)

    def test_batch_document_create(self):
        self.db.execute_batch([
            (self.col.create_document, [{"_key": "doc01", "value": 1}], {}),
            (self.col.create_document, [{"_key": "doc02", "value": 2}], {}),
            (self.col.create_document, [{"_key": "doc03", "value": 3}], {}),
        ])
        self.assertEqual(len(self.col), 3)
        self.assertEqual(self.col.document("doc01")["value"], 1)
        self.assertEqual(self.col.document("doc02")["value"], 2)
        self.assertEqual(self.col.document("doc03")["value"], 3)

    def test_batch_document_replace(self):
        self.col.import_documents([
            {"_key": "doc01", "value": 1},
            {"_key": "doc02", "value": 1},
            {"_key": "doc03", "value": 1}
        ])
        self.db.execute_batch([
            (self.col.replace_document, ["doc01", {"value": 2}], {}),
            (self.col.replace_document, ["doc02", {"value": 2}], {}),
            (self.col.replace_document, ["doc03", {"value": 2}], {}),
        ])
        self.assertEqual(self.col.document("doc01")["value"], 2)
        self.assertEqual(self.col.document("doc02")["value"], 2)
        self.assertEqual(self.col.document("doc03")["value"], 2)

    def test_batch_document_update(self):
        self.col.import_documents([
            {"_key": "doc01", "value": 1},
            {"_key": "doc02", "value": 1},
            {"_key": "doc03", "value": 1}
        ])
        self.db.execute_batch([
            (
                self.col.update_document,
                ["doc01", {"value": 2}],
                {"wait_for_sync": True}
            ),
            (
                self.col.update_document,
                ["doc02", {"value": 2}],
                {"wait_for_sync": True}
            ),
            (
                self.col.update_document,
                ["doc03", {"value": 2}],
                {"wait_for_sync": True}
            ),
        ])
        self.assertEqual(self.col.document("doc01")["value"], 2)
        self.assertEqual(self.col.document("doc02")["value"], 2)
        self.assertEqual(self.col.document("doc03")["value"], 2)

    def test_batch_document_Delete(self):
        self.col.import_documents([
            {"_key": "doc01", "value": 1},
            {"_key": "doc02", "value": 1},
            {"_key": "doc03", "value": 1}
        ])
        self.db.execute_batch([
            (self.col.delete_document, ["doc01"], {}),
            (self.col.delete_document, ["doc02"], {}),
            (self.col.delete_document, ["doc03"], {}),
        ])
        self.assertEqual(len(self.col), 0)

    def test_batch_document_mixed(self):
        self.col.import_documents([
            {"_key": "doc01", "value": 0},
            {"_key": "doc02", "value": 0},
            {"_key": "doc03", "value": 0}
        ])
        self.db.execute_batch([
            (
                self.col.create_document,
                [{"_key": "doc04", "value": 1}],
                {"wait_for_sync": True}
            ),
            (
                self.col.update_document,
                ["doc01", {"value": 2}],
                {"wait_for_sync": True}
            ),
            (
                self.col.replace_document,
                ["doc02", {"new_value": 3}],
                {"wait_for_sync": True}
            ),
            (
                self.col.delete_document,
                ["doc03"],
                {"wait_for_sync": True}
            ),
            (
                self.col.create_document,
                [{"_key": "doc05", "value": 5}],
                {"wait_for_sync": True}
            ),
        ])
        self.assertEqual(len(self.col), 4)
        self.assertEqual(self.col.document("doc01")["value"], 2)
        self.assertEqual(self.col.document("doc02")["new_value"], 3)
        self.assertNotIn("doc03", self.col)
        self.assertEqual(self.col.document("doc04")["value"], 1)
        self.assertEqual(self.col.document("doc05")["value"], 5)

    def test_batch_vertex_create(self):
        self.db.execute_batch([
            (
                self.graph.create_vertex,
                [self.vertex_col_name, {"_key": "v01", "value": 1}],
                {"wait_for_sync": True}
            ),
            (
                self.graph.create_vertex,
                [self.vertex_col_name, {"_key": "v02", "value": 2}],
                {"wait_for_sync": True}
            ),
            (
                self.graph.create_vertex,
                [self.vertex_col_name, {"_key": "v03", "value": 3}],
                {"wait_for_sync": True}
            ),
        ])
        self.assertEqual(self.vertex_col.document("v01")["value"], 1)
        self.assertEqual(self.vertex_col.document("v02")["value"], 2)
        self.assertEqual(self.vertex_col.document("v03")["value"], 3)

    def test_batch_vertex_replace(self):
        self.vertex_col.import_documents([
            {"_key": "v01", "value": 1},
            {"_key": "v02", "value": 1},
            {"_key": "v03", "value": 1}
        ])
        self.db.execute_batch([
            (
                self.graph.replace_vertex,
                [
                    "{}/{}".format(self.vertex_col_name, "v01"),
                    {"new_val": 2}
                ],
                {"wait_for_sync": True}
            ),
            (
                self.graph.replace_vertex,
                [
                    "{}/{}".format(self.vertex_col_name, "v02"),
                    {"new_val": 3}
                ],
                {"wait_for_sync": True}
            ),
            (
                self.graph.replace_vertex,
                [
                    "{}/{}".format(self.vertex_col_name, "v03"),
                    {"new_val": 4}
                ],
                {"wait_for_sync": True}
            ),
        ])
        self.assertEqual(self.vertex_col.document("v01")["new_val"], 2)
        self.assertEqual(self.vertex_col.document("v02")["new_val"], 3)
        self.assertEqual(self.vertex_col.document("v03")["new_val"], 4)

    def test_batch_vertex_update(self):
        self.vertex_col.import_documents([
            {"_key": "v01", "value": 1},
            {"_key": "v02", "value": 1},
            {"_key": "v03", "value": 1}
        ])
        self.db.execute_batch([
            (
                self.graph.update_vertex,
                ["{}/{}".format(self.vertex_col_name, "v01"), {"value": 2}],
                {"wait_for_sync": True}
            ),
            (
                self.graph.update_vertex,
                ["{}/{}".format(self.vertex_col_name, "v02"), {"value": 3}],
                {"wait_for_sync": True}
            ),
            (
                self.graph.update_vertex,
                ["{}/{}".format(self.vertex_col_name, "v03"), {"value": 4}],
                {"wait_for_sync": True}
            ),
        ])
        self.assertEqual(self.vertex_col.document("v01")["value"], 2)
        self.assertEqual(self.vertex_col.document("v02")["value"], 3)
        self.assertEqual(self.vertex_col.document("v03")["value"], 4)

    def test_batch_vertex_Delete(self):
        self.vertex_col.import_documents([
            {"_key": "v01", "value": 1},
            {"_key": "v02", "value": 1},
            {"_key": "v03", "value": 1}
        ])
        self.db.execute_batch([
            (
                self.graph.delete_vertex,
                ["{}/{}".format(self.vertex_col_name, "v01")],
                {"wait_for_sync": True}
            ),
            (
                self.graph.delete_vertex,
                ["{}/{}".format(self.vertex_col_name, "v02")],
                {"wait_for_sync": True}
            ),
            (
                self.graph.delete_vertex,
                ["{}/{}".format(self.vertex_col_name, "v03")],
                {"wait_for_sync": True}
            ),
        ])
        self.assertEqual(len(self.vertex_col), 0)

    def test_batch_edge_create(self):
        self.vertex_col.import_documents([
            {"_key": "v01", "value": 1},
            {"_key": "v02", "value": 1},
            {"_key": "v03", "value": 1}
        ])
        self.db.execute_batch([
            (
                self.graph.create_edge,
                [self.edge_col_name],
                {
                    "data": {
                        "_key": "e01",
                        "_from": "{}/{}".format(self.vertex_col_name, "v01"),
                        "_to": "{}/{}".format(self.vertex_col_name, "v02"),
                        "value": 4
                    }
                }
            ),
            (
                self.graph.create_edge,
                [self.edge_col_name],
                {
                    "data": {
                        "_key": "e02",
                        "_from": "{}/{}".format(self.vertex_col_name, "v02"),
                        "_to": "{}/{}".format(self.vertex_col_name, "v03"),
                        "value": 5
                    }
                }
            ),
        ])
        self.assertEqual(self.edge_col.document("e01")["value"], 4)
        self.assertEqual(self.edge_col.document("e02")["value"], 5)

    def test_batch_edge_replace(self):
        self.vertex_col.import_documents([
            {"_key": "v01", "value": 1},
            {"_key": "v02", "value": 1},
            {"_key": "v03", "value": 1}
        ])
        self.db.execute_batch([
            (
                self.graph.create_edge,
                [self.edge_col_name],
                {
                    "data": {
                        "_key": "e01",
                        "_from": "{}/{}".format(self.vertex_col_name, "v01"),
                        "_to": "{}/{}".format(self.vertex_col_name, "v02"),
                        "value": 4
                    }
                }
            ),
            (
                self.graph.create_edge,
                [self.edge_col_name],
                {
                    "data": {
                        "_key": "e02",
                        "_from": "{}/{}".format(self.vertex_col_name, "v02"),
                        "_to": "{}/{}".format(self.vertex_col_name, "v03"),
                        "value": 5
                    }
                }
            ),
        ])
        self.db.execute_batch([
            (
                self.graph.replace_edge,
                ["{}/{}".format(self.edge_col_name, "e01")],
                {"data": {"new_val": 1}}
            ),
            (
                self.graph.replace_edge,
                ["{}/{}".format(self.edge_col_name, "e02")],
                {"data": {"new_val": 2}}
            ),
        ])
        self.assertEqual(self.edge_col.document("e01")["new_val"], 1)
        self.assertEqual(self.edge_col.document("e02")["new_val"], 2)

    def test_batch_edge_update(self):
        self.vertex_col.import_documents([
            {"_key": "v01", "value": 1},
            {"_key": "v02", "value": 1},
            {"_key": "v03", "value": 1}
        ])
        self.db.execute_batch([
            (
                self.graph.create_edge,
                [self.edge_col_name],
                {
                    "data": {
                        "_key": "e01",
                        "_from": "{}/{}".format(self.vertex_col_name, "v01"),
                        "_to": "{}/{}".format(self.vertex_col_name, "v02"),
                        "value": 4
                    }
                }
            ),
            (
                self.graph.create_edge,
                [self.edge_col_name],
                {
                    "data": {
                        "_key": "e02",
                        "_from": "{}/{}".format(self.vertex_col_name, "v02"),
                        "_to": "{}/{}".format(self.vertex_col_name, "v03"),
                        "value": 5
                    }
                }
            ),
        ])
        self.db.execute_batch([
            (
                self.graph.replace_edge,
                ["{}/{}".format(self.edge_col_name, "e01")],
                {"data": {"value": 1}}
            ),
            (
                self.graph.replace_edge,
                ["{}/{}".format(self.edge_col_name, "e02")],
                {"data": {"value": 2}}
            ),
        ])
        self.assertEqual(self.edge_col.document("e01")["value"], 1)
        self.assertEqual(self.edge_col.document("e02")["value"], 2)

    def test_batch_edge_Delete(self):
        self.vertex_col.import_documents([
            {"_key": "v01", "value": 1},
            {"_key": "v02", "value": 1},
            {"_key": "v03", "value": 1}
        ])
        self.db.execute_batch([
            (
                self.graph.create_edge,
                [self.edge_col_name],
                {
                    "data": {
                        "_key": "e01",
                        "_from": "{}/{}".format(self.vertex_col_name, "v01"),
                        "_to": "{}/{}".format(self.vertex_col_name, "v02"),
                        "value": 4
                    }
                }
            ),
            (
                self.graph.create_edge,
                [self.edge_col_name],
                {
                    "data": {
                        "_key": "e02",
                        "_from": "{}/{}".format(self.vertex_col_name, "v02"),
                        "_to": "{}/{}".format(self.vertex_col_name, "v03"),
                        "value": 5
                    }
                }
            ),
        ])
        self.assertEqual(len(self.edge_col), 2)
        self.db.execute_batch([
            (
                self.graph.delete_edge,
                ["{}/{}".format(self.edge_col_name, "e01")],
                {}
            ),
            (
                self.graph.delete_edge,
                ["{}/{}".format(self.edge_col_name, "e02")],
                {}
            ),
        ])
        self.assertEqual(len(self.edge_col), 0)
예제 #47
0
    def setUp(self):
        # Create the test database
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        # Create the test vertex "from" collection
        self.from_col_name = generate_col_name(self.db)
        self.from_col = self.db.create_collection(self.from_col_name)
        # Create the test vertex "to" collection
        self.to_col_name = generate_col_name(self.db)
        self.to_col = self.db.create_collection(self.to_col_name)
        # Create the test edge collection
        self.edge_col_name = generate_col_name(self.db)
        self.edge_col = self.db.create_collection(
            self.edge_col_name, is_edge=True
        )
        # Create the test graph
        self.graph_name = generate_graph_name(self.db)
        self.graph = self.db.create_graph(
            name=self.graph_name,
            edge_definitions=[{
                "collection": self.edge_col_name,
                "from": [self.from_col_name],
                "to": [self.to_col_name]
            }],
        )
        # Create a few test "from" vertices
        self.graph.create_vertex(
            self.from_col_name,
            data={"_key": "from01", "value": 1}
        )
        self.graph.create_vertex(
            self.from_col_name,
            data={"_key": "from02", "value": 2}
        )
        # Create a few test "to" vertices
        self.graph.create_vertex(
            self.to_col_name,
            data={"_key": "to01", "value": 1}
        )
        self.graph.create_vertex(
            self.to_col_name,
            data={"_key": "to02", "value": 2}
        )
        self.graph.create_vertex(
            self.to_col_name,
            data={"_key": "to03", "value": 3}
        )

        # Create a few test edges
        self.graph.create_edge(
            self.edge_col_name,
            {
                "_from": "{}/{}".format(self.from_col_name, "from01"),
                "_to": "{}/{}".format(self.to_col_name, "to01"),
            }
        )
        self.graph.create_edge(
            self.edge_col_name,
            {
                "_from": "{}/{}".format(self.from_col_name, "from02"),
                "_to": "{}/{}".format(self.to_col_name, "to02"),
            }
        )
        self.graph.create_edge(
            self.edge_col_name,
            {
                "_from": "{}/{}".format(self.from_col_name, "from02"),
                "_to": "{}/{}".format(self.to_col_name, "to03"),
            }

        )
        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)
예제 #48
0
class IndexManagementTest(unittest.TestCase):
    """Tests for managing ArangoDB indexes."""

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = get_next_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)

        # Test database cleaup
        self.addCleanup(self.arango.delete_database, name=self.db_name, safe_delete=True)

    def test_list_indexes(self):
        self.assertIn(
            {"selectivity_estimate": 1, "sparse": False, "type": "primary", "fields": ["_key"], "unique": True},
            self.col.indexes.values(),
        )

    def test_create_hash_index(self):
        self.col.create_hash_index(["attr1", "attr2"], unique=True)
        self.assertIn(
            {"selectivity_estimate": 1, "sparse": False, "type": "hash", "fields": ["attr1", "attr2"], "unique": True},
            self.col.indexes.values(),
        )

    def test_create_cap_constraint(self):
        self.col.create_cap_constraint(size=10, byte_size=40000)
        self.assertIn({"type": "cap", "size": 10, "byte_size": 40000, "unique": False}, self.col.indexes.values())

    def test_create_skiplist_index(self):
        self.col.create_skiplist_index(["attr1", "attr2"], unique=True)
        self.assertIn(
            {"sparse": False, "type": "skiplist", "fields": ["attr1", "attr2"], "unique": True},
            self.col.indexes.values(),
        )

    def test_create_geo_index_with_one_attr(self):
        self.col.create_geo_index(fields=["attr1"], geo_json=False)
        self.assertIn(
            {
                "sparse": True,
                "type": "geo1",
                "fields": ["attr1"],
                "unique": False,
                "geo_json": False,
                "ignore_null": True,
                "constraint": False,
            },
            self.col.indexes.values(),
        )

    def test_create_geo_index_with_two_attrs(self):
        self.col.create_geo_index(fields=["attr1", "attr2"], geo_json=False)
        self.assertIn(
            {
                "sparse": True,
                "type": "geo2",
                "fields": ["attr1", "attr2"],
                "unique": False,
                "ignore_null": True,
                "constraint": False,
            },
            self.col.indexes.values(),
        )

    def test_create_geo_index_with_more_than_two_attrs(self):
        self.assertRaises(IndexCreateError, self.col.create_geo_index, fields=["attr1", "attr2", "attr3"])

    def test_create_fulltext_index(self):
        self.assertRaises(IndexCreateError, self.col.create_fulltext_index, fields=["attr1", "attr2"])
        self.col.create_fulltext_index(fields=["attr1"], min_length=10)
        self.assertIn(
            {"sparse": True, "type": "fulltext", "fields": ["attr1"], "min_length": 10, "unique": False},
            self.col.indexes.values(),
        )

    def test_delete_index(self):
        old_indexes = set(self.col.indexes)
        self.col.create_hash_index(["attr1", "attr2"], unique=True)
        self.col.create_skiplist_index(["attr1", "attr2"], unique=True)
        self.col.create_fulltext_index(fields=["attr1"], min_length=10)
        new_indexes = set(self.col.indexes)
        self.assertNotEqual(old_indexes, new_indexes)

        for index_id in new_indexes - old_indexes:
            self.col.delete_index(index_id)

        self.assertEqual(old_indexes, set(self.col.indexes))
예제 #49
0
class IndexManagementTest(unittest.TestCase):
    """Tests for managing ArangoDB indexes."""
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = generate_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)

    def test_list_indexes(self):
        self.assertIn(
            {
                "selectivity_estimate": 1,
                "sparse": False,
                "type": "primary",
                "fields": ["_key"],
                "unique": True
            }, self.col.indexes.values())

    def test_create_hash_index(self):
        self.col.create_hash_index(["attr1", "attr2"], unique=True)
        self.assertIn(
            {
                "selectivity_estimate": 1,
                "sparse": False,
                "type": "hash",
                "fields": ["attr1", "attr2"],
                "unique": True
            }, self.col.indexes.values())

    def test_create_cap_constraint(self):
        self.col.create_cap_constraint(size=10, byte_size=40000)
        self.assertIn(
            {
                "type": "cap",
                "size": 10,
                "byte_size": 40000,
                "unique": False
            }, self.col.indexes.values())

    def test_create_skiplist_index(self):
        self.col.create_skiplist_index(["attr1", "attr2"], unique=True)
        self.assertIn(
            {
                "sparse": False,
                "type": "skiplist",
                "fields": ["attr1", "attr2"],
                "unique": True
            }, self.col.indexes.values())

    def test_create_geo_index_with_one_attr(self):
        self.col.create_geo_index(
            fields=["attr1"],
            geo_json=False,
        )
        self.assertIn(
            {
                "sparse": True,
                "type": "geo1",
                "fields": ["attr1"],
                "unique": False,
                "geo_json": False,
                "ignore_null": True,
                "constraint": False
            }, self.col.indexes.values())

    def test_create_geo_index_with_two_attrs(self):
        self.col.create_geo_index(
            fields=["attr1", "attr2"],
            geo_json=False,
        )
        self.assertIn(
            {
                "sparse": True,
                "type": "geo2",
                "fields": ["attr1", "attr2"],
                "unique": False,
                "ignore_null": True,
                "constraint": False
            }, self.col.indexes.values())

    def test_create_geo_index_with_more_than_two_attrs(self):
        self.assertRaises(IndexCreateError,
                          self.col.create_geo_index,
                          fields=["attr1", "attr2", "attr3"])

    def test_create_fulltext_index(self):
        self.assertRaises(IndexCreateError,
                          self.col.create_fulltext_index,
                          fields=["attr1", "attr2"])
        self.col.create_fulltext_index(
            fields=["attr1"],
            min_length=10,
        )
        self.assertIn(
            {
                "sparse": True,
                "type": "fulltext",
                "fields": ["attr1"],
                "min_length": 10,
                "unique": False,
            }, self.col.indexes.values())

    def test_delete_index(self):
        old_indexes = set(self.col.indexes)
        self.col.create_hash_index(["attr1", "attr2"], unique=True)
        self.col.create_skiplist_index(["attr1", "attr2"], unique=True)
        self.col.create_fulltext_index(
            fields=["attr1"],
            min_length=10,
        )
        new_indexes = set(self.col.indexes)
        self.assertNotEqual(old_indexes, new_indexes)

        for index_id in new_indexes - old_indexes:
            self.col.delete_index(index_id)

        self.assertEqual(old_indexes, set(self.col.indexes))
예제 #50
0
class SimpleQueriesTest(unittest.TestCase):
    """Tests for managing ArangoDB documents."""
    def setUp(self):
        self.arango = Arango()
        self.db_name = generate_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = generate_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)
        self.col.create_geo_index(["coord"])
        self.col.create_skiplist_index(["value"])
        self.col.create_fulltext_index(["text"])

        # Test database cleanup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name,
                        safe_delete=True)

    def test_first(self):
        self.assertEqual(strip_system_keys(self.col.first(1)), [])
        self.col.import_documents([{
            "name": "test_doc_01"
        }, {
            "name": "test_doc_02"
        }, {
            "name": "test_doc_03"
        }])
        self.assertEqual(len(self.col), 3)
        self.assertEqual(strip_system_keys(self.col.first(1)),
                         [{
                             "name": "test_doc_01"
                         }])
        self.assertEqual(strip_system_keys(self.col.first(2)),
                         [{
                             "name": "test_doc_01"
                         }, {
                             "name": "test_doc_02"
                         }])

    def test_last(self):
        self.assertEqual(strip_system_keys(self.col.last(1)), [])
        self.col.import_documents([{
            "name": "test_doc_01"
        }, {
            "name": "test_doc_02"
        }, {
            "name": "test_doc_03"
        }])
        self.assertEqual(len(self.col), 3)
        self.assertEqual(strip_system_keys(self.col.last(1)),
                         [{
                             "name": "test_doc_03"
                         }])
        docs = strip_system_keys(self.col.last(2))
        self.assertIn({"name": "test_doc_03"}, docs)
        self.assertIn({"name": "test_doc_02"}, docs)

    def test_all(self):
        self.assertEqual(list(self.col.all()), [])
        self.col.import_documents([{
            "name": "test_doc_01"
        }, {
            "name": "test_doc_02"
        }, {
            "name": "test_doc_03"
        }])
        self.assertEqual(len(self.col), 3)

        docs = strip_system_keys(self.col.all())
        self.assertIn({"name": "test_doc_01"}, docs)
        self.assertIn({"name": "test_doc_02"}, docs)
        self.assertIn({"name": "test_doc_03"}, docs)

    def test_any(self):
        self.assertEqual(strip_system_keys(self.col.all()), [])
        self.col.import_documents([{
            "name": "test_doc_01"
        }, {
            "name": "test_doc_02"
        }, {
            "name": "test_doc_03"
        }])
        self.assertIn(strip_system_keys(self.col.any()), [{
            "name": "test_doc_01"
        }, {
            "name": "test_doc_02"
        }, {
            "name": "test_doc_03"
        }])

    def test_get_first_example(self):
        self.assertEqual(self.col.get_first_example({"value": 1}), None)
        self.col.import_documents([{
            "name": "test_doc_01",
            "value": 1
        }, {
            "name": "test_doc_02",
            "value": 1
        }, {
            "name": "test_doc_03",
            "value": 3
        }])
        self.assertIn(
            strip_system_keys(self.col.get_first_example({"value": 1})),
            [{
                "name": "test_doc_01",
                "value": 1
            }, {
                "name": "test_doc_02",
                "value": 1
            }])

    def test_get_by_example(self):
        self.col.import_documents([{
            "name": "test_doc_01",
            "value": 1
        }, {
            "name": "test_doc_02",
            "value": 1
        }, {
            "name": "test_doc_03",
            "value": 3
        }])
        docs = strip_system_keys(self.col.get_by_example({"value": 1}))
        self.assertIn({"name": "test_doc_01", "value": 1}, docs)
        self.assertIn({"name": "test_doc_02", "value": 1}, docs)
        self.assertEqual(
            strip_system_keys(self.col.get_by_example({"value": 2})), [])
        self.assertTrue(
            len(list(self.col.get_by_example({"value": 1}, limit=1))), 1)

    def test_update_by_example(self):
        self.col.import_documents([{
            "name": "test_doc_01",
            "value": 1
        }, {
            "name": "test_doc_02",
            "value": 1
        }, {
            "name": "test_doc_03",
            "value": 3
        }])
        self.col.update_by_example({"value": 1}, {"value": 2})
        docs = strip_system_keys(self.col.all())
        self.assertIn({"name": "test_doc_01", "value": 2}, docs)
        self.assertIn({"name": "test_doc_02", "value": 2}, docs)
        self.assertIn({"name": "test_doc_03", "value": 3}, docs)

    def test_replace_by_example(self):
        self.col.import_documents([{
            "name": "test_doc_01",
            "value": 1
        }, {
            "name": "test_doc_02",
            "value": 1
        }, {
            "name": "test_doc_03",
            "value": 3
        }])
        self.col.replace_by_example({"value": 1}, {"foo": "bar"})

        docs = strip_system_keys(self.col.all())
        self.assertIn({"foo": "bar"}, docs)
        self.assertIn({"name": "test_doc_03", "value": 3}, docs)

    def test_remove_by_example(self):
        self.col.import_documents([{
            "name": "test_doc_01",
            "value": 1
        }, {
            "name": "test_doc_02",
            "value": 1
        }, {
            "name": "test_doc_03",
            "value": 3
        }])
        self.col.remove_by_example({"value": 1})
        self.col.remove_by_example({"value": 2})
        self.assertEqual(strip_system_keys(self.col.all()), [{
            "name": "test_doc_03",
            "value": 3
        }])

    def test_range(self):
        self.col.import_documents([{
            "name": "test_doc_01",
            "value": 1
        }, {
            "name": "test_doc_02",
            "value": 2
        }, {
            "name": "test_doc_03",
            "value": 3
        }, {
            "name": "test_doc_04",
            "value": 4
        }, {
            "name": "test_doc_05",
            "value": 5
        }])
        self.assertEqual(
            strip_system_keys(
                self.col.range(attribute="value",
                               left=2,
                               right=5,
                               closed=True,
                               skip=1,
                               limit=2)), [
                                   {
                                       "name": "test_doc_03",
                                       "value": 3
                                   },
                                   {
                                       "name": "test_doc_04",
                                       "value": 4
                                   },
                               ])

    def test_near(self):
        self.col.import_documents([
            {
                "name": "test_doc_01",
                "coord": [1, 1]
            },
            {
                "name": "test_doc_02",
                "coord": [1, 4]
            },
            {
                "name": "test_doc_03",
                "coord": [4, 1]
            },
            {
                "name": "test_doc_03",
                "coord": [4, 4]
            },
        ])
        self.assertEqual(
            strip_system_keys(self.col.near(
                latitude=1,
                longitude=1,
                limit=1,
            )), [{
                "name": "test_doc_01",
                "coord": [1, 1]
            }])

    def test_fulltext(self):
        self.col.import_documents([
            {
                "name": "test_doc_01",
                "text": "Hello World!"
            },
            {
                "name": "test_doc_02",
                "text": "foo"
            },
            {
                "name": "test_doc_03",
                "text": "bar"
            },
            {
                "name": "test_doc_03",
                "text": "baz"
            },
        ])
        self.assertEqual(
            strip_system_keys(self.col.fulltext("text", "foo,|bar")), [
                {
                    "name": "test_doc_02",
                    "text": "foo"
                },
                {
                    "name": "test_doc_03",
                    "text": "bar"
                },
            ])

    def test_lookup_by_keys(self):
        self.col.import_documents([
            {
                "_key": "key01",
                "value": 1
            },
            {
                "_key": "key02",
                "value": 2
            },
            {
                "_key": "key03",
                "value": 3
            },
            {
                "_key": "key04",
                "value": 4
            },
            {
                "_key": "key05",
                "value": 5
            },
            {
                "_key": "key06",
                "value": 6
            },
        ])
        self.assertEqual(
            strip_system_keys(
                self.col.lookup_by_keys(["key01", "key03", "key06"])), [
                    {
                        "value": 1
                    },
                    {
                        "value": 3
                    },
                    {
                        "value": 6
                    },
                ])
        self.assertEqual(len(self.col), 6)

    def test_remove_by_keys(self):
        self.col.import_documents([
            {
                "_key": "key01",
                "value": 1
            },
            {
                "_key": "key02",
                "value": 2
            },
            {
                "_key": "key03",
                "value": 3
            },
            {
                "_key": "key04",
                "value": 4
            },
            {
                "_key": "key05",
                "value": 5
            },
            {
                "_key": "key06",
                "value": 6
            },
        ])
        self.assertEqual(self.col.remove_by_keys(["key01", "key03", "key06"]),
                         {
                             "removed": 3,
                             "ignored": 0
                         })
        leftover = strip_system_keys(self.col.all())
        self.assertEqual(len(leftover), 3)
        self.assertIn({"value": 2}, leftover)
        self.assertIn({"value": 4}, leftover)
        self.assertIn({"value": 5}, leftover)
예제 #51
0
class CollectionManagementTest(unittest.TestCase):
    """Tests for managing ArangoDB collections."""

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)

        # Test database cleaup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)

    def test_create_collection(self):
        col_name = get_next_col_name(self.db)
        self.db.create_collection(col_name)
        self.assertIn(col_name, self.db.collections["all"])

    def test_rename_collection(self):
        # Create a new collection
        col_name = get_next_col_name(self.db)
        self.db.create_collection(col_name)
        col_id = self.db.collection(col_name).id
        # Rename the collection
        new_col_name = get_next_col_name(self.db)
        self.db.rename_collection(col_name, new_col_name)
        self.assertNotIn(col_name, self.db.collections["all"])
        self.assertIn(new_col_name, self.db.collections["all"])
        # Ensure it is the same collection by checking the ID
        self.assertEqual(self.db.collection(new_col_name).id, col_id)

    def test_delete_collection(self):
        # Create a new collection
        col_name = get_next_col_name(self.db)
        self.db.create_collection(col_name)
        self.assertIn(col_name, self.db.collections["all"])
        # Delete the collection and ensure that it's gone
        self.db.delete_collection(col_name)
        self.assertNotIn(col_name, self.db.collections)

    def test_collection_create_with_config(self):
        # Create a new collection with custom defined properties
        col_name = get_next_col_name(self.db)
        col = self.db.create_collection(
            name=col_name,
            wait_for_sync=True,
            do_compact=False,
            journal_size=7774208,
            is_system=False,
            is_volatile=False,
            key_generator_type="autoincrement",
            allow_user_keys=False,
            key_increment=9,
            key_offset=100,
            is_edge=True,
            number_of_shards=2,
            shard_keys=["test_attr"],
        )
        # Ensure that the new collection's properties are set correctly
        self.assertEqual(col.name, col_name)
        self.assertTrue(col.revision, "0")
        self.assertEqual(col.status, "loaded")
        self.assertEqual(col.journal_size, 7774208)
        self.assertEqual(col.checksum(), 0)
        self.assertEqual(
            col.key_options,
            {
                "allow_user_keys": False,
                "increment": 9,
                "offset": 100,
                "type": "autoincrement"
            }
        )
        self.assertFalse(col.is_system)
        self.assertFalse(col.is_volatile)
        self.assertFalse(col.do_compact)
        self.assertTrue(col.wait_for_sync)
        self.assertTrue(col.is_edge)
        self.assertTrue(is_string(col.id))
        self.assertTrue(isinstance(col.figures, dict))

    def test_collection_setters(self):
        # Create a new collection with predefined properties
        col = self.db.create_collection(
            name=get_next_col_name(self.db),
            wait_for_sync=False,
            journal_size=7774208
        )
        self.assertFalse(col.wait_for_sync)
        self.assertEqual(col.journal_size, 7774208)
        # Change the properties of the graph and ensure that it went through
        col.wait_for_sync = True
        col.journal_size = 8884208
        self.assertTrue(col.wait_for_sync)
        self.assertEqual(col.journal_size, 8884208)

    def test_collection_load_unload(self):
        col = self.db.create_collection(get_next_col_name(self.db))
        self.assertIn(col.unload(), {"unloaded", "unloading"})
        self.assertIn(col.load(), {"loaded", "loading"})

    def test_collection_rotate_journal(self):
        col = self.db.create_collection(get_next_col_name(self.db))
        self.assertRaises(
            CollectionRotateJournalError,
            col.rotate_journal
        )
예제 #52
0
from arango import Arango

from dace.util import utility

from lac import log


ELASTICSEARCH_PORT = os.getenv('ELASTICSEARCH_PORT', 'localhost:9200')
es = Elasticsearch(ELASTICSEARCH_PORT)

ARANGO_HOST, ARANGO_PORT = os.getenv(
    'ARANGO_PORT', 'localhost:8529').split(':')
ARANGO_ROOT_PASSWORD = os.getenv('ARANGO_ROOT_PASSWORD', '')

arango_server = Arango(
    host=ARANGO_HOST, port=ARANGO_PORT,
    password=ARANGO_ROOT_PASSWORD)


class IResourceManager(Interface):

    def add_entry(self, key, value, mapping={}, id=None):
        pass

    def set_entry(self, key, value, id):
        pass

    def get_entry(self, id):
        pass

    def get_entries(self, key=None, query={"match_all": {}},
예제 #53
0
 def setUp(self):
     self.arango = Arango()
예제 #54
0
class VertexManagementTest(unittest.TestCase):
    """Tests for managing ArangoDB vertices."""

    def setUp(self):
        self.arango = Arango()
        self.db_name = get_next_db_name(self.arango)
        self.db = self.arango.create_database(self.db_name)
        self.col_name = get_next_col_name(self.db)
        self.col = self.db.create_collection(self.col_name)
        # Create the vertex collection
        self.vertex_col_name = get_next_col_name(self.db)
        self.vertex_col = self.db.create_collection(self.vertex_col_name)
        # Create the edge collection
        self.edge_col_name = get_next_col_name(self.db)
        self.edge_col = self.db.create_collection(
            self.edge_col_name, is_edge=True
        )
        # Create the graph
        self.graph_name = get_next_graph_name(self.db)
        self.graph = self.db.create_graph(
            name=self.graph_name,
            edge_definitions=[{
                "collection": self.edge_col_name,
                "from": [self.vertex_col_name],
                "to": [self.vertex_col_name]
            }],
        )
        # Test database cleaup
        self.addCleanup(self.arango.delete_database,
                        name=self.db_name, safe_delete=True)

    def test_create_vertex(self):
        self.graph.create_vertex(
            self.vertex_col_name,
            data={"_key": "vertex01", "value": 10}
        )
        self.assertEqual(self.vertex_col.count, 1)
        self.assertEqual(
            self.graph.get_vertex(
                "{}/{}".format(self.vertex_col_name, "vertex01")
            )["value"],
            10
        )

    def test_update_vertex(self):
        self.graph.create_vertex(
            self.vertex_col_name,
            data={"_key": "vertex01", "value": 10}
        )
        self.graph.update_vertex(
            "{}/{}".format(self.vertex_col_name, "vertex01"),
            data={"value": 20, "new_value": 30}
        )
        self.assertEqual(
            self.graph.get_vertex(
                "{}/{}".format(self.vertex_col_name, "vertex01")
            )["value"],
            20
        )
        self.assertEqual(
            self.graph.get_vertex(
                "{}/{}".format(self.vertex_col_name, "vertex01")
            )["new_value"],
            30
        )

    def test_replace_vertex(self):
        self.graph.create_vertex(
            self.vertex_col_name,
            data={"_key": "vertex01", "value": 10}
        )
        self.graph.replace_vertex(
            "{}/{}".format(self.vertex_col_name, "vertex01"),
            data={"new_value": 30}
        )
        self.assertNotIn(
            "value",
            self.graph.get_vertex(
                "{}/{}".format(self.vertex_col_name, "vertex01")
            )
        )
        self.assertEqual(
            self.graph.get_vertex(
                "{}/{}".format(self.vertex_col_name, "vertex01")
            )["new_value"],
            30
        )

    def test_delete_vertex(self):
        self.graph.create_vertex(
            self.vertex_col_name,
            data={"_key": "vertex01", "value": 10}
        )
        self.graph.delete_vertex(
            "{}/{}".format(self.vertex_col_name, "vertex01")
        )
        self.assertNotIn("vertex01", self.vertex_col)
        self.assertEqual(len(self.vertex_col), 0)