Ejemplo n.º 1
0
    def test_limit(self):
        query_builder = QueryBuilder().match().node(variable="n").limit("3")
        expected_query = " MATCH (n) LIMIT 3 "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 2
0
    def test_add_string_complete(self):
        query_builder = QueryBuilder().add_custom_cypher("MATCH (n) RETURN n")
        expected_query = "MATCH (n) RETURN n"

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 3
0
    def test_remove_label(self):
        query_builder = QueryBuilder().match().node(
            variable="n", labels=["Node1", "Node2"]).remove({"n:Node2"})
        expected_query = " MATCH (n:Node1:Node2) REMOVE n:Node2 "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 4
0
    def test_call_procedure_node2vec(self):
        query_builder = QueryBuilder().call(
            procedure="node2vec_online.get_embeddings",
            arguments="False, 2.0, 0.5")
        expected_query = " CALL node2vec_online.get_embeddings(False, 2.0, 0.5) "
        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 5
0
    def test_orderby_desc(self):
        query_builder = QueryBuilder().match().node(
            variable="n").order_by("n.id DESC")
        expected_query = " MATCH (n) ORDER BY n.id DESC "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 6
0
    def test_simple_merge(self):
        query_builder = QueryBuilder().merge().node(
            "L1", variable="n").to("TO").node("L2").return_()
        expected_query = " MERGE (n:L1)-[:TO]->(:L2) RETURN * "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 7
0
    def test_with_empty(self):
        query_builder = QueryBuilder().match().node(
            "L1", variable="n").to("TO").node("L2", variable="m").with_()
        expected_query = " MATCH (n:L1)-[:TO]->(m:L2) WITH * "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 8
0
    def test_with(self):
        query_builder = QueryBuilder().match().node(variable="n").with_(
            {"n": ""})
        expected_query = " MATCH (n) WITH n "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 9
0
    def test_delete(self):
        query_builder = QueryBuilder().match().node(variable="n1",
                                                    labels="Node1").delete(
                                                        {"n1"})
        expected_query = " MATCH (n1:Node1) DELETE n1 "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 10
0
    def test_simple_match_with_variables(self):
        query_builder = (QueryBuilder().match().node("L1", variable="n").to(
            "TO", variable="e").node("L2", variable="m").return_())
        expected_query = " MATCH (n:L1)-[e:TO]->(m:L2) RETURN * "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 11
0
    def test_delete_detach(self):
        query_builder = (QueryBuilder().match().node(
            variable="n1", labels="Node1").to(edge_label="EDGE").node(
                variable="n2", labels="Node2").delete(["n1", "n2"], True))
        expected_query = " MATCH (n1:Node1)-[:EDGE]->(n2:Node2) DETACH DELETE n1, n2 "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 12
0
    def test_where(self):
        query_builder = (QueryBuilder().match().node(
            "L1", variable="n").to("TO").node("L2", variable="m").where(
                "n.name", "=", "best_name").or_where("m.id", "<", 4).return_())
        expected_query = " MATCH (n:L1)-[:TO]->(m:L2) WHERE n.name = 'best_name' OR m.id < 4 RETURN * "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 13
0
    def test_multiple_matches(self):
        query_builder = (QueryBuilder().match().node(
            "L1",
            variable="n").to("TO").node("L2", variable="m").match(True).node(
                variable="n").to("TO").node("L3").return_())
        expected_query = " MATCH (n:L1)-[:TO]->(m:L2) OPTIONAL MATCH (n)-[:TO]->(:L3) RETURN * "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 14
0
    def test_return_alias_same_as_variable(self):
        query_builder = (QueryBuilder().match().node(
            "L1",
            variable="n").to("TO").node("L2",
                                        variable="m").return_({"L1": "L1"}))
        expected_query = " MATCH (n:L1)-[:TO]->(m:L2) RETURN L1 "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 15
0
    def test_call_procedure_nxalg_betweenness_centrality(self):
        query_builder = (QueryBuilder().call(
            procedure="nxalg.betweenness_centrality",
            arguments="20, True").yield_().return_({
                "node": "",
                "betweenness": ""
            }))
        expected_query = " CALL nxalg.betweenness_centrality(20, True) YIELD * RETURN node, betweenness "
        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 16
0
    def test_union_all(self):
        query_builder = (QueryBuilder().match().node(
            variable="n1", labels="Node1").return_({
                "n1": ""
            }).union().match().node(variable="n2",
                                    labels="Node2").return_({"n2": ""}))
        expected_query = " MATCH (n1:Node1) RETURN n1 UNION ALL MATCH (n2:Node2) RETURN n2 "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 17
0
    def test_get_single(self):
        query_builder = (QueryBuilder().match().node(
            "L1", variable="n").to("TO").node("L2",
                                              variable="m").return_({"n": ""}))
        expected_query = " MATCH (n:L1)-[:TO]->(m:L2) RETURN n "

        with patch.object(Memgraph,
                          "execute_and_fetch",
                          return_value=iter([{
                              "n": None
                          }])) as mock:
            query_builder.get_single(retrieve="n")

        mock.assert_called_with(expected_query)
Ejemplo n.º 18
0
    def test_unwind(self):
        query_builder = (QueryBuilder().unwind(
            list_expression="[1, 2, 3, null]", variable="x").return_({
                "x":
                "",
                "'val'":
                "y"
            }))
        expected_query = " UNWIND [1, 2, 3, null] AS x RETURN x, 'val' AS y "

        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 19
0
    def test_call_procedure_pagerank(self):
        query_builder = (QueryBuilder().call(procedure="pagerank.get").yield_({
            "node":
            "",
            "rank":
            ""
        }).return_({
            "node": "node",
            "rank": "rank"
        }))
        expected_query = " CALL pagerank.get() YIELD node, rank RETURN node, rank "
        with patch.object(Memgraph, "execute_and_fetch",
                          return_value=None) as mock:
            query_builder.execute()

        mock.assert_called_with(expected_query)
Ejemplo n.º 20
0
def test_invalid_match_chain_throws_exception():
    with pytest.raises(InvalidMatchChainException):
        QueryBuilder().node(":Label", "n").node(":Label", "m").return_()