Skip to content

Commit

Permalink
add tests for orgApp
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Mar 8, 2019
1 parent 1c20967 commit f63702d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
16 changes: 16 additions & 0 deletions mackerelclient/mock_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import mackerel "github.com/mackerelio/mackerel-client-go"
// MockClient represents a mock client of Mackerel API
type MockClient struct {
findHostsCallback func(param *mackerel.FindHostsParam) ([]*mackerel.Host, error)
getOrgCallback func() (*mackerel.Org, error)
}

// MockClientOption represents an option of mock client of Mackerel API
Expand Down Expand Up @@ -44,3 +45,18 @@ func MockFindHosts(callback func(param *mackerel.FindHostsParam) ([]*mackerel.Ho
c.findHostsCallback = callback
}
}

// GetOrg ...
func (c *MockClient) GetOrg() (*mackerel.Org, error) {
if c.getOrgCallback != nil {
return c.getOrgCallback()
}
return nil, errCallbackNotFound("GetOrg")
}

// MockGetOrg returns an option to set the callback of GetOrg
func MockGetOrg(callback func() (*mackerel.Org, error)) MockClientOption {
return func(c *MockClient) {
c.getOrgCallback = callback
}
}
45 changes: 45 additions & 0 deletions org/app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org

import (
"bytes"
"testing"

"github.com/stretchr/testify/assert"

mackerel "github.com/mackerelio/mackerel-client-go"

"github.com/mackerelio/mkr/mackerelclient"
)

func TestOrgApp_Run(t *testing.T) {
testCases := []struct {
id string
org *mackerel.Org
expected string
}{
{
id: "default",
org: &mackerel.Org{Name: "sample-org"},
expected: `{
"name": "sample-org"
}
`,
},
}
for _, tc := range testCases {
client := mackerelclient.NewMockClient(
mackerelclient.MockGetOrg(func() (*mackerel.Org, error) {
return tc.org, nil
}),
)
t.Run(tc.id, func(t *testing.T) {
out := new(bytes.Buffer)
app := &orgApp{
client: client,
outStream: out,
}
assert.NoError(t, app.run())
assert.Equal(t, tc.expected, out.String())
})
}
}

0 comments on commit f63702d

Please sign in to comment.