Skip to content

Commit

Permalink
increase code coverage substantially (#11)
Browse files Browse the repository at this point in the history
* added tests around client
* use an interface for client for testability reasons
* added coverage
  • Loading branch information
foundev authored Mar 30, 2021
1 parent 981be31 commit 60498ef
Show file tree
Hide file tree
Showing 33 changed files with 1,223 additions and 221 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Apache 2.0 licensed Astra Cloud Management CLI
[![Go Report Card](https://goreportcard.com/badge/github.com/rsds143/astra-cli)](https://goreportcard.com/report/github.com/rsds143/astra-cli)
[![GitHub go.mod Go version of a Go module](https://img.shields.io/github/go-mod/go-version/rsds143/astra-cli)](https://img.shields.io/github/go-mod/go-version/rsds143/astra-cli)
[![Latest Version](https://img.shields.io/github/v/release/rsds143/astra-cli?include_prereleases)](https://github.com/rsds143/astra-cli/releases)
<a href='https://github.com/jpoles1/gopherbadger' target='_blank'>![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-63%25-brightgreen.svg?longCache=true&style=flat)</a>
<a href='https://github.com/jpoles1/gopherbadger' target='_blank'>![gopherbadger-tag-do-not-edit](https://img.shields.io/badge/Go%20Coverage-81%25-brightgreen.svg?longCache=true&style=flat)</a>

## status

Expand Down
11 changes: 9 additions & 2 deletions cmd/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,15 @@ var dbCmd = &cobra.Command{
Short: "Shows all the db commands",
Long: `Shows all other db commands. Create, Delete, Get information on your databases`,
Run: func(cobraCmd *cobra.Command, args []string) {
if err := cobraCmd.Usage(); err != nil {
fmt.Fprintf(os.Stderr, "Unable to print usage with error %v\n", err)
if err := executeDB(cobraCmd.Usage); err != nil {
os.Exit(1)
}
},
}

func executeDB(usage func() error) error {
if err := usage(); err != nil {
return fmt.Errorf("warn unable to show usage %v", err)
}
return nil
}
16 changes: 7 additions & 9 deletions cmd/db/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,22 @@ func init() {
var CreateCmd = &cobra.Command{
Use: "create",
Short: "creates a database by id",
Long: ``,
Long: `creates a database by id`,
Run: func(cobraCmd *cobra.Command, args []string) {
creds := &pkg.Creds{}
client, err := creds.Login()
if err != nil {
fmt.Fprintf(os.Stderr, "unable to login with error %v\n", err)
os.Exit(1)
}

err = executeCreate(client)
err := executeCreate(creds.Login)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
},
}

func executeCreate(client *astraops.AuthenticatedClient) error {
func executeCreate(makeClient func() (pkg.Client, error)) error {
client, err := makeClient()
if err != nil {
return fmt.Errorf("unable to login with error %v", err)
}
capacity := int32(createDbCapacityUnit)
createDb := astraops.CreateDb{
Name: createDbName,
Expand Down
186 changes: 183 additions & 3 deletions cmd/db/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,188 @@
//Package db is where the Astra DB commands are
package db

import "testing"
import (
"fmt"
"testing"

func TestCreate(t *testing.T) {
t.Skip("ignore")
"github.com/rsds143/astra-cli/pkg"
tests "github.com/rsds143/astra-cli/pkg/tests"
"github.com/rsds143/astra-devops-sdk-go/astraops"
)

func TestCreateGetsId(t *testing.T) {
expectedID := "abcd"
//setting package variables by hand, there be dragons
mockClient := &tests.MockClient{
Databases: []astraops.Database{
{
ID: expectedID,
},
},
}
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}

if len(mockClient.Calls()) != 1 {
t.Fatalf("expected 1 call but was %v", len(mockClient.Calls()))
}
}
func TestCreateLoginFails(t *testing.T) {
//setting package variables by hand, there be dragons
mockClient := &tests.MockClient{}
err := executeCreate(func() (pkg.Client, error) {
return mockClient, fmt.Errorf("service down")
})
if err == nil {
t.Fatal("expected error")
}

expected := "unable to login with error service down"
if err.Error() != expected {
t.Errorf("expected '%v' but was '%v'", expected, err.Error())
}
if len(mockClient.Calls()) != 0 {
t.Fatalf("expected 0 call but was %v", len(mockClient.Calls()))
}
}

func TestCreateFails(t *testing.T) {
//setting package variables by hand, there be dragons
mockClient := &tests.MockClient{
ErrorQueue: []error{fmt.Errorf("service down")},
}
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err == nil {
t.Fatal("expected error")
}

if len(mockClient.Calls()) != 1 {
t.Fatalf("expected 1 call but was %v", len(mockClient.Calls()))
}
}

func TestCreateSetsName(t *testing.T) {
mockClient := &tests.MockClient{}
createDbName = "mydb"
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.Name != createDbName {
t.Errorf("expected '%v' but was '%v'", arg0.Name, createDbName)
}
}

func TestCreateSetsKeyspace(t *testing.T) {
mockClient := &tests.MockClient{}
createDbKeyspace = "myKeyspace"
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.Keyspace != createDbKeyspace {
t.Errorf("expected '%v' but was '%v'", arg0.Keyspace, createDbKeyspace)
}
}

func TestCreateSetsCapacityUnit(t *testing.T) {
mockClient := &tests.MockClient{}
createDbCapacityUnit = 10000
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.CapacityUnits != int32(createDbCapacityUnit) {
t.Errorf("expected '%v' but was '%v'", arg0.CapacityUnits, createDbCapacityUnit)
}
}

func TestCreateSetsRegion(t *testing.T) {
mockClient := &tests.MockClient{}
createDbRegion = "EU-West1"
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.Region != createDbRegion {
t.Errorf("expected '%v' but was '%v'", arg0.Region, createDbRegion)
}
}

func TestCreateSetsUser(t *testing.T) {
mockClient := &tests.MockClient{}
createDbUser = "[email protected]"
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.User != createDbUser {
t.Errorf("expected '%v' but was '%v'", arg0.User, createDbUser)
}
}

func TestCreateSetsPass(t *testing.T) {
mockClient := &tests.MockClient{}
createDbUser = "afdfdf"
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.Password != createDbPassword {
t.Errorf("expected '%v' but was '%v'", arg0.Password, createDbPassword)
}
}

func TestCreateSetsTier(t *testing.T) {
mockClient := &tests.MockClient{}
createDbTier = "afdfdf"
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.Tier != createDbTier {
t.Errorf("expected '%v' but was '%v'", arg0.Tier, createDbTier)
}
}

func TestCreateSetsProvider(t *testing.T) {
mockClient := &tests.MockClient{}
createDbCloudProvider = "ryanscloud"
err := executeCreate(func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
arg0 := mockClient.Call(0).(astraops.CreateDb)
if arg0.CloudProvider != createDbCloudProvider {
t.Errorf("expected '%v' but was '%v'", arg0.CloudProvider, createDbCloudProvider)
}
}
8 changes: 4 additions & 4 deletions cmd/db/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var DeleteCmd = &cobra.Command{
Long: `deletes a database from your Astra account by ID`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
msg, err := execteDelete(args)
creds := &pkg.Creds{}
msg, err := executeDelete(args, creds.Login)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand All @@ -39,9 +40,8 @@ var DeleteCmd = &cobra.Command{
},
}

func execteDelete(args []string) (string, error) {
creds := &pkg.Creds{}
client, err := creds.Login()
func executeDelete(args []string, makeClient func() (pkg.Client, error)) (string, error) {
client, err := makeClient()
if err != nil {
return "", fmt.Errorf("unable to login with error '%v'", err)
}
Expand Down
72 changes: 70 additions & 2 deletions cmd/db/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,76 @@
//Package db is where the Astra DB commands are
package db

import "testing"
import (
"fmt"
"testing"

"github.com/rsds143/astra-cli/pkg"
tests "github.com/rsds143/astra-cli/pkg/tests"
)

func TestDelete(t *testing.T) {
t.Skip("ignore")
mockClient := &tests.MockClient{}
id := "123"
msg, err := executeDelete([]string{id}, func() (pkg.Client, error) {
return mockClient, nil
})
if err != nil {
t.Fatalf("unexpected error '%v'", err)
}
if len(mockClient.Calls()) != 1 {
t.Fatalf("expected 1 call but was %v", len(mockClient.Calls()))
}
if id != mockClient.Call(0) {
t.Errorf("expected '%v' but was '%v'", id, mockClient.Call(0))
}
expected := "database 123 deleted"
if expected != msg {
t.Errorf("expected '%v' but was '%v'", expected, msg)
}
}

func TestDeleteLoginError(t *testing.T) {
mockClient := &tests.MockClient{}
id := "123"
msg, err := executeDelete([]string{id}, func() (pkg.Client, error) {
return mockClient, fmt.Errorf("unable to login")
})
if err == nil {
t.Fatalf("should be returning an error and is not")
}
expectedError := "unable to login with error 'unable to login'"
if err.Error() != expectedError {
t.Errorf("expected '%v' but was '%v'", expectedError, err)
}
if len(mockClient.Calls()) != 0 {
t.Fatalf("expected no calls but was %v", len(mockClient.Calls()))
}

if "" != msg {
t.Errorf("expected emtpy but was '%v'", msg)
}
}

func TestDeleteError(t *testing.T) {
mockClient := &tests.MockClient{
ErrorQueue: []error{fmt.Errorf("timeout error")},
}
id := "123"
msg, err := executeDelete([]string{id}, func() (pkg.Client, error) {
return mockClient, nil
})
if err == nil {
t.Fatal("expected error but none came")
}
if len(mockClient.Calls()) != 1 {
t.Fatalf("expected 1 call but was %v", len(mockClient.Calls()))
}
if id != mockClient.Call(0) {
t.Errorf("expected '%v' but was '%v'", id, mockClient.Call(0))
}
expected := ""
if expected != msg {
t.Errorf("expected '%v' but was '%v'", expected, msg)
}
}
Loading

0 comments on commit 60498ef

Please sign in to comment.