|
22 | 22 | import pytest
|
23 | 23 |
|
24 | 24 | # tag::custom-resolver-import[]
|
25 |
| -from neo4j import GraphDatabase |
| 25 | +from neo4j import ( |
| 26 | + GraphDatabase, |
| 27 | + WRITE_ACCESS, |
| 28 | +) |
26 | 29 | # end::custom-resolver-import[]
|
27 | 30 |
|
28 | 31 | from neo4j.exceptions import ServiceUnavailable
|
29 | 32 | from neo4j._exceptions import BoltHandshakeError
|
30 |
| -from tests.integration.examples import DriverSetupExample |
31 | 33 |
|
32 | 34 |
|
33 | 35 | # python -m pytest tests/integration/examples/test_custom_resolver_example.py -s -v
|
34 | 36 |
|
| 37 | +# tag::custom-resolver[] |
| 38 | +def create_driver(uri, user, password): |
35 | 39 |
|
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 |
37 | 48 |
|
38 |
| - # tag::custom-resolver[] |
39 |
| - def __init__(self, uri, auth): |
| 49 | + return GraphDatabase.driver(uri, auth=(user, password), resolver=resolver) |
40 | 50 |
|
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 |
49 | 51 |
|
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[] |
52 | 59 |
|
53 | 60 |
|
54 | 61 | def test_example(uri, auth):
|
55 | 62 | try:
|
56 |
| - CustomResolverExample.test(uri, auth) |
| 63 | + add_person("testing_resolver") |
57 | 64 | except ServiceUnavailable as error:
|
58 | 65 | if isinstance(error.__cause__, BoltHandshakeError):
|
59 | 66 | 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