|
21 | 21 |
|
22 | 22 | from unittest import TestCase
|
23 | 23 |
|
| 24 | +# tag::minimal-example-import[] |
24 | 25 | from neo4j.v1 import GraphDatabase
|
| 26 | +# end::minimal-example-import[] |
25 | 27 |
|
26 | 28 |
|
27 |
| -class ExamplesTestCase(TestCase): |
| 29 | +class MinimalWorkingExampleTestCase(TestCase): |
28 | 30 |
|
29 | 31 | def setUp(self):
|
30 | 32 | session = GraphDatabase.driver("bolt://localhost").session()
|
31 |
| - session.run("MATCH (n) DETACH DELETE n") |
| 33 | + session.run("MATCH (n) DETACH DELETE n").close() |
32 | 34 | session.close()
|
33 | 35 |
|
34 |
| - def test_minimum_snippet(self): |
35 |
| - #tag::minimum-snippet[] |
| 36 | + def test_minimal_working_example(self): |
| 37 | + # tag::minimal-example[] |
36 | 38 | driver = GraphDatabase.driver("bolt://localhost")
|
37 | 39 | session = driver.session()
|
| 40 | + |
38 | 41 | session.run("CREATE (neo:Person {name:'Neo', age:23})")
|
39 | 42 |
|
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() |
42 | 57 | session.close()
|
43 |
| - #end::minimum-snippet[] |
44 | 58 |
|
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): |
46 | 90 | driver = GraphDatabase.driver("bolt://localhost")
|
47 | 91 | 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[] |
54 | 95 | session.close()
|
| 96 | + driver.close() |
55 | 97 |
|
56 | 98 | def test_statement_without_parameters(self):
|
57 | 99 | driver = GraphDatabase.driver("bolt://localhost")
|
58 | 100 | 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[] |
65 | 118 | session.close()
|
| 119 | + driver.close() |
66 | 120 |
|
67 |
| - def test_commit_a_transaction(self): |
| 121 | + def test_transaction_commit(self): |
68 | 122 | driver = GraphDatabase.driver("bolt://localhost")
|
69 | 123 | session = driver.session()
|
70 |
| - #tag::transaction-commit[] |
| 124 | + # tag::transaction-commit[] |
71 | 125 | tx = session.begin_transaction()
|
72 |
| - tx.run("CREATE (p:Person { name: 'The One' })") |
| 126 | + tx.run("CREATE (p:Person {name: 'The One'})") |
73 | 127 | 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)") |
76 | 130 | assert cursor.single()
|
77 | 131 | assert cursor.record()["count(p)"] == 1
|
78 | 132 | session.close()
|
| 133 | + driver.close() |
79 | 134 |
|
80 |
| - def test_rollback_a_transaction(self): |
| 135 | + def test_transaction_rollback(self): |
81 | 136 | driver = GraphDatabase.driver("bolt://localhost")
|
82 | 137 | session = driver.session()
|
83 |
| - #tag::transaction-rollback[] |
| 138 | + # tag::transaction-rollback[] |
84 | 139 | tx = session.begin_transaction()
|
85 |
| - tx.run("CREATE (p:Person { name: 'The One' })") |
| 140 | + tx.run("CREATE (p:Person {name: 'The One'})") |
86 | 141 | 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)") |
89 | 144 | assert cursor.single()
|
90 | 145 | assert cursor.record()["count(p)"] == 0
|
91 | 146 | session.close()
|
| 147 | + driver.close() |
92 | 148 |
|
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