def test_graph_properties(graph, bad_graph, db): assert repr(graph) == '<Graph {}>'.format(graph.name) properties = graph.properties() assert properties['id'] == '_graphs/{}'.format(graph.name) assert properties['name'] == graph.name assert len(properties['edge_definitions']) == 1 assert 'orphan_collections' in properties assert 'smart' in properties # assert 'smart_field' in properties assert 'shard_count' in properties assert isinstance(properties['revision'], string_types) # Test properties with bad database with assert_raises(GraphPropertiesError): bad_graph.properties() new_graph_name = generate_graph_name() new_graph = db.create_graph( new_graph_name, # TODO only possible with enterprise edition # smart=True, # smart_field='foo', # shard_count=2 ) properties = new_graph.properties() assert properties['id'] == '_graphs/{}'.format(new_graph_name) assert properties['name'] == new_graph_name assert properties['edge_definitions'] == [] assert properties['orphan_collections'] == [] assert isinstance(properties['revision'], string_types)
def test_graph_properties(graph, bad_graph, db): assert repr(graph) == '<Graph {}>'.format(graph.name) properties = graph.properties() assert properties['id'] == '_graphs/{}'.format(graph.name) assert properties['name'] == graph.name assert len(properties['edge_definitions']) == 1 assert len(properties['orphan_collections']) == 2 assert 'smart' in properties assert 'smart_field' in properties assert 'shard_count' in properties assert isinstance(properties['revision'], string_types) # Test properties with bad fabric with assert_raises(GraphPropertiesError): bad_graph.properties() new_graph_name = generate_graph_name() new_graph = db.create_graph( new_graph_name, # TODO only possible with enterprise edition # smart=True, # smart_field='foo', # shard_count=2 ) properties = new_graph.properties() assert properties['id'] == '_graphs/{}'.format(new_graph_name) assert properties['name'] == new_graph_name assert properties['edge_definitions'] == [] assert properties['orphan_collections'] == [] assert isinstance(properties['revision'], string_types)
def pytest_configure(config): client = C8Client(host=config.getoption('host'), port=config.getoption('port')) sys_fabric = client.fabric(name='_system', username='******', password=config.getoption('passwd')) # Create a user and non-system fabric for testing. username = generate_username() password = generate_string() tst_fabric_name = generate_fabric_name() bad_fabric_name = generate_fabric_name() sys_fabric.create_fabric(name=tst_fabric_name, users=[{ 'active': True, 'username': username, 'password': password, }]) tst_fabric = client.fabric(tst_fabric_name, username, password) bad_fabric = client.fabric(bad_fabric_name, username, password) # Create a standard collection for testing. col_name = generate_col_name() tst_col = tst_fabric.create_collection(col_name, edge=False) tst_col.add_skiplist_index(['val']) tst_col.add_fulltext_index(['text']) geo_index = tst_col.add_geo_index(['loc']) # Create a legacy edge collection for testing. lecol_name = generate_col_name() tst_fabric.create_collection(lecol_name, edge=True) # Create test vertex & edge collections and graph. graph_name = generate_graph_name() ecol_name = generate_col_name() fvcol_name = generate_col_name() tvcol_name = generate_col_name() tst_graph = tst_fabric.create_graph(graph_name) tst_graph.create_vertex_collection(fvcol_name) tst_graph.create_vertex_collection(tvcol_name) tst_graph.create_edge_definition(edge_collection=ecol_name, from_vertex_collections=[fvcol_name], to_vertex_collections=[tvcol_name]) global_data.update({ 'client': client, 'username': username, 'password': password, 'sys_fabric': sys_fabric, 'tst_fabric': tst_fabric, 'bad_fabric': bad_fabric, 'geo_index': geo_index, 'col_name': col_name, 'lecol_name': lecol_name, 'graph_name': graph_name, 'ecol_name': ecol_name, 'fvcol_name': fvcol_name, 'tvcol_name': tvcol_name, })
def test_create_graph_with_edge_definition(db): new_graph_name = generate_graph_name() new_ecol_name = generate_col_name() fvcol_name = generate_col_name() tvcol_name = generate_col_name() ovcol_name = generate_col_name() edge_definition = { 'edge_collection': new_ecol_name, 'from_vertex_collections': [fvcol_name], 'to_vertex_collections': [tvcol_name] } new_graph = db.create_graph(new_graph_name, edge_definitions=[edge_definition], orphan_collections=[ovcol_name]) assert edge_definition in new_graph.edge_definitions()
def test_create_graph_with_edge_definition(db): new_graph_name = generate_graph_name() new_ecol_name = generate_col_name() fvcol_name = generate_col_name() tvcol_name = generate_col_name() ovcol_name = generate_col_name() edge_definition = { 'edge_collection': new_ecol_name, 'from_vertex_collections': [fvcol_name], 'to_vertex_collections': [tvcol_name] } new_graph = db.create_graph( new_graph_name, edge_definitions=[edge_definition], orphan_collections=[ovcol_name] ) assert edge_definition in new_graph.edge_definitions()
def test_graph_properties(graph, bad_graph, db): assert repr(graph) == '<Graph {}>'.format(graph.name) properties = graph.properties() assert properties['id'] == '_graphs/{}'.format(graph.name) assert properties['name'] == graph.name assert len(properties['edge_definitions']) == 1 assert 'orphan_collections' in properties assert isinstance(properties['revision'], string_types) # Test properties with bad database with assert_raises(GraphPropertiesError): bad_graph.properties() new_graph_name = generate_graph_name() new_graph = db.create_graph(new_graph_name) properties = new_graph.properties() assert properties['id'] == '_graphs/{}'.format(new_graph_name) assert properties['name'] == new_graph_name assert properties['edge_definitions'] == [] assert properties['orphan_collections'] == [] assert isinstance(properties['revision'], string_types)
def test_graph_properties(graph, bad_graph, db): assert repr(graph) == f"<Graph {graph.name}>" properties = graph.properties() assert properties["id"] == f"_graphs/{graph.name}" assert properties["name"] == graph.name assert len(properties["edge_definitions"]) == 1 assert "orphan_collections" in properties assert isinstance(properties["revision"], str) # Test properties with bad database with assert_raises(GraphPropertiesError): bad_graph.properties() new_graph_name = generate_graph_name() new_graph = db.create_graph(new_graph_name) properties = new_graph.properties() assert properties["id"] == f"_graphs/{new_graph_name}" assert properties["name"] == new_graph_name assert properties["edge_definitions"] == [] assert properties["orphan_collections"] == [] assert isinstance(properties["revision"], str)
def test_traverse(db): # Create test graph, vertex and edge collections school = db.create_graph(generate_graph_name()) profs = school.create_vertex_collection(generate_col_name()) classes = school.create_vertex_collection(generate_col_name()) teaches = school.create_edge_definition( edge_collection=generate_col_name(), from_vertex_collections=[profs.name], to_vertex_collections=[classes.name]) # Insert test vertices into the graph profs.insert({'_key': 'anna', 'name': 'Professor Anna'}) profs.insert({'_key': 'andy', 'name': 'Professor Andy'}) classes.insert({'_key': 'CSC101', 'name': 'Introduction to CS'}) classes.insert({'_key': 'MAT223', 'name': 'Linear Algebra'}) classes.insert({'_key': 'STA201', 'name': 'Statistics'}) classes.insert({'_key': 'MAT101', 'name': 'Calculus I'}) classes.insert({'_key': 'MAT102', 'name': 'Calculus II'}) # Insert test edges into the graph teaches.insert({ '_from': '{}/anna'.format(profs.name), '_to': '{}/CSC101'.format(classes.name) }) teaches.insert({ '_from': '{}/anna'.format(profs.name), '_to': '{}/STA201'.format(classes.name) }) teaches.insert({ '_from': '{}/anna'.format(profs.name), '_to': '{}/MAT223'.format(classes.name) }) teaches.insert({ '_from': '{}/andy'.format(profs.name), '_to': '{}/MAT101'.format(classes.name) }) teaches.insert({ '_from': '{}/andy'.format(profs.name), '_to': '{}/MAT102'.format(classes.name) }) teaches.insert({ '_from': '{}/andy'.format(profs.name), '_to': '{}/MAT223'.format(classes.name) }) # Traverse the graph with default settings result = school.traverse('{}/anna'.format(profs.name)) visited = extract('_key', result['vertices']) assert visited == ['CSC101', 'MAT223', 'STA201', 'anna'] for path in result['paths']: for vertex in path['vertices']: assert set(vertex) == {'_id', '_key', '_rev', 'name'} for edge in path['edges']: assert set(edge) == {'_id', '_key', '_rev', '_to', '_from'} result = school.traverse('{}/andy'.format(profs.name)) visited = extract('_key', result['vertices']) assert visited == ['MAT101', 'MAT102', 'MAT223', 'andy'] # Traverse the graph with an invalid start vertex with assert_raises(GraphTraverseError): school.traverse('invalid') with assert_raises(GraphTraverseError): bad_col_name = generate_col_name() school.traverse('{}/hanna'.format(bad_col_name)) with assert_raises(GraphTraverseError): school.traverse('{}/anderson'.format(profs.name)) # Travers the graph with max iteration of 0 with assert_raises(GraphTraverseError): school.traverse('{}/andy'.format(profs.name), max_iter=0) # Traverse the graph with max depth of 0 result = school.traverse('{}/andy'.format(profs.name), max_depth=0) assert extract('_key', result['vertices']) == ['andy'] result = school.traverse('{}/anna'.format(profs.name), max_depth=0) assert extract('_key', result['vertices']) == ['anna'] # Traverse the graph with min depth of 2 result = school.traverse('{}/andy'.format(profs.name), min_depth=2) assert extract('_key', result['vertices']) == [] result = school.traverse('{}/anna'.format(profs.name), min_depth=2) assert extract('_key', result['vertices']) == [] # Traverse the graph with DFS and BFS result = school.traverse( {'_id': '{}/anna'.format(profs.name)}, strategy='dfs', direction='any', ) dfs_vertices = extract('_key', result['vertices']) result = school.traverse({'_id': '{}/anna'.format(profs.name)}, strategy='bfs', direction='any') bfs_vertices = extract('_key', result['vertices']) assert sorted(dfs_vertices) == sorted(bfs_vertices) # Traverse the graph with filter function result = school.traverse( {'_id': '{}/andy'.format(profs.name)}, filter_func='if (vertex._key == "MAT101") {return "exclude";} return;') assert extract('_key', result['vertices']) == ['MAT102', 'MAT223', 'andy'] # Traverse the graph with global uniqueness (should be same as before) result = school.traverse( {'_id': '{}/andy'.format(profs.name)}, vertex_uniqueness='global', edge_uniqueness='global', filter_func='if (vertex._key == "MAT101") {return "exclude";} return;') assert extract('_key', result['vertices']) == ['MAT102', 'MAT223', 'andy'] with assert_raises(DocumentParseError) as err: school.traverse({}) assert err.value.message == 'field "_id" required'
def pytest_configure(config): client = ArangoClient( host=config.getoption('host'), port=config.getoption('port') ) sys_db = client.db( name='_system', username='******', password=config.getoption('passwd') ) # Create a user and non-system database for testing. username = generate_username() password = generate_string() tst_db_name = generate_db_name() bad_db_name = generate_db_name() sys_db.create_database( name=tst_db_name, users=[{ 'active': True, 'username': username, 'password': password, }] ) sys_db.update_permission( username=username, permission='rw', database=tst_db_name ) tst_db = client.db(tst_db_name, username, password) bad_db = client.db(bad_db_name, username, password) # Create a standard collection for testing. col_name = generate_col_name() tst_col = tst_db.create_collection(col_name, edge=False) tst_col.add_skiplist_index(['val']) tst_col.add_fulltext_index(['text']) geo_index = tst_col.add_geo_index(['loc']) # Create a legacy edge collection for testing. lecol_name = generate_col_name() tst_db.create_collection(lecol_name, edge=True) # Create test vertex & edge collections and graph. graph_name = generate_graph_name() ecol_name = generate_col_name() fvcol_name = generate_col_name() tvcol_name = generate_col_name() tst_graph = tst_db.create_graph(graph_name) tst_graph.create_vertex_collection(fvcol_name) tst_graph.create_vertex_collection(tvcol_name) tst_graph.create_edge_definition( edge_collection=ecol_name, from_vertex_collections=[fvcol_name], to_vertex_collections=[tvcol_name] ) global_data.update({ 'client': client, 'username': username, 'password': password, 'sys_db': sys_db, 'tst_db': tst_db, 'bad_db': bad_db, 'geo_index': geo_index, 'col_name': col_name, 'lecol_name': lecol_name, 'graph_name': graph_name, 'ecol_name': ecol_name, 'fvcol_name': fvcol_name, 'tvcol_name': tvcol_name, })
def pytest_configure(config): url = 'http://{}:{}'.format( config.getoption('host'), config.getoption('port') ) client = ArangoClient(hosts=[url, url, url]) sys_db = client.db( name='_system', username='******', password=config.getoption('passwd') ) # Create a user and non-system database for testing. username = generate_username() password = generate_string() tst_db_name = generate_db_name() bad_db_name = generate_db_name() sys_db.create_database( name=tst_db_name, users=[{ 'active': True, 'username': username, 'password': password, }] ) # sys_db.update_permission( # username=username, # permission='rw', # database=tst_db_name # ) tst_db = client.db(tst_db_name, username, password) bad_db = client.db(bad_db_name, username, password) # Create a standard collection for testing. col_name = generate_col_name() tst_col = tst_db.create_collection(col_name, edge=False) tst_col.add_skiplist_index(['val']) tst_col.add_fulltext_index(['text']) geo_index = tst_col.add_geo_index(['loc']) # Create a legacy edge collection for testing. icol_name = generate_col_name() tst_db.create_collection(icol_name, edge=True) # Create test vertex & edge collections and graph. graph_name = generate_graph_name() ecol_name = generate_col_name() fvcol_name = generate_col_name() tvcol_name = generate_col_name() tst_graph = tst_db.create_graph(graph_name) tst_graph.create_vertex_collection(fvcol_name) tst_graph.create_vertex_collection(tvcol_name) tst_graph.create_edge_definition( edge_collection=ecol_name, from_vertex_collections=[fvcol_name], to_vertex_collections=[tvcol_name] ) # noinspection PyProtectedMember global_data.update({ 'url': url, 'client': client, 'username': username, 'password': password, 'sys_db': sys_db, 'tst_db': tst_db, 'bad_db': bad_db, 'geo_index': geo_index, 'col_name': col_name, 'icol_name': icol_name, 'graph_name': graph_name, 'ecol_name': ecol_name, 'fvcol_name': fvcol_name, 'tvcol_name': tvcol_name, 'cluster': config.getoption('cluster'), 'complete': config.getoption('complete'), 'replication': config.getoption('replication') })
_db = _client.db(_db_name, _username, _password) _bad_db_name = generate_db_name() _bad_db = _client.db(_bad_db_name, '', '') print('Setting up test collections ...') _col_name = generate_col_name() _col = _db.create_collection(_col_name) _skiplist_index = _col.add_skiplist_index(['val']) _fulltext_index = _col.add_fulltext_index(['text']) _geo_index = _col.add_geo_index(['loc']) _bad_col = _bad_db.collection(_col_name) _lecol_name = generate_col_name() _lecol = _db.create_collection(_lecol_name, edge=True) print('Setting up test graphs ...') _graph_name = generate_graph_name() _graph = _db.create_graph(_graph_name) _bad_graph = _bad_db.graph(_graph_name) print('Setting up test "_from" vertex collections ...') _fvcol_name = generate_col_name() _fvcol = _graph.create_vertex_collection(_fvcol_name) _bad_fvcol = _bad_graph.vertex_collection(_fvcol_name) print('Setting up test "_to" vertex collections ...') _tvcol_name = generate_col_name() _tvcol = _graph.create_vertex_collection(_tvcol_name) _bad_tvcol = _bad_graph.vertex_collection(_tvcol_name) print('Setting up test edge collection and definition ...') _ecol_name = generate_col_name()
def test_traverse(db): # Create test graph, vertex and edge collections school = db.create_graph(generate_graph_name()) profs = school.create_vertex_collection(generate_col_name()) classes = school.create_vertex_collection(generate_col_name()) teaches = school.create_edge_definition( edge_collection=generate_col_name(), from_vertex_collections=[profs.name], to_vertex_collections=[classes.name], ) # Insert test vertices into the graph profs.insert({"_key": "anna", "name": "Professor Anna"}) profs.insert({"_key": "andy", "name": "Professor Andy"}) classes.insert({"_key": "CSC101", "name": "Introduction to CS"}) classes.insert({"_key": "MAT223", "name": "Linear Algebra"}) classes.insert({"_key": "STA201", "name": "Statistics"}) classes.insert({"_key": "MAT101", "name": "Calculus I"}) classes.insert({"_key": "MAT102", "name": "Calculus II"}) # Insert test edges into the graph teaches.insert({ "_from": f"{profs.name}/anna", "_to": f"{classes.name}/CSC101" }) teaches.insert({ "_from": f"{profs.name}/anna", "_to": f"{classes.name}/STA201" }) teaches.insert({ "_from": f"{profs.name}/anna", "_to": f"{classes.name}/MAT223" }) teaches.insert({ "_from": f"{profs.name}/andy", "_to": f"{classes.name}/MAT101" }) teaches.insert({ "_from": f"{profs.name}/andy", "_to": f"{classes.name}/MAT102" }) teaches.insert({ "_from": f"{profs.name}/andy", "_to": f"{classes.name}/MAT223" }) # Traverse the graph with default settings result = school.traverse(f"{profs.name}/anna") visited = extract("_key", result["vertices"]) assert visited == ["CSC101", "MAT223", "STA201", "anna"] for path in result["paths"]: for vertex in path["vertices"]: assert set(vertex) == {"_id", "_key", "_rev", "name"} for edge in path["edges"]: assert set(edge) == {"_id", "_key", "_rev", "_to", "_from"} result = school.traverse(f"{profs.name}/andy") visited = extract("_key", result["vertices"]) assert visited == ["MAT101", "MAT102", "MAT223", "andy"] # Traverse the graph with an invalid start vertex with assert_raises(GraphTraverseError): school.traverse("invalid") with assert_raises(GraphTraverseError): bad_col_name = generate_col_name() school.traverse(f"{bad_col_name}/hanna") with assert_raises(GraphTraverseError): school.traverse(f"{profs.name}/anderson") # Travers the graph with max iteration of 0 with assert_raises(GraphTraverseError): school.traverse(f"{profs.name}/andy", max_iter=0) # Traverse the graph with max depth of 0 result = school.traverse(f"{profs.name}/andy", max_depth=0) assert extract("_key", result["vertices"]) == ["andy"] result = school.traverse(f"{profs.name}/anna", max_depth=0) assert extract("_key", result["vertices"]) == ["anna"] # Traverse the graph with min depth of 2 result = school.traverse(f"{profs.name}/andy", min_depth=2) assert extract("_key", result["vertices"]) == [] result = school.traverse(f"{profs.name}/anna", min_depth=2) assert extract("_key", result["vertices"]) == [] # Traverse the graph with DFS and BFS result = school.traverse( {"_id": f"{profs.name}/anna"}, strategy="dfs", direction="any", ) dfs_vertices = extract("_key", result["vertices"]) result = school.traverse({"_id": f"{profs.name}/anna"}, strategy="bfs", direction="any") bfs_vertices = extract("_key", result["vertices"]) assert sorted(dfs_vertices) == sorted(bfs_vertices) # Traverse the graph with filter function result = school.traverse( {"_id": f"{profs.name}/andy"}, filter_func='if (vertex._key == "MAT101") {return "exclude";} return;', ) assert extract("_key", result["vertices"]) == ["MAT102", "MAT223", "andy"] # Traverse the graph with global uniqueness (should be same as before) result = school.traverse( {"_id": f"{profs.name}/andy"}, vertex_uniqueness="global", edge_uniqueness="global", filter_func='if (vertex._key == "MAT101") {return "exclude";} return;', ) assert extract("_key", result["vertices"]) == ["MAT102", "MAT223", "andy"] with assert_raises(DocumentParseError) as err: school.traverse({}) assert err.value.message == 'field "_id" required'
def test_graph_management(db, bad_db): # Test create graph graph_name = generate_graph_name() assert db.has_graph(graph_name) is False graph = db.create_graph(graph_name) assert db.has_graph(graph_name) is True assert graph.name == graph_name assert graph.db_name == db.name # Test create duplicate graph with assert_raises(GraphCreateError) as err: db.create_graph(graph_name) assert err.value.error_code == 1925 # Test get graph result = db.graph(graph_name) assert result.name == graph.name assert result.db_name == graph.db_name # Test get graphs result = db.graphs() for entry in result: assert 'revision' in entry assert 'edge_definitions' in entry assert 'orphan_collections' in entry assert graph_name in extract('name', db.graphs()) # Test get graphs with bad fabric with assert_raises(GraphListError) as err: bad_db.graphs() assert err.value.error_code == 1228 # Test delete graph assert db.delete_graph(graph_name) is True assert graph_name not in extract('name', db.graphs()) # Test delete missing graph with assert_raises(GraphDeleteError) as err: db.delete_graph(graph_name) assert err.value.error_code == 1924 assert db.delete_graph(graph_name, ignore_missing=True) is False # Create a graph with vertex and edge collections and delete the graph graph = db.create_graph(graph_name) ecol_name = generate_col_name() fvcol_name = generate_col_name() tvcol_name = generate_col_name() graph.create_vertex_collection(fvcol_name) graph.create_vertex_collection(tvcol_name) graph.create_edge_definition( edge_collection=ecol_name, from_vertex_collections=[fvcol_name], to_vertex_collections=[tvcol_name] ) collections = extract('name', db.collections()) assert fvcol_name in collections assert tvcol_name in collections assert ecol_name in collections db.delete_graph(graph_name) collections = extract('name', db.collections()) assert fvcol_name in collections assert tvcol_name in collections assert ecol_name in collections # Create a graph with vertex and edge collections and delete all graph = db.create_graph(graph_name) graph.create_edge_definition( edge_collection=ecol_name, from_vertex_collections=[fvcol_name], to_vertex_collections=[tvcol_name] ) db.delete_graph(graph_name, drop_collections=True) collections = extract('name', db.collections()) assert fvcol_name not in collections assert tvcol_name not in collections assert ecol_name not in collections
def test_traverse(db): # Create test graph, vertex and edge collections school = db.create_graph(generate_graph_name()) profs = school.create_vertex_collection(generate_col_name()) classes = school.create_vertex_collection(generate_col_name()) teaches = school.create_edge_definition( edge_collection=generate_col_name(), from_vertex_collections=[profs.name], to_vertex_collections=[classes.name] ) # Insert test vertices into the graph profs.insert({'_key': 'anna', 'name': 'Professor Anna'}) profs.insert({'_key': 'andy', 'name': 'Professor Andy'}) classes.insert({'_key': 'CSC101', 'name': 'Introduction to CS'}) classes.insert({'_key': 'MAT223', 'name': 'Linear Algebra'}) classes.insert({'_key': 'STA201', 'name': 'Statistics'}) classes.insert({'_key': 'MAT101', 'name': 'Calculus I'}) classes.insert({'_key': 'MAT102', 'name': 'Calculus II'}) # Insert test edges into the graph teaches.insert({ '_from': '{}/anna'.format(profs.name), '_to': '{}/CSC101'.format(classes.name) }) teaches.insert({ '_from': '{}/anna'.format(profs.name), '_to': '{}/STA201'.format(classes.name) }) teaches.insert({ '_from': '{}/anna'.format(profs.name), '_to': '{}/MAT223'.format(classes.name) }) teaches.insert({ '_from': '{}/andy'.format(profs.name), '_to': '{}/MAT101'.format(classes.name) }) teaches.insert({ '_from': '{}/andy'.format(profs.name), '_to': '{}/MAT102'.format(classes.name) }) teaches.insert({ '_from': '{}/andy'.format(profs.name), '_to': '{}/MAT223'.format(classes.name) }) # Traverse the graph with default settings result = school.traverse('{}/anna'.format(profs.name)) visited = extract('_key', result['vertices']) assert visited == ['CSC101', 'MAT223', 'STA201', 'anna'] for path in result['paths']: for vertex in path['vertices']: assert set(vertex) == {'_id', '_key', '_rev', 'name'} for edge in path['edges']: assert set(edge) == {'_id', '_key', '_rev', '_to', '_from'} result = school.traverse('{}/andy'.format(profs.name)) visited = extract('_key', result['vertices']) assert visited == ['MAT101', 'MAT102', 'MAT223', 'andy'] # Traverse the graph with an invalid start vertex with assert_raises(GraphTraverseError): school.traverse('invalid') with assert_raises(GraphTraverseError): bad_col_name = generate_col_name() school.traverse('{}/hanna'.format(bad_col_name)) with assert_raises(GraphTraverseError): school.traverse('{}/anderson'.format(profs.name)) # Travers the graph with max iteration of 0 with assert_raises(GraphTraverseError): school.traverse('{}/andy'.format(profs.name), max_iter=0) # Traverse the graph with max depth of 0 result = school.traverse('{}/andy'.format(profs.name), max_depth=0) assert extract('_key', result['vertices']) == ['andy'] result = school.traverse('{}/anna'.format(profs.name), max_depth=0) assert extract('_key', result['vertices']) == ['anna'] # Traverse the graph with min depth of 2 result = school.traverse('{}/andy'.format(profs.name), min_depth=2) assert extract('_key', result['vertices']) == [] result = school.traverse('{}/anna'.format(profs.name), min_depth=2) assert extract('_key', result['vertices']) == [] # Traverse the graph with DFS and BFS result = school.traverse( {'_id': '{}/anna'.format(profs.name)}, strategy='dfs', direction='any', ) dfs_vertices = extract('_key', result['vertices']) result = school.traverse( {'_id': '{}/anna'.format(profs.name)}, strategy='bfs', direction='any' ) bfs_vertices = extract('_key', result['vertices']) assert sorted(dfs_vertices) == sorted(bfs_vertices) # Traverse the graph with filter function result = school.traverse( {'_id': '{}/andy'.format(profs.name)}, filter_func='if (vertex._key == "MAT101") {return "exclude";} return;' ) assert extract('_key', result['vertices']) == ['MAT102', 'MAT223', 'andy'] # Traverse the graph with global uniqueness (should be same as before) result = school.traverse( {'_id': '{}/andy'.format(profs.name)}, vertex_uniqueness='global', edge_uniqueness='global', filter_func='if (vertex._key == "MAT101") {return "exclude";} return;' ) assert extract('_key', result['vertices']) == ['MAT102', 'MAT223', 'andy'] with assert_raises(DocumentParseError) as err: school.traverse({}) assert err.value.message == 'field "_id" required'
def test_vertex_edges(db, bad_db): graph_name = generate_graph_name() vcol_name = generate_col_name() ecol_name = generate_col_name() # Prepare test documents anna = {"_id": f"{vcol_name}/anna"} dave = {"_id": f"{vcol_name}/dave"} josh = {"_id": f"{vcol_name}/josh"} mary = {"_id": f"{vcol_name}/mary"} tony = {"_id": f"{vcol_name}/tony"} # Create test graph, vertex and edge collections school = db.create_graph(graph_name) vcol = school.create_vertex_collection(vcol_name) ecol = school.create_edge_definition( edge_collection=ecol_name, from_vertex_collections=[vcol_name], to_vertex_collections=[vcol_name], ) # Insert test vertices into the graph vcol.insert(anna) vcol.insert(dave) vcol.insert(josh) vcol.insert(mary) vcol.insert(tony) # Insert test edges into the graph ecol.link(anna, dave) ecol.link(josh, dave) ecol.link(mary, dave) ecol.link(tony, dave) ecol.link(dave, anna) # Test edges with default direction (both) result = ecol.edges(dave) assert "stats" in result assert "filtered" in result["stats"] assert "scanned_index" in result["stats"] assert len(result["edges"]) == 5 result = ecol.edges(anna) assert len(result["edges"]) == 2 # Test edges with direction set to "in" result = ecol.edges(dave, direction="in") assert len(result["edges"]) == 4 result = ecol.edges(anna, direction="in") assert len(result["edges"]) == 1 # Test edges with direction set to "out" result = ecol.edges(dave, direction="out") assert len(result["edges"]) == 1 result = ecol.edges(anna, direction="out") assert len(result["edges"]) == 1 bad_graph = bad_db.graph(graph_name) with assert_raises(EdgeListError) as err: bad_graph.edge_collection(ecol_name).edges(dave) assert err.value.error_code in {11, 1228}
def test_graph_management(db, bad_db): # Test create graph graph_name = generate_graph_name() assert db.has_graph(graph_name) is False graph = db.create_graph(graph_name) assert db.has_graph(graph_name) is True assert graph.name == graph_name assert graph.db_name == db.name # Test create duplicate graph with assert_raises(GraphCreateError) as err: db.create_graph(graph_name) assert err.value.error_code == 1925 # Test get graph result = db.graph(graph_name) assert result.name == graph.name assert result.db_name == graph.db_name # Test get graphs result = db.graphs() for entry in result: assert 'revision' in entry assert 'edge_definitions' in entry assert 'orphan_collections' in entry assert graph_name in extract('name', db.graphs()) # Test get graphs with bad database with assert_raises(GraphListError) as err: bad_db.graphs() assert err.value.error_code in {11, 1228} # Test delete graph assert db.delete_graph(graph_name) is True assert graph_name not in extract('name', db.graphs()) # Test delete missing graph with assert_raises(GraphDeleteError) as err: db.delete_graph(graph_name) assert err.value.error_code == 1924 assert db.delete_graph(graph_name, ignore_missing=True) is False # Create a graph with vertex and edge collections and delete the graph graph = db.create_graph(graph_name) ecol_name = generate_col_name() fvcol_name = generate_col_name() tvcol_name = generate_col_name() graph.create_vertex_collection(fvcol_name) graph.create_vertex_collection(tvcol_name) graph.create_edge_definition( edge_collection=ecol_name, from_vertex_collections=[fvcol_name], to_vertex_collections=[tvcol_name] ) collections = extract('name', db.collections()) assert fvcol_name in collections assert tvcol_name in collections assert ecol_name in collections db.delete_graph(graph_name) collections = extract('name', db.collections()) assert fvcol_name in collections assert tvcol_name in collections assert ecol_name in collections # Create a graph with vertex and edge collections and delete all graph = db.create_graph(graph_name) graph.create_edge_definition( edge_collection=ecol_name, from_vertex_collections=[fvcol_name], to_vertex_collections=[tvcol_name] ) db.delete_graph(graph_name, drop_collections=True) collections = extract('name', db.collections()) assert fvcol_name not in collections assert tvcol_name not in collections assert ecol_name not in collections
def test_vertex_edges(db, bad_db): graph_name = generate_graph_name() vcol_name = generate_col_name() ecol_name = generate_col_name() # Prepare test documents anna = {'_id': '{}/anna'.format(vcol_name)} dave = {'_id': '{}/dave'.format(vcol_name)} josh = {'_id': '{}/josh'.format(vcol_name)} mary = {'_id': '{}/mary'.format(vcol_name)} tony = {'_id': '{}/tony'.format(vcol_name)} # Create test graph, vertex and edge collections school = db.create_graph(graph_name) vcol = school.create_vertex_collection(vcol_name) ecol = school.create_edge_definition( edge_collection=ecol_name, from_vertex_collections=[vcol_name], to_vertex_collections=[vcol_name] ) # Insert test vertices into the graph vcol.insert(anna) vcol.insert(dave) vcol.insert(josh) vcol.insert(mary) vcol.insert(tony) # Insert test edges into the graph ecol.link(anna, dave) ecol.link(josh, dave) ecol.link(mary, dave) ecol.link(tony, dave) ecol.link(dave, anna) # Test edges with default direction (both) result = ecol.edges(dave) assert 'stats' in result assert 'filtered' in result['stats'] assert 'scanned_index' in result['stats'] assert len(result['edges']) == 5 result = ecol.edges(anna) assert len(result['edges']) == 2 # Test edges with direction set to "in" result = ecol.edges(dave, direction='in') assert len(result['edges']) == 4 result = ecol.edges(anna, direction='in') assert len(result['edges']) == 1 # Test edges with direction set to "out" result = ecol.edges(dave, direction='out') assert len(result['edges']) == 1 result = ecol.edges(anna, direction='out') assert len(result['edges']) == 1 bad_graph = bad_db.graph(graph_name) with assert_raises(EdgeListError) as err: bad_graph.edge_collection(ecol_name).edges(dave) assert err.value.error_code in {11, 1228}
def test_vertex_edges(db, bad_db): graph_name = generate_graph_name() vcol_name = generate_col_name() ecol_name = generate_col_name() # Prepare test documents anna = {'_id': '{}/anna'.format(vcol_name)} dave = {'_id': '{}/dave'.format(vcol_name)} josh = {'_id': '{}/josh'.format(vcol_name)} mary = {'_id': '{}/mary'.format(vcol_name)} tony = {'_id': '{}/tony'.format(vcol_name)} # Create test graph, vertex and edge collections school = db.create_graph(graph_name) vcol = school.create_vertex_collection(vcol_name) ecol = school.create_edge_definition( edge_collection=ecol_name, from_vertex_collections=[vcol_name], to_vertex_collections=[vcol_name] ) # Insert test vertices into the graph vcol.insert(anna) vcol.insert(dave) vcol.insert(josh) vcol.insert(mary) vcol.insert(tony) # Insert test edges into the graph ecol.link(anna, dave) ecol.link(josh, dave) ecol.link(mary, dave) ecol.link(tony, dave) ecol.link(dave, anna) # Test edges with default direction (both) result = ecol.edges(dave) assert 'stats' in result assert 'filtered' in result['stats'] assert 'scanned_index' in result['stats'] assert len(result['edges']) == 5 result = ecol.edges(anna) assert len(result['edges']) == 2 # Test edges with direction set to "in" result = ecol.edges(dave, direction='in') assert len(result['edges']) == 4 result = ecol.edges(anna, direction='in') assert len(result['edges']) == 1 # Test edges with direction set to "out" result = ecol.edges(dave, direction='out') assert len(result['edges']) == 1 result = ecol.edges(anna, direction='out') assert len(result['edges']) == 1 bad_graph = bad_db.graph(graph_name) with assert_raises(EdgeListError) as err: bad_graph.edge_collection(ecol_name).edges(dave) assert err.value.error_code == 1228
def pytest_configure(config): url = f"http://{config.getoption('host')}:{config.getoption('port')}" secret = config.getoption("secret") client = ArangoClient(hosts=[url, url, url]) sys_db = client.db( name="_system", username="******", password=config.getoption("passwd"), superuser_token=generate_jwt(secret), ) sys_db.version() # Create a user and non-system database for testing. username = generate_username() password = generate_string() tst_db_name = generate_db_name() bad_db_name = generate_db_name() sys_db.create_database( name=tst_db_name, users=[ { "active": True, "username": username, "password": password, } ], ) tst_db = client.db(tst_db_name, username, password) bad_db = client.db(bad_db_name, username, password) # Create a standard collection for testing. col_name = generate_col_name() tst_col = tst_db.create_collection(col_name, edge=False) tst_col.add_skiplist_index(["val"]) tst_col.add_fulltext_index(["text"]) geo_index = tst_col.add_geo_index(["loc"]) # Create a legacy edge collection for testing. icol_name = generate_col_name() tst_db.create_collection(icol_name, edge=True) # Create test vertex & edge collections and graph. graph_name = generate_graph_name() ecol_name = generate_col_name() fvcol_name = generate_col_name() tvcol_name = generate_col_name() tst_graph = tst_db.create_graph(graph_name) tst_graph.create_vertex_collection(fvcol_name) tst_graph.create_vertex_collection(tvcol_name) tst_graph.create_edge_definition( edge_collection=ecol_name, from_vertex_collections=[fvcol_name], to_vertex_collections=[tvcol_name], ) # Update global config global_data.url = url global_data.client = client global_data.username = username global_data.password = password global_data.db_name = tst_db_name global_data.sys_db = sys_db global_data.tst_db = tst_db global_data.bad_db = bad_db global_data.geo_index = geo_index global_data.col_name = col_name global_data.icol_name = icol_name global_data.graph_name = graph_name global_data.ecol_name = ecol_name global_data.fvcol_name = fvcol_name global_data.tvcol_name = tvcol_name global_data.cluster = config.getoption("cluster") global_data.complete = config.getoption("complete") global_data.replication = config.getoption("replication") global_data.enterprise = config.getoption("enterprise") global_data.secret = secret global_data.root_password = config.getoption("passwd")