From 2107d4baba8e580271c4feaaa309d53cc299b586 Mon Sep 17 00:00:00 2001 From: Dery Rahman Ahaddienata Date: Tue, 22 Oct 2024 14:18:08 +0700 Subject: [PATCH] test: add test case for maxcompute identifier --- .../maxcompute_upstream_identifier.go | 6 +- .../maxcompute_upstream_identifier_test.go | 60 ++++++++++++++++++- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/plugin/upstream_identifier/maxcompute_upstream_identifier.go b/plugin/upstream_identifier/maxcompute_upstream_identifier.go index 318810920b..10664a1021 100644 --- a/plugin/upstream_identifier/maxcompute_upstream_identifier.go +++ b/plugin/upstream_identifier/maxcompute_upstream_identifier.go @@ -63,7 +63,11 @@ func (g MaxcomputeUpstreamIdentifier) identifyResources(query string) []resource resources := g.parserFunc(query) resourceURNs := make([]resource.URN, len(resources)) for i, r := range resources { - resourceURN, _ := resource.NewURN("maxcompute", r) // TODO: use dedicated function new resource from string + resourceURN, err := resource.ParseURN(r) + if err != nil { + g.logger.Error("error when parsing resource urn %s", r) + continue + } resourceURNs[i] = resourceURN } return resourceURNs diff --git a/plugin/upstream_identifier/maxcompute_upstream_identifier_test.go b/plugin/upstream_identifier/maxcompute_upstream_identifier_test.go index 52a7ba24e2..e80b40f804 100644 --- a/plugin/upstream_identifier/maxcompute_upstream_identifier_test.go +++ b/plugin/upstream_identifier/maxcompute_upstream_identifier_test.go @@ -1,3 +1,61 @@ package upstreamidentifier_test -// TODO: Implement test +import ( + "context" + "testing" + + upstreamidentifier "github.com/goto/optimus/plugin/upstream_identifier" + "github.com/goto/salt/log" + "github.com/stretchr/testify/assert" +) + +func TestNewMaxcomputeUpstreamIdentifier(t *testing.T) { + logger := log.NewNoop() + parserFunc := func(string) []string { return nil } + evaluatorFunc := func(map[string]string) string { return "" } + t.Run("return error when logger is nil", func(t *testing.T) { + upstreamIdentifier, err := upstreamidentifier.NewMaxcomputeUpstreamIdentifier(nil, parserFunc, evaluatorFunc) + assert.Error(t, err) + assert.Nil(t, upstreamIdentifier) + }) + t.Run("return error when parserFunc is nil", func(t *testing.T) { + upstreamIdentifier, err := upstreamidentifier.NewMaxcomputeUpstreamIdentifier(logger, nil, evaluatorFunc) + assert.Error(t, err) + assert.Nil(t, upstreamIdentifier) + }) + t.Run("return error when no evaluators", func(t *testing.T) { + upstreamIdentifier, err := upstreamidentifier.NewMaxcomputeUpstreamIdentifier(logger, parserFunc) + assert.Error(t, err) + assert.Nil(t, upstreamIdentifier) + }) + t.Run("return error when evaluatorFuncs is nil", func(t *testing.T) { + upstreamIdentifier, err := upstreamidentifier.NewMaxcomputeUpstreamIdentifier(logger, parserFunc, nil) + assert.Error(t, err) + assert.Nil(t, upstreamIdentifier) + }) + t.Run("return success", func(t *testing.T) { + upstreamIdentifier, err := upstreamidentifier.NewMaxcomputeUpstreamIdentifier(logger, parserFunc, evaluatorFunc) + assert.NoError(t, err) + assert.NotNil(t, upstreamIdentifier) + }) +} + +func TestMaxcomputeUpstreamIdentifier_IdentifyResources(t *testing.T) { + ctx := context.Background() + logger := log.NewNoop() + assets := map[string]string{ + "./query.sql": "select 1 from project1.schema1.name1", + } + // TODO: adding failure test cases + t.Run("return success", func(t *testing.T) { + parserFunc := func(string) []string { return []string{"project1.schema1.name1"} } + evaluatorFunc := func(map[string]string) string { return "./query.sql" } + upstreamIdentifier, err := upstreamidentifier.NewMaxcomputeUpstreamIdentifier(logger, parserFunc, evaluatorFunc) + assert.NoError(t, err) + assert.NotNil(t, upstreamIdentifier) + resourceURNs, err := upstreamIdentifier.IdentifyResources(ctx, assets) + assert.NoError(t, err) + assert.Len(t, resourceURNs, 1) + assert.Equal(t, "maxcompute://project1.schema1.name1", resourceURNs[0].String()) + }) +}