Skip to content

Commit

Permalink
Merge pull request #111 from neo4j/1.1-example-code
Browse files Browse the repository at this point in the history
Fix example code
  • Loading branch information
nigelsmall authored Dec 14, 2016
2 parents 3b020eb + c02ae04 commit 696f4ef
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 40 deletions.
27 changes: 14 additions & 13 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,25 @@ Example

.. code-block:: python
from neo4j.v1 import GraphDatabase
from neo4j.v1 import GraphDatabase, basic_auth
driver = GraphDatabase.driver("bolt://localhost")
session = driver.session()
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "password"))
session.run("MERGE (a:Person {name:'Alice'})")
with driver.session() as session:
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
with session.begin_transaction() as tx:
session.run("MERGE (a:Person {name:'Alice'})")
for friend, in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(x) RETURN x"):
print('Alice says, "hello, %s"' % friend["name"])
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})
session.close()
for record in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(friend) RETURN friend"):
print('Alice says, "hello, %s"' % record["friend"]["name"])
driver.close()
Indices and tables
Expand Down
38 changes: 19 additions & 19 deletions examples/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class FreshDatabaseTestCase(ServerTestCase):

def setUp(self):
ServerTestCase.setUp(self)
session = GraphDatabase.driver("bolt://localhost", auth=auth_token).session()
session = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token).session()
session.run("MATCH (n) DETACH DELETE n")
session.close()

Expand All @@ -54,7 +54,7 @@ class MinimalWorkingExampleTestCase(FreshDatabaseTestCase):

def test_minimal_working_example(self):
# tag::minimal-example[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"))
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neotest", "neotest"))
session = driver.session()

session.run("CREATE (a:Person {name:'Arthur', title:'King'})")
Expand All @@ -71,45 +71,45 @@ class ExamplesTestCase(FreshDatabaseTestCase):

def test_construct_driver(self):
# tag::construct-driver[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"))
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neotest", "neotest"))
# end::construct-driver[]
return driver

def test_configuration(self):
# tag::configuration[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"), max_pool_size=10)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neotest", "neotest"), max_pool_size=10)
# end::configuration[]
return driver

@skipUnless(SSL_AVAILABLE, "Bolt over TLS is not supported by this version of Python")
def test_tls_require_encryption(self):
# tag::tls-require-encryption[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"), encrypted=True)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neotest", "neotest"), encrypted=True)
# end::tls-require-encryption[]

@skipUnless(SSL_AVAILABLE, "Bolt over TLS is not supported by this version of Python")
def test_tls_trust_on_first_use(self):
# tag::tls-trust-on-first-use[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"), encrypted=True, trust=TRUST_ON_FIRST_USE)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neotest", "neotest"), encrypted=True, trust=TRUST_ON_FIRST_USE)
# end::tls-trust-on-first-use[]
assert driver

@skip("testing verified certificates not yet supported ")
def test_tls_signed(self):
# tag::tls-signed[]
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"), encrypted=True, trust=TRUST_SIGNED_CERTIFICATES)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neotest", "neotest"), encrypted=True, trust=TRUST_SIGNED_CERTIFICATES)
# end::tls-signed[]
assert driver

@skipUnless(SSL_AVAILABLE, "Bolt over TLS is not supported by this version of Python")
def test_connect_with_auth_disabled(self):
# tag::connect-with-auth-disabled[]
driver = GraphDatabase.driver("bolt://localhost", encrypted=True)
driver = GraphDatabase.driver("bolt://localhost:7687", encrypted=True)
# end::connect-with-auth-disabled[]
assert driver

