github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/romulus/createbudget/createbudget_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package createbudget_test 5 6 import ( 7 "github.com/juju/cmd/cmdtesting" 8 "github.com/juju/errors" 9 "github.com/juju/testing" 10 jc "github.com/juju/testing/checkers" 11 gc "gopkg.in/check.v1" 12 13 "github.com/juju/juju/cmd/juju/romulus/createbudget" 14 coretesting "github.com/juju/juju/testing" 15 ) 16 17 var _ = gc.Suite(&createBudgetSuite{}) 18 19 type createBudgetSuite struct { 20 coretesting.FakeJujuXDGDataHomeSuite 21 stub *testing.Stub 22 mockAPI *mockapi 23 } 24 25 func (s *createBudgetSuite) SetUpTest(c *gc.C) { 26 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 27 s.stub = &testing.Stub{} 28 s.mockAPI = newMockAPI(s.stub) 29 s.PatchValue(createbudget.NewAPIClient, createbudget.APIClientFnc(s.mockAPI)) 30 } 31 32 func (s *createBudgetSuite) TestCreateBudget(c *gc.C) { 33 s.mockAPI.resp = "name budget set to 5" 34 createCmd := createbudget.NewCreateBudgetCommand() 35 ctx, err := cmdtesting.RunCommand(c, createCmd, "name", "5") 36 c.Assert(err, jc.ErrorIsNil) 37 c.Assert(cmdtesting.Stdout(ctx), jc.DeepEquals, "name budget set to 5\n") 38 s.mockAPI.CheckCall(c, 0, "CreateBudget", "name", "5") 39 } 40 41 func (s *createBudgetSuite) TestCreateBudgetAPIError(c *gc.C) { 42 s.mockAPI.SetErrors(errors.New("something failed")) 43 createCmd := createbudget.NewCreateBudgetCommand() 44 _, err := cmdtesting.RunCommand(c, createCmd, "name", "5") 45 c.Assert(err, gc.ErrorMatches, "failed to create the budget: something failed") 46 s.mockAPI.CheckCall(c, 0, "CreateBudget", "name", "5") 47 } 48 49 func (s *createBudgetSuite) TestCreateBudgetErrors(c *gc.C) { 50 tests := []struct { 51 about string 52 args []string 53 expectedError string 54 }{ 55 { 56 about: "test value needs to be a number", 57 args: []string{"name", "badvalue"}, 58 expectedError: "budget value needs to be a whole number", 59 }, 60 { 61 about: "value is missing", 62 args: []string{"name"}, 63 expectedError: "name and value required", 64 }, 65 { 66 about: "no args", 67 args: []string{}, 68 expectedError: "name and value required", 69 }, 70 } 71 for i, test := range tests { 72 c.Logf("test %d: %s", i, test.about) 73 if test.expectedError != "" { 74 s.mockAPI.SetErrors(errors.New(test.expectedError)) 75 } 76 createCmd := createbudget.NewCreateBudgetCommand() 77 _, err := cmdtesting.RunCommand(c, createCmd, test.args...) 78 c.Assert(err, gc.ErrorMatches, test.expectedError) 79 s.mockAPI.CheckNoCalls(c) 80 } 81 } 82 83 func newMockAPI(s *testing.Stub) *mockapi { 84 return &mockapi{Stub: s} 85 } 86 87 type mockapi struct { 88 *testing.Stub 89 resp string 90 } 91 92 func (api *mockapi) CreateBudget(name, value string) (string, error) { 93 api.MethodCall(api, "CreateBudget", name, value) 94 return api.resp, api.NextErr() 95 }