Skip to content

Commit 43fb83e

Browse files
4.0 examples update (#385)
* hello world up to date * custom resolver example updated * fixed the url in the example * updated the all import * added a todo * added example for connection timeout * renamed example file name * added unencrypted example * aligned naming for configuration max_retry_time to be max_transaction_retry_time * fiexed typo * database-selection * transaction-function example updated * result-consume example updated * result_retain updated * updated examples * fixed test to run against Neo4j 3.5 * fixed test against Neo4j 3.4 * fixed import for examples * added what examples have imports in the comment * updated comment
1 parent b28e91c commit 43fb83e

28 files changed

+422
-149
lines changed

docs/source/index.rst

+3
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ Dependency Changes
163163
The dependency :code:`neobolt` have been removed.
164164

165165

166+
Configuration Name Changes
167+
==========================
166168

169+
* :code:`max_retry_time` is now :code:`max_transaction_retry_time`
167170

168171

169172
*****************

docs/source/usage_patterns.rst

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Driver Initialization Work Pattern
5353
"max_connection_lifetime": 1000,
5454
"max_connection_pool_size": 100,
5555
"keep_alive": False,
56-
"max_retry_time": 10,
56+
"max_transaction_retry_time": 10,
5757
"resolver": None,
5858
}
5959
@@ -99,7 +99,7 @@ Session Initialization Work Pattern
9999
"bookmarks": ["bookmark-1",],
100100
"access_mode": ACCESS_WRITE,
101101
"connection_acquisition_timeout": 60.0,
102-
"max_retry_time": 30.0,
102+
"max_transaction_retry_time": 30.0,
103103
"initial_retry_delay": 1.0,
104104
"retry_delay_multiplier": 2.0,
105105
"retry_delay_jitter_factor": 0.2,

neo4j/__init__.py

+25
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,31 @@
2727
"Neo4jDriver",
2828
"Auth",
2929
"AuthToken",
30+
"basic_auth",
31+
"kerberos_auth",
32+
"custom_auth",
33+
"Bookmark",
34+
"ServerInfo",
35+
"Version",
36+
"READ_ACCESS",
37+
"WRITE_ACCESS",
38+
"DEFAULT_DATABASE",
39+
"TRUST_ALL_CERTIFICATES",
40+
"TRUST_SYSTEM_CA_SIGNED_CERTIFICATES",
41+
"Address",
42+
"IPv4Address",
43+
"IPv6Address",
44+
"Config",
45+
"PoolConfig",
46+
"WorkspaceConfig",
47+
"SessionConfig",
48+
"Record",
49+
"Transaction",
50+
"Result",
51+
"ResultSummary",
52+
"Query",
53+
"Session",
54+
"unit_of_work",
3055
]
3156

3257
from logging import getLogger

neo4j/api.py

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
DEFAULT_DATABASE = None # Must be a non string hashable value
5757

5858

59+
# TODO: This class is not tested
5960
class Auth:
6061
""" Container for auth details.
6162
"""

neo4j/conf.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,9 @@ class WorkspaceConfig(Config):
275275
# Since the process of acquiring a connection may involve creating a new connection, ensure that the value
276276
# of this configuration is higher than the configured Connection Timeout.
277277

278-
#: Max Retry Time
279-
max_retry_time = 30.0 # seconds
280-
# The maximum amount of time that a managed transaction will retry for before failing.
278+
#: Max Transaction Retry Time
279+
max_transaction_retry_time = 30.0 # seconds
280+
# The maximum amount of time that a managed transaction will retry before failing.
281281

282282
#: Initial Retry Delay
283283
initial_retry_delay = 1.0 # seconds

