Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
kyoshiro.maruo committed Mar 16, 2021
1 parent 33f8000 commit edb470d
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 12 deletions.
8 changes: 6 additions & 2 deletions cmd/impl/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ func AddCmd(client redashClient, users, groups []string) error {
return nil
}

return addCmd(client, users, groups)
}

func addCmd(client redashClient, users, groups []string) error {
groupIds := make([]int, len(groups))
for i, g := range groups {
id, err := findGroupID(client, g)
if err != nil {
return xerrors.Errorf("%+w", err)
return xerrors.Errorf("findGroupID: %+w", err)
}
groupIds[i] = id
}
Expand All @@ -35,7 +39,7 @@ func AddCmd(client redashClient, users, groups []string) error {
for i, u := range users {
id, err := findUserID(client, u)
if err != nil {
return xerrors.Errorf("%+w", err)
return xerrors.Errorf("findUserID: %+w", err)
}
userIds[i] = id
}
Expand Down
124 changes: 124 additions & 0 deletions cmd/impl/add_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package impl

import (
"testing"

"github.com/stretchr/testify/assert"
)

func Test_addCmd(t *testing.T) {
cases := []struct {
name string
users []string
groups []string

valid bool
}{
{name: "it successes", users: []string{"user1@email.com"}, groups: []string{"my-group-3", "my-group-4"}, valid: true},
{name: "it fails when the user is not found", users: []string{"user2@email.com"}, groups: []string{"my-group-3"}, valid: false},
{name: "it fails when the group is not found", users: []string{"user1@email.com"}, groups: []string{"my-group-5"}, valid: false},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
err := addCmd(&mockClient{respSearchUser: []byte(respSearchUserJSON), respGetGroups: []byte(respGetGroupJSON)}, c.users, c.groups)
if c.valid {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
})
}
}

var respSearchUserJSON = `
{
"count": 1,
"page": 1,
"page_size": 20,
"results": [
{
"active_at": "2021-03-16T11:12:58Z",
"auth_type": "external",
"created_at": "2018-08-07T01:13:40.265Z",
"disabled_at": null,
"email": "user1@email.com",
"groups": [
{
"id": 33,
"name": "my-group-33"
}
],
"id": 1,
"is_disabled": false,
"is_email_verified": true,
"is_invitation_pending": false,
"name": "snowhork",
"profile_image_url": "example.com",
"updated_at": "2021-03-16T11:13:03.856Z"
}
]
}
`

var respGetGroupJSON = `
[
{
"created_at": "2016-05-22T09:30:53.770Z",
"id": 1,
"name": "admin",
"permissions": [
"admin",
"super_admin"
],
"type": "builtin"
},
{
"created_at": "2016-05-22T09:30:53.772Z",
"id": 2,
"name": "default",
"permissions": [],
"type": "builtin"
},
{
"created_at": "2016-08-22T06:37:52.138Z",
"id": 3,
"name": "my-group-3",
"permissions": [
"create_dashboard",
"create_query",
"edit_dashboard",
"edit_query",
"view_query",
"view_source",
"execute_query",
"list_users",
"schedule_query",
"list_dashboards",
"list_alerts",
"list_data_sources"
],
"type": "regular"
},
{
"created_at": "2016-08-22T06:37:52.138Z",
"id": 4,
"name": "my-group-4",
"permissions": [
"create_dashboard",
"create_query",
"edit_dashboard",
"edit_query",
"view_query",
"view_source",
"execute_query",
"list_users",
"schedule_query",
"list_dashboards",
"list_alerts",
"list_data_sources"
],
"type": "regular"
}
]
`
20 changes: 10 additions & 10 deletions cmd/impl/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ type responseSearchUser struct {
func requestSearchUser(client redashClient, q string) (resp responseSearchUser, err error) {
raw, err := client.SearchUser(q)
if err != nil {
return resp, xerrors.Errorf("%+w", err)
return resp, xerrors.Errorf("client.SearchUse: %+w", err)
}

if err := json.Unmarshal(raw, &resp); err != nil {
return resp, xerrors.Errorf("%+w", err)
return resp, xerrors.Errorf("json.Unmarshal: %+w", err)
}

return resp, nil
Expand All @@ -43,11 +43,11 @@ type responseGetGroups []struct {
func requestGetGroups(client redashClient) (resp responseGetGroups, err error) {
raw, err := client.GetGroups()
if err != nil {
return resp, xerrors.Errorf("%+w", err)
return resp, xerrors.Errorf("client.SearchUse: %+w", err)
}

if err := json.Unmarshal(raw, &resp); err != nil {
return resp, xerrors.Errorf("%+w", err)
return resp, xerrors.Errorf("json.Unmarshal: %+w", err)
}

return resp, nil
Expand All @@ -62,11 +62,11 @@ type responseGetQuery struct {
func requestGetQuery(client redashClient, id int) (resp responseGetQuery, err error) {
raw, err := client.GetQuery(id)
if err != nil {
return resp, xerrors.Errorf("%+w", err)
return resp, xerrors.Errorf("client.SearchUse: %+w", err)
}

if err := json.Unmarshal(raw, &resp); err != nil {
return resp, xerrors.Errorf("%+w", err)
return resp, xerrors.Errorf("json.Unmarshal: %+w", err)
}

return resp, nil
Expand All @@ -81,11 +81,11 @@ type responseGetDataSource struct {
func requestGetDataSource(client redashClient, id int) (resp responseGetDataSource, err error) {
raw, err := client.GetDataSource(id)
if err != nil {
return resp, xerrors.Errorf("%+w", err)
return resp, xerrors.Errorf("client.SearchUse: %+w", err)
}

if err := json.Unmarshal(raw, &resp); err != nil {
return resp, xerrors.Errorf("%+w", err)
return resp, xerrors.Errorf("json.Unmarshal: %+w", err)
}

return resp, nil
Expand All @@ -106,11 +106,11 @@ type responseGetDashboard struct {
func requestGetDashboard(client redashClient, slug string) (resp responseGetDashboard, err error) {
raw, err := client.GetDashboard(slug)
if err != nil {
return resp, xerrors.Errorf("%+w", err)
return resp, xerrors.Errorf("client.SearchUse: %+w", err)
}

if err := json.Unmarshal(raw, &resp); err != nil {
return resp, xerrors.Errorf("%+w", err)
return resp, xerrors.Errorf("json.Unmarshal: %+w", err)
}

return resp, nil
Expand Down
34 changes: 34 additions & 0 deletions cmd/impl/request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package impl

type mockClient struct {
respSearchUser,
respGetGroups,
respGetQuery,
respDataSource,
respDashboard,
respAddMember []byte
}

func (c *mockClient) SearchUser(q string) ([]byte, error) {
return c.respSearchUser, nil
}

func (c *mockClient) GetGroups() ([]byte, error) {
return c.respGetGroups, nil
}

func (c *mockClient) GetQuery(id int) ([]byte, error) {
return c.respGetQuery, nil
}

func (c *mockClient) GetDataSource(id int) ([]byte, error) {
return c.respDataSource, nil
}

func (c *mockClient) GetDashboard(id string) ([]byte, error) {
return c.respDashboard, nil
}

func (c *mockClient) AddMember(groupID, userID int) ([]byte, error) {
return c.respAddMember, nil
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ require (
github.com/spf13/cobra v1.1.3
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
golang.org/x/sys v0.0.0-20210314195730-07df6a141424 // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
Expand Down

0 comments on commit edb470d

Please sign in to comment.