diff --git a/core/asset/type.go b/core/asset/type.go index b6bf378b..f1147416 100644 --- a/core/asset/type.go +++ b/core/asset/type.go @@ -29,19 +29,7 @@ const ( typeMetric Type = "metric" ) -var supportedTypes = []Type{ - typeTable, - typeJob, - typeDashboard, - typeTopic, - typeFeatureTable, - typeApplication, - typeModel, - typeQuery, - typeMetric, -} - -var isTypeSupported = map[Type]bool{ +var supportedTypeMap = map[Type]bool{ typeTable: true, typeJob: true, typeDashboard: true, @@ -54,8 +42,10 @@ var isTypeSupported = map[Type]bool{ } func GetSupportedTypes() []Type { - output := make([]Type, 0, len(supportedTypes)) - output = append(output, supportedTypes...) + output := make([]Type, 0, len(supportedTypeMap)) + for _type := range supportedTypeMap { + output = append(output, _type) + } return output } @@ -67,9 +57,8 @@ func RegisterSupportedTypes(types ...Type) error { } for _, t := range types { - if supported := isTypeSupported[t]; !supported { - supportedTypes = append(supportedTypes, t) - isTypeSupported[t] = true + if supported := supportedTypeMap[t]; !supported { + supportedTypeMap[t] = true } } @@ -86,7 +75,7 @@ func (t Type) String() string { // IsValid will validate whether the typename is valid or not func (t Type) IsValid() bool { - return isTypeSupported[t] + return supportedTypeMap[t] } func (t Type) validate() error { diff --git a/core/asset/type_test.go b/core/asset/type_test.go index fba92b69..4f796656 100644 --- a/core/asset/type_test.go +++ b/core/asset/type_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/goto/compass/core/asset" + "github.com/goto/compass/internal/testutils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -105,7 +106,7 @@ func TestGetSupportedTypes(t *testing.T) { actualTypes := asset.GetSupportedTypes() - assert.EqualValues(t, expectedTypes, actualTypes) + assert.True(t, testutils.AreSlicesEqualIgnoringOrder(expectedTypes, actualTypes, compareTypes)) }) } @@ -174,7 +175,7 @@ func TestRegisterSupportedTypes(t *testing.T) { require.Error(t, actualError) actualTypes := asset.GetSupportedTypes() - assert.EqualValues(t, expectedTypes, actualTypes) + assert.True(t, testutils.AreSlicesEqualIgnoringOrder(expectedTypes, actualTypes, compareTypes)) }) t.Run("should update supported types if no error is returned", func(t *testing.T) { @@ -201,6 +202,16 @@ func TestRegisterSupportedTypes(t *testing.T) { require.NoError(t, actualError) actualTypes := asset.GetSupportedTypes() - assert.EqualValues(t, expectedTypes, actualTypes) + assert.True(t, testutils.AreSlicesEqualIgnoringOrder(expectedTypes, actualTypes, compareTypes)) }) } + +func compareTypes(left, right asset.Type) int { + if left < right { + return -1 + } + if right > left { + return 1 + } + return 0 +} diff --git a/internal/server/v1beta1/type_test.go b/internal/server/v1beta1/type_test.go index 8e2a6d51..56d6a6ef 100644 --- a/internal/server/v1beta1/type_test.go +++ b/internal/server/v1beta1/type_test.go @@ -6,16 +6,15 @@ import ( "fmt" "testing" - "github.com/google/go-cmp/cmp" "github.com/google/uuid" "github.com/goto/compass/core/asset" "github.com/goto/compass/core/user" "github.com/goto/compass/internal/server/v1beta1/mocks" + "github.com/goto/compass/internal/testutils" compassv1beta1 "github.com/goto/compass/proto/gotocompany/compass/v1beta1" "github.com/goto/salt/log" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "google.golang.org/protobuf/testing/protocmp" ) func TestGetTypes(t *testing.T) { @@ -56,6 +55,16 @@ func TestGetTypes(t *testing.T) { }, nil) }, PostCheck: func(resp *compassv1beta1.GetAllTypesResponse) error { + compare := func(left, right *compassv1beta1.Type) int { + if left.Name == right.Name && left.Count == right.Count { + return 0 + } + if left.Name < right.Name { + return -1 + } + return 1 + } + expected := &compassv1beta1.GetAllTypesResponse{ Data: []*compassv1beta1.Type{ { @@ -97,7 +106,7 @@ func TestGetTypes(t *testing.T) { }, } - if diff := cmp.Diff(resp, expected, protocmp.Transform()); diff != "" { + if !testutils.AreSlicesEqualIgnoringOrder(resp.Data, expected.Data, compare) { return fmt.Errorf("expected response to be %+v, was %+v", expected, resp) } return nil diff --git a/internal/testutils/utils.go b/internal/testutils/utils.go index 1a10eb2f..1b29d75d 100644 --- a/internal/testutils/utils.go +++ b/internal/testutils/utils.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "sort" "testing" "github.com/google/go-cmp/cmp" @@ -43,3 +44,30 @@ type ArgMatcher interface{ Matches(interface{}) bool } func OfTypeContext() ArgMatcher { return mock.MatchedBy(func(ctx context.Context) bool { return ctx != nil }) } + +func AreSlicesEqualIgnoringOrder[T any](s1, s2 []T, compare func(l, r T) int) bool { + if len(s1) != len(s2) { + return false + } + + s1Duplicate := make([]T, len(s1)) + copy(s1Duplicate, s1) + s2Duplicate := make([]T, len(s2)) + copy(s2Duplicate, s2) + + sort.Slice(s1Duplicate, func(i, j int) bool { + return compare(s1Duplicate[i], s1Duplicate[j]) < 0 + }) + + sort.Slice(s2Duplicate, func(i, j int) bool { + return compare(s2Duplicate[i], s2Duplicate[j]) < 0 + }) + + for i := 0; i < len(s1Duplicate); i++ { + if compare(s1Duplicate[i], s2Duplicate[i]) != 0 { + return false + } + } + + return true +}