Skip to content

Commit

Permalink
refactor internal/helper/image_unit_test : use table driven tests
Browse files Browse the repository at this point in the history
Unit tests were spread across various functions, but most of them
can be organized into table driven test cases.
  • Loading branch information
PapePathe committed Oct 3, 2023
1 parent da24e25 commit b5239d4
Showing 1 changed file with 39 additions and 24 deletions.
63 changes: 39 additions & 24 deletions golang/internal/helper/image/image_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,50 @@ import (
"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 {
Registry *string
RegistryUrl *string
ExpectedUrl string
}

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

url := imageHelper.GetRegistryURL(&registry, auth)
assert.Equal(t, url, "test")
func NewPTR[T any](value T) *T {
return &value
}

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

url := imageHelper.GetRegistryURL(&registry, nil)
assert.Equal(t, url, "other")
}
func TestRegistryWithTable(t *testing.T) {
testCases := []RegistryTestCase{
{
Registry: NewPTR[string](""),
RegistryUrl: NewPTR[string]("test"),
ExpectedUrl: "test",
},
{
Registry: NewPTR[string]("other"),
RegistryUrl: NewPTR[string]("test"),
ExpectedUrl: "test",
},
{
Registry: nil,
RegistryUrl: nil,
ExpectedUrl: "",
},
{
Registry: NewPTR[string]("other"),
RegistryUrl: nil,
ExpectedUrl: "other",
},
}

func TestRegistryUrlEmpty(t *testing.T) {
url := imageHelper.GetRegistryURL(nil, nil)
assert.Equal(t, url, "")
for _, tC := range testCases {
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

0 comments on commit b5239d4

Please sign in to comment.