def test_save_then_get_states(self): dapr = DaprClient(f'localhost:{self.server_port}') key = str(uuid.uuid4()) value = str(uuid.uuid4()) another_key = str(uuid.uuid4()) another_value = str(uuid.uuid4()) dapr.save_states(store_name="statestore", states=[ StateItem(key=key, value=value, metadata={"capitalize": "1"}), StateItem(key=another_key, value=another_value, etag="1"), ], metadata=(("metakey", "metavalue"), )) resp = dapr.get_states(store_name="statestore", keys=[key, another_key]) self.assertEqual(resp.items[0].key, key) self.assertEqual(resp.items[0].data, to_bytes(value.capitalize())) self.assertEqual(resp.items[1].key, another_key) self.assertEqual(resp.items[1].data, to_bytes(another_value)) resp = dapr.get_states(store_name="statestore", keys=[key, another_key], states_metadata={"upper": "1"}) self.assertEqual(resp.items[0].key, key) self.assertEqual(resp.items[0].etag, "fake_etag") self.assertEqual(resp.items[0].data, to_bytes(value.upper())) self.assertEqual(resp.items[1].key, another_key) self.assertEqual(resp.items[1].etag, "fake_etag") self.assertEqual(resp.items[1].data, to_bytes(another_value.upper()))
updated_value = "value_1_updated" another_key = "key_2" another_value = "value_2" yet_another_key = "key_3" yet_another_value = "value_3" # Save single state. d.save_state(store_name=storeName, key=key, value=value) print(f"State store has successfully saved {value} with {key} as key") # Save multiple states. d.save_states(store_name=storeName, states=[ StateItem(key=another_key, value=another_value), StateItem(key=yet_another_key, value=yet_another_value) ]) print( f"State store has successfully saved {another_value} with {another_key} as key" ) print( f"State store has successfully saved {yet_another_value} with {yet_another_key} as key" ) # Get one state by key. data = d.get_state(store_name=storeName, key=key, state_metadata={ "metakey": "metavalue" }).data
# Wait for sidecar to be up within 5 seconds. d.wait(5) # Save single state. d.save_state(store_name=storeName, key=key, value=value) print(f"State store has successfully saved {value} with {key} as key") # Save with an etag that is different from the one stored in the database. try: d.save_state(store_name=storeName, key=key, value=another_value, etag="9999") except grpc.RpcError as err: # StatusCode should be StatusCode.ABORTED. print(f"Cannot save due to bad etag. ErrorCode={err.code()} Details={err.details()}") # Save multiple states. d.save_bulk_state(store_name=storeName, states=[StateItem(key=another_key, value=another_value), StateItem(key=yet_another_key, value=yet_another_value)]) print(f"State store has successfully saved {another_value} with {another_key} as key") print(f"State store has successfully saved {yet_another_value} with {yet_another_key} as key") # Save bulk with etag that is different from the one stored in the database. try: d.save_bulk_state(store_name=storeName, states=[ StateItem(key=another_key, value=another_value, etag="999"), StateItem(key=yet_another_key, value=yet_another_value, etag="999")]) except grpc.RpcError as err: # StatusCode should be StatusCode.ABORTED. print(f"Cannot save bulk due to bad etags. ErrorCode={err.code()} Details={err.details()}") # Get one state by key. state = d.get_state(store_name=storeName, key=key, state_metadata={"metakey": "metavalue"})
another_key = "key_2" another_value = "value_2" yet_another_key = "key_3" yet_another_value = "value_3" # Wait for sidecar to be up within 5 seconds. d.wait(5) # Save single state. d.save_state(store_name=storeName, key=key, value=value) print(f"State store has successfully saved {value} with {key} as key") # Save multiple states. d.save_states(store_name=storeName, states=[StateItem(key=another_key, value=another_value), StateItem(key=yet_another_key, value=yet_another_value)]) print(f"State store has successfully saved {another_value} with {another_key} as key") print(f"State store has successfully saved {yet_another_value} with {yet_another_key} as key") # Get one state by key. data = d.get_state(store_name=storeName, key=key, state_metadata={"metakey": "metavalue"}).data print(f"Got value: {data}") # Transaction upsert d.execute_transaction(store_name=storeName, operations=[ TransactionalStateOperation( operation_type=TransactionOperationType.upsert, key=key, data=updated_value), TransactionalStateOperation(key=another_key, data=another_value),