neo4j/work/simple.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ def _run_transaction(self, access_mode, unit_of_work, *args, **kwargs):
416416
else:
417417
return result
418418
t1 = perf_counter()
419-
if t1 - t0 > self._config.max_retry_time:
419+
if t1 - t0 > self._config.max_transaction_retry_time:
420420
break
421421
delay = next(retry_delay)
422422
log.warning("Transaction failed and will be retried in {}s "

tests/integration/examples/__init__.py

+30
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,36 @@
1919
# limitations under the License.
2020

2121

22+
# List of example tags used in the drivers manual version 4.0
23+
24+
25+
# hello-world, hello-world-import - Example 1.6. Hello World
26+
# driver-lifecycle, driver-lifecycle-import - Example 2.1. The driver lifecycle
27+
# custom-resolver, custom-resolver-import - Example 2.2. Custom Address Resolver
28+
# basic-auth, basic-auth-import - Example 2.5. Basic authentication
29+
# kerberos-auth, kerberos-auth-import - Example 2.6. Kerberos authentication
30+
# custom-auth, custom-auth-import - Example 2.7. Custom authentication
31+
# config-connection-pool, config-connection-pool-import - Example 2.8. Configure connection pool
32+
# config-connection-timeout, config-connection-timeout-import - Example 2.9. Configure connection timeout
33+
# config-unencrypted, config-unencrypted-import - Example 2.10. Unencrypted configuration
34+
# config-max-retry-time, config-max-retry-time-import - Example 2.11. Configure maximum transaction retry time
35+
# config-trust, config-trust-import - Example 2.12. Configure trusted certificates
36+
# pass-bookmarks, pass-bookmarks-import - Example 3.1. Pass bookmarks
37+
# read-write-transaction - Example 3.2. Read-write transaction
38+
# database-selection, database-selection-import - Example 3.3. Database selection on session creation
39+
# Hard coded (session) - Example 4.1. Session construction and closure
40+
# transaction-function, transaction-function-import - Example 4.2. Transaction function
41+
# autocommit-transaction, autocommit-transaction-import - Example 4.3. Simple auto-commit transactions
42+
# result-consume - Example 4.4. Consuming results
43+
# result-retain - Example 4.5. Retain results for further processing
44+
# - Example 4.6. Asynchronous transaction functions
45+
# - Example 4.7. Asynchronous auto-commit transactions
46+
# - Example 4.8. Asynchronous consuming results
47+
# - Example 4.9. Reactive transaction functions
48+
# - Example 4.10.Reactive auto-commit transactions
49+
# - Example 4.11.Reactive consuming results
50+
51+
2252
class DriverSetupExample:
2353

2454
driver = None

tests/integration/examples/test_autocommit_transaction_example.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
# tag::autocommit-transaction-import[]
23-
from neo4j.work.simple import Query
23+
from neo4j import Query
2424
# end::autocommit-transaction-import[]
2525

2626

tests/integration/examples/test_auth_example.py renamed to tests/integration/examples/test_basic_auth_example.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from tests.integration.examples import DriverSetupExample
3131

3232

33-
# python -m pytest tests/integration/examples/test_auth_example.py -s -v
33+
# python -m pytest tests/integration/examples/test_basic_auth_example.py -s -v
3434

3535
class BasicAuthExample(DriverSetupExample):
3636

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) 2002-2020 "Neo4j,"
5+
# Neo4j Sweden AB [http://neo4j.com]
6+
#
7+
# This file is part of Neo4j.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
21+
22+
import pytest
23+
24+
from neo4j.exceptions import ServiceUnavailable
25+
from neo4j._exceptions import BoltHandshakeError
26+
27+
# tag::config-connection-timeout-import[]
28+
from neo4j import GraphDatabase
29+
# end::config-connection-timeout-import[]
30+
31+
from tests.integration.examples import DriverSetupExample
32+
33+
34+
# python -m pytest tests/integration/examples/test_config_connection_timeout_example.py -s -v
35+
36+
class ConfigConnectionTimeoutExample(DriverSetupExample):
37+
38+
# tag::config-connection-timeout[]
39+
def __init__(self, uri, auth):
40+
self.driver = GraphDatabase.driver(uri, auth=auth, connection_timeout=15)
41+
# end::config-connection-timeout[]
42+
43+
44+
def test(uri, auth):
45+
try:
46+
ConfigConnectionTimeoutExample.test(uri, auth)
47+
except ServiceUnavailable as error:
48+
if isinstance(error.__cause__, BoltHandshakeError):
49+
pytest.skip(error.args[0])

tests/integration/examples/test_config_max_retry_time_example.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class ConfigMaxRetryTimeExample(DriverSetupExample):
3636

3737
# tag::config-max-retry-time[]
3838
def __init__(self, uri, auth):
39-
self.driver = GraphDatabase.driver(uri, auth=auth, max_retry_time=15)
39+
self.driver = GraphDatabase.driver(uri, auth=auth, max_transaction_retry_time=15)
4040
# end::config-max-retry-time[]
4141

