Skip to content

Commit

Permalink
Add unit test for s3 model
Browse files Browse the repository at this point in the history
  • Loading branch information
nao1215 committed Jan 5, 2024
1 parent ab3761e commit a0e3fd0
Show file tree
Hide file tree
Showing 9 changed files with 443 additions and 14 deletions.
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
32 changes: 25 additions & 7 deletions app/domain/model/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ func (b Bucket) WithProtocol() Bucket {
// 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 @@ -328,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 @@ -392,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

0 comments on commit a0e3fd0

Please sign in to comment.