Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: internal/helper/image_unit_test : use table driven tests #838

Merged
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 {
nandor-magyar marked this conversation as resolved.
Show resolved Hide resolved
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
8 changes: 8 additions & 0 deletions golang/internal/pointer/pointer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package pointer

/*
* Creates a new pointer of type T
*/
nandor-magyar marked this conversation as resolved.
Show resolved Hide resolved
func NewPTR[T any](value T) *T {
return &value
}