Skip to content

Commit 6a58353

Browse files
committed
BulkResponse: Be more strict on empty records or results
1 parent a96e375 commit 6a58353

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/crate/client/result.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ class BulkResponse:
2020

2121
def __init__(
2222
self,
23-
records: t.Union[t.Iterable[t.Dict[str, t.Any]], None],
24-
results: t.Union[t.Iterable[BulkResultItem], None]):
23+
records: t.List[t.Dict[str, t.Any]],
24+
results: t.List[BulkResultItem]):
25+
if records is None:
26+
raise ValueError("Processing a bulk response without records is an invalid operation")
27+
if results is None:
28+
raise ValueError("Processing a bulk response without results is an invalid operation")
2529
self.records = records
2630
self.results = results
2731

@@ -34,8 +38,6 @@ def failed_records(self) -> t.List[t.Dict[str, t.Any]]:
3438
3539
https://cratedb.com/docs/crate/reference/en/latest/interfaces/http.html#error-handling
3640
"""
37-
if self.records is None or self.results is None:
38-
return []
3941
errors: t.List[t.Dict[str, t.Any]] = []
4042
for record, status in zip(self.records, self.results):
4143
if status["rowcount"] == -2:

src/crate/client/test_result.py

+18
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,21 @@ def test_executemany_empty(self):
6868

6969
cursor.close()
7070
connection.close()
71+
72+
@unittest.skipIf(sys.version_info < (3, 8), "BulkResponse needs Python 3.8 or higher")
73+
def test_bulk_response_empty_records_or_results(self):
74+
75+
# Import at runtime is on purpose, to permit skipping the test case.
76+
from crate.client.result import BulkResponse
77+
78+
with self.assertRaises(ValueError) as cm:
79+
BulkResponse(records=None, results=None)
80+
self.assertEqual(
81+
str(cm.exception),
82+
"Processing a bulk response without records is an invalid operation")
83+
84+
with self.assertRaises(ValueError) as cm:
85+
BulkResponse(records=[], results=None)
86+
self.assertEqual(
87+
str(cm.exception),
88+
"Processing a bulk response without results is an invalid operation")

0 commit comments

Comments
 (0)