Ejemplo n.º 1
0
    def test_transaction_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.execute_transaction(
            store_name="statestore",
            operations=[
                TransactionalStateOperation(key=key, data=value),
                TransactionalStateOperation(key=another_key,
                                            data=another_value),
            ],
            transactional_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))
        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].data, to_bytes(value.upper()))
        self.assertEqual(resp.items[1].key, another_key)
        self.assertEqual(resp.items[1].data, to_bytes(another_value.upper()))
Ejemplo n.º 2
0
    # 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"})
    print(f"Got value={state.data} eTag={state.etag}")

    # Transaction upsert
    d.execute_state_transaction(store_name=storeName, operations=[
        TransactionalStateOperation(
            operation_type=TransactionOperationType.upsert,
            key=key,
            data=updated_value,
            etag=state.etag),
        TransactionalStateOperation(key=another_key, data=another_value),
    ])

    # Batch get
    items = d.get_bulk_state(store_name=storeName, keys=[key, another_key], states_metadata={"metakey": "metavalue"}).items
    print(f"Got items with etags: {[(i.data, i.etag) for i 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
    print(f"Got value after delete: {data}")