Skip to content

Commit

Permalink
test: add test case for maxcompute identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
deryrahman committed Oct 22, 2024
1 parent a76da38 commit 2107d4b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
6 changes: 5 additions & 1 deletion plugin/upstream_identifier/maxcompute_upstream_identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,61 @@
package upstreamidentifier_test

// TODO: Implement test
import (
"context"
"testing"

upstreamidentifier "github.com/goto/optimus/plugin/upstream_identifier"

Check failure on line 7 in plugin/upstream_identifier/maxcompute_upstream_identifier_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/goto/optimus) (gci)
"github.com/goto/salt/log"
"github.com/stretchr/testify/assert"

Check failure on line 9 in plugin/upstream_identifier/maxcompute_upstream_identifier_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/goto/optimus) (gci)
)

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())
})
}

0 comments on commit 2107d4b

Please sign in to comment.