Skip to content

Commit 447faeb

Browse files
committed
Updated examples
1 parent 563e1cc commit 447faeb

File tree

2 files changed

+112
-47
lines changed

2 files changed

+112
-47
lines changed

neo4j/v1/session.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ def recycle(self, session):
114114
if session.healthy and len(pool) < self.max_pool_size and session not in pool:
115115
pool.appendleft(session)
116116

117+
def close(self):
118+
pass # TODO
119+
117120

118121
class ResultCursor(object):
119122
""" A handler for the result of Cypher statement execution.

test/test_examples.py

Lines changed: 109 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -21,89 +21,151 @@
2121

2222
from unittest import TestCase
2323

24+
# tag::minimal-example-import[]
2425
from neo4j.v1 import GraphDatabase
26+
# end::minimal-example-import[]
2527

2628

27-
class ExamplesTestCase(TestCase):
29+
class MinimalWorkingExampleTestCase(TestCase):
2830

2931
def setUp(self):
3032
session = GraphDatabase.driver("bolt://localhost").session()
31-
session.run("MATCH (n) DETACH DELETE n")
33+
session.run("MATCH (n) DETACH DELETE n").close()
3234
session.close()
3335

34-
def test_minimum_snippet(self):
35-
#tag::minimum-snippet[]
36+
def test_minimal_working_example(self):
37+
# tag::minimal-example[]
3638
driver = GraphDatabase.driver("bolt://localhost")
3739
session = driver.session()
40+
3841
session.run("CREATE (neo:Person {name:'Neo', age:23})")
3942

40-
for record in session.run("MATCH (p:Person) WHERE p.name = 'Neo' RETURN p.age").records():
41-
print("Neo is {0} years old.".format(record["p.age"]))
43+
cursor = session.run("MATCH (p:Person) WHERE p.name = 'Neo' RETURN p.age")
44+
while cursor.next():
45+
print("Neo is %d years old." % cursor["p.age"])
46+
47+
session.close()
48+
driver.close()
49+
# end::minimal-example[]
50+
51+
52+
class ExamplesTestCase(TestCase):
53+
54+
def setUp(self):
55+
session = GraphDatabase.driver("bolt://localhost").session()
56+
session.run("MATCH (n) DETACH DELETE n").close()
4257
session.close()
43-
#end::minimum-snippet[]
4458

45-
def test_statement_with_parameters(self):
59+
def test_construct_driver(self):
60+
# tag::construct-driver[]
61+
driver = GraphDatabase.driver("bolt://localhost")
62+
# end::construct-driver[]
63+
return driver
64+
65+
def test_configuration(self):
66+
# tag::configuration[]
67+
driver = GraphDatabase.driver("bolt://localhost", max_pool_size=10)
68+
# end::configuration[]
69+
return driver
70+
71+
def test_tls_require_encryption(self):
72+
# tag::tls-require-encryption[]
73+
# TODO: Unfortunately, this feature is not yet implemented for Python
74+
pass
75+
# end::tls-require-encryption[]
76+
77+
def test_tls_trust_on_first_use(self):
78+
# tag::tls-trust-on-first-use[]
79+
# TODO: Unfortunately, this feature is not yet implemented for Python
80+
pass
81+
# end::tls-trust-on-first-use[]
82+
83+
def test_tls_signed(self):
84+
# tag::tls-signed[]
85+
# TODO: Unfortunately, this feature is not yet implemented for Python
86+
pass
87+
# end::tls-signed[]
88+
89+
def test_statement(self):
4690
driver = GraphDatabase.driver("bolt://localhost")
4791
session = driver.session()
48-
#tag::statement[]
49-
cursor = session.run("CREATE (p:Person { name: {name} })", {"name": "The One"})
50-
ones_created = cursor.summarize().statistics.nodes_created
51-
print("There were {0} the ones created.".format(ones_created))
52-
#end::statement[]
53-
assert ones_created == 1
92+
# tag::statement[]
93+
session.run("CREATE (person:Person {name: {name}})", {"name": "Neo"}).close()
94+
# end::statement[]
5495
session.close()
96+
driver.close()
5597

5698
def test_statement_without_parameters(self):
5799
driver = GraphDatabase.driver("bolt://localhost")
58100
session = driver.session()
59-
#tag::statement-without-parameters[]
60-
cursor = session.run("CREATE (p:Person { name: 'The One' })")
61-
ones_created = cursor.summarize().statistics.nodes_created
62-
print("There were {0} the ones created.".format(ones_created))
63-
#end::statement-without-parameters[]
64-
assert ones_created == 1
101+
# tag::statement-without-parameters[]
102+
session.run("CREATE (person:Person {name: 'Neo'})").close()
103+
# end::statement-without-parameters[]
104+
session.close()
105+
driver.close()
106+
107+
def test_result_cursor(self):
108+
driver = GraphDatabase.driver("bolt://localhost")
109+
session = driver.session()
110+
# tag::result-cursor[]
111+
search_term = "hammer"
112+
cursor = session.run("MATCH (tool:Tool) WHERE tool.name CONTAINS {term} "
113+
"RETURN tool.name", {"term": search_term})
114+
print("List of tools called %r:" % search_term)
115+
while cursor.next():
116+
print(cursor["tool.name"])
117+
# end::result-cursor[]
65118
session.close()
119+
driver.close()
66120

67-
def test_commit_a_transaction(self):
121+
def test_transaction_commit(self):
68122
driver = GraphDatabase.driver("bolt://localhost")
69123
session = driver.session()
70-
#tag::transaction-commit[]
124+
# tag::transaction-commit[]
71125
tx = session.begin_transaction()
72-
tx.run("CREATE (p:Person { name: 'The One' })")
126+
tx.run("CREATE (p:Person {name: 'The One'})")
73127
tx.commit()
74-
#end::transaction-commit[]
75-
cursor = session.run("MATCH (p:Person { name: 'The One' }) RETURN count(p)")
128+
# end::transaction-commit[]
129+
cursor = session.run("MATCH (p:Person {name: 'The One'}) RETURN count(p)")
76130
assert cursor.single()
77131
assert cursor.record()["count(p)"] == 1
78132
session.close()
133+
driver.close()
79134

80-
def test_rollback_a_transaction(self):
135+
def test_transaction_rollback(self):
81136
driver = GraphDatabase.driver("bolt://localhost")
82137
session = driver.session()
83-
#tag::transaction-rollback[]
138+
# tag::transaction-rollback[]
84139
tx = session.begin_transaction()
85-
tx.run("CREATE (p:Person { name: 'The One' })")
140+
tx.run("CREATE (p:Person {name: 'The One'})")
86141
tx.rollback()
87-
#end::transaction-rollback[]
88-
cursor = session.run("MATCH (p:Person { name: 'The One' }) RETURN count(p)")
142+
# end::transaction-rollback[]
143+
cursor = session.run("MATCH (p:Person {name: 'The One'}) RETURN count(p)")
89144
assert cursor.single()
90145
assert cursor.record()["count(p)"] == 0
91146
session.close()
147+
driver.close()
92148

93-
def test_require_encryption(self):
94-
#tag::tls-require-encryption[]
95-
#Unfortunately, this feature is not yet implemented for Python
96-
pass
97-
#end::tls-require-encryption[]
98-
99-
def test_trust_on_first_use(self):
100-
#tag::tls-trust-on-first-use[]
101-
#Unfortunately, this feature is not yet implemented for Python
102-
pass
103-
#end::tls-trust-on-first-use[]
104-
105-
def test_signed_certificate(self):
106-
#tag::tls-signed[]
107-
#Unfortunately, this feature is not yet implemented for Python
108-
pass
109-
#end::tls-signed[]
149+
def test_result_summary_query_profile(self):
150+
driver = GraphDatabase.driver("bolt://localhost")
151+
session = driver.session()
152+
# tag::result-summary-query-profile[]
153+
cursor = session.run("PROFILE MATCH (p:Person {name: {name}}) "
154+
"RETURN id(p)", {"name": "The One"})
155+
summary = cursor.summarize()
156+
print(summary.statement_type)
157+
print(summary.profile)
158+
# end::result-summary-query-profile[]
159+
session.close()
160+
driver.close()
161+
\
162+
def test_result_summary_notifications(self):
163+
driver = GraphDatabase.driver("bolt://localhost")
164+
session = driver.session()
165+
# tag::result-summary-notifications[]
166+
summary = session.run("EXPLAIN MATCH (a), (b) RETURN a,b").summarize()
167+
for notification in summary.notifications:
168+
print(notification)
169+
# end::result-summary-notifications[]
170+
session.close()
171+
driver.close()

0 commit comments

Comments
 (0)