def test_subgraph_creation_from_text(self): s = geoff.Subgraph( '(A) {"name": "Alice"}', '(B) {"name": "Bob"}', '(A)-[:KNOWS]->(B)' ) self.assertEqual('(A) {"name": "Alice"}\n(B) {"name": "Bob"}\n(A)-[0:KNOWS]->(B) {}', s.dumps())
def test_subgraph_creation_from_db(self): graph_db = neo4j.GraphDatabaseService() a, b, ab = graph_db.create( {"name": "Alice"}, {"name": "Bob"}, (0, "KNOWS", 1) ) s = geoff.Subgraph(a, b, ab) self.assertEqual('(0) {"name": "Alice"}\n(1) {"name": "Bob"}\n(0)-[0:KNOWS]->(1) {}', s.dumps())
def test_merge_from_abstract(self): s = geoff.Subgraph( {"name": "Alice"}, {"name": "Bob"}, (0, "KNOWS", 1) ) params = s.merge_into(self.graph_db) self.assertIn("(0)", params) self.assertIn("(1)", params) self.assertIn("[0]", params)
def test_subgraph_dump(self): a, b, ab = self.graph_db.create( {"name": "Alice"}, {"name": "Bob"}, (0, "KNOWS", 1) ) out = geoff.Subgraph(a, b, ab).dumps() self.assertEqual('(0) {"name": "Alice"}\n' \ '(1) {"name": "Bob"}\n' \ '(0)-[0:KNOWS]->(1) {}', out)
def test_can_insert_single_node(): graph_db = neo4j.GraphDatabaseService() source = '(a {"name": "Alice"})' subgraph = geoff.Subgraph(source) out = subgraph.insert_into(graph_db) assert isinstance(out["a"], neo4j.Node) assert out["a"].get_properties() == {"name": "Alice"} matches = list(out["a"].match_outgoing()) assert len(matches) == 0
def test_merge_from_text(self): s = geoff.Subgraph( '(A) {"name": "Alice"}', '(B) {"name": "Bob"}', '(A)-[:KNOWS]->(B)' ) params = s.merge_into(self.graph_db) self.assertIn("(A)", params) self.assertIn("(B)", params) self.assertIn("[0]", params)
def test_subgraph_creation_from_text_with_alternate_ordering(self): s = geoff.Subgraph( '(D) {"name": "Dave"}', '(B) {"name": "Bob"}', '(C) {"name": "Carol"}', '(C)-[:KNOWS]->(D)', '(A) {"name": "Alice"}', '(A)-[:KNOWS]->(B)', ) self.assertEqual('(D) {"name": "Dave"}\n(B) {"name": "Bob"}\n' \ '(C) {"name": "Carol"}\n(C)-[0:KNOWS]->(D) {}\n' \ '(A) {"name": "Alice"}\n(A)-[1:KNOWS]->(B) {}', s.dumps())
def test_can_merge_simple_graph(): graph_db = neo4j.GraphDatabaseService() source = '(a {"name": "Alice"}) (b {"name": "Bob"}) (a)-[:KNOWS]->(b)' subgraph = geoff.Subgraph(source) out = subgraph.merge_into(graph_db) assert isinstance(out["a"], neo4j.Node) assert isinstance(out["b"], neo4j.Node) assert out["a"].get_properties() == {"name": "Alice"} assert out["b"].get_properties() == {"name": "Bob"} matches = list(out["a"].match_outgoing(end_node=out["b"])) assert len(matches) == 1 assert matches[0].type == "KNOWS"
def test_can_identify_non_unique_paths(): graph_db = neo4j.GraphDatabaseService() graph_db.clear() source = """\ |People {"email":"*****@*****.**"}|=>(a) |People {"email":"*****@*****.**"}|=>(b) (a {"name": "Alice"}) (b {"name": "Bob"}) (a)-[:KNOWS]->(b) (a)-[:KNOWS]->(b) """ geoff.Subgraph(source).insert_into(graph_db) source = """\ |People {"email":"*****@*****.**"}|=>(a) |People {"email":"*****@*****.**"}|=>(b) (a)-[:KNOWS]->(b) """ try: geoff.Subgraph(source).merge_into(graph_db) except geoff.ConstraintViolation: assert True else: assert False
def test_stuff(self): source = r""" |People {"email":"*****@*****.**"}|=>(b) |People {"email":"*****@*****.**"}|=>(e) |People {"email":"*****@*****.**"}|=>(e) (a {name:"Alice"}) (b) {"name":"Bob Robertson"} (a {age:43})-[:KNOWS]->(b)-[:KNOWS]->(c)<-[:LOVES {amount:"lots"}]-(d) (f {name:"Lonely Frank"}) /* Alice and Bob got married twice */ (a)-[:MARRIED {date:"1970-01-01"}]->(b) (a)-[:MARRIED {date:"2001-09-11"}]->(b) """ s = geoff.Subgraph(source) print(s.nodes) print(s.relationships) print(s.index_entries) print(s._indexed_nodes) print(s._related_nodes) print(s._odd_nodes) for name, node in s.insert_into(self.graph_db).items(): print(name, node)
def test_can_insert_reasonably_complex_graph(): graph_db = neo4j.GraphDatabaseService() source = r""" |People {"email":"*****@*****.**"}|=>(b) |People {"email":"*****@*****.**"}|=>(e) |People {"email":"*****@*****.**"}|=>(e) (a {name:"Alice"}) (b) {"name":"Bob Robertson"} (a {age:43})-[:KNOWS]->(b)-[:KNOWS]->(c)<-[:LOVES {amount:"lots"}]-(d) (f {name:"Lonely Frank"}) /* Alice and Bob got married twice */ (a)-[:MARRIED {date:"1970-01-01"}]->(b) (a)-[:MARRIED {date:"2001-09-11"}]->(b) """ subgraph = geoff.Subgraph(source) out = subgraph.insert_into(graph_db) assert len(out) == 6 assert all(isinstance(node, neo4j.Node) for node in out.values()) assert out["a"].get_properties() == {"name": "Alice", "age": 43} assert out["b"].get_properties() == {"name": "Bob Robertson"} assert out["c"].get_properties() == {} assert out["d"].get_properties() == {} assert out["e"].get_properties() == {} assert out["f"].get_properties() == {"name": "Lonely Frank"}
def test_simple_subgraph_creation(self): s = geoff.Subgraph({"name": "Alice"}, {"name": "Bob"}, (0, "KNOWS", 1)) self.assertEqual(2, len(s.nodes)) self.assertEqual(1, len(s.relationships))
def test_empty_subgraph_creation(self): s = geoff.Subgraph() self.assertEqual(0, len(s.nodes)) self.assertEqual(0, len(s.relationships))
def test_node_dump(self): a, = self.graph_db.create( {"name": "Alice"} ) out = geoff.Subgraph(a).dumps() self.assertEqual('(0) {"name": "Alice"}', out)
def test_can_insert_empty_subgraph(): graph_db = neo4j.GraphDatabaseService() source = '' subgraph = geoff.Subgraph(source) out = subgraph.insert_into(graph_db) assert out == {}