Skip to content

Commit afdd9f4

Browse files
authored
direct: Add a test to validate RecreateFields against the type (#3340)
## Changes The fields in RecreateFields will be validated against the corresponding type. If they are not found there, you'll get when testing that package: > resource_test.go:50: invalid field '.ingestion_definition.connection_namex' for pipelines.CreatePipeline ## Why Extra safety without having to write a dedicated test.
1 parent b6da1f9 commit afdd9f4

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

bundle/terranova/tnresources/resource.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ type ResourceSettings struct {
3838
// If any of these fields are changed, recreation (Delete + Create) is triggered.
3939
// This overrides ClassifyChanges() function (so you don't need to implement that one).
4040
// Fields are in structdiff.Change.String() format.
41-
// A couple limitations:
42-
// - Patterns like hello.*.world and hello[*].world are not supported
43-
// - We do not validate this setting. We could write a test that does it since we have ConfigType.
41+
// Limitation: patterns like hello.*.world and hello[*].world are not supported
4442
RecreateFields map[string]struct{}
4543

4644
// If resource does not set RecreateFields, RecreateAllowed, UpdateUpdatesID then

bundle/terranova/tnresources/resource_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"testing"
66

77
"github.com/databricks/cli/bundle/config/resources"
8+
"github.com/databricks/cli/libs/structdiff/structpath"
9+
"github.com/databricks/cli/libs/structwalk"
810
"github.com/databricks/databricks-sdk-go"
911
"github.com/databricks/databricks-sdk-go/service/jobs"
1012
"github.com/stretchr/testify/require"
@@ -32,3 +34,33 @@ func TestNewJobResource(t *testing.T) {
3234
r := res.(*ResourceJob)
3335
require.Equal(t, cfg.JobSettings, r.config)
3436
}
37+
38+
// validateFields uses structwalk to generate all valid field paths and checks membership.
39+
func validateFields(t *testing.T, configType reflect.Type, fields map[string]struct{}) {
40+
validPaths := make(map[string]struct{})
41+
42+
err := structwalk.WalkType(configType, func(path *structpath.PathNode, typ reflect.Type) bool {
43+
validPaths[path.String()] = struct{}{}
44+
return true // continue walking
45+
})
46+
require.NoError(t, err)
47+
48+
for fieldPath := range fields {
49+
if _, exists := validPaths[fieldPath]; !exists {
50+
t.Errorf("invalid field '%s' for %s", fieldPath, configType)
51+
}
52+
}
53+
}
54+
55+
// TestRecreateFieldsValidation validates that all fields in RecreateFields
56+
// exist in the corresponding ConfigType for each resource.
57+
func TestRecreateFieldsValidation(t *testing.T) {
58+
for resourceName, settings := range SupportedResources {
59+
if len(settings.RecreateFields) == 0 {
60+
continue
61+
}
62+
t.Run(resourceName, func(t *testing.T) {
63+
validateFields(t, settings.ConfigType, settings.RecreateFields)
64+
})
65+
}
66+
}

0 commit comments

Comments
 (0)