Skip to content

Commit

Permalink
more testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Svihla committed Apr 1, 2021
1 parent 78a501d commit 69423c5
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cmd/db/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
42 changes: 42 additions & 0 deletions cmd/db/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package db

import (
"encoding/json"
"errors"
"strings"
"testing"

Expand Down Expand Up @@ -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)
}
}
41 changes: 41 additions & 0 deletions cmd/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package cmd

import (
"bytes"
"errors"
"io/ioutil"
"testing"
)

Expand All @@ -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))
}
}
3 changes: 3 additions & 0 deletions pkg/tests/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 69423c5

Please sign in to comment.