Skip to content

Commit

Permalink
Use Must/Should error checks for fixtures
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Aufart <[email protected]>
  • Loading branch information
aufi committed Apr 5, 2023
1 parent de34238 commit 7cbe932
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 22 deletions.
25 changes: 24 additions & 1 deletion test/api/client/assertion.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
package client

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

//
// Check error and if present, fail the test case.
// Examples usage: client.Should(t, task.Create(&r))
func Should(t *testing.T, err error) {
if err != nil {
t.Errorf(err.Error())
}
}

//
// Check error and if present, fail and stop the test suite.
// Examples usage: client.Must(t, task.Create(&r))
func Must(t *testing.T, err error) {
if err != nil {
t.Fatalf(err.Error())
}
}

//
// Simple equality check working for flat types (no nested types passed by reference).
func FlatEqual(got, expected interface{}) bool {
return fmt.Sprintf("%v", got) == fmt.Sprintf("%v", expected)
}
5 changes: 4 additions & 1 deletion test/api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ func New() (client *addon.Client, err error) {
// Setup client.
client = addon.NewClient(baseUrl, "")

// Disable HTTP requests retry for network-related errors to fail quickly.
client.Retry = 0

// Login.
err = client.Post(api.AuthLoginRoot, &login)
if err != nil {
Expand All @@ -46,5 +49,5 @@ type Params map[string]interface{}
// Merge path with params.
func Path(base string, paramsMap map[string]interface{}) string {
params := addon.Params(paramsMap)
return params.Inject(base)
return addon.Path(base).Inject(params)
}
12 changes: 6 additions & 6 deletions test/api/tag/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/test/api/client"
c "github.com/konveyor/tackle2-hub/test/api/client"
)

func TestTagCRUD(t *testing.T) {
Expand All @@ -17,15 +17,15 @@ func TestTagCRUD(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}
rPath := client.Path(api.TagRoot, client.Params{api.ID: r.ID})
rPath := c.Path(api.TagRoot, c.Params{api.ID: r.ID})

// Get.
got := api.Tag{}
err = Client.Get(rPath, &got)
if err != nil {
t.Errorf(err.Error())
}
if client.FlatEqual(got, &r) {
if c.FlatEqual(got, &r) {
t.Errorf("Different response error. Got %v, expected %v", got, r)
}

Expand Down Expand Up @@ -65,20 +65,20 @@ func TestTagList(t *testing.T) {
samples := Samples()

for i := range samples {
Create(t, &samples[i])
c.Must(t, Create(&samples[i]))
}

got := []api.Tag{}
err := Client.Get(api.TagsRoot, &got)
if err != nil {
t.Errorf(err.Error())
}
if client.FlatEqual(got, samples) {
if c.FlatEqual(got, samples) {
t.Errorf("Different response error. Got %v, expected %v", got, samples)
}

for _, r := range samples {
Delete(t, &r)
c.Must(t, Delete(&r))
}
}

Expand Down
22 changes: 8 additions & 14 deletions test/api/tag/fixtures.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package tag

import (
"testing"

"github.com/konveyor/tackle2-hub/api"
"github.com/konveyor/tackle2-hub/test/api/client"
c "github.com/konveyor/tackle2-hub/test/api/client"
)

var (
// Setup Hub API client
Client = client.Client
Client = c.Client
)

//
Expand All @@ -35,18 +33,14 @@ func Samples() (samples []api.Tag) {

//
// Create a Tag.
func Create(t *testing.T, r *api.Tag) {
err := Client.Post(api.TagsRoot, &r)
if err != nil {
t.Fatalf("Create fatal error: %v", err.Error())
}
func Create(r *api.Tag) (err error) {
err = Client.Post(api.TagsRoot, &r)
return
}

//
// Delete the Tag.
func Delete(t *testing.T, r *api.Tag) {
err := Client.Delete(client.Path(api.TagRoot, client.Params{api.ID: r.ID}))
if err != nil {
t.Fatalf("Delete fatal error: %v", err.Error())
}
func Delete(r *api.Tag) (err error) {
err = Client.Delete(c.Path(api.TagRoot, c.Params{api.ID: r.ID}))
return
}

0 comments on commit 7cbe932

Please sign in to comment.