def test_statement(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
session = driver.session()
# tag::statement[]
result = session.run("CREATE (person:Person {name: {name}})", {"name": "Arthur"})
Expand All @@ -118,7 +118,7 @@ def test_statement(self):
session.close()

def test_statement_without_parameters(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
session = driver.session()
# tag::statement-without-parameters[]
result = session.run("CREATE (person:Person {name: 'Arthur'})")
Expand All @@ -127,7 +127,7 @@ def test_statement_without_parameters(self):
session.close()

def test_result_traversal(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
session = driver.session()
# tag::result-traversal[]
search_term = "Sword"
Expand All @@ -140,7 +140,7 @@ def test_result_traversal(self):
session.close()

def test_access_record(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
session = driver.session()
# tag::access-record[]
search_term = "Arthur"
Expand All @@ -153,7 +153,7 @@ def test_access_record(self):
session.close()

def test_result_retention(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
# tag::retain-result[]
session = driver.session()
result = session.run("MATCH (knight:Person:Knight) WHERE knight.castle = {castle} "
Expand All @@ -166,7 +166,7 @@ def test_result_retention(self):
assert isinstance(retained_result, list)

def test_nested_statements(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
session = driver.session()
# tag::nested-statements[]
result = session.run("MATCH (knight:Person:Knight) WHERE knight.castle = {castle} "
Expand All @@ -179,7 +179,7 @@ def test_nested_statements(self):
session.close()

def test_transaction_commit(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
session = driver.session()
# tag::transaction-commit[]
with session.begin_transaction() as tx:
Expand All @@ -192,7 +192,7 @@ def test_transaction_commit(self):
session.close()

def test_transaction_rollback(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
session = driver.session()
# tag::transaction-rollback[]
with session.begin_transaction() as tx:
Expand All @@ -205,7 +205,7 @@ def test_transaction_rollback(self):
session.close()

def test_result_summary_query_profile(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
session = driver.session()
# tag::result-summary-query-profile[]
result = session.run("PROFILE MATCH (p:Person {name: {name}}) "
Expand All @@ -217,7 +217,7 @@ def test_result_summary_query_profile(self):
session.close()

def test_result_summary_notifications(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
session = driver.session()
# tag::result-summary-notifications[]
result = session.run("EXPLAIN MATCH (king), (queen) RETURN king, queen")
Expand All @@ -228,7 +228,7 @@ def test_result_summary_notifications(self):
session.close()

def test_handle_cypher_error(self):
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
session = driver.session()
with self.assertRaises(RuntimeError):
# tag::handle-cypher-error[]
Expand Down
2 changes: 1 addition & 1 deletion neo4j/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def main():
parser.add_argument("-p", "--parameter", action="append", metavar="NAME=VALUE")
parser.add_argument("-q", "--quiet", action="store_true")
parser.add_argument("-U", "--user", default="neo4j")
parser.add_argument("-u", "--url", default="bolt://localhost", metavar="CONNECTION_URL")
parser.add_argument("-u", "--url", default="bolt://localhost:7687", metavar="CONNECTION_URL")
parser.add_argument("-v", "--verbose", action="count")
parser.add_argument("-x", "--times", type=int, default=1)
parser.add_argument("-z", "--summary", action="store_true")
Expand Down
6 changes: 1 addition & 5 deletions neo4j/v1/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from __future__ import division

from collections import deque
import re
from warnings import warn

from .bolt import connect, Response, RUN, PULL_ALL, ConnectionPool
Expand All @@ -44,9 +43,6 @@
from .types import hydrated


localhost = re.compile(r"^(localhost|127(\.\d+){3})$", re.IGNORECASE)


class AuthToken(object):
""" Container for auth information
"""
Expand Down Expand Up @@ -76,7 +72,7 @@ def driver(uri, **config):
configuration:
>>> from neo4j.v1 import GraphDatabase
>>> driver = GraphDatabase.driver("bolt://localhost")
>>> driver = GraphDatabase.driver("bolt://localhost:7687")
:param uri: URI for a graph database
:param config: configuration and authentication details (valid keys are listed below)
Expand Down
2 changes: 1 addition & 1 deletion test/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def update_password(user, password, new_password):

token = basic_auth(user, password)
setattr(token, "new-credentials", new_password) # TODO: hopefully switch hyphen to underscore on server
GraphDatabase.driver("bolt://localhost", auth=token).session().close()
GraphDatabase.driver("bolt://localhost:7687", auth=token).session().close()


if __name__ == "__main__":
Expand Down
2 changes: 1 addition & 1 deletion test/test_stability.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ServerRestartTestCase(ServerTestCase):

# @skipIf(platform.system() == "Windows", "restart testing not supported on Windows")
# def test_server_shutdown_detection(self):
# driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
# driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
# session = driver.session()
# session.run("RETURN 1").consume()
# assert restart_server()
Expand Down

0 comments on commit 696f4ef

Please sign in to comment.