def test_stringify_query_result(client): graph = client.graph() john = Node( alias="a", label="person", properties={ "name": "John Doe", "age": 33, "gender": "male", "status": "single", }, ) graph.add_node(john) japan = Node(alias="b", label="country", properties={"name": "Japan"}) graph.add_node(japan) edge = Edge(john, "visited", japan, properties={"purpose": "pleasure"}) graph.add_edge(edge) assert ( str(john) == """(a:person{age:33,gender:"male",name:"John Doe",status:"single"})""" # noqa ) assert ( str(edge) == """(a:person{age:33,gender:"male",name:"John Doe",status:"single"})""" # noqa + """-[:visited{purpose:"pleasure"}]->""" + """(b:country{name:"Japan"})""" ) assert str(japan) == """(b:country{name:"Japan"})""" graph.commit() query = """MATCH (p:person)-[v:visited {purpose:"pleasure"}]->(c:country) RETURN p, v, c""" result = client.graph().query(query) person = result.result_set[0][0] visit = result.result_set[0][1] country = result.result_set[0][2] assert ( str(person) == """(:person{age:33,gender:"male",name:"John Doe",status:"single"})""" # noqa ) assert str(visit) == """()-[:visited{purpose:"pleasure"}]->()""" assert str(country) == """(:country{name:"Japan"})""" graph.delete()
def test_path(client): node0 = Node(node_id=0, label="L1") node1 = Node(node_id=1, label="L1") edge01 = Edge(node0, "R1", node1, edge_id=0, properties={"value": 1}) graph = client.graph() graph.add_node(node0) graph.add_node(node1) graph.add_edge(edge01) graph.flush() path01 = Path.new_empty_path().add_node(node0).add_edge(edge01).add_node(node1) expected_results = [[path01]] query = "MATCH p=(:L1)-[:R1]->(:L1) RETURN p ORDER BY p" result = graph.query(query) assert expected_results == result.result_set
def test_graph_creation(client): graph = client.graph() john = Node( label="person", properties={ "name": "John Doe", "age": 33, "gender": "male", "status": "single", }, ) graph.add_node(john) japan = Node(label="country", properties={"name": "Japan"}) graph.add_node(japan) edge = Edge(john, "visited", japan, properties={"purpose": "pleasure"}) graph.add_edge(edge) graph.commit() query = ( 'MATCH (p:person)-[v:visited {purpose:"pleasure"}]->(c:country) ' "RETURN p, v, c" ) result = graph.query(query) person = result.result_set[0][0] visit = result.result_set[0][1] country = result.result_set[0][2] assert person == john assert visit.properties == edge.properties assert country == japan query = """RETURN [1, 2.3, "4", true, false, null]""" result = graph.query(query) assert [1, 2.3, "4", True, False, None] == result.result_set[0][0] # All done, remove graph. graph.delete()
def test_optional_match(client): # Build a graph of form (a)-[R]->(b) node0 = Node(node_id=0, label="L1", properties={"value": "a"}) node1 = Node(node_id=1, label="L1", properties={"value": "b"}) edge01 = Edge(node0, "R", node1, edge_id=0) graph = client.graph() graph.add_node(node0) graph.add_node(node1) graph.add_edge(edge01) graph.flush() # Issue a query that collects all outgoing edges from both nodes # (the second has none) query = """MATCH (a) OPTIONAL MATCH (a)-[e]->(b) RETURN a, e, b ORDER BY a.value""" # noqa expected_results = [[node0, edge01, node1], [node1, None, None]] result = client.graph().query(query) assert expected_results == result.result_set graph.delete()
def test_multi_label(client): redis_graph = client.graph("g") node = Node(label=["l", "ll"]) redis_graph.add_node(node) redis_graph.commit() query = "MATCH (n) RETURN n" result = redis_graph.query(query) result_node = result.result_set[0][0] assert result_node == node try: Node(label=1) assert False except AssertionError: assert True try: Node(label=["l", 1]) assert False except AssertionError: assert True
def test_array_functions(client): query = """CREATE (p:person{name:'a',age:32, array:[0,1,2]})""" client.graph().query(query) query = """WITH [0,1,2] as x return x""" result = client.graph().query(query) assert [0, 1, 2] == result.result_set[0][0] query = """MATCH(n) return collect(n)""" result = client.graph().query(query) a = Node( node_id=0, label="person", properties={"name": "a", "age": 32, "array": [0, 1, 2]}, ) assert [a] == result.result_set[0][0]