diff --git a/cmd/db/get_test.go b/cmd/db/get_test.go index 55db120..bf46c45 100644 --- a/cmd/db/get_test.go +++ b/cmd/db/get_test.go @@ -74,15 +74,15 @@ func TestGetFindDbFails(t *testing.T) { func TestGetFailedLogin(t *testing.T) { // setting package variables by hand, there be dragons mockClient := &tests.MockClient{} - mockClient.ErrorQueue = []error{errors.New("no db")} + mockClient.ErrorQueue = []error{} id := "12345" msg, err := executeGet([]string{id}, func() (pkg.Client, error) { - return mockClient, nil + return mockClient, errors.New("no db") }) if err == nil { t.Fatalf("expected error") } - expectedErr := "unable to get '12345' with error no db" + expectedErr := tests.LoginError if err.Error() != expectedErr { t.Errorf("expected '%v' but was '%v'", expectedErr, err) } diff --git a/cmd/db/list_test.go b/cmd/db/list_test.go index d85852f..4c86430 100644 --- a/cmd/db/list_test.go +++ b/cmd/db/list_test.go @@ -17,6 +17,7 @@ package db import ( "encoding/json" + "errors" "strings" "testing" @@ -105,3 +106,44 @@ func TestListInvalidFmt(t *testing.T) { t.Errorf("expected '%v' but was '%v'", expected, err.Error()) } } + +func TestListFails(t *testing.T) { + getFmt = pkg.JSONFormat + dbs := []astraops.Database{} + jsonTxt, err := executeList(func() (pkg.Client, error) { + return &tests.MockClient{ + Databases: dbs, + ErrorQueue: []error{errors.New("cant find db")}, + }, nil + }) + if err == nil { + t.Fatal("expected error") + } + expected := "unable to get list of dbs with error 'cant find db'" + if err.Error() != expected { + t.Errorf("expected '%v' but was '%v'", expected, err.Error()) + } + if jsonTxt != "" { + t.Errorf("expected '%v' but was '%v'", "", jsonTxt) + } +} + +func TestListFailedLogin(t *testing.T) { + // setting package variables by hand, there be dragons + mockClient := &tests.MockClient{} + mockClient.ErrorQueue = []error{errors.New("no db")} + msg, err := executeList(func() (pkg.Client, error) { + return mockClient, nil + }) + if err == nil { + t.Fatalf("expected error") + } + expectedErr := "unable to get list of dbs with error 'no db'" + if err.Error() != expectedErr { + t.Errorf("expected '%v' but was '%v'", expectedErr, err) + } + expected := "" + if msg != expected { + t.Errorf("expected '%v' but was '%v'", expected, msg) + } +} diff --git a/cmd/db_test.go b/cmd/db_test.go index 9ec4dfc..6e4f85c 100644 --- a/cmd/db_test.go +++ b/cmd/db_test.go @@ -16,7 +16,9 @@ package cmd import ( + "bytes" "errors" + "io/ioutil" "testing" ) @@ -33,3 +35,42 @@ func TestDBUsageFails(t *testing.T) { t.Errorf("expected '%v' but was '%v'", expected, err.Error()) } } + +func TestDBUsage(t *testing.T) { + fails := func() error { + return nil + } + err := executeDB(fails) + if err != nil { + t.Fatalf("unexpected eror %v", err) + } +} + +func TestDBShowHelp(t *testing.T) { + clientJSON = "" + authToken = "" + clientName = "" + clientSecret = "" + clientID = "" + originalOut := RootCmd.OutOrStderr() + defer func() { + RootCmd.SetOut(originalOut) + RootCmd.SetArgs([]string{}) + }() + b := bytes.NewBufferString("") + RootCmd.SetOut(b) + RootCmd.SetArgs([]string{"db"}) + err := RootCmd.Execute() + if err != nil { + t.Errorf("unexpected error '%v'", err) + } + out, err := ioutil.ReadAll(b) + if err != nil { + t.Fatal(err) + } + expected := dbCmd.UsageString() + + if string(out) != expected { + t.Errorf("expected\n'%q'\nbut was\n'%q'", expected, string(out)) + } +} diff --git a/pkg/tests/client.go b/pkg/tests/client.go index 60ac134..7773d32 100644 --- a/pkg/tests/client.go +++ b/pkg/tests/client.go @@ -17,6 +17,9 @@ package test import "github.com/rsds143/astra-devops-sdk-go/astraops" +// LoginError is a pretty common error message +const LoginError = "unable to login with error no db" + // MockClient is used for testing type MockClient struct { ErrorQueue []error