Skip to content

Commit

Permalink
Makes data argument optional on transactional delete (#752)
Browse files Browse the repository at this point in the history
      * Makes data argument optional on transactional delete

Signed-off-by: Elena Kolevska <[email protected]>

* linter

Signed-off-by: Elena Kolevska <[email protected]>

* Linter/type check

Signed-off-by: Elena Kolevska <[email protected]>

---------

Signed-off-by: Elena Kolevska <[email protected]>
Co-authored-by: Bernd Verst <[email protected]>
  • Loading branch information
elena-kolevska and berndverst authored Nov 21, 2024
1 parent afd13ab commit d7d84f2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
6 changes: 3 additions & 3 deletions dapr/clients/grpc/_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ class TransactionalStateOperation:
def __init__(
self,
key: str,
data: Union[bytes, str],
data: Optional[Union[bytes, str]] = None,
etag: Optional[str] = None,
operation_type: TransactionOperationType = TransactionOperationType.upsert,
):
Expand All @@ -297,7 +297,7 @@ def __init__(
Raises:
ValueError: data is not bytes or str.
"""
if not isinstance(data, (bytes, str)):
if operation_type != TransactionOperationType.delete and not isinstance(data, (bytes, str)):
raise ValueError(f'invalid type for data {type(data)}')

self._key = key
Expand All @@ -311,7 +311,7 @@ def key(self) -> str:
return self._key

@property
def data(self) -> Union[bytes, str]:
def data(self) -> Union[bytes, str, None]:
"""Gets raw data."""
return self._data

Expand Down
2 changes: 1 addition & 1 deletion dapr/clients/grpc/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ def execute_state_transaction(
operationType=o.operation_type.value,
request=common_v1.StateItem(
key=o.key,
value=to_bytes(o.data),
value=to_bytes(o.data) if o.data is not None else to_bytes(''),
etag=common_v1.Etag(value=o.etag) if o.etag is not None else None,
),
)
Expand Down
3 changes: 3 additions & 0 deletions examples/state_store/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ expected_stdout_lines:
- "== APP == Cannot save bulk due to bad etags. ErrorCode=StatusCode.ABORTED"
- "== APP == Got value=b'value_1' eTag=1"
- "== APP == Got items with etags: [(b'value_1_updated', '2'), (b'value_2', '2')]"
- "== APP == Got values after transaction delete: [b'', b'']"
- "== APP == Got value after delete: b''"
timeout_seconds: 5
-->
Expand All @@ -67,6 +68,8 @@ The output should be as follows:
== APP == Got items with etags: [(b'value_1_updated', '2'), (b'value_2', '2')]
== APP == Got values after transaction delete: [b'', b'']
== APP == Got value after delete: b''
```

Expand Down
24 changes: 22 additions & 2 deletions examples/state_store/state_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
etag=state.etag,
),
TransactionalStateOperation(key=another_key, data=another_value),
TransactionalStateOperation(key=yet_another_key, data=yet_another_value),
],
)

Expand All @@ -87,7 +88,26 @@
).items
print(f'Got items with etags: {[(i.data, i.etag) for i in items]}')

# Transaction delete
d.execute_state_transaction(
store_name=storeName,
operations=[
TransactionalStateOperation(operation_type=TransactionOperationType.delete, key=key),
TransactionalStateOperation(
operation_type=TransactionOperationType.delete, key=another_key
),
],
)

# Batch get
items = d.get_bulk_state(
store_name=storeName, keys=[key, another_key], states_metadata={'metakey': 'metavalue'}
).items
print(f'Got values after transaction delete: {[data.data for data in items]}')

# Delete one state by key.
d.delete_state(store_name=storeName, key=key, state_metadata={'metakey': 'metavalue'})
data = d.get_state(store_name=storeName, key=key).data
d.delete_state(
store_name=storeName, key=yet_another_key, state_metadata={'metakey': 'metavalue'}
)
data = d.get_state(store_name=storeName, key=yet_another_key).data
print(f'Got value after delete: {data}')
19 changes: 18 additions & 1 deletion tests/clients/test_dapr_grpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from .fake_dapr_server import FakeDaprSidecar
from dapr.conf import settings
from dapr.clients.grpc._helpers import to_bytes
from dapr.clients.grpc._request import TransactionalStateOperation
from dapr.clients.grpc._request import TransactionalStateOperation, TransactionOperationType
from dapr.clients.grpc._state import StateOptions, Consistency, Concurrency, StateItem
from dapr.clients.grpc._crypto import EncryptOptions, DecryptOptions
from dapr.clients.grpc._response import (
Expand Down Expand Up @@ -508,6 +508,23 @@ def test_transaction_then_get_states(self):
self.assertEqual(resp.items[1].key, another_key)
self.assertEqual(resp.items[1].data, to_bytes(another_value.upper()))

dapr.execute_state_transaction(
store_name='statestore',
operations=[
TransactionalStateOperation(
key=key, operation_type=TransactionOperationType.delete
),
TransactionalStateOperation(
key=another_key, operation_type=TransactionOperationType.delete
),
],
)
resp = dapr.get_state(store_name='statestore', key=key)
self.assertEqual(resp.data, b'')

resp = dapr.get_state(store_name='statestore', key=another_key)
self.assertEqual(resp.data, b'')

self._fake_dapr_server.raise_exception_on_next_call(
status_pb2.Status(code=code_pb2.INVALID_ARGUMENT, message='my invalid argument message')
)
Expand Down

0 comments on commit d7d84f2

Please sign in to comment.