Skip to content

Commit

Permalink
misc: add ut
Browse files Browse the repository at this point in the history
  • Loading branch information
voidint committed Nov 27, 2023
1 parent a5fbd58 commit 37852fc
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 15 deletions.
3 changes: 1 addition & 2 deletions pkg/http/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/k0kubun/go-ansi"
"github.com/schollz/progressbar/v3"

"github.com/voidint/g/pkg/errs"
)

Expand Down Expand Up @@ -69,7 +68,7 @@ func Download(srcURL string, filename string, flag int, perm fs.FileMode, withPr
func DownloadAsBytes(srcURL string) (data []byte, err error) {
resp, err := http.Get(srcURL)
if err != nil {
return nil, err
return nil, errs.NewDownloadError(srcURL, err)
}
defer resp.Body.Close()
return io.ReadAll(resp.Body)
Expand Down
130 changes: 130 additions & 0 deletions pkg/http/download_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package http

import (
"errors"
"fmt"
"io/fs"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

"github.com/agiledragon/gomonkey/v2"
"github.com/stretchr/testify/assert"
"github.com/voidint/g/pkg/errs"
)

func TestIsSuccess(t *testing.T) {
Expand Down Expand Up @@ -58,3 +66,125 @@ func TestIsSuccess(t *testing.T) {
})
}
}

func TestDownload(t *testing.T) {
e := errors.New("unknown error")
url := "http://github.com/voidint/g"
filename := fmt.Sprintf("%d.txt", time.Now().Unix())
defer os.Remove(filename)

rr1 := httptest.NewRecorder()
rr1.WriteHeader(http.StatusNotFound)

rr2 := httptest.NewRecorder()
rr2.WriteHeader(http.StatusOK)
_, _ = rr2.WriteString("hello world")

patches := gomonkey.ApplyMethodSeq(&http.Client{}, "Get", []gomonkey.OutputCell{
{Values: gomonkey.Params{nil, e}},
{Values: gomonkey.Params{rr1.Result(), nil}},
{Values: gomonkey.Params{rr2.Result(), nil}},
})
defer patches.Reset()

type args struct {
srcURL string
filename string
flag int
perm fs.FileMode
withProgress bool
}
tests := []struct {
name string
args args
wantSize int64
wantErr error
}{
{
name: "发送请求返回异常响应",
args: args{
srcURL: url,
filename: filename,
flag: os.O_RDWR | os.O_CREATE,
perm: 0600,
withProgress: true,
},
wantSize: 0,
wantErr: errs.NewDownloadError(url, e),
},
{
name: "资源不存在",
args: args{
srcURL: url,
filename: filename,
flag: os.O_RDWR | os.O_CREATE,
perm: 0600,
withProgress: true,
},
wantSize: 0,
wantErr: errs.NewURLUnreachableError(url, fmt.Errorf("%d", http.StatusNotFound)),
},
{
name: "下载资源成功",
args: args{
srcURL: url,
filename: filename,
flag: os.O_RDWR | os.O_CREATE,
perm: 0600,
withProgress: true,
},
wantSize: int64(len([]byte("hello world"))),
wantErr: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotSize, err := Download(tt.args.srcURL, tt.args.filename, tt.args.flag, tt.args.perm, tt.args.withProgress)
assert.Equal(t, tt.wantErr, err)
assert.Equal(t, tt.wantSize, gotSize)
})
}
}

func TestDownloadAsBytes(t *testing.T) {
e := errors.New("unknown error")
url := "http://github.com/voidint/g"

rr := httptest.NewRecorder()
rr.WriteHeader(http.StatusOK)
_, _ = rr.WriteString("hello world")

patches := gomonkey.ApplyMethodSeq(&http.Client{}, "Get", []gomonkey.OutputCell{
{Values: gomonkey.Params{nil, e}},
{Values: gomonkey.Params{rr.Result(), nil}},
})
defer patches.Reset()

tests := []struct {
name string
url string
wantData []byte
wantErr error
}{
{
name: "发送请求返回异常响应",
url: url,
wantData: nil,
wantErr: errs.NewDownloadError(url, e),
},
{
name: "发送请求并得到正常响应",
url: url,
wantData: []byte("hello world"),
wantErr: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
data, err := DownloadAsBytes(tt.url)
assert.Equal(t, err, tt.wantErr)
assert.Equal(t, data, tt.wantData)
})
}
}
19 changes: 6 additions & 13 deletions pkg/sdk/github/release_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,15 @@ func TestReleaseUpdater_CheckForUpdates(t *testing.T) {
_, _ = rr6.WriteString(`{"tag_name": "1.6.0"}`)

patches := gomonkey.ApplyMethodSeq(&http.Client{}, "Do", []gomonkey.OutputCell{
{Values: gomonkey.Params{nil, errors.New("unknown error")}},
{Values: gomonkey.Params{rr2.Result(), nil}},
{Values: gomonkey.Params{rr3.Result(), nil}},
{Values: gomonkey.Params{rr4.Result(), nil}},
{Values: gomonkey.Params{rr5.Result(), nil}},
{Values: gomonkey.Params{rr6.Result(), nil}},
{Values: gomonkey.Params{nil, errors.New("unknown error")}}, // 1、发送查询请求失败
{Values: gomonkey.Params{rr2.Result(), nil}}, // 2、得到非成功响应
{Values: gomonkey.Params{rr3.Result(), nil}}, // 3、响应内容反序列化错误
{Values: gomonkey.Params{rr4.Result(), nil}}, // 4、响应内容中版本号为非语义化版本号
{Values: gomonkey.Params{rr5.Result(), nil}}, // 5、响应内容中版本号不大于当前版本号
{Values: gomonkey.Params{rr6.Result(), nil}}, // 6、存在新版本号
})
defer patches.Reset()

// 1、发送查询请求失败
// 2、得到非成功响应
// 3、响应内容反序列化错误
// 4、响应内容中版本号为非语义化版本号
// 5、响应内容中版本号不大于当前版本号
// 6、存在新版本号

owner := "voidint"
repo := "g"
url := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", owner, repo)
Expand Down

0 comments on commit 37852fc

Please sign in to comment.