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)
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)
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, {})
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)
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] )
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_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_batch_document_add(self): self.db.execute_batch([ (self.col.add_document, [{"_key": "doc01", "value": 1}], {}), (self.col.add_document, [{"_key": "doc02", "value": 2}], {}), (self.col.add_document, [{"_key": "doc03", "value": 3}], {}), ]) self.assertEqual(len(self.col), 3) self.assertEqual(self.col.get_document("doc01")["value"], 1) self.assertEqual(self.col.get_document("doc02")["value"], 2) self.assertEqual(self.col.get_document("doc03")["value"], 3) def test_batch_document_replace(self): self.col.bulk_import([ {"_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.get_document("doc01")["value"], 2) self.assertEqual(self.col.get_document("doc02")["value"], 2) self.assertEqual(self.col.get_document("doc03")["value"], 2) def test_batch_document_update(self): self.col.bulk_import([ {"_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.get_document("doc01")["value"], 2) self.assertEqual(self.col.get_document("doc02")["value"], 2) self.assertEqual(self.col.get_document("doc03")["value"], 2) def test_batch_document_remove(self): self.col.bulk_import([ {"_key": "doc01", "value": 1}, {"_key": "doc02", "value": 1}, {"_key": "doc03", "value": 1} ]) self.db.execute_batch([ (self.col.remove_document, ["doc01"], {}), (self.col.remove_document, ["doc02"], {}), (self.col.remove_document, ["doc03"], {}), ]) self.assertEqual(len(self.col), 0) def test_batch_document_mixed(self): self.col.bulk_import([ {"_key": "doc01", "value": 0}, {"_key": "doc02", "value": 0}, {"_key": "doc03", "value": 0} ]) self.db.execute_batch([ ( self.col.add_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.remove_document, ["doc03"], {"wait_for_sync": True} ), ( self.col.add_document, [{"_key": "doc05", "value": 5}], {"wait_for_sync": True} ), ]) self.assertEqual(len(self.col), 4) self.assertEqual(self.col.get_document("doc01")["value"], 2) self.assertEqual(self.col.get_document("doc02")["new_value"], 3) self.assertNotIn("doc03", self.col) self.assertEqual(self.col.get_document("doc04")["value"], 1) self.assertEqual(self.col.get_document("doc05")["value"], 5) def test_batch_vertex_add(self): self.db.execute_batch([ ( self.graph.add_vertex, [self.vertex_col_name, {"_key": "v01", "value": 1}], {"wait_for_sync": True} ), ( self.graph.add_vertex, [self.vertex_col_name, {"_key": "v02", "value": 2}], {"wait_for_sync": True} ), ( self.graph.add_vertex, [self.vertex_col_name, {"_key": "v03", "value": 3}], {"wait_for_sync": True} ), ]) self.assertEqual(self.vertex_col.get_document("v01")["value"], 1) self.assertEqual(self.vertex_col.get_document("v02")["value"], 2) self.assertEqual(self.vertex_col.get_document("v03")["value"], 3) def test_batch_vertex_replace(self): self.vertex_col.bulk_import([ {"_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.get_document("v01")["new_val"], 2) self.assertEqual(self.vertex_col.get_document("v02")["new_val"], 3) self.assertEqual(self.vertex_col.get_document("v03")["new_val"], 4) def test_batch_vertex_update(self): self.vertex_col.bulk_import([ {"_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.get_document("v01")["value"], 2) self.assertEqual(self.vertex_col.get_document("v02")["value"], 3) self.assertEqual(self.vertex_col.get_document("v03")["value"], 4) def test_batch_vertex_remove(self): self.vertex_col.bulk_import([ {"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1} ]) self.db.execute_batch([ ( self.graph.remove_vertex, ["{}/{}".format(self.vertex_col_name, "v01")], {"wait_for_sync": True} ), ( self.graph.remove_vertex, ["{}/{}".format(self.vertex_col_name, "v02")], {"wait_for_sync": True} ), ( self.graph.remove_vertex, ["{}/{}".format(self.vertex_col_name, "v03")], {"wait_for_sync": True} ), ]) self.assertEqual(len(self.vertex_col), 0) def test_batch_edge_add(self): self.vertex_col.bulk_import([ {"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1} ]) self.db.execute_batch([ ( self.graph.add_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.add_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.get_document("e01")["value"], 4) self.assertEqual(self.edge_col.get_document("e02")["value"], 5) def test_batch_edge_replace(self): self.vertex_col.bulk_import([ {"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1} ]) self.db.execute_batch([ ( self.graph.add_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.add_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.get_document("e01")["new_val"], 1) self.assertEqual(self.edge_col.get_document("e02")["new_val"], 2) def test_batch_edge_update(self): self.vertex_col.bulk_import([ {"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1} ]) self.db.execute_batch([ ( self.graph.add_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.add_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.get_document("e01")["value"], 1) self.assertEqual(self.edge_col.get_document("e02")["value"], 2) def test_batch_edge_remove(self): self.vertex_col.bulk_import([ {"_key": "v01", "value": 1}, {"_key": "v02", "value": 1}, {"_key": "v03", "value": 1} ]) self.db.execute_batch([ ( self.graph.add_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.add_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.remove_edge, ["{}/{}".format(self.edge_col_name, "e01")], {} ), ( self.graph.remove_edge, ["{}/{}".format(self.edge_col_name, "e02")], {} ), ]) self.assertEqual(len(self.edge_col), 0)
class ArangoDBQueryTest(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.db.add_collection(self.col_name) def tearDown(self): self.arango.remove_database(self.db_name) def test_explain_query(self): self.assertRaises( QueryValidateError, 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( QueryValidateError, 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.bulk_import([ {"_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.bulk_import([ {"_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"] )
class EdgeManagementTest(unittest.TestCase): def setUp(self): # Create the test database self.arango = Arango() self.db_name = get_next_db_name(self.arango) self.db = self.arango.add_database(self.db_name) # Create the test vertex "from" collection self.from_col_name = get_next_col_name(self.db) self.from_col = self.db.add_collection(self.from_col_name) # Create the test vertex "to" collection self.to_col_name = get_next_col_name(self.db) self.to_col = self.db.add_collection(self.to_col_name) # Create the test 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 test 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.from_col_name], "to": [self.to_col_name] }], ) # Add a few test "from" vertices self.graph.add_vertex( self.from_col_name, data={"_key": "from01", "value": 1} ) self.graph.add_vertex( self.from_col_name, data={"_key": "from02", "value": 2} ) # Add a few test "to" vertices self.graph.add_vertex( self.to_col_name, data={"_key": "to01", "value": 1} ) self.graph.add_vertex( self.to_col_name, data={"_key": "to02", "value": 2} ) self.graph.add_vertex( self.to_col_name, data={"_key": "to03", "value": 3} ) # Add a few test edges self.graph.add_edge( self.edge_col_name, { "_from": "{}/{}".format(self.from_col_name, "from01"), "_to": "{}/{}".format(self.to_col_name, "to01"), } ) self.graph.add_edge( self.edge_col_name, { "_from": "{}/{}".format(self.from_col_name, "from02"), "_to": "{}/{}".format(self.to_col_name, "to02"), } ) self.graph.add_edge( self.edge_col_name, { "_from": "{}/{}".format(self.from_col_name, "from02"), "_to": "{}/{}".format(self.to_col_name, "to03"), } ) def tearDown(self): self.arango.remove_database(self.db_name) 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"), ] )
class EdgeManagementTest(unittest.TestCase): def setUp(self): # Create the test database self.arango = Arango() self.db_name = get_next_db_name(self.arango) self.db = self.arango.add_database(self.db_name) # Create the test 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 test 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 test 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] }], ) # Add a few test vertices self.graph.add_vertex( self.vertex_col_name, data={ "_key": "vertex01", "value": 1 } ) self.graph.add_vertex( self.vertex_col_name, data={ "_key": "vertex02", "value": 1 } ) self.graph.add_vertex( self.vertex_col_name, data={ "_key": "vertex03", "value": 1 } ) def tearDown(self): self.arango.remove_database(self.db_name) def test_add_edge(self): self.graph.add_edge( self.edge_col_name, data={ "_key": "edge01", "_from": "{}/{}".format(self.vertex_col_name, "vertex01"), "_to": "{}/{}".format(self.vertex_col_name, "vertex01"), "value": "foobar" } ) self.assertEqual(self.edge_col.count, 1) self.assertEqual( self.graph.get_edge( "{}/{}".format(self.edge_col_name, "edge01") )["value"], "foobar" ) self.assertEqual( self.graph.get_edge( "{}/{}".format(self.edge_col_name, "edge01") )["_from"], "{}/{}".format(self.vertex_col_name, "vertex01") ) self.assertEqual( self.graph.get_edge( "{}/{}".format(self.edge_col_name, "edge01") )["_to"], "{}/{}".format(self.vertex_col_name, "vertex01") ) def test_update_edge(self): self.graph.add_edge( self.edge_col_name, data={ "_key": "edge01", "_from": "{}/{}".format(self.vertex_col_name, "vertex01"), "_to": "{}/{}".format(self.vertex_col_name, "vertex01"), "value": 10 } ) self.graph.update_edge( "{}/{}".format(self.edge_col_name, "edge01"), data={"value": 20, "new_value": 30} ) self.assertEqual( self.graph.get_edge( "{}/{}".format(self.edge_col_name, "edge01"), )["value"], 20 ) self.assertEqual( self.graph.get_edge( "{}/{}".format(self.edge_col_name, "edge01"), )["new_value"], 30 ) def test_replace_edge(self): self.graph.add_edge( self.edge_col_name, data={ "_key": "edge01", "_from": "{}/{}".format(self.vertex_col_name, "vertex01"), "_to": "{}/{}".format(self.vertex_col_name, "vertex01"), "value": 10 } ) self.graph.replace_edge( "{}/{}".format(self.edge_col_name, "edge01"), data={"new_value": 20} ) self.assertNotIn( "value", self.graph.get_edge( "{}/{}".format(self.edge_col_name, "edge01") ) ) self.assertEqual( self.graph.get_edge( "{}/{}".format(self.edge_col_name, "edge01") )["new_value"], 20 ) def test_remove_edge(self): self.graph.add_edge( self.edge_col_name, data={ "_key": "edge01", "_from": "{}/{}".format(self.vertex_col_name, "vertex01"), "_to": "{}/{}".format(self.vertex_col_name, "vertex01"), "value": 10 } ) self.graph.remove_edge("{}/{}".format(self.edge_col_name, "edge01")) self.assertNotIn("edge01", self.edge_col) self.assertEqual(len(self.edge_col), 0)
class DocumentManagementTest(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) self.col.add_geo_index(["coord"]) self.col.add_skiplist_index(["value"]) self.col.add_fulltext_index(["text"]) def tearDown(self): self.arango.remove_database(self.db_name) def test_add_document(self): self.assertEqual(len(self.col), 0) self.col.add_document({"_key": "test_doc"}) self.assertEqual(len(self.col), 1) self.assertIn("test_doc", self.col) def test_remove_document(self): rev = self.col.add_document({"_key": "test_doc"})["_rev"] self.assertEqual(len(self.col), 1) self.assertRaises( DocumentRemoveError, self.col.remove_document, "test_doc", rev="wrong_revision" ) self.col.remove_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.add_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.add_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.add_document({"_key": "test_doc_01"}) self.col.add_document({"_key": "test_doc_02"}) self.col.add_document({"_key": "test_doc_03"}) self.assertEqual(len(self.col), 3) self.col.truncate() self.assertEqual(len(self.col), 0) def test_bulk_import(self): documents = [ {"_key": "test_doc_01"}, {"_key": "test_doc_02"}, {"_key": 1} # invalid key ] # This should succeed partially res = self.col.bulk_import(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( CollectionBulkImportError, self.col.bulk_import, documents, complete=True ) self.assertEqual(len(self.col), 0) # This should succeed completely since all documents are valid self.col.truncate() res = self.col.bulk_import(documents[:2], complete=True) self.assertEqual(len(self.col), 2) self.assertEqual(res["errors"], 0) self.assertEqual(res["created"], 2) def test_first(self): self.assertEqual(strip_system_keys(self.col.first(1)), []) self.col.bulk_import([ {"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.bulk_import([ {"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.bulk_import([ {"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.bulk_import([ {"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.bulk_import([ {"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.bulk_import([ {"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.bulk_import([ {"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.bulk_import([ {"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.bulk_import([ {"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.bulk_import([ {"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.bulk_import([ {"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.bulk_import([ {"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"}, ] )
class IndexManagementTest(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) def tearDown(self): self.arango.remove_database(self.db_name) def test_list_indexes(self): self.assertIn( { "selectivity_estimate": 1, "sparse": False, "type": "primary", "fields": ["_key"], "unique": True }, self.col.indexes.values() ) def test_add_hash_index(self): self.col.add_hash_index(["attr1", "attr2"], unique=True) self.assertIn( { "selectivity_estimate": 1, "sparse": False, "type": "hash", "fields": ["attr1", "attr2"], "unique": True }, self.col.indexes.values() ) self.assertIn( { "selectivity_estimate": 1, "sparse": False, "type": "primary", "fields": ["_key"], "unique": True }, self.col.indexes.values() ) def test_add_cap_constraint(self): self.col.add_cap_constraint(size=10, byte_size=40000) self.assertIn( { "type": "cap", "size": 10, "byte_size": 40000, "unique": False }, self.col.indexes.values() ) self.assertIn( { "selectivity_estimate": 1, "sparse": False, "type": "primary", "fields": ["_key"], "unique": True }, self.col.indexes.values() ) def test_add_skiplist_index(self): self.col.add_skiplist_index(["attr1", "attr2"], unique=True) self.assertIn( { "sparse": False, "type": "skiplist", "fields": ["attr1", "attr2"], "unique": True }, self.col.indexes.values() ) self.assertIn( { "selectivity_estimate": 1, "sparse": False, "type": "primary", "fields": ["_key"], "unique": True }, self.col.indexes.values() ) def test_add_geo_index_with_one_attr(self): self.skipTest("I have no idea why unique comes back as false, on the geo creation." "Perhaps that index type doesn't support it.") self.col.add_geo_index( fields=["attr1"], geo_json=False, unique=True, ignore_null=False ) self.assertIn( { "sparse": True, "type": "geo1", "fields": ["attr1"], "unique": True, "geo_json": False, "ignore_null": False, "constraint": True }, self.col.indexes.values() ) self.assertIn( { "selectivity_estimate": 1, "sparse": False, "type": "primary", "fields": ["_key"], "unique": True }, self.col.indexes.values() ) def test_add_geo_index_with_two_attrs(self): self.skipTest("I have no idea why unique comes back as false, on the geo creation." "Perhaps that index type doesn't support it.") self.col.add_geo_index( fields=["attr1", "attr2"], geo_json=False, unique=True, ignore_null=False ) self.assertIn( { "sparse": True, "type": "geo2", "fields": ["attr1", "attr2"], "unique": True, "ignore_null": False, "constraint": True }, self.col.indexes.values() ) self.assertIn( { "type": "primary", "fields": ["_key"], "unique": True }, self.col.indexes.values() ) def test_add_geo_index_with_more_than_two_attrs(self): self.assertRaises( IndexAddError, self.col.add_geo_index, fields=["attr1", "attr2", "attr3"] ) def test_add_fulltext_index(self): self.assertRaises( IndexAddError, self.col.add_fulltext_index, fields=["attr1", "attr2"] ) self.col.add_fulltext_index( fields=["attr1"], min_length=10, ) self.assertIn( { "selectivity_estimate": 1, "sparse": False, "type": "primary", "fields": ["_key"], "unique": True }, self.col.indexes.values() ) self.assertIn( { "sparse": True, "type": "fulltext", "fields": ["attr1"], "min_length": 10, "unique": False, }, self.col.indexes.values() ) def test_remove_index(self): old_indexes = set(self.col.indexes) self.col.add_hash_index(["attr1", "attr2"], unique=True) self.col.add_skiplist_index(["attr1", "attr2"], unique=True) self.col.add_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.remove_index(index_id) self.assertEqual(old_indexes, set(self.col.indexes))