Skip to content

Commit

Permalink
add unit tests for the writers
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-goenka committed Jan 6, 2025
1 parent a095b08 commit 0176742
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
1 change: 0 additions & 1 deletion libs/template/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ func (writer *defaultWriter) LogTelemetry(ctx context.Context) {
}

telemetry.Log(ctx, event)
return
}

type writerWithFullTelemetry struct {
Expand Down
56 changes: 56 additions & 0 deletions libs/template/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"github.com/databricks/cli/cmd/root"
"github.com/databricks/cli/libs/dbr"
"github.com/databricks/cli/libs/filer"
"github.com/databricks/cli/libs/jsonschema"
"github.com/databricks/cli/libs/telemetry"
"github.com/databricks/cli/libs/telemetry/events"
"github.com/databricks/databricks-sdk-go"
workspaceConfig "github.com/databricks/databricks-sdk-go/config"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -56,3 +59,56 @@ func TestMaterializeForNonTemplateDirectory(t *testing.T) {
err = w.Materialize(ctx, &localReader{path: tmpDir2})
assert.EqualError(t, err, "not a bundle template: expected to find a template schema file at databricks_template_schema.json")
}

func TestDefaultWriterLogTelemetry(t *testing.T) {
ctx := telemetry.WithMockLogger(context.Background())
w := &defaultWriter{templateName: Custom}
w.LogTelemetry(ctx)

logs := telemetry.Introspect(ctx)
assert.Len(t, logs, 1)
assert.Equal(t, &events.BundleInitEvent{
TemplateName: string(Custom),
Uuid: bundleUuid,
}, logs[0].BundleInitEvent)
}

func TestWriterWithFullTelemetry(t *testing.T) {
ctx := telemetry.WithMockLogger(context.Background())
w := &writerWithFullTelemetry{
defaultWriter: defaultWriter{
templateName: DefaultPython,
config: &config{
values: map[string]any{
"foo": "v1",
"bar": "v2",
},
schema: &jsonschema.Schema{
Properties: map[string]*jsonschema.Schema{
"foo": {
Type: jsonschema.StringType,
Enum: []any{"v1", "v2"},
},
"bar": {
Type: jsonschema.StringType,
},
},
},
},
},
}
w.LogTelemetry(ctx)

logs := telemetry.Introspect(ctx)
assert.Len(t, logs, 1)
assert.Equal(t, &events.BundleInitEvent{
TemplateName: string(DefaultPython),
TemplateEnumArgs: []events.BundleInitTemplateEnumArg{
{
Key: "foo",
Value: "v1",
},
},
Uuid: bundleUuid,
}, logs[0].BundleInitEvent)
}

0 comments on commit 0176742

Please sign in to comment.