Skip to content
This repository was archived by the owner on Feb 20, 2024. It is now read-only.

Commit 899c557

Browse files
committedOct 22, 2019
added tests + refactor
1 parent 1b9c7fb commit 899c557

File tree

6 files changed

+67
-21
lines changed

6 files changed

+67
-21
lines changed
 

‎cmd/following.go

+5-14
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,11 @@ package cli
33
import (
44
"fmt"
55
"github.com/spf13/cobra"
6+
"github.com/thamaraiselvam/git-api-cli/cli/service"
67
"os"
78
"text/tabwriter"
89
)
910

10-
//FollowingUser contains following user information
11-
type FollowingUser struct {
12-
Name string `json:"login"`
13-
URL string `json:"html_url"`
14-
}
15-
16-
//FollowingUserList stores a list of FollowingUser
17-
type FollowingUserList = []FollowingUser
18-
1911
func followingCmd() *cobra.Command {
2012
return &cobra.Command{
2113
Use: "following <user name>",
@@ -28,15 +20,14 @@ func followingCmd() *cobra.Command {
2820
return nil
2921
},
3022
Run: func(cmd *cobra.Command, args []string) {
31-
config := createConfig()
32-
getFollowingList(config, args[0])
23+
getFollowingList(args[0])
3324
},
3425
}
3526
}
3627

37-
func getFollowingList(httpConfig HTTPConfig, name string) {
38-
httpConfig.URL = fmt.Sprintf("%s/users/%s/following", httpConfig.BaseURL, name)
39-
userInfoList, err := httpConfig.GetFollowing()
28+
func getFollowingList(name string) {
29+
client := service.CreateClient(fmt.Sprintf("/users/%s/following", name))
30+
userInfoList, err := client.GetFollowing()
4031
if err != nil {
4132
_ = fmt.Errorf("%v", err)
4233
os.Exit(1)

‎cmd/service/service.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const githubURL = "https://api.github.com"
1313
//Client is interface of service
1414
type Client interface {
1515
GetUser() (types.UserInfo, error)
16+
GetFollowing() (types.FollowingUserList, error)
1617
GetFollowers() (types.Followers, error)
1718
}
1819

@@ -62,14 +63,14 @@ func (config config) GetUser() (types.UserInfo, error) {
6263
}
6364

6465
//GetFollowing get following user information from github.com
65-
func (config HTTPConfig) GetFollowing() (FollowingUserList, error) {
66+
func (config config) GetFollowing() (types.FollowingUserList, error) {
6667
resp, err := makeRequest(http.MethodGet, config.URL, nil)
6768
if err != nil {
68-
return FollowingUserList{}, err
69+
return types.FollowingUserList{}, err
6970
}
70-
userInfoList := make(FollowingUserList, 0)
71+
userInfoList := make(types.FollowingUserList, 0)
7172
if err := json.NewDecoder(resp.Body).Decode(&userInfoList); err != nil {
72-
return FollowingUserList{}, fmt.Errorf("error decoding response %v", err)
73+
return types.FollowingUserList{}, fmt.Errorf("error decoding response %v", err)
7374
}
7475

7576
return userInfoList, nil

‎cmd/service/service_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,51 @@ func Test_makeRequest(t *testing.T) {
177177
})
178178
}
179179
}
180+
181+
func TestHTTPConfig_GetFollowing(t *testing.T) {
182+
t.Run("should return FollowingUserList on valid request", func(t *testing.T) {
183+
gock.New(githubHost).
184+
Get("/users/username/following").
185+
Reply(200).
186+
BodyString(`[{"login": "following", "html_url": "https://github.com/following"}]`)
187+
188+
expectedUserInfo := types.FollowingUserList{{
189+
Name: "following",
190+
URL: "https://github.com/following",
191+
}}
192+
193+
client := CreateClient("/users/username/following")
194+
195+
actualFollowingUserList, err := client.GetFollowing()
196+
197+
assert.NoError(t, err)
198+
assert.Equal(t, expectedUserInfo, actualFollowingUserList)
199+
})
200+
201+
t.Run("should return not found on invalid username", func(t *testing.T) {
202+
gock.New(githubHost).
203+
Get("/users/username/following").
204+
Reply(404).
205+
BodyString(`{}`)
206+
207+
client := CreateClient("/users/username/following")
208+
_, err := client.GetFollowing()
209+
210+
assert.Error(t, err)
211+
assert.Equal(t, "user not found", err.Error())
212+
})
213+
214+
t.Run("should return error if response is not a valid json", func(t *testing.T) {
215+
gock.New(githubHost).
216+
Get("/users/username/following").
217+
Reply(200).
218+
BodyString(`string`)
219+
220+
client := CreateClient("/users/username/following")
221+
_, err := client.GetFollowing()
222+
223+
assert.Error(t, err)
224+
assert.Equal(t, "error decoding response invalid character 's' looking for beginning of value", err.Error())
225+
})
226+
227+
}

‎cmd/types/types.go

+9
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@ type follower struct {
1515

1616
//Followers represents list of followers
1717
type Followers []follower
18+
19+
//FollowingUser contains following user information
20+
type FollowingUser struct {
21+
Name string `json:"login"`
22+
URL string `json:"html_url"`
23+
}
24+
25+
//FollowingUserList stores a list of FollowingUser
26+
type FollowingUserList = []FollowingUser

‎go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ require (
66
github.com/golang/mock v1.3.1
77
github.com/spf13/cobra v0.0.5
88
github.com/spf13/pflag v1.0.5 // indirect
9-
github.com/stretchr/objx v0.2.0 // indirect
109
github.com/stretchr/testify v1.3.0
1110
gopkg.in/h2non/gock.v1 v1.0.15
1211
)

‎go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
4545
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
4646
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
4747
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
48-
github.com/stretchr/objx v0.2.0 h1:Hbg2NidpLE8veEBkEZTL3CvlkUIVzuU9jDplZO54c48=
49-
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
5048
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
5149
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
5250
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=

0 commit comments

Comments
 (0)
This repository has been archived.