Skip to content

Commit

Permalink
add: add mock test (#44)
Browse files Browse the repository at this point in the history
通过mock的方式完善测试用例
  • Loading branch information
Zherphy authored Dec 18, 2024
1 parent 6c7319c commit 4d8272f
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 16 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
toolchain go1.21.4

require (
bou.ke/monkey v1.0.2
github.com/go-chi/chi v4.1.2+incompatible
sigs.k8s.io/yaml v1.4.0
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
165 changes: 149 additions & 16 deletions server/server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"bou.ke/monkey"
"context"
"encoding/base64"
"errors"
Expand All @@ -12,6 +13,7 @@ import (
"math"
"net/http"
"net/http/httptest"
"net/url"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -284,13 +286,16 @@ func Test_server_downloadObject(t *testing.T) {
out *batch.Object
}
tests := []struct {
name string
fields ServerInfo
args args
wantErr bool
name string
fields ServerInfo
args args
wantErr bool
mockMetaData bool
mockMetaSize bool
wantErrorCode int
}{
{
name: "download object failed",
name: "download object success",
fields: serverInfo,
args: args{
in: &batch.RequestObject{
Expand All @@ -302,11 +307,79 @@ func Test_server_downloadObject(t *testing.T) {
Size: 100,
},
},
wantErr: true,
wantErr: false,
mockMetaData: true,
mockMetaSize: true,
},
{
name: "download getObjectMetadataInput failed",
fields: serverInfo,
args: args{
in: &batch.RequestObject{
OID: "123456789",
Size: 100,
},
out: &batch.Object{
OID: "123456789",
Size: 100,
},
},
wantErr: false,
mockMetaData: false,
mockMetaSize: true,
wantErrorCode: 404,
},
{
name: "download getObjectMetadataInput size error",
fields: serverInfo,
args: args{
in: &batch.RequestObject{
OID: "123456789",
Size: 100,
},
out: &batch.Object{
OID: "123456789",
Size: 100,
},
},
wantErr: false,
mockMetaData: false,
mockMetaSize: false,
wantErrorCode: 422,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
o := obs.GetObjectMetadataOutput{
ContentLength: int64(tt.args.in.Size),
}
getObjectMetadataInputPtr := reflect.ValueOf((*server).getObjectMetadataInput)
if tt.mockMetaData {
monkey.Patch(getObjectMetadataInputPtr.Interface(),
func(s *server, key string) (output *obs.GetObjectMetadataOutput, err error) {
return &o, nil
})
} else if tt.mockMetaSize {
monkey.Patch(getObjectMetadataInputPtr.Interface(),
func(s *server, key string) (output *obs.GetObjectMetadataOutput, err error) {
return &o, errors.New("get Metadata error")
})
} else {
monkey.Patch(getObjectMetadataInputPtr.Interface(),
func(s *server, key string) (output *obs.GetObjectMetadataOutput, err error) {
o.ContentLength = int64(101)
return &o, nil
})
}

defer monkey.Unpatch(getObjectMetadataInputPtr.Interface())
downloadUrl, _ := url.Parse("test.url")
generateDownloadUrlPtr := reflect.ValueOf((*server).generateDownloadUrl)
monkey.Patch(generateDownloadUrlPtr.Interface(),
func(s *server, getObjectInput *obs.CreateSignedUrlInput) *url.URL {
return downloadUrl
})
defer monkey.Unpatch(generateDownloadUrlPtr.Interface())
s := &server{
ttl: tt.fields.ttl,
client: tt.fields.client,
Expand All @@ -317,6 +390,9 @@ func Test_server_downloadObject(t *testing.T) {
}
defer panicCheck(t, tt.wantErr)
s.downloadObject(tt.args.in, tt.args.out)
if tt.args.out.Error != nil && tt.args.out.Error.Code != tt.wantErrorCode {
t.Errorf("download failed with unexpected code = %v", tt.args.out.Error.Code)
}
})
}
}
Expand Down Expand Up @@ -431,30 +507,50 @@ func Test_server_handleBatch(t *testing.T) {
validatecfg.ownerRegexp, _ = regexp.Compile(`^[a-zA-Z]([-_.]?[a-zA-Z0-9]+)*$`)
validatecfg.reponameRegexp, _ = regexp.Compile(`^[a-zA-Z0-9_.-]{1,189}[a-zA-Z0-9]$`)
tests := []struct {
name string
fields ServerInfo
args args
wantErr bool
name string
args args
wantErr bool
fields ServerInfo
wantDealWithAuthError bool
}{
{
name: "server handleBatch success with nil requestBody",
fields: serverInfo,
args: args{
r: httptest.NewRequest(http.MethodGet, batchUrlPath, nil),
},
wantErr: false,
wantErr: false,
wantDealWithAuthError: false,
},
{
name: "server handleBatch success",
fields: serverInfo,
args: args{
r: req,
},
wantErr: false,
wantErr: false,
wantDealWithAuthError: false,
},
{
name: "server handleBatch dealWithAuthError success",
fields: serverInfo,
args: args{
r: req,
},
wantErr: false,
wantDealWithAuthError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.wantDealWithAuthError {
dealWithAuthErrorPtr := reflect.ValueOf((*server).dealWithAuthError)
monkey.Patch(dealWithAuthErrorPtr.Interface(),
func(s *server, userInRepo auth.UserInRepo, w http.ResponseWriter, r *http.Request) error {
return nil
})
defer monkey.Unpatch(dealWithAuthErrorPtr.Interface())
}
s := &server{
ttl: tt.fields.ttl,
client: tt.fields.client,
Expand Down Expand Up @@ -614,11 +710,15 @@ func Test_server_uploadObject(t *testing.T) {
OID: "123456789",
Size: 1000,
}
inObject := batch.RequestObject{
OID: "123456789",
}
tests := []struct {
name string
fields ServerInfo
args args
wantErr bool
name string
args args
wantErr bool
fields ServerInfo
wantGetMeta bool
}{
{
name: "server uploadObject size large than limit",
Expand All @@ -636,9 +736,42 @@ func Test_server_uploadObject(t *testing.T) {
},
wantErr: true,
},
{
name: "server upload get metadata success",
fields: serverInfo,
args: args{
in: &inObject,
out: &outObject,
},
wantErr: false,
wantGetMeta: true,
},
{
name: "server upload get metadata success",
fields: serverInfo,
args: args{
in: &inObject,
out: &outObject,
},
wantErr: true,
wantGetMeta: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
getObjectMetadataInputPtr := reflect.ValueOf((*server).getObjectMetadataInput)
if tt.wantGetMeta {
monkey.Patch(getObjectMetadataInputPtr.Interface(),
func(s *server, key string) (output *obs.GetObjectMetadataOutput, err error) {
return &obs.GetObjectMetadataOutput{}, nil
})
} else {
monkey.Patch(getObjectMetadataInputPtr.Interface(),
func(s *server, key string) (output *obs.GetObjectMetadataOutput, err error) {
return &obs.GetObjectMetadataOutput{}, errors.New("get meta data error")
})
}
defer monkey.Unpatch(getObjectMetadataInputPtr.Interface())
s := &server{
ttl: tt.fields.ttl,
client: tt.fields.client,
Expand Down

0 comments on commit 4d8272f

Please sign in to comment.