def delete_command(key): """ Delete a key value pair \b Arguments: key TEXT Key to delete """ try: kv_store.delete_key(key=key) click.secho(f"Key {key!r} has been deleted", fg="green") except Exception as exc: log_exception(exc) raise TerminalError("An error occurred deleting the key")
def test_delete_key_value_raises_if_key_not_found(self, monkeypatch, cloud_api): Client = MagicMock() Client().graphql.return_value = GraphQLResult( data=dict(key_value=[], )) monkeypatch.setattr("prefect.backend.kv_store.Client", Client) with pytest.raises(ValueError): delete_key(key="foo") Client.return_value.graphql.assert_called_with(query={ "query": { 'key_value(where: { key: { _eq: "foo" } })': {"id"} } })
def test_get_key_value_calls_client_query_correctly( self, monkeypatch, cloud_api): key_value_id_gql_return = GraphQLResult( data=dict(key_value=[GraphQLResult({"id": "123"})], )) delete_key_value_gql_return = GraphQLResult( data=dict(delete_key_value=GraphQLResult({"success": True}), )) # helper function to return key value id # and the delete_key_value depending on input def fake_graphql_responses(*args, **kwargs): if "query" in kwargs["query"]: return key_value_id_gql_return return delete_key_value_gql_return Client = MagicMock() Client.return_value.graphql.side_effect = fake_graphql_responses monkeypatch.setattr("prefect.backend.kv_store.Client", Client) success = delete_key(key="foo") assert success assert Client.return_value.graphql.call_args_list == [ call(query={ "query": { 'key_value(where: { key: { _eq: "foo" } })': {"id"} } }), call( query={ "mutation($input: delete_key_value_input!)": { "delete_key_value(input: $input)": {"success"} } }, variables={"input": { "key_value_id": "123" }}, ), ]
def test_delete_key_value_raises_on_server_backend(self, server_api): with pytest.raises(ClientError): delete_key(key="foo")