Skip to content

Commit 696f4ef

Browse files
authored
Merge pull request #111 from neo4j/1.1-example-code
Fix example code
2 parents 3b020eb + c02ae04 commit 696f4ef

File tree

6 files changed

+37
-40
lines changed

6 files changed

+37
-40
lines changed

docs/source/index.rst

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,25 @@ Example
7171

7272
.. code-block:: python
7373
74-
from neo4j.v1 import GraphDatabase
74+
from neo4j.v1 import GraphDatabase, basic_auth
7575
76-
driver = GraphDatabase.driver("bolt://localhost")
77-
session = driver.session()
76+
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "password"))
7877
79-
session.run("MERGE (a:Person {name:'Alice'})")
78+
with driver.session() as session:
8079
81-
friends = ["Bob", "Carol", "Dave", "Eve", "Frank"]
82-
with session.begin_transaction() as tx:
83-
for friend in friends:
84-
tx.run("MATCH (a:Person {name:'Alice'}) "
85-
"MERGE (a)-[:KNOWS]->(x:Person {name:{n}})", {"n": friend})
86-
tx.success = True
80+
with session.begin_transaction() as tx:
81+
session.run("MERGE (a:Person {name:'Alice'})")
8782
88-
for friend, in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(x) RETURN x"):
89-
print('Alice says, "hello, %s"' % friend["name"])
83+
friends = ["Bob", "Carol", "Dave", "Eve", "Frank"]
84+
with session.begin_transaction() as tx:
85+
for friend in friends:
86+
tx.run("MATCH (a:Person {name:'Alice'}) "
87+
"MERGE (a)-[:KNOWS]->(x:Person {name:{n}})", {"n": friend})
9088
91-
session.close()
89+
for record in session.run("MATCH (a:Person {name:'Alice'})-[:KNOWS]->(friend) RETURN friend"):
90+
print('Alice says, "hello, %s"' % record["friend"]["name"])
91+
92+
driver.close()
9293
9394
9495
Indices and tables

examples/test_examples.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class FreshDatabaseTestCase(ServerTestCase):
4545

4646
def setUp(self):
4747
ServerTestCase.setUp(self)
48-
session = GraphDatabase.driver("bolt://localhost", auth=auth_token).session()
48+
session = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token).session()
4949
session.run("MATCH (n) DETACH DELETE n")
5050
session.close()
5151

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

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

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

7272
def test_construct_driver(self):
7373
# tag::construct-driver[]
74-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"))
74+
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neotest", "neotest"))
7575
# end::construct-driver[]
7676
return driver
7777

7878
def test_configuration(self):
7979
# tag::configuration[]
80-
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neotest", "neotest"), max_pool_size=10)
80+
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neotest", "neotest"), max_pool_size=10)
8181
# end::configuration[]
8282
return driver
8383

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

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

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

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

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

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

129129
def test_result_traversal(self):
130-
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
130+
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
131131
session = driver.session()
132132
# tag::result-traversal[]
133133
search_term = "Sword"
@@ -140,7 +140,7 @@ def test_result_traversal(self):
140140
session.close()
141141

142142
def test_access_record(self):
143-
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
143+
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
144144
session = driver.session()
145145
# tag::access-record[]
146146
search_term = "Arthur"
@@ -153,7 +153,7 @@ def test_access_record(self):
153153
session.close()
154154

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

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

181181
def test_transaction_commit(self):
182-
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
182+
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
183183
session = driver.session()
184184
# tag::transaction-commit[]
185185
with session.begin_transaction() as tx:
@@ -192,7 +192,7 @@ def test_transaction_commit(self):
192192
session.close()
193193

194194
def test_transaction_rollback(self):
195-
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
195+
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
196196
session = driver.session()
197197
# tag::transaction-rollback[]
198198
with session.begin_transaction() as tx:
@@ -205,7 +205,7 @@ def test_transaction_rollback(self):
205205
session.close()
206206

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

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

230230
def test_handle_cypher_error(self):
231-
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
231+
driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
232232
session = driver.session()
233233
with self.assertRaises(RuntimeError):
234234
# tag::handle-cypher-error[]

neo4j/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def main():
3838
parser.add_argument("-p", "--parameter", action="append", metavar="NAME=VALUE")
3939
parser.add_argument("-q", "--quiet", action="store_true")
4040
parser.add_argument("-U", "--user", default="neo4j")
41-
parser.add_argument("-u", "--url", default="bolt://localhost", metavar="CONNECTION_URL")
41+
parser.add_argument("-u", "--url", default="bolt://localhost:7687", metavar="CONNECTION_URL")
4242
parser.add_argument("-v", "--verbose", action="count")
4343
parser.add_argument("-x", "--times", type=int, default=1)
4444
parser.add_argument("-z", "--summary", action="store_true")

neo4j/v1/session.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from __future__ import division
2929

3030
from collections import deque
31-
import re
3231
from warnings import warn
3332

3433
from .bolt import connect, Response, RUN, PULL_ALL, ConnectionPool
@@ -44,9 +43,6 @@
4443
from .types import hydrated
4544

4645

47-
localhost = re.compile(r"^(localhost|127(\.\d+){3})$", re.IGNORECASE)
48-
49-
5046
class AuthToken(object):
5147
""" Container for auth information
5248
"""
@@ -76,7 +72,7 @@ def driver(uri, **config):
7672
configuration:
7773
7874
>>> from neo4j.v1 import GraphDatabase
79-
>>> driver = GraphDatabase.driver("bolt://localhost")
75+
>>> driver = GraphDatabase.driver("bolt://localhost:7687")
8076
8177
:param uri: URI for a graph database
8278
:param config: configuration and authentication details (valid keys are listed below)

test/auth.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def update_password(user, password, new_password):
3535

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

4040

4141
if __name__ == "__main__":

test/test_stability.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ServerRestartTestCase(ServerTestCase):
3434

3535
# @skipIf(platform.system() == "Windows", "restart testing not supported on Windows")
3636
# def test_server_shutdown_detection(self):
37-
# driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
37+
# driver = GraphDatabase.driver("bolt://localhost:7687", auth=auth_token)
3838
# session = driver.session()
3939
# session.run("RETURN 1").consume()
4040
# assert restart_server()

0 commit comments

Comments
 (0)