Skip to content

Commit 4da893a

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

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-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

+16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from crate import client
55
from crate.client.exceptions import ProgrammingError
6+
from crate.client.result import BulkResponse
67
from crate.client.test_support import setUpCrateLayerBaseline, tearDownDropEntitiesBaseline
78
from crate.testing.settings import crate_host
89

@@ -68,3 +69,18 @@ def test_executemany_empty(self):
6869

6970
cursor.close()
7071
connection.close()
72+
73+
@unittest.skipIf(sys.version_info < (3, 8), "BulkResponse needs Python 3.8 or higher")
74+
def test_bulk_response_empty_records_or_results(self):
75+
76+
with self.assertRaises(ValueError) as cm:
77+
BulkResponse(records=None, results=None)
78+
self.assertEqual(
79+
str(cm.exception),
80+
"Processing a bulk response without records is an invalid operation")
81+
82+
with self.assertRaises(ValueError) as cm:
83+
BulkResponse(records=[], results=None)
84+
self.assertEqual(
85+
str(cm.exception),
86+
"Processing a bulk response without results is an invalid operation")

0 commit comments

Comments
 (0)