Skip to content

Commit 5ba362d

Browse files
Added class-method protocol_handlers
Protocol handlers returns a dictionary where keys are the version and value is the protocol handler for that version. This is the same behaviour as in the aio part.
1 parent 3024ac2 commit 5ba362d

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

neo4j/io/__init__.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,42 @@ class Bolt:
9494

9595
PROTOCOL_VERSION = None
9696

97+
@classmethod
98+
def protocol_handlers(cls, protocol_version=None):
99+
""" Return a dictionary of available Bolt protocol handlers,
100+
keyed by version tuple. If an explicit protocol version is
101+
provided, the dictionary will contain either zero or one items,
102+
depending on whether that version is supported. If no protocol
103+
version is provided, all available versions will be returned.
104+
105+
:param protocol_version: tuple identifying a specific protocol
106+
version (e.g. (3, 5)) or None
107+
:return: dictionary of version tuple to handler class for all
108+
relevant and supported protocol versions
109+
:raise TypeError: if protocol version is not passed in a tuple
110+
"""
111+
112+
# Carry out subclass imports locally to avoid circular
113+
# dependency issues.
114+
from neo4j.io._bolt3 import Bolt3
115+
from neo4j.io._bolt4x0 import Bolt4x0
116+
117+
handlers = {
118+
Bolt3.PROTOCOL_VERSION: Bolt3,
119+
Bolt4x0.PROTOCOL_VERSION: Bolt4x0
120+
}
121+
122+
if protocol_version is None:
123+
return handlers
124+
125+
if not isinstance(protocol_version, tuple):
126+
raise TypeError("Protocol version must be specified as a tuple")
127+
128+
if protocol_version in handlers:
129+
return {protocol_version: handlers[protocol_version]}
130+
131+
return {}
132+
97133
@classmethod
98134
def ping(cls, address, *, timeout=None, **config):
99135
""" Attempt to establish a Bolt connection, returning the

tests/unit/io/test_class_bolt.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
from neo4j.io import Bolt
24+
25+
# python -m pytest tests/unit/io/test_class_bolt.py -s -v
26+
27+
28+
def test_class_method_protocol_handlers():
29+
# python -m pytest tests/unit/io/test_class_bolt.py -s -v -k test_class_method_protocol_handlers
30+
protocol_handlers = Bolt.protocol_handlers()
31+
assert len(protocol_handlers) == 2
32+
33+
34+
@pytest.mark.parametrize(
35+
"test_input, expected",
36+
[
37+
((0, 0), 0),
38+
((4, 0), 1),
39+
]
40+
)
41+
def test_class_method_protocol_handlers_with_protocol_version(test_input, expected):
42+
# python -m pytest tests/unit/io/test_class_bolt.py -s -v -k test_class_method_protocol_handlers_with_protocol_version
43+
protocol_handlers = Bolt.protocol_handlers(protocol_version=test_input)
44+
assert len(protocol_handlers) == expected
45+
46+
47+
def test_class_method_protocol_handlers_with_invalid_protocol_version():
48+
# python -m pytest tests/unit/io/test_class_bolt.py -s -v -k test_class_method_protocol_handlers_with_invalid_protocol_version
49+
with pytest.raises(TypeError):
50+
Bolt.protocol_handlers(protocol_version=2)

0 commit comments

Comments
 (0)