Skip to content

Commit a47ee4d

Browse files
author
Zhen Li
committed
Merge pull request #55 from neo4j/1.0-doc-example
Added three missing examples
2 parents 6494c04 + e88bca0 commit a47ee4d

File tree

1 file changed

+37
-3
lines changed

1 file changed

+37
-3
lines changed

examples/test_examples.py

+37-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from unittest import skip, skipUnless
2323

2424
from neo4j.v1 import TRUST_ON_FIRST_USE, TRUST_SIGNED_CERTIFICATES, SSL_AVAILABLE
25+
from neo4j.v1.exceptions import CypherError
2526
from test.util import ServerTestCase
2627

2728
# Do not change the contents of this tagged section without good reason*
@@ -50,7 +51,7 @@ def test_minimal_working_example(self):
5051
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"))
5152
session = driver.session()
5253

53-
session.run("CREATE (a:Person {name:'Arthur', title:'King'})", )
54+
session.run("CREATE (a:Person {name:'Arthur', title:'King'})")
5455

5556
result = session.run("MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title")
5657
for record in result:
@@ -70,7 +71,7 @@ def test_construct_driver(self):
7071

7172
def test_configuration(self):
7273
# tag::configuration[]
73-
driver = GraphDatabase.driver("bolt://localhost", max_pool_size=10)
74+
driver = GraphDatabase.driver("bolt://localhost", auth=basic_auth("neo4j", "password"), max_pool_size=10)
7475
# end::configuration[]
7576
return driver
7677

@@ -94,6 +95,13 @@ def test_tls_signed(self):
9495
# end::tls-signed[]
9596
assert driver
9697

98+
@skipUnless(SSL_AVAILABLE, "Bolt over TLS is not supported by this version of Python")
99+
def test_connect_with_auth_disabled(self):
100+
# tag::connect-with-auth-disabled[]
101+
driver = GraphDatabase.driver("bolt://localhost", encrypted=True)
102+
# end::connect-with-auth-disabled[]
103+
assert driver
104+
97105
def test_statement(self):
98106
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
99107
session = driver.session()
@@ -116,7 +124,7 @@ def test_result_traversal(self):
116124
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
117125
session = driver.session()
118126
# tag::result-traversal[]
119-
search_term = "sword"
127+
search_term = "Sword"
120128
result = session.run("MATCH (weapon:Weapon) WHERE weapon.name CONTAINS {term} "
121129
"RETURN weapon.name", {"term": search_term})
122130
print("List of weapons called %r:" % search_term)
@@ -125,6 +133,19 @@ def test_result_traversal(self):
125133
# end::result-traversal[]
126134
session.close()
127135

136+
def test_access_record(self):
137+
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
138+
session = driver.session()
139+
# tag::access-record[]
140+
search_term = "Arthur"
141+
result = session.run("MATCH (weapon:Weapon) WHERE weapon.owner CONTAINS {term} "
142+
"RETURN weapon.name, weapon.material, weapon.size", {"term": search_term})
143+
print("List of weapons owned by %r:" % search_term)
144+
for record in result:
145+
print(", ".join("%s: %s" % (key, record[key]) for key in record.keys()))
146+
# end::access-record[]
147+
session.close()
148+
128149
def test_result_retention(self):
129150
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
130151
# tag::retain-result[]
@@ -199,3 +220,16 @@ def test_result_summary_notifications(self):
199220
print(notification)
200221
# end::result-summary-notifications[]
201222
session.close()
223+
224+
def test_handle_cypher_error(self):
225+
driver = GraphDatabase.driver("bolt://localhost", auth=auth_token)
226+
session = driver.session()
227+
with self.assertRaises(RuntimeError):
228+
# tag::handle-cypher-error
229+
try:
230+
session.run("This will cause a syntax error").consume()
231+
except CypherError:
232+
raise RuntimeError("Something really bad has happened!")
233+
finally:
234+
session.close()
235+
# end::handle-cypher-error

0 commit comments

Comments
 (0)