Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add export test #228

Merged
merged 3 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/cli/serverless/export/cancel.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func CancelCmd(h *internal.Helper) *cobra.Command {

if !force {
if !h.IOStreams.CanPrompt {
return fmt.Errorf("the terminal doesn't support prompt, please run with --force to delete the branch")
return fmt.Errorf("the terminal doesn't support prompt, please run with --force to cancel the export")
}

confirmationMessage := fmt.Sprintf("%s %s %s", color.BlueString("Please type"), color.HiBlueString(confirmed), color.BlueString("to confirm:"))
Expand Down
113 changes: 113 additions & 0 deletions internal/cli/serverless/export/cancel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package export

import (
"bytes"
"context"
"encoding/json"
"fmt"
"os"
"testing"

"tidbcloud-cli/internal"
"tidbcloud-cli/internal/iostream"
"tidbcloud-cli/internal/mock"
"tidbcloud-cli/internal/service/cloud"
"tidbcloud-cli/pkg/tidbcloud/v1beta1/serverless/export"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type CancelExportSuite struct {
suite.Suite
h *internal.Helper
mockClient *mock.TiDBCloudClient
}

func (suite *CancelExportSuite) SetupTest() {
if err := os.Setenv("NO_COLOR", "true"); err != nil {
suite.T().Error(err)
}

var pageSize int64 = 10
suite.mockClient = new(mock.TiDBCloudClient)
suite.h = &internal.Helper{
Client: func() (cloud.TiDBCloudClient, error) {
return suite.mockClient, nil
},
QueryPageSize: pageSize,
IOStreams: iostream.Test(),
}
}

func (suite *DescribeExportSuite) TestCancelExportArgs() {
assert := require.New(suite.T())
ctx := context.Background()

body := &export.Export{}
err := json.Unmarshal([]byte(getExportResp), body)
assert.Nil(err)
clusterId := "fake-cluster-id"
exportId := "fake-export-id"
suite.mockClient.On("CancelExport", ctx, clusterId, exportId).Return(body, nil)

tests := []struct {
name string
args []string
err error
stdoutString string
stderrString string
}{
{
name: "cancel export success",
args: []string{"-c", clusterId, "-e", exportId, "--force"},
stdoutString: "export fake-export-id canceled\n",
},
{
name: "cancel export without force",
args: []string{"-c", clusterId, "-e", exportId},
err: fmt.Errorf("the terminal doesn't support prompt, please run with --force to cancel the export"),
},
{
name: "cancel export without required cluster id",
args: []string{"-e", exportId},
err: fmt.Errorf("required flag(s) \"cluster-id\" not set"),
},
}

for _, tt := range tests {
suite.T().Run(tt.name, func(t *testing.T) {
cmd := CancelCmd(suite.h)
cmd.SetContext(ctx)
suite.h.IOStreams.Out.(*bytes.Buffer).Reset()
suite.h.IOStreams.Err.(*bytes.Buffer).Reset()
cmd.SetArgs(tt.args)
err = cmd.Execute()
assert.Equal(tt.err, err)

assert.Equal(tt.stdoutString, suite.h.IOStreams.Out.(*bytes.Buffer).String())
assert.Equal(tt.stderrString, suite.h.IOStreams.Err.(*bytes.Buffer).String())
if tt.err == nil {
suite.mockClient.AssertExpectations(suite.T())
}
})
}
}

func TestCancelExportSuite(t *testing.T) {
suite.Run(t, new(CancelExportSuite))
}
11 changes: 6 additions & 5 deletions internal/cli/serverless/export/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ const (
CSVSeparatorDefaultValue = ","
CSVDelimiterDefaultValue = "\""
CSVNullValueDefaultValue = "\\N"
CompressionDefaultValue = "GZIP"
ParquetCompressionDefaultValue = "ZSTD"
CSVSkipHeaderDefaultValue = false
CompressionDefaultValue = export.EXPORTCOMPRESSIONTYPEENUM_GZIP
ParquetCompressionDefaultValue = export.EXPORTPARQUETCOMPRESSIONTYPEENUM_ZSTD
)

type CreateOpts struct {
Expand Down Expand Up @@ -557,10 +558,10 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
// apply default values
if strings.ToUpper(fileType) == string(FileTypePARQUET) {
if parquetCompression == "" {
parquetCompression = ParquetCompressionDefaultValue
parquetCompression = string(ParquetCompressionDefaultValue)
}
} else if compression == "" {
compression = CompressionDefaultValue
compression = string(CompressionDefaultValue)
}
// build param to create export
fileTypeEnum := export.ExportFileTypeEnum(strings.ToUpper(fileType))
Expand Down Expand Up @@ -668,7 +669,7 @@ func CreateCmd(h *internal.Helper) *cobra.Command {
createCmd.Flags().String(flag.CSVDelimiter, CSVDelimiterDefaultValue, "Delimiter of string type variables in CSV files.")
createCmd.Flags().String(flag.CSVSeparator, CSVSeparatorDefaultValue, "Separator of each value in CSV files.")
createCmd.Flags().String(flag.CSVNullValue, CSVNullValueDefaultValue, "Representation of null values in CSV files.")
createCmd.Flags().Bool(flag.CSVSkipHeader, false, "Export CSV files of the tables without header.")
createCmd.Flags().Bool(flag.CSVSkipHeader, CSVSkipHeaderDefaultValue, "Export CSV files of the tables without header.")
createCmd.Flags().String(flag.S3RoleArn, "", "The role arn of the S3. You only need to set one of the s3.role-arn and [s3.access-key-id, s3.secret-access-key].")
createCmd.Flags().String(flag.GCSURI, "", "The GCS URI in gcs://<bucket>/<path> format. Required when target type is GCS.")
createCmd.Flags().String(flag.GCSServiceAccountKey, "", "The base64 encoded service account key of GCS.")
Expand Down
Loading
Loading