Skip to content

Commit 86183e6

Browse files
committed
feat: add client test
1 parent 0fd6d2b commit 86183e6

File tree

9 files changed

+182
-1
lines changed

9 files changed

+182
-1
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ mod:
1616
go mod tidy
1717
lint:
1818
golangci-lint run
19+
mock:
20+
sh scripts/mock.sh
1921
.PHONY: build
2022
build:
2123
go build -o dingtalk cmd/main.go

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# dingtalk
22

33
![Go](https://github.com/CatchZeng/dingtalk/workflows/Go/badge.svg)
4+
[![codecov](https://codecov.io/gh/CatchZeng/dingtalk/branch/master/graph/badge.svg)](https://codecov.io/gh/CatchZeng/dingtalk)
45
[![Go Report Card](https://goreportcard.com/badge/github.com/CatchZeng/dingtalk)](https://goreportcard.com/report/github.com/CatchZeng/dingtalk)
56
[![Release](https://img.shields.io/github/release/CatchZeng/dingtalk.svg)](https://github.com/CatchZeng/dingtalk/releases)
67
[![GoDoc](https://godoc.org/github.com/CatchZeng/dingtalk?status.svg)](https://pkg.go.dev/github.com/CatchZeng/dingtalk?tab=doc)

READMEEN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# dingtalk
22

33
![Go](https://github.com/CatchZeng/dingtalk/workflows/Go/badge.svg)
4+
[![codecov](https://codecov.io/gh/CatchZeng/dingtalk/branch/master/graph/badge.svg)](https://codecov.io/gh/CatchZeng/dingtalk)
45
[![Go Report Card](https://goreportcard.com/badge/github.com/CatchZeng/dingtalk)](https://goreportcard.com/report/github.com/CatchZeng/dingtalk)
56
[![Release](https://img.shields.io/github/release/CatchZeng/dingtalk.svg)](https://github.com/CatchZeng/dingtalk/releases)
67
[![GoDoc](https://godoc.org/github.com/CatchZeng/dingtalk?status.svg)](https://pkg.go.dev/github.com/CatchZeng/dingtalk?tab=doc)

client.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"github.com/CatchZeng/dingtalk/internal/security"
87
"io/ioutil"
98
"net/http"
109
"time"
10+
11+
"github.com/CatchZeng/dingtalk/internal/security"
1112
)
1213

1314
// Client dingtalk client

client_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package dingtalk
2+
3+
import (
4+
"bou.ke/monkey"
5+
"errors"
6+
"github.com/CatchZeng/dingtalk/internal/security"
7+
"io"
8+
"net/http"
9+
"reflect"
10+
"testing"
11+
12+
mock_message "github.com/CatchZeng/dingtalk/test/mocks/message"
13+
"github.com/golang/mock/gomock"
14+
)
15+
16+
func TestNewClient(t *testing.T) {
17+
type args struct {
18+
accessToken string
19+
secret string
20+
}
21+
tests := []struct {
22+
name string
23+
args args
24+
want *Client
25+
}{
26+
{
27+
name: "",
28+
args: args{
29+
accessToken: "123456",
30+
secret: "111111",
31+
},
32+
want: &Client{
33+
AccessToken: "123456",
34+
Secret: "111111",
35+
},
36+
},
37+
}
38+
for _, tt := range tests {
39+
t.Run(tt.name, func(t *testing.T) {
40+
if got := NewClient(tt.args.accessToken, tt.args.secret); !reflect.DeepEqual(got, tt.want) {
41+
t.Errorf("NewClient() = %v, want %v", got, tt.want)
42+
}
43+
})
44+
}
45+
}
46+
47+
func TestClient_Send(t *testing.T) {
48+
ctrl := gomock.NewController(t)
49+
defer ctrl.Finish()
50+
51+
messgae := mock_message.NewMockMessage(ctrl)
52+
53+
t.Run("message return error", func(t *testing.T) {
54+
c := &Client{}
55+
56+
messgae.EXPECT().ToByte().Return([]byte{}, errors.New("test"))
57+
58+
if _, err := c.Send(messgae); err == nil {
59+
t.Error("send error")
60+
}
61+
})
62+
63+
t.Run("security.GetDingTalkURL return error", func(t *testing.T) {
64+
c := &Client{
65+
AccessToken: "dasfsafewfewfwfewf",
66+
Secret: "ewfewfwfwefwafew",
67+
}
68+
69+
messgae.EXPECT().ToByte().Return([]byte{}, nil)
70+
monkey.Patch(security.GetDingTalkURL, func(accessToken string, secret string) (string, error) {
71+
return "", errors.New("GetDingTalkURL error")
72+
})
73+
74+
if _, err := c.Send(messgae); err == nil {
75+
t.Error("send error")
76+
}
77+
})
78+
79+
80+
t.Run("http.NewRequest return error", func(t *testing.T) {
81+
c := &Client{
82+
AccessToken: "dasfsafewfewfwfewf",
83+
Secret: "ewfewfwfwefwafew",
84+
}
85+
86+
messgae.EXPECT().ToByte().Return([]byte{}, nil)
87+
monkey.Patch(security.GetDingTalkURL, func(accessToken string, secret string) (string, error) {
88+
return "https://oapi.dingtalk.com/robot/send?access_token=ewfewfwfwefwafew", nil
89+
})
90+
91+
monkey.Patch(http.NewRequest, func(method, url string, body io.Reader) (*http.Request, error) {
92+
return nil, errors.New("NewRequest error")
93+
})
94+
95+
if _, err := c.Send(messgae); err == nil {
96+
t.Error("send error")
97+
}
98+
})
99+
}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ module github.com/CatchZeng/dingtalk
33
go 1.13
44

55
require (
6+
bou.ke/monkey v1.0.2
67
github.com/CatchZeng/gutils v0.0.3
8+
github.com/golang/mock v1.4.4
79
github.com/spf13/cobra v0.0.5
810
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
2+
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
13
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
24
github.com/CatchZeng/gutils v0.0.3 h1:jFiUixFWOz+pMCe+Y3n9vzkIWh0vnf6/XX97HVyFEF0=
35
github.com/CatchZeng/gutils v0.0.3/go.mod h1:Uz8tJTZDM9XWGTFQt3oIYs+zY3/2fOy0TVQaQTgPtWg=
@@ -8,6 +10,8 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
810
github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwcJI5acqYI6dE=
911
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1012
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
13+
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
14+
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
1115
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
1216
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
1317
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
@@ -29,7 +33,12 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
2933
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
3034
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
3135
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
36+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
37+
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
38+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
3239
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
40+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3341
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
42+
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
3443
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3544
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

scripts/mock.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
shell_dir=$(dirname $0)
3+
cd ${shell_dir}
4+
5+
cd ..
6+
7+
rm -rf test/mocks
8+
mkdir -p test/mocks/message
9+
10+
# open with vscode
11+
if which mockgen >/dev/null; then
12+
echo "mockgen has installed in PATH"
13+
else
14+
echo "warning: 'mockgen' command has not installed in PATH"
15+
GO111MODULE=on go get github.com/golang/mock/[email protected]
16+
fi
17+
18+
mockgen -package=mock_message -source=message.go > test/mocks/message/message.go

test/mocks/message/message.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)