1919# limitations under the License.
2020
2121
22- from unittest import TestCase
22+ from unittest import skip
2323
24+ from neo4j .v1 import TRUST_ON_FIRST_USE , TRUST_SIGNED_CERTIFICATES
25+ from test .util import ServerTestCase
26+
27+ # Do not change the contents of this tagged section without good reason*
2428# tag::minimal-example-import[]
25- from neo4j .v1 import GraphDatabase
29+ from neo4j .v1 import GraphDatabase , basic_auth
2630# end::minimal-example-import[]
31+ # (* "good reason" is defined as knowing what you are doing)
32+
33+
34+ auth_token = basic_auth ("neo4j" , "password" )
2735
2836
29- class FreshDatabaseTestCase (TestCase ):
37+ class FreshDatabaseTestCase (ServerTestCase ):
3038
3139 def setUp (self ):
32- session = GraphDatabase .driver ("bolt://localhost" ).session ()
40+ ServerTestCase .setUp (self )
41+ session = GraphDatabase .driver ("bolt://localhost" , auth = auth_token ).session ()
3342 session .run ("MATCH (n) DETACH DELETE n" )
3443 session .close ()
3544
@@ -38,14 +47,14 @@ class MinimalWorkingExampleTestCase(FreshDatabaseTestCase):
3847
3948 def test_minimal_working_example (self ):
4049 # tag::minimal-example[]
41- driver = GraphDatabase .driver ("bolt://localhost" )
50+ driver = GraphDatabase .driver ("bolt://localhost" , auth = basic_auth ( "neo4j" , "password" ) )
4251 session = driver .session ()
4352
44- session .run ("CREATE (neo :Person {name:'Neo ', age:23 })" )
53+ session .run ("CREATE (a :Person {name:'Arthur ', title:'King' })" , )
4554
46- cursor = session .run ("MATCH (p :Person) WHERE p .name = 'Neo ' RETURN p.age " )
47- while cursor . next () :
48- print ("Neo is %d years old. " % cursor [ "p.age" ] )
55+ result = session .run ("MATCH (a :Person) WHERE a .name = 'Arthur ' RETURN a.name AS name, a.title AS title " )
56+ for record in result :
57+ print ("%s %s " % ( record [ "title" ], record [ "name" ]) )
4958
5059 session .close ()
5160 # end::minimal-example[]
@@ -55,7 +64,7 @@ class ExamplesTestCase(FreshDatabaseTestCase):
5564
5665 def test_construct_driver (self ):
5766 # tag::construct-driver[]
58- driver = GraphDatabase .driver ("bolt://localhost" )
67+ driver = GraphDatabase .driver ("bolt://localhost" , auth = basic_auth ( "neo4j" , "password" ) )
5968 # end::construct-driver[]
6069 return driver
6170
@@ -67,124 +76,123 @@ def test_configuration(self):
6776
6877 def test_tls_require_encryption (self ):
6978 # tag::tls-require-encryption[]
70- # TODO: Unfortunately, this feature is not yet implemented for Python
71- pass
79+ driver = GraphDatabase .driver ("bolt://localhost" , auth = basic_auth ("neo4j" , "password" ), encrypted = True )
7280 # end::tls-require-encryption[]
7381
7482 def test_tls_trust_on_first_use (self ):
7583 # tag::tls-trust-on-first-use[]
76- # TODO: Unfortunately, this feature is not yet implemented for Python
77- pass
84+ driver = GraphDatabase .driver ("bolt://localhost" , auth = basic_auth ("neo4j" , "password" ), encrypted = True , trust = TRUST_ON_FIRST_USE )
7885 # end::tls-trust-on-first-use[]
86+ assert driver
7987
88+ @skip ("testing verified certificates not yet supported " )
8089 def test_tls_signed (self ):
8190 # tag::tls-signed[]
82- # TODO: Unfortunately, this feature is not yet implemented for Python
83- pass
91+ driver = GraphDatabase .driver ("bolt://localhost" , auth = basic_auth ("neo4j" , "password" ), encrypted = True , trust = TRUST_SIGNED_CERTIFICATES )
8492 # end::tls-signed[]
93+ assert driver
8594
8695 def test_statement (self ):
87- driver = GraphDatabase .driver ("bolt://localhost" )
96+ driver = GraphDatabase .driver ("bolt://localhost" , auth = auth_token )
8897 session = driver .session ()
8998 # tag::statement[]
90- session .run ("CREATE (person:Person {name: {name}})" , {"name" : "Neo" }). close ( )
99+ result = session .run ("CREATE (person:Person {name: {name}})" , {"name" : "Arthur" } )
91100 # end::statement[]
101+ result .consume ()
92102 session .close ()
93103
94104 def test_statement_without_parameters (self ):
95- driver = GraphDatabase .driver ("bolt://localhost" )
105+ driver = GraphDatabase .driver ("bolt://localhost" , auth = auth_token )
96106 session = driver .session ()
97107 # tag::statement-without-parameters[]
98- session .run ("CREATE (person:Person {name: 'Neo '})" ). close ( )
108+ result = session .run ("CREATE (person:Person {name: 'Arthur '})" )
99109 # end::statement-without-parameters[]
110+ result .consume ()
100111 session .close ()
101112
102- def test_result_cursor (self ):
103- driver = GraphDatabase .driver ("bolt://localhost" )
113+ def test_result_traversal (self ):
114+ driver = GraphDatabase .driver ("bolt://localhost" , auth = auth_token )
104115 session = driver .session ()
105- # tag::result-cursor []
106- search_term = "hammer "
107- cursor = session .run ("MATCH (tool:Tool ) WHERE tool .name CONTAINS {term} "
108- "RETURN tool .name" , {"term" : search_term })
109- print ("List of tools called %r:" % search_term )
110- while cursor . next () :
111- print (cursor [ "tool .name" ])
112- # end::result-cursor []
116+ # tag::result-traversal []
117+ search_term = "sword "
118+ result = session .run ("MATCH (weapon:Weapon ) WHERE weapon .name CONTAINS {term} "
119+ "RETURN weapon .name" , {"term" : search_term })
120+ print ("List of weapons called %r:" % search_term )
121+ for record in result :
122+ print (record [ "weapon .name" ])
123+ # end::result-traversal []
113124 session .close ()
114125
115- def test_cursor_nesting (self ):
116- driver = GraphDatabase .driver ("bolt://localhost" )
126+ def test_result_retention (self ):
127+ driver = GraphDatabase .driver ("bolt://localhost" , auth = auth_token )
128+ # tag::retain-result[]
117129 session = driver .session ()
118- # tag::retain-result-query[]
119- cursor = session .run ("MATCH (person:Person) WHERE person.dept = {dept} "
120- "RETURN id(person) AS minion" , {"dept" : "IT" })
121- while cursor .next ():
122- session .run ("MATCH (person) WHERE id(person) = {id} "
123- "MATCH (boss:Person) WHERE boss.name = {boss} "
124- "CREATE (person)-[:REPORTS_TO]->(boss)" , {"id" : cursor ["minion" ], "boss" : "Bob" })
125- # end::retain-result-query[]
130+ result = session .run ("MATCH (knight:Person:Knight) WHERE knight.castle = {castle} "
131+ "RETURN knight.name AS name" , {"castle" : "Camelot" })
132+ retained_result = list (result )
126133 session .close ()
134+ for record in retained_result :
135+ print ("%s is a knight of Camelot" % record ["name" ])
136+ # end::retain-result[]
137+ assert isinstance (retained_result , list )
127138
128- def test_result_retention (self ):
129- driver = GraphDatabase .driver ("bolt://localhost" )
139+ def test_nested_statements (self ):
140+ driver = GraphDatabase .driver ("bolt://localhost" , auth = auth_token )
130141 session = driver .session ()
131- # tag::retain-result-process[]
132- cursor = session .run ("MATCH (person:Person) WHERE person.dept = {dept} "
133- "RETURN id(person) AS minion" , {"dept" : "IT" })
134- minion_records = list (cursor .stream ())
135-
136- for record in minion_records :
137- session .run ("MATCH (person) WHERE id(person) = {id} "
138- "MATCH (boss:Person) WHERE boss.name = {boss} "
139- "CREATE (person)-[:REPORTS_TO]->(boss)" , {"id" : record ["minion" ], "boss" : "Bob" })
140- # end::retain-result-process[]
142+ # tag::nested-statements[]
143+ result = session .run ("MATCH (knight:Person:Knight) WHERE knight.castle = {castle} "
144+ "RETURN id(knight) AS knight_id" , {"castle" : "Camelot" })
145+ for record in result :
146+ session .run ("MATCH (knight) WHERE id(knight) = {id} "
147+ "MATCH (king:Person) WHERE king.name = {king} "
148+ "CREATE (knight)-[:DEFENDS]->(king)" , {"id" : record ["knight_id" ], "king" : "Arthur" })
149+ # end::nested-statements[]
141150 session .close ()
142151
143152 def test_transaction_commit (self ):
144- driver = GraphDatabase .driver ("bolt://localhost" )
153+ driver = GraphDatabase .driver ("bolt://localhost" , auth = auth_token )
145154 session = driver .session ()
146155 # tag::transaction-commit[]
147156 tx = session .begin_transaction ()
148- tx .run ("CREATE (p :Person {name: 'The One '})" )
157+ tx .run ("CREATE (:Person {name: 'Guinevere '})" )
149158 tx .commit ()
150159 # end::transaction-commit[]
151- cursor = session .run ("MATCH (p:Person {name: 'The One'}) RETURN count(p)" )
152- assert cursor .next ()
153- assert cursor ["count(p)" ] == 1
154- assert cursor .at_end ()
160+ result = session .run ("MATCH (p:Person {name: 'Guinevere'}) RETURN count(p)" )
161+ record = next (result )
162+ assert record ["count(p)" ] == 1
155163 session .close ()
156164
157165 def test_transaction_rollback (self ):
158- driver = GraphDatabase .driver ("bolt://localhost" )
166+ driver = GraphDatabase .driver ("bolt://localhost" , auth = auth_token )
159167 session = driver .session ()
160168 # tag::transaction-rollback[]
161169 tx = session .begin_transaction ()
162- tx .run ("CREATE (p :Person {name: 'The One '})" )
170+ tx .run ("CREATE (:Person {name: 'Merlin '})" )
163171 tx .rollback ()
164172 # end::transaction-rollback[]
165- cursor = session .run ("MATCH (p:Person {name: 'The One'}) RETURN count(p)" )
166- assert cursor .next ()
167- assert cursor ["count(p)" ] == 0
168- assert cursor .at_end ()
173+ result = session .run ("MATCH (p:Person {name: 'Merlin'}) RETURN count(p)" )
174+ record = next (result )
175+ assert record ["count(p)" ] == 0
169176 session .close ()
170177
171178 def test_result_summary_query_profile (self ):
172- driver = GraphDatabase .driver ("bolt://localhost" )
179+ driver = GraphDatabase .driver ("bolt://localhost" , auth = auth_token )
173180 session = driver .session ()
174181 # tag::result-summary-query-profile[]
175- cursor = session .run ("PROFILE MATCH (p:Person {name: {name}}) "
176- "RETURN id(p)" , {"name" : "The One " })
177- summary = cursor . summarize ()
182+ result = session .run ("PROFILE MATCH (p:Person {name: {name}}) "
183+ "RETURN id(p)" , {"name" : "Arthur " })
184+ summary = result . consume ()
178185 print (summary .statement_type )
179186 print (summary .profile )
180187 # end::result-summary-query-profile[]
181188 session .close ()
182189
183190 def test_result_summary_notifications (self ):
184- driver = GraphDatabase .driver ("bolt://localhost" )
191+ driver = GraphDatabase .driver ("bolt://localhost" , auth = auth_token )
185192 session = driver .session ()
186193 # tag::result-summary-notifications[]
187- summary = session .run ("EXPLAIN MATCH (a), (b) RETURN a,b" ).summarize ()
194+ result = session .run ("EXPLAIN MATCH (king), (queen) RETURN king, queen" )
195+ summary = result .consume ()
188196 for notification in summary .notifications :
189197 print (notification )
190198 # end::result-summary-notifications[]
0 commit comments