Skip to content

Commit

Permalink
refactor: internal/helper/image_unit_test : use table driven tests (d…
Browse files Browse the repository at this point in the history
…yrector-io#838)

* refactor internal/helper/image_unit_test : use table driven tests

Unit tests were spread across various functions, but most of them
can be organized into table driven test cases.

* refactor internal/helper/image/image_unit_test.go extract utility function

Promot NewPTR as a general helper function .

* refactor: internal/helper/image_unit_test : add name to table tests

* feat: add tests to pointer module

---------

Co-authored-by: Nandor Magyar <[email protected]>
Co-authored-by: Levente Orban <[email protected]>
  • Loading branch information
3 people authored and chandhuDev committed Nov 7, 2023
1 parent ad9461e commit 800cfad
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 25 deletions.
69 changes: 44 additions & 25 deletions golang/internal/helper/image/image_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,57 @@ import (
"github.com/stretchr/testify/assert"

imageHelper "github.com/dyrector-io/dyrectorio/golang/internal/helper/image"
"github.com/dyrector-io/dyrectorio/golang/internal/pointer"
"github.com/dyrector-io/dyrectorio/protobuf/go/agent"
)

func TestRegistryUrl(t *testing.T) {
auth := &imageHelper.RegistryAuth{
URL: "test",
}

url := imageHelper.GetRegistryURL(nil, auth)
assert.Equal(t, url, "test")
type RegistryTestCase struct {
Name string
Registry *string
RegistryUrl *string
ExpectedUrl string
}

func TestRegistryUrlPriority(t *testing.T) {
registry := "other"
auth := &imageHelper.RegistryAuth{
URL: "test",
func TestRegistryWithTable(t *testing.T) {
testCases := []RegistryTestCase{
{
Name: "Test registry url",
Registry: pointer.NewPTR[string](""),
RegistryUrl: pointer.NewPTR[string]("test"),
ExpectedUrl: "test",
},
{
Name: "Test registry url priority",
Registry: pointer.NewPTR[string]("other"),
RegistryUrl: pointer.NewPTR[string]("test"),
ExpectedUrl: "test",
},
{
Name: "Test registry url empty",
Registry: nil,
RegistryUrl: nil,
ExpectedUrl: "",
},
{
Name: "Test registry url registry",
Registry: pointer.NewPTR[string]("other"),
RegistryUrl: nil,
ExpectedUrl: "other",
},
}

url := imageHelper.GetRegistryURL(&registry, auth)
assert.Equal(t, url, "test")
}

func TestRegistryUrlRegistry(t *testing.T) {
registry := "other"

url := imageHelper.GetRegistryURL(&registry, nil)
assert.Equal(t, url, "other")
}

func TestRegistryUrlEmpty(t *testing.T) {
url := imageHelper.GetRegistryURL(nil, nil)
assert.Equal(t, url, "")
for _, tC := range testCases {
t.Run(tC.Name, func(t *testing.T) {
if tC.RegistryUrl == nil {
url := imageHelper.GetRegistryURL(tC.Registry, nil)
assert.Equal(t, url, tC.ExpectedUrl)
} else {
auth := &imageHelper.RegistryAuth{URL: *tC.RegistryUrl}
url := imageHelper.GetRegistryURL(tC.Registry, auth)
assert.Equal(t, url, tC.ExpectedUrl)
}
})
}
}

func TestProtoRegistryUrl(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions golang/internal/pointer/pointer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package pointer

// Creates a new pointer of type T
func NewPTR[T any](value T) *T {
return &value
}
15 changes: 15 additions & 0 deletions golang/internal/pointer/pointer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package pointer

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNewPTR(t *testing.T) {
sPtr := NewPTR[string]("")
assert.Equal(t, *sPtr, "")

iPtr := NewPTR[int](120)
assert.Equal(t, *iPtr, 120)
}

0 comments on commit 800cfad

Please sign in to comment.