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 uint test for model package #21

Merged
merged 4 commits into from
Jan 5, 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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ clean: ## Clean project
-rm -rf $(S3HUB) $(SPARE) cover.out cover.html

test: ## Start unit test
env GOOS=$(GOOS) $(GO_TEST) -cover $(GO_PKGROOT) -coverprofile=cover.out
env GOOS=$(GOOS) $(GO_TEST) -coverpkg=./... -coverprofile=cover.out -cover ./...
$(GO_TOOL) cover -html=cover.out -o cover.html

coverage-tree: test ## Generate coverage tree
Expand Down
46 changes: 46 additions & 0 deletions app/domain/model/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package model

import (
"testing"

"github.com/aws/aws-sdk-go-v2/aws"
)

func TestNewAWSProfile(t *testing.T) { //nolint
Expand Down Expand Up @@ -74,3 +76,47 @@ func TestAWSProfileString(t *testing.T) {
})
}
}

func TestAWSConfig_Region(t *testing.T) {
t.Parallel()

type fields struct {
Config *aws.Config
}
tests := []struct {
name string
fields fields
want Region
}{
{
name: "If aws config region is ap-northeast-1, return RegionAPNortheast1",
fields: fields{
Config: &aws.Config{
Region: string(RegionAPNortheast1),
},
},
want: RegionAPNortheast1,
},
{
name: "If aws config region isempty, return RegionUSEast1",
fields: fields{
Config: &aws.Config{
Region: "",
},
},
want: RegionUSEast1,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := &AWSConfig{
Config: tt.fields.Config,
}
if got := c.Region(); got != tt.want {
t.Errorf("AWSConfig.Region() = %v, want %v", got, tt.want)
}
})
}
}
35 changes: 28 additions & 7 deletions app/domain/model/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,21 @@ func NewBucketWithoutProtocol(s string) Bucket {

// WithProtocol returns the Bucket with the protocol.
func (b Bucket) WithProtocol() Bucket {
if strings.HasPrefix(b.String(), S3Protocol) {
return b
}
return Bucket(S3Protocol + b.String())
}

// Join returns the Bucket with the S3Key.
// e.g. "bucket" + "key" -> "bucket/key"
func (b Bucket) Join(key S3Key) Bucket {
if b.Empty() || key.Empty() {
return b
}
if strings.HasSuffix(key.String(), "/") {
key = S3Key(strings.TrimSuffix(key.String(), "/"))
}
return Bucket(fmt.Sprintf("%s/%s", b.String(), key.String()))
}

Expand Down Expand Up @@ -325,26 +334,26 @@ type BucketSet struct {
CreationDate time.Time
}

// S3ObjectIdentifierSets is the set of the S3ObjectSet.
type S3ObjectIdentifierSets []S3ObjectIdentifier
// S3ObjectIdentifiers is the set of the S3ObjectSet.
type S3ObjectIdentifiers []S3ObjectIdentifier

// Len returns the length of the S3ObjectIdentifierSets.
func (s S3ObjectIdentifierSets) Len() int {
// Len returns the length of the S3ObjectIdentifiers.
func (s S3ObjectIdentifiers) Len() int {
return len(s)
}

// Less defines the ordering of S3ObjectIdentifier instances.
func (s S3ObjectIdentifierSets) Less(i, j int) bool {
func (s S3ObjectIdentifiers) Less(i, j int) bool {
return s[i].S3Key < s[j].S3Key
}

// Swap swaps the elements with indexes i and j.
func (s S3ObjectIdentifierSets) Swap(i, j int) {
func (s S3ObjectIdentifiers) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

// ToS3ObjectIdentifiers converts the S3ObjectSets to the ObjectIdentifiers.
func (s S3ObjectIdentifierSets) ToS3ObjectIdentifiers() []types.ObjectIdentifier {
func (s S3ObjectIdentifiers) ToS3ObjectIdentifiers() []types.ObjectIdentifier {
ids := make([]types.ObjectIdentifier, 0, s.Len())
for _, o := range s {
ids = append(ids, *o.ToAWSS3ObjectIdentifier())
Expand Down Expand Up @@ -389,6 +398,18 @@ func (k S3Key) IsAll() bool {
}

func (k S3Key) Join(key S3Key) S3Key {
if key.Empty() {
return k
}
if strings.HasPrefix(key.String(), "/") {
key = S3Key(strings.TrimPrefix(key.String(), "/"))
}
if strings.HasSuffix(key.String(), "/") {
key = S3Key(strings.TrimSuffix(key.String(), "/"))
}
if k.Empty() {
return key
}
return S3Key(fmt.Sprintf("%s/%s", k.String(), key))
}

Expand Down
Loading
Loading