コード例 #1
0
 def test_can_obtain_summary_info(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("CREATE (n) RETURN n")
         summary = result.summarize()
         assert summary.statement == "CREATE (n) RETURN n"
         assert summary.parameters == {}
         assert summary.statement_type == "rw"
         assert summary.statistics.nodes_created == 1
コード例 #2
0
 def test_can_run_statement_that_returns_multiple_records(self):
     session = GraphDatabase.driver("bolt://localhost").session()
     count = 0
     for record in session.run("unwind(range(1, 10)) AS z RETURN z"):
         assert 1 <= record[0] <= 10
         count += 1
     session.close()
     assert count == 10
コード例 #3
0
 def test_can_return_node(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("MERGE (a:Person {name:'Alice'}) RETURN a")
         assert len(result) == 1
         for record in result:
             alice = record[0]
             assert isinstance(alice, Node)
             assert alice.labels == {"Person"}
             assert alice.properties == {"name": "Alice"}
コード例 #4
0
 def test_can_return_relationship(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("MERGE ()-[r:KNOWS {since:1999}]->() RETURN r")
         assert len(result) == 1
         for record in result:
             rel = record[0]
             assert isinstance(rel, Relationship)
             assert rel.type == "KNOWS"
             assert rel.properties == {"since": 1999}
コード例 #5
0
 def test_can_obtain_plan_info(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("EXPLAIN CREATE (n) RETURN n")
         plan = result.summarize().plan
         assert plan.operator_type == "ProduceResults"
         assert plan.identifiers == ["n"]
         assert plan.arguments == {"planner": "COST", "EstimatedRows": 1.0, "version": "CYPHER 3.0",
                                   "KeyNames": "n", "runtime-impl": "INTERPRETED", "planner-impl": "IDP",
                                   "runtime": "INTERPRETED"}
         assert len(plan.children) == 1
コード例 #6
0
 def test_can_run_simple_statement_with_params(self):
     session = GraphDatabase.driver("bolt://localhost").session()
     count = 0
     for record in session.run("RETURN {x} AS n", {"x": {"abc": ["d", "e", "f"]}}):
         assert record[0] == {"abc": ["d", "e", "f"]}
         assert record["n"] == {"abc": ["d", "e", "f"]}
         assert repr(record)
         assert len(record) == 1
         count += 1
     session.close()
     assert count == 1
コード例 #7
0
 def test_can_obtain_profile_info(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("PROFILE CREATE (n) RETURN n")
         profile = result.summarize().profile
         assert profile.db_hits == 0
         assert profile.rows == 1
         assert profile.operator_type == "ProduceResults"
         assert profile.identifiers == ["n"]
         assert profile.arguments == {"planner": "COST", "EstimatedRows": 1.0, "version": "CYPHER 3.0",
                                      "KeyNames": "n", "runtime-impl": "INTERPRETED", "planner-impl": "IDP",
                                      "runtime": "INTERPRETED", "Rows": 1, "DbHits": 0}
         assert len(profile.children) == 1
コード例 #8
0
 def test_can_return_path(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("MERGE p=({name:'Alice'})-[:KNOWS]->({name:'Bob'}) RETURN p")
         assert len(result) == 1
         for record in result:
             path = record[0]
             assert isinstance(path, Path)
             assert path.start.properties == {"name": "Alice"}
             assert path.end.properties == {"name": "Bob"}
             assert path.relationships[0].type == "KNOWS"
             assert len(path.nodes) == 2
             assert len(path.relationships) == 1
コード例 #9
0
 def test_can_run_simple_statement_from_bytes_string(self):
     session = GraphDatabase.driver("bolt://localhost").session()
     count = 0
     for record in session.run(b"RETURN 1 AS n"):
         assert record[0] == 1
         assert record["n"] == 1
         assert record.n == 1
         assert repr(record)
         assert len(record) == 1
         count += 1
     session.close()
     assert count == 1
コード例 #10
0
    def test_can_rollback_transaction_using_with_block(self):
        with GraphDatabase.driver("bolt://localhost").session() as session:
            with session.begin_transaction() as tx:
                # Create a node
                result = tx.run("CREATE (a) RETURN id(a)")
                node_id = result[0][0]
                assert isinstance(node_id, int)

                # Update a property
                tx.run("MATCH (a) WHERE id(a) = {n} "
                       "SET a.foo = {foo}", {"n": node_id, "foo": "bar"})

            # Check the property value
            result = session.run("MATCH (a) WHERE id(a) = {n} "
                                 "RETURN a.foo", {"n": node_id})
            assert len(result) == 0
コード例 #11
0
 def test_can_run_simple_statement(self):
     session = GraphDatabase.driver("bolt://localhost").session()
     count = 0
     for record in session.run("RETURN 1 AS n"):
         assert record[0] == 1
         assert record["n"] == 1
         with self.assertRaises(AttributeError):
             _ = record["x"]
         assert record.n == 1
         with self.assertRaises(AttributeError):
             _ = record.x
         with self.assertRaises(TypeError):
             _ = record[object()]
         assert repr(record)
         assert len(record) == 1
         count += 1
     session.close()
     assert count == 1
コード例 #12
0
    def test_can_rollback_transaction_using_with_block(self):
        with GraphDatabase.driver("bolt://localhost").session() as session:
            with session.begin_transaction() as tx:
                # Create a node
                result = tx.run("CREATE (a) RETURN id(a)")
                node_id = result[0][0]
                assert isinstance(node_id, int)

                # Update a property
                tx.run("MATCH (a) WHERE id(a) = {n} "
                       "SET a.foo = {foo}", {
                           "n": node_id,
                           "foo": "bar"
                       })

            # Check the property value
            result = session.run("MATCH (a) WHERE id(a) = {n} "
                                 "RETURN a.foo", {"n": node_id})
            assert len(result) == 0
コード例 #13
0
 def test_can_obtain_profile_info(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("PROFILE CREATE (n) RETURN n")
         profile = result.summarize().profile
         assert profile.db_hits == 0
         assert profile.rows == 1
         assert profile.operator_type == "ProduceResults"
         assert profile.identifiers == ["n"]
         assert profile.arguments == {
             "planner": "COST",
             "EstimatedRows": 1.0,
             "version": "CYPHER 3.0",
             "KeyNames": "n",
             "runtime-impl": "INTERPRETED",
             "planner-impl": "IDP",
             "runtime": "INTERPRETED",
             "Rows": 1,
             "DbHits": 0
         }
         assert len(profile.children) == 1
コード例 #14
0
    def test_can_commit_transaction(self):
        with GraphDatabase.driver("bolt://localhost").session() as session:
            tx = session.begin_transaction()

            # Create a node
            result = tx.run("CREATE (a) RETURN id(a)")
            node_id = result[0][0]
            assert isinstance(node_id, int)

            # Update a property
            tx.run("MATCH (a) WHERE id(a) = {n} "
                   "SET a.foo = {foo}", {"n": node_id, "foo": "bar"})

            tx.commit()

            # Check the property value
            result = session.run("MATCH (a) WHERE id(a) = {n} "
                                 "RETURN a.foo", {"n": node_id})
            foo = result[0][0]
            assert foo == "bar"
コード例 #15
0
    def test_can_obtain_notification_info(self):
        with GraphDatabase.driver("bolt://localhost").session() as session:
            result = session.run("EXPLAIN MATCH (n), (m) RETURN n, m")
            notifications = result.summarize().notifications

            assert len(notifications) == 1
            notification = notifications[0]
            assert notification.code == "Neo.ClientNotification.Statement.CartesianProduct"
            assert notification.title == "This query builds a cartesian product between disconnected patterns."
            assert notification.description == \
                   "If a part of a query contains multiple disconnected patterns, " \
                   "this will build a cartesian product between all those parts. " \
                   "This may produce a large amount of data and slow down query processing. " \
                   "While occasionally intended, it may often be possible to reformulate the query " \
                   "that avoids the use of this cross product, perhaps by adding a relationship between " \
                   "the different parts or by using OPTIONAL MATCH (identifier is: (m))"
            position = notification.position
            assert position.offset == 0
            assert position.line == 1
            assert position.column == 1
コード例 #16
0
    def test_can_obtain_notification_info(self):
        with GraphDatabase.driver("bolt://localhost").session() as session:
            result = session.run("EXPLAIN MATCH (n), (m) RETURN n, m")
            notifications = result.summarize().notifications

            assert len(notifications) == 1
            notification = notifications[0]
            assert notification.code == "Neo.ClientNotification.Statement.CartesianProduct"
            assert notification.title == "This query builds a cartesian product between disconnected patterns."
            assert notification.description == \
                   "If a part of a query contains multiple disconnected patterns, " \
                   "this will build a cartesian product between all those parts. " \
                   "This may produce a large amount of data and slow down query processing. " \
                   "While occasionally intended, it may often be possible to reformulate the query " \
                   "that avoids the use of this cross product, perhaps by adding a relationship between " \
                   "the different parts or by using OPTIONAL MATCH (identifier is: (m))"
            position = notification.position
            assert position.offset == 0
            assert position.line == 1
            assert position.column == 1
コード例 #17
0
    def test_can_rollback_transaction(self):
        with GraphDatabase.driver("bolt://localhost",
                                  auth=auth_token).session() as session:
            tx = session.begin_transaction()

            # Create a node
            result = tx.run("CREATE (a) RETURN id(a)")
            record = next(iter(result))
            node_id = record[0]
            assert isinstance(node_id, int)

            # Update a property
            tx.run("MATCH (a) WHERE id(a) = {n} "
                   "SET a.foo = {foo}", {
                       "n": node_id,
                       "foo": "bar"
                   })

            tx.rollback()

            # Check the property value
            result = session.run("MATCH (a) WHERE id(a) = {n} "
                                 "RETURN a.foo", {"n": node_id})
            assert len(list(result)) == 0
コード例 #18
0
 def test_fails_on_bad_syntax(self):
     session = GraphDatabase.driver("bolt://localhost",
                                    auth=auth_token).session()
     with self.assertRaises(CypherError):
         session.run("X").consume()
コード例 #19
0
 def test_fails_on_missing_parameter(self):
     session = GraphDatabase.driver("bolt://localhost",
                                    auth=auth_token).session()
     with self.assertRaises(CypherError):
         session.run("RETURN {x}").consume()
コード例 #20
0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from neo4j.v1.session import GraphDatabase

driver = GraphDatabase.driver("bolt://localhost")
session = driver.session()

session.run("MERGE (a:Person {name:'Alice'})")

friends = ["Bob", "Carol", "Dave", "Eve", "Frank"]
with session.begin_transaction() as tx:
    for friend in friends:
        tx.run("MATCH (a:Person {name:'Alice'}) "
               "MERGE (a)-[:KNOWS]->(x:Person {name:{n}})", {"n": friend})
    tx.success = True

for friend, in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(x) RETURN x"):
    print('Alice says, "hello, %s"' % friend["name"])

session.close()
コード例 #21
0
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from neo4j.v1.session import GraphDatabase, basic_auth


driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "neo4j"))
session = driver.session()

session.run("MERGE (a:Person {name:'Alice'})")

friends = ["Bob", "Carol", "Dave", "Eve", "Frank"]
with session.begin_transaction() as tx:
    for friend in friends:
        tx.run("MATCH (a:Person {name:'Alice'}) "
               "MERGE (a)-[:KNOWS]->(x:Person {name:{n}})", {"n": friend})
    tx.success = True

for friend, in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(x) RETURN x"):
    print('Alice says, "hello, %s"' % friend["name"])

session.close()
コード例 #22
0
 def test_fails_on_bad_syntax(self):
     session = GraphDatabase.driver("bolt://localhost").session()
     with self.assertRaises(CypherError):
         session.run("X").consume()
コード例 #23
0
 def test_can_use_with_to_auto_close_session(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("RETURN 1")
         assert len(result) == 1
         for record in result:
             assert record[0] == 1
コード例 #24
0
 def test_no_notification_info(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("CREATE (n) RETURN n")
         notifications = result.summarize().notifications
         assert notifications == []
コード例 #25
0
 def test_healthy_session_will_be_returned_to_the_pool_on_close(self):
     driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
     assert len(driver.session_pool) == 0
     driver.session().close()
     assert len(driver.session_pool) == 1
コード例 #26
0
 def test_must_use_valid_url_scheme(self):
     with self.assertRaises(ValueError):
         GraphDatabase.driver("x://xxx")
コード例 #27
0
 def test_no_notification_info(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("CREATE (n) RETURN n")
         notifications = result.summarize().notifications
         assert notifications == []
コード例 #28
0
 def test_no_plan_info(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("CREATE (n) RETURN n")
         assert result.summarize().plan is None
         assert result.summarize().profile is None
コード例 #29
0
 def test_record_equality(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("unwind([1, 1]) AS a RETURN a")
         assert result[0] == result[1]
         assert result[0] != "this is not a record"
コード例 #30
0
 def setUp(self):
     self.driver = GraphDatabase.driver("bolt://localhost",
                                        auth=auth_token,
                                        encrypted=False)
コード例 #31
0
 def test_can_handle_cypher_error(self):
     with GraphDatabase.driver("bolt://localhost",
                               auth=auth_token).session() as session:
         with self.assertRaises(CypherError):
             session.run("X").consume()
コード例 #32
0
 def test_must_use_valid_url_scheme(self):
     with self.assertRaises(ValueError):
         GraphDatabase.driver("x://xxx")
コード例 #33
0
 def test_keys_with_an_error(self):
     with GraphDatabase.driver("bolt://localhost",
                               auth=auth_token).session() as session:
         result = session.run("X")
         with self.assertRaises(CypherError):
             list(result.keys())
コード例 #34
0
 def test_no_plan_info(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("CREATE (n) RETURN n")
         assert result.summarize().plan is None
         assert result.summarize().profile is None
コード例 #35
0
 def test_can_use_with_to_auto_close_session(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         result = session.run("RETURN 1")
         assert len(result) == 1
         for record in result:
             assert record[0] == 1
コード例 #36
0
 def test_fails_on_missing_parameter(self):
     session = GraphDatabase.driver("bolt://localhost").session()
     with self.assertRaises(CypherError):
         session.run("RETURN {x}").consume()
コード例 #37
0
 def test_must_use_valid_url_scheme(self):
     with self.assertRaises(ProtocolError):
         GraphDatabase.driver("x://xxx", auth=auth_token)
コード例 #38
0
 def test_can_handle_cypher_error(self):
     with GraphDatabase.driver("bolt://localhost").session() as session:
         with self.assertRaises(CypherError):
             session.run("X")