github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/query/session_test.go (about)

     1  package query
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
     8  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Query"
     9  	"go.uber.org/mock/gomock"
    10  	grpcCodes "google.golang.org/grpc/codes"
    11  	grpcStatus "google.golang.org/grpc/status"
    12  
    13  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    14  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
    15  	"github.com/ydb-platform/ydb-go-sdk/v3/query"
    16  )
    17  
    18  func TestBegin(t *testing.T) {
    19  	t.Run("HappyWay", func(t *testing.T) {
    20  		ctx := xtest.Context(t)
    21  		ctrl := gomock.NewController(t)
    22  		service := NewMockQueryServiceClient(ctrl)
    23  		service.EXPECT().BeginTransaction(gomock.Any(), gomock.Any()).Return(&Ydb_Query.BeginTransactionResponse{
    24  			Status: Ydb.StatusIds_SUCCESS,
    25  			TxMeta: &Ydb_Query.TransactionMeta{
    26  				Id: "123",
    27  			},
    28  		}, nil)
    29  		t.Log("begin")
    30  		tx, err := begin(ctx, service, "123", query.TxSettings())
    31  		require.NoError(t, err)
    32  		require.Equal(t, "123", tx.id)
    33  	})
    34  	t.Run("TransportError", func(t *testing.T) {
    35  		ctx := xtest.Context(t)
    36  		ctrl := gomock.NewController(t)
    37  		service := NewMockQueryServiceClient(ctrl)
    38  		service.EXPECT().BeginTransaction(gomock.Any(), gomock.Any()).Return(nil, grpcStatus.Error(grpcCodes.Unavailable, ""))
    39  		t.Log("begin")
    40  		_, err := begin(ctx, service, "123", query.TxSettings())
    41  		require.Error(t, err)
    42  		require.True(t, xerrors.IsTransportError(err, grpcCodes.Unavailable))
    43  	})
    44  	t.Run("OperationError", func(t *testing.T) {
    45  		ctx := xtest.Context(t)
    46  		ctrl := gomock.NewController(t)
    47  		service := NewMockQueryServiceClient(ctrl)
    48  		service.EXPECT().BeginTransaction(gomock.Any(), gomock.Any()).Return(&Ydb_Query.BeginTransactionResponse{
    49  			Status: Ydb.StatusIds_UNAVAILABLE,
    50  		}, nil)
    51  		t.Log("begin")
    52  		_, err := begin(ctx, service, "123", query.TxSettings())
    53  		require.Error(t, err)
    54  		require.True(t, xerrors.IsOperationError(err, Ydb.StatusIds_UNAVAILABLE))
    55  	})
    56  }