Skip to content

Commit 4f62301

Browse files
author
Zhen
committed
Added boltkit test to verify that the driver actually works with the routing context in uri
1 parent 1cf92d7 commit 4f62301

File tree

8 files changed

+61
-2
lines changed

8 files changed

+61
-2
lines changed

neo4j/v1/direct.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ def __init__(self, uri, **config):
5656
# will carry out DNS resolution, leading to the possibility that
5757
# the connection pool may contain multiple IP address keys, one for
5858
# an old address and one for a new address.
59+
if SocketAddress.parse_routing_context(uri):
60+
raise ValueError("Routing parameters are not supported with scheme 'bolt'. Given URI: '" + uri + "'.")
5961
self.address = SocketAddress.from_uri(uri, DEFAULT_PORT)
6062
self.security_plan = security_plan = SecurityPlan.build(**config)
6163
self.encrypted = security_plan.encrypted

neo4j/v1/routing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ def __init__(self, uri, **config):
333333
def connector(a):
334334
return connect(a, security_plan.ssl_context, **config)
335335

336-
pool = RoutingConnectionPool(connector, initial_address, routing_context *resolve(initial_address))
336+
pool = RoutingConnectionPool(connector, initial_address, routing_context, *resolve(initial_address))
337337
try:
338338
pool.update_routing_table()
339339
except:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
4+
S: SUCCESS {"server": "Neo4j/3.2.2"}
5+
C: RUN "CALL dbms.cluster.routing.getRoutingTable({context})" {"context": {}}
6+
PULL_ALL
7+
S: SUCCESS {"fields": ["ttl", "servers"]}
8+
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9001"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9002"], "role": "READ"},{"addresses": ["127.0.0.1:9001", "127.0.0.1:9002"], "role": "ROUTE"}]]
9+
SUCCESS {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
!: AUTO INIT
2+
!: AUTO RESET
3+
4+
S: SUCCESS {"server": "Neo4j/3.2.3"}
5+
C: RUN "CALL dbms.cluster.routing.getRoutingTable({context})" {"context": {"name": "molly", "age": "1"}}
6+
PULL_ALL
7+
S: SUCCESS {"fields": ["ttl", "servers"]}
8+
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9001"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9002"], "role": "READ"},{"addresses": ["127.0.0.1:9001", "127.0.0.1:9002"], "role": "ROUTE"}]]
9+
SUCCESS {}

test/stub/test_directdriver.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,9 @@ def test_direct_disconnect_on_pull_all(self):
4848
with self.assertRaises(ServiceUnavailable):
4949
with driver.session() as session:
5050
session.run("RETURN $x", {"x": 1}).consume()
51+
52+
def test_direct_should_reject_routing_context(self):
53+
uri = "bolt://127.0.0.1:9001/?name=molly&age=1"
54+
with self.assertRaises(ValueError):
55+
GraphDatabase.driver(uri, auth=self.auth_token, encrypted=False)
56+

test/stub/test_routing.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ def test_should_fail_if_database_error(self):
131131
with self.assertRaises(ServiceUnavailable):
132132
_ = pool.fetch_routing_info(address)
133133

134+
def test_should_call_get_routing_tables_with_context(self):
135+
with StubCluster({9001: "get_routing_table_with_context.script"}):
136+
address = ("127.0.0.1", 9001)
137+
routing_context = {"name": "molly", "age": "1"}
138+
with RoutingConnectionPool(connector, UNREACHABLE_ADDRESS, routing_context) as pool:
139+
pool.fetch_routing_info(address)
140+
141+
def test_should_call_get_routing_tables(self):
142+
with StubCluster({9001: "get_routing_table.script"}):
143+
address = ("127.0.0.1", 9001)
144+
with RoutingConnectionPool(connector, UNREACHABLE_ADDRESS, {}) as pool:
145+
pool.fetch_routing_info(address)
146+
134147

135148
class RoutingConnectionPoolFetchRoutingTableTestCase(StubTestCase):
136149
def test_should_get_table_from_router(self):

test/stub/test_routingdriver.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,23 @@ def test_two_sessions_can_share_a_connection(self):
179179

180180
session_2.close()
181181
session_1.close()
182+
183+
def test_should_call_get_routing_table_procedure(self):
184+
with StubCluster({9001: "get_routing_table.script", 9002: "return_1.script"}):
185+
uri = "bolt+routing://127.0.0.1:9001"
186+
with GraphDatabase.driver(uri, auth=self.auth_token, encrypted=False) as driver:
187+
with driver.session(READ_ACCESS) as session:
188+
result = session.run("RETURN $x", {"x": 1})
189+
for record in result:
190+
assert record["x"] == 1
191+
assert result.summary().server.address == ('127.0.0.1', 9002)
192+
193+
def test_should_call_get_routing_table_with_context(self):
194+
with StubCluster({9001: "get_routing_table_with_context.script", 9002: "return_1.script"}):
195+
uri = "bolt+routing://127.0.0.1:9001/?name=molly&age=1"
196+
with GraphDatabase.driver(uri, auth=self.auth_token, encrypted=False) as driver:
197+
with driver.session(READ_ACCESS) as session:
198+
result = session.run("RETURN $x", {"x": 1})
199+
for record in result:
200+
assert record["x"] == 1
201+
assert result.summary().server.address == ('127.0.0.1', 9002)

test/unit/test_routing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,5 @@ class RoutingConnectionPoolConstructionTestCase(TestCase):
229229
def test_should_populate_initial_router(self):
230230
initial_router = ("127.0.0.1", 9001)
231231
router = ("127.0.0.1", 9002)
232-
with RoutingConnectionPool(connector, initial_router, router) as pool:
232+
with RoutingConnectionPool(connector, initial_router, {}, router) as pool:
233233
assert pool.routing_table.routers == {("127.0.0.1", 9002)}

0 commit comments

Comments
 (0)