From b5239d49eff8300d5efac662e057f3ebb1a204b5 Mon Sep 17 00:00:00 2001 From: Pathe SENE Date: Tue, 3 Oct 2023 01:09:16 +0000 Subject: [PATCH 1/4] 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. --- .../internal/helper/image/image_unit_test.go | 63 ++++++++++++------- 1 file changed, 39 insertions(+), 24 deletions(-) diff --git a/golang/internal/helper/image/image_unit_test.go b/golang/internal/helper/image/image_unit_test.go index cff282be6..4a9421211 100644 --- a/golang/internal/helper/image/image_unit_test.go +++ b/golang/internal/helper/image/image_unit_test.go @@ -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(®istry, 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(®istry, 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) { From 32e335f766f7d670fd9f31eb9cfbb6b7e4d68a60 Mon Sep 17 00:00:00 2001 From: Pathe SENE Date: Tue, 3 Oct 2023 01:09:16 +0000 Subject: [PATCH 2/4] refactor internal/helper/image/image_unit_test.go extract utility function Promot NewPTR as a general helper function . --- golang/internal/helper/image/image_unit_test.go | 15 ++++++--------- golang/internal/pointer/pointer.go | 8 ++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 golang/internal/pointer/pointer.go diff --git a/golang/internal/helper/image/image_unit_test.go b/golang/internal/helper/image/image_unit_test.go index 4a9421211..41ed3cb50 100644 --- a/golang/internal/helper/image/image_unit_test.go +++ b/golang/internal/helper/image/image_unit_test.go @@ -10,6 +10,7 @@ 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" ) @@ -19,20 +20,16 @@ type RegistryTestCase struct { ExpectedUrl string } -func NewPTR[T any](value T) *T { - return &value -} - func TestRegistryWithTable(t *testing.T) { testCases := []RegistryTestCase{ { - Registry: NewPTR[string](""), - RegistryUrl: NewPTR[string]("test"), + Registry: pointer.NewPTR[string](""), + RegistryUrl: pointer.NewPTR[string]("test"), ExpectedUrl: "test", }, { - Registry: NewPTR[string]("other"), - RegistryUrl: NewPTR[string]("test"), + Registry: pointer.NewPTR[string]("other"), + RegistryUrl: pointer.NewPTR[string]("test"), ExpectedUrl: "test", }, { @@ -41,7 +38,7 @@ func TestRegistryWithTable(t *testing.T) { ExpectedUrl: "", }, { - Registry: NewPTR[string]("other"), + Registry: pointer.NewPTR[string]("other"), RegistryUrl: nil, ExpectedUrl: "other", }, diff --git a/golang/internal/pointer/pointer.go b/golang/internal/pointer/pointer.go new file mode 100644 index 000000000..b4c75f365 --- /dev/null +++ b/golang/internal/pointer/pointer.go @@ -0,0 +1,8 @@ +package pointer + +/* + * Creates a new pointer of type T + */ +func NewPTR[T any](value T) *T { + return &value +} From 732dcd0479920e287e2616e5e67b2842d68d5d7e Mon Sep 17 00:00:00 2001 From: Pathe SENE Date: Tue, 3 Oct 2023 01:09:16 +0000 Subject: [PATCH 3/4] refactor: internal/helper/image_unit_test : add name to table tests --- .../internal/helper/image/image_unit_test.go | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/golang/internal/helper/image/image_unit_test.go b/golang/internal/helper/image/image_unit_test.go index 41ed3cb50..8894f9fe0 100644 --- a/golang/internal/helper/image/image_unit_test.go +++ b/golang/internal/helper/image/image_unit_test.go @@ -15,6 +15,7 @@ import ( ) type RegistryTestCase struct { + Name string Registry *string RegistryUrl *string ExpectedUrl string @@ -23,21 +24,25 @@ type RegistryTestCase struct { 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", @@ -45,14 +50,16 @@ func TestRegistryWithTable(t *testing.T) { } 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) - } + 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) + } + }) } } From 97caf6e69b43a57b0ffb6780344171f0705f4a7f Mon Sep 17 00:00:00 2001 From: Pathe SENE Date: Fri, 6 Oct 2023 20:12:29 +0000 Subject: [PATCH 4/4] feat: add tests to pointer module --- golang/internal/pointer/pointer.go | 4 +--- golang/internal/pointer/pointer_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 golang/internal/pointer/pointer_test.go diff --git a/golang/internal/pointer/pointer.go b/golang/internal/pointer/pointer.go index b4c75f365..25cfe4c0d 100644 --- a/golang/internal/pointer/pointer.go +++ b/golang/internal/pointer/pointer.go @@ -1,8 +1,6 @@ package pointer -/* - * Creates a new pointer of type T - */ +// Creates a new pointer of type T func NewPTR[T any](value T) *T { return &value } diff --git a/golang/internal/pointer/pointer_test.go b/golang/internal/pointer/pointer_test.go new file mode 100644 index 000000000..50c96a085 --- /dev/null +++ b/golang/internal/pointer/pointer_test.go @@ -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) +}