From 3505e14bae8d88eda1d595f88e4e91c719bed3aa Mon Sep 17 00:00:00 2001 From: bilalcaliskan Date: Mon, 27 Nov 2023 23:58:28 +0300 Subject: [PATCH] test: cover missing lines --- internal/aws/aws.go | 7 +++++-- internal/aws/aws_test.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/internal/aws/aws.go b/internal/aws/aws.go index 02a4e31..c971108 100644 --- a/internal/aws/aws.go +++ b/internal/aws/aws.go @@ -29,13 +29,16 @@ import ( "github.com/bilalcaliskan/s3-manager/internal/utils" ) -func CreateClient(opts *options.RootOptions) (*s3.Client, error) { +func createConfig(opts *options.RootOptions) (cfg aws.Config, err error) { appCreds := aws.NewCredentialsCache(credentials.NewStaticCredentialsProvider(opts.AccessKey, opts.SecretKey, "")) - cfg, err := config.LoadDefaultConfig(context.Background(), + return config.LoadDefaultConfig(context.Background(), config.WithRegion(opts.Region), config.WithCredentialsProvider(appCreds), ) +} +func CreateClient(opts *options.RootOptions) (*s3.Client, error) { + cfg, err := createConfig(opts) if err != nil { return nil, err } diff --git a/internal/aws/aws_test.go b/internal/aws/aws_test.go index 91bca8d..77d1f79 100644 --- a/internal/aws/aws_test.go +++ b/internal/aws/aws_test.go @@ -111,6 +111,37 @@ func TestDeleteFiles(t *testing.T) { }, }, }, + { + "Success with non-empty file list and dry-run enabled", + nil, + func(ctx context.Context, params *s3.DeleteObjectInput, optFns ...func(*s3.Options)) (*s3.DeleteObjectOutput, error) { + return &s3.DeleteObjectOutput{}, nil + }, + true, + []types.Object{ + { + ETag: aws.String("03c0fe42b7efa3470fc99037a8e5449d"), + Key: aws.String("../../testdata/file4.txt"), + StorageClass: types.ObjectStorageClass("STANDART"), + Size: aws.Int64(500), + LastModified: aws.Time(time.Now().Add(-5 * time.Hour)), + }, + { + ETag: aws.String("03c0fe42b7efa3470fc99037a8e54122"), + Key: aws.String("../../testdata/file5.txt"), + StorageClass: types.ObjectStorageClass("STANDART"), + Size: aws.Int64(1000), + LastModified: aws.Time(time.Now().Add(-2 * time.Hour)), + }, + { + ETag: aws.String("03c0fe42b7efa3470fc99037a8e5443d"), + Key: aws.String("../../testdata/file6.txt"), + StorageClass: types.ObjectStorageClass("STANDART"), + Size: aws.Int64(1500), + LastModified: aws.Time(time.Now().Add(-10 * time.Hour)), + }, + }, + }, { "Failure caused by delete object err", constants.ErrInjected, @@ -171,6 +202,13 @@ func getMockBody(path string) io.ReadCloser { return io.NopCloser(body) } +func TestCreateClient(t *testing.T) { + rootOpts := options.GetMockedRootOptions() + cl, err := CreateClient(rootOpts) + assert.Nil(t, err) + assert.NotNil(t, cl) +} + // TestSearchString is a test function that tests the behavior of the SearchString function. // // It creates test cases with different scenarios and verifies the expected results.