github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/romulus/setbudget/setbudget_test.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package setbudget_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/setbudget" 14 coretesting "github.com/juju/juju/testing" 15 ) 16 17 var _ = gc.Suite(&setBudgetSuite{}) 18 19 type setBudgetSuite struct { 20 coretesting.FakeJujuXDGDataHomeSuite 21 stub *testing.Stub 22 mockAPI *mockapi 23 } 24 25 func (s *setBudgetSuite) SetUpTest(c *gc.C) { 26 s.FakeJujuXDGDataHomeSuite.SetUpTest(c) 27 s.stub = &testing.Stub{} 28 s.mockAPI = newMockAPI(s.stub) 29 s.PatchValue(setbudget.NewAPIClient, setbudget.APIClientFnc(s.mockAPI)) 30 } 31 32 func (s *setBudgetSuite) TestSetBudget(c *gc.C) { 33 s.mockAPI.resp = "name budget set to 5" 34 set := setbudget.NewSetBudgetCommand() 35 ctx, err := cmdtesting.RunCommand(c, set, "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, "SetBudget", "name", "5") 39 } 40 41 func (s *setBudgetSuite) TestSetBudgetAPIError(c *gc.C) { 42 s.stub.SetErrors(errors.New("something failed")) 43 set := setbudget.NewSetBudgetCommand() 44 _, err := cmdtesting.RunCommand(c, set, "name", "5") 45 c.Assert(err, gc.ErrorMatches, "failed to set the budget: something failed") 46 s.mockAPI.CheckCall(c, 0, "SetBudget", "name", "5") 47 } 48 49 func (s *setBudgetSuite) TestSetBudgetErrors(c *gc.C) { 50 tests := []struct { 51 about string 52 args []string 53 expectedError string 54 }{ 55 { 56 about: "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 s.stub.SetErrors(errors.New(test.expectedError)) 74 defer s.mockAPI.ResetCalls() 75 set := setbudget.NewSetBudgetCommand() 76 _, err := cmdtesting.RunCommand(c, set, test.args...) 77 c.Assert(err, gc.ErrorMatches, test.expectedError) 78 s.mockAPI.CheckNoCalls(c) 79 } 80 } 81 82 func newMockAPI(s *testing.Stub) *mockapi { 83 return &mockapi{Stub: s} 84 } 85 86 type mockapi struct { 87 *testing.Stub 88 resp string 89 } 90 91 func (api *mockapi) SetBudget(name, value string) (string, error) { 92 api.MethodCall(api, "SetBudget", name, value) 93 return api.resp, api.NextErr() 94 }