Skip to content

Commit 0a05f10

Browse files
author
Zhen
committed
Add examples for driver documentation
1 parent a7b315b commit 0a05f10

5 files changed

+104
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) 2002-2017 "Neo Technology,"
5+
# Network Engine for Objects in Lund AB [http://neotechnology.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+
# tag::config-connection-pool-import[]
22+
from neo4j.v1 import GraphDatabase
23+
# end::config-connection-pool-import[]
24+
25+
26+
class ConfigConnectionPoolExample:
27+
# tag::config-connection-pool[]
28+
def __init__(self, uri, user, password):
29+
self._driver = GraphDatabase.driver(uri, auth=(user, password),
30+
max_connection_lifetime=30 * 60, max_connection_pool_size=50,
31+
connection_acquisition_timeout=2 * 60)
32+
# end::config-connection-pool[]
33+
34+
def close(self):
35+
self._driver.close()
36+
37+
def can_connect(driver):
38+
result = driver.session().run("RETURN 1")
39+
return result.single()[0] == 1

test/examples/config_connection_timeout_example.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ def __init__(self, uri, user, password):
3131

3232
def close(self):
3333
self._driver.close()
34+
35+
def can_connect(self):
36+
result = self._driver.session().run("RETURN 1")
37+
return result.single()[0] == 1
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
# Copyright (c) 2002-2017 "Neo Technology,"
5+
# Network Engine for Objects in Lund AB [http://neotechnology.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+
# tag::config-load-balancing-strategy-import[]
22+
from neo4j.v1 import GraphDatabase, LOAD_BALANCING_STRATEGY_LEAST_CONNECTED
23+
# end::config-load-balancing-strategy-import[]
24+
25+
26+
class ConfigLoadBalancingStrategyExample:
27+
# tag::config-load-balancing-strategy[]
28+
def __init__(self, uri, user, password):
29+
self._driver = GraphDatabase.driver(uri, auth=(user, password), load_balancing_strategy=LOAD_BALANCING_STRATEGY_LEAST_CONNECTED)
30+
# end::config-load-balancing-strategy[]
31+
32+
def close(self):
33+
self._driver.close()
34+
35+
def can_connect(self):
36+
result = self._driver.session().run("RETURN 1")
37+
return result.single()[0] == 1

test/examples/config_max_retry_time_example.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ def __init__(self, uri, user, password):
3131

3232
def close(self):
3333
self._driver.close()
34+
35+
def can_connect(self):
36+
result = self._driver.session().run("RETURN 1")
37+
return result.single()[0] == 1

test/examples/test_examples.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ def test_autocommit_transaction_example(self):
5252

5353
self.assertTrue(self.person_count('Alice') > 0)
5454

55+
def test_config_connection_pool_example(self):
56+
from test.examples.config_connection_pool_example import ConfigConnectionPoolExample
57+
example = ConfigConnectionPoolExample(self.bolt_uri, self.user, self.password)
58+
self.assertTrue(example.can_connect())
59+
60+
def test_connection_timeout_example(self):
61+
from test.examples.config_connection_timeout_example import ConfigConnectionTimeoutExample
62+
example = ConfigConnectionTimeoutExample(self.bolt_uri, self.user, self.password)
63+
self.assertTrue(example.can_connect())
64+
65+
def test_load_balancing_strategy_example(self):
66+
from test.examples.config_load_balancing_strategy_example import ConfigLoadBalancingStrategyExample
67+
example = ConfigLoadBalancingStrategyExample(self.bolt_uri, self.user, self.password)
68+
self.assertTrue(example.can_connect())
69+
70+
def test_max_retry_time_example(self):
71+
from test.examples.config_max_retry_time_example import ConfigMaxRetryTimeExample
72+
example = ConfigMaxRetryTimeExample(self.bolt_uri, self.user, self.password)
73+
self.assertTrue(example.can_connect())
74+
5575
def test_basic_auth_example(self):
5676
from test.examples.auth_example import BasicAuthExample
5777

0 commit comments

Comments
 (0)