Skip to content

Commit

Permalink
BulkResponse: Be more strict on empty records or results
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Oct 3, 2024
1 parent a96e375 commit 4da893a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/crate/client/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ class BulkResponse:

def __init__(
self,
records: t.Union[t.Iterable[t.Dict[str, t.Any]], None],
results: t.Union[t.Iterable[BulkResultItem], None]):
records: t.List[t.Dict[str, t.Any]],
results: t.List[BulkResultItem]):
if records is None:
raise ValueError("Processing a bulk response without records is an invalid operation")
if results is None:
raise ValueError("Processing a bulk response without results is an invalid operation")
self.records = records
self.results = results

Expand All @@ -34,8 +38,6 @@ def failed_records(self) -> t.List[t.Dict[str, t.Any]]:
https://cratedb.com/docs/crate/reference/en/latest/interfaces/http.html#error-handling
"""
if self.records is None or self.results is None:
return []
errors: t.List[t.Dict[str, t.Any]] = []
for record, status in zip(self.records, self.results):
if status["rowcount"] == -2:
Expand Down
16 changes: 16 additions & 0 deletions src/crate/client/test_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from crate import client
from crate.client.exceptions import ProgrammingError
from crate.client.result import BulkResponse
from crate.client.test_support import setUpCrateLayerBaseline, tearDownDropEntitiesBaseline
from crate.testing.settings import crate_host

Expand Down Expand Up @@ -68,3 +69,18 @@ def test_executemany_empty(self):

cursor.close()
connection.close()

@unittest.skipIf(sys.version_info < (3, 8), "BulkResponse needs Python 3.8 or higher")
def test_bulk_response_empty_records_or_results(self):

with self.assertRaises(ValueError) as cm:
BulkResponse(records=None, results=None)
self.assertEqual(
str(cm.exception),
"Processing a bulk response without records is an invalid operation")

with self.assertRaises(ValueError) as cm:
BulkResponse(records=[], results=None)
self.assertEqual(
str(cm.exception),
"Processing a bulk response without results is an invalid operation")

0 comments on commit 4da893a

Please sign in to comment.