Skip to content

Commit d66d324

Browse files
technigemartin-neotech
authored andcommitted
Make qid optional
1 parent 117f3ee commit d66d324

File tree

2 files changed

+54
-28
lines changed

2 files changed

+54
-28
lines changed

neo4j/io/_bolt4x0.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,16 @@ def run(self, statement, parameters=None, mode=None, bookmarks=None, metadata=No
174174
self._append(b"\x10", fields, Response(self, **handlers))
175175

176176
def discard(self, n=-1, qid=-1, **handlers):
177-
# TODO: find out whether qid is optional, and optimise if so
178-
extra = {
179-
"n": n,
180-
"qid": qid,
181-
}
177+
extra = {"n": n}
178+
if qid != -1:
179+
extra["qid"] = qid
182180
log.debug("[#%04X] C: DISCARD %r", self.local_port, extra)
183181
self._append(b"\x2F", (extra,), Response(self, **handlers))
184182

185183
def pull(self, n=-1, qid=-1, **handlers):
186-
# TODO: find out whether qid is optional, and optimise if so
187-
extra = {
188-
"n": n,
189-
"qid": qid,
190-
}
184+
extra = {"n": n}
185+
if qid != -1:
186+
extra["qid"] = qid
191187
log.debug("[#%04X] C: PULL %r", self.local_port, extra)
192188
self._append(b"\x3F", (extra,), Response(self, **handlers))
193189

tests/unit/io/test_class_bolt4x0.py

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
# limitations under the License.
2020

2121

22+
import pytest
2223
from neo4j.io._bolt4x0 import Bolt4x0
2324

24-
2525
def test_conn_timed_out(fake_socket):
2626
address = ("127.0.0.1", 7687)
2727
connection = Bolt4x0(address, fake_socket(address), max_age=0)
@@ -75,55 +75,85 @@ def test_n_extra_in_discard(fake_socket):
7575
tag, fields = socket.pop_message()
7676
assert tag == b"\x2F"
7777
assert len(fields) == 1
78-
assert fields[0] == {"n": 666, "qid": -1}
78+
assert fields[0] == {"n": 666}
7979

8080

81-
def test_qid_extra_in_discard(fake_socket):
81+
@pytest.mark.parametrize(
82+
"test_input, expected",
83+
[
84+
(666, {"n": -1, "qid": 666}),
85+
(-1, {"n": -1}),
86+
]
87+
)
88+
def test_qid_extra_in_discard(fake_socket, test_input, expected):
8289
address = ("127.0.0.1", 7687)
8390
socket = fake_socket(address)
8491
connection = Bolt4x0(address, socket)
85-
connection.discard(qid=666)
92+
connection.discard(qid=test_input)
8693
connection.send_all()
8794
tag, fields = socket.pop_message()
8895
assert tag == b"\x2F"
8996
assert len(fields) == 1
90-
assert fields[0] == {"n": -1, "qid": 666}
91-
92-
93-
def test_n_and_qid_extras_in_discard(fake_socket):
97+
assert fields[0] == expected
98+
99+
100+
@pytest.mark.parametrize(
101+
"test_input, expected",
102+
[
103+
(777, {"n": 666, "qid": 777}),
104+
(-1, {"n": 666}),
105+
]
106+
)
107+
def test_n_and_qid_extras_in_discard(fake_socket, test_input, expected):
108+
# python -m pytest tests/unit/io/test_class_bolt4x0.py -s -k test_n_and_qid_extras_in_discard
94109
address = ("127.0.0.1", 7687)
95110
socket = fake_socket(address)
96111
connection = Bolt4x0(address, socket)
97-
connection.discard(n=666, qid=777)
112+
connection.discard(n=666, qid=test_input)
98113
connection.send_all()
99114
tag, fields = socket.pop_message()
100115
assert tag == b"\x2F"
101116
assert len(fields) == 1
102-
assert fields[0] == {"n": 666, "qid": 777}
117+
assert fields[0] == expected
103118

104119

105-
def test_n_extra_in_pull(fake_socket):
120+
@pytest.mark.parametrize(
121+
"test_input, expected",
122+
[
123+
(666, {"n": 666}),
124+
(-1, {"n": -1}),
125+
]
126+
)
127+
def test_n_extra_in_pull(fake_socket, test_input, expected):
106128
address = ("127.0.0.1", 7687)
107129
socket = fake_socket(address)
108130
connection = Bolt4x0(address, socket)
109-
connection.pull(n=666)
131+
connection.pull(n=test_input)
110132
connection.send_all()
111133
tag, fields = socket.pop_message()
112134
assert tag == b"\x3F"
113135
assert len(fields) == 1
114-
assert fields[0] == {"n": 666, "qid": -1}
115-
116-
117-
def test_qid_extra_in_pull(fake_socket):
136+
assert fields[0] == expected
137+
138+
139+
@pytest.mark.parametrize(
140+
"test_input, expected",
141+
[
142+
(777, {"n": -1, "qid": 777}),
143+
(-1, {"n": -1}),
144+
]
145+
)
146+
def test_qid_extra_in_pull(fake_socket, test_input, expected):
147+
# python -m pytest tests/unit/io/test_class_bolt4x0.py -s -k test_qid_extra_in_pull
118148
address = ("127.0.0.1", 7687)
119149
socket = fake_socket(address)
120150
connection = Bolt4x0(address, socket)
121-
connection.pull(qid=666)
151+
connection.pull(qid=test_input)
122152
connection.send_all()
123153
tag, fields = socket.pop_message()
124154
assert tag == b"\x3F"
125155
assert len(fields) == 1
126-
assert fields[0] == {"n": -1, "qid": 666}
156+
assert fields[0] == expected
127157

128158

129159
def test_n_and_qid_extras_in_pull(fake_socket):

0 commit comments

Comments
 (0)