4242

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) 2002-2020 "Neo4j,"
5+
# Neo4j Sweden AB [http://neo4j.com]
6+
#
7+
# This file is part of Neo4j.
8+
#
9+
# Licensed under the Apache License, Version 2.0 (the "License");
10+
# you may not use this file except in compliance with the License.
11+
# You may obtain a copy of the License at
12+
#
13+
# http://www.apache.org/licenses/LICENSE-2.0
14+
#
15+
# Unless required by applicable law or agreed to in writing, software
16+
# distributed under the License is distributed on an "AS IS" BASIS,
17+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
# See the License for the specific language governing permissions and
19+
# limitations under the License.
20+
21+
22+
import pytest
23+
24+
# tag::config-unencrypted-import[]
25+
from neo4j import GraphDatabase
26+
# end::config-unencrypted-import[]
27+
28+
from neo4j.exceptions import ServiceUnavailable
29+
from neo4j._exceptions import BoltHandshakeError
30+
31+
from tests.integration.examples import DriverSetupExample
32+
33+
34+
# python -m pytest tests/integration/examples/test_config_unencrypted_example.py -s -v
35+
36+
class ConfigUnencryptedExample(DriverSetupExample):
37+
38+
# tag::config-unencrypted[]
39+
def __init__(self, uri, auth):
40+
self.driver = GraphDatabase.driver(uri, auth=auth, encrypted=False)
41+
# end::config-unencrypted[]
42+
43+
44+
def test_example(uri, auth):
45+
try:
46+
ConfigUnencryptedExample.test(uri, auth)
47+
except ServiceUnavailable as error:
48+
if isinstance(error.__cause__, BoltHandshakeError):
49+
pytest.skip(error.args[0])

tests/integration/examples/test_custom_auth_example.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
import pytest
2323

2424
# tag::custom-auth-import[]
25-
from neo4j import GraphDatabase, custom_auth
25+
from neo4j import (
26+
GraphDatabase,
27+
custom_auth,
28+
)
2629
# end::custom-auth-import[]
2730

2831
from neo4j.exceptions import ServiceUnavailable

tests/integration/examples/test_custom_resolver_example.py

+25-16
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,47 @@
2222
import pytest
2323

2424
# tag::custom-resolver-import[]
25-
from neo4j import GraphDatabase
25+
from neo4j import (
26+
GraphDatabase,
27+
WRITE_ACCESS,
28+
)
2629
# end::custom-resolver-import[]
2730

2831
from neo4j.exceptions import ServiceUnavailable
2932
from neo4j._exceptions import BoltHandshakeError
30-
from tests.integration.examples import DriverSetupExample
3133

3234

3335
# python -m pytest tests/integration/examples/test_custom_resolver_example.py -s -v
3436

37+
# tag::custom-resolver[]
38+
def create_driver(uri, user, password):
3539

36-
class CustomResolverExample(DriverSetupExample):
40+
def resolver(address):
41+
host, port = address
42+
if host == "x.example.com":
43+
yield "a.example.com", port
44+
yield "b.example.com", port
45+
yield "c.example.com", port
46+
else:
47+
yield host, port
3748

38-
# tag::custom-resolver[]
39-
def __init__(self, uri, auth):
49+
return GraphDatabase.driver(uri, auth=(user, password), resolver=resolver)
4050

41-
def resolve(address):
42-
host, port = address
43-
if host == "x.example.com":
44-
yield "a.example.com", port
45-
yield "b.example.com", port
46-
yield "c.example.com", port
47-
else:
48-
yield host, port
4951

50-
self.driver = GraphDatabase.driver(uri, auth=auth, resolver=resolve)
51-
# end::custom-resolver[]
52+
def add_person(name):
53+
driver = create_driver("neo4j://x.example.com", user="neo4j", password="password")
54+
session = driver.session(default_access_mode=WRITE_ACCESS)
55+
session.run("CREATE (a:Person {name: $name})", {"name", name})
56+
session.close()
57+
driver.close()
58+
# end::custom-resolver[]
5259

5360

5461
def test_example(uri, auth):
5562
try:
56-
CustomResolverExample.test(uri, auth)
63+
add_person("testing_resolver")
5764
except ServiceUnavailable as error:
5865
if isinstance(error.__cause__, BoltHandshakeError):
5966
pytest.skip(error.args[0])
67+
except ValueError as error:
68+
assert error.args[0] == "Cannot resolve address a.example.com:7687"

0 commit comments

Comments
 (0)