Skip to content

Commit 0a6eb80

Browse files
improve test coverage
1 parent c4a24a4 commit 0a6eb80

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

cmd/mdatagen/internal/cfggen/generation_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package cfggen
55

66
import (
7+
"maps"
8+
"slices"
79
"testing"
810

911
"github.com/stretchr/testify/require"
@@ -518,6 +520,88 @@ func TestExtractImports_ResolvedReferenceIncludesDefaultOverrideImports(t *testi
518520
require.ElementsMatch(t, []string{"go.opentelemetry.io/collector/scraper/scraperhelper", "time"}, result)
519521
}
520522

523+
func TestCollectCustomDefaultImports(t *testing.T) {
524+
tests := []struct {
525+
name string
526+
metadata *ConfigMetadata
527+
defaultValue any
528+
expected []string
529+
}{
530+
{
531+
name: "nil metadata",
532+
defaultValue: map[string]any{"timeout": "30s"},
533+
},
534+
{
535+
name: "ignored default",
536+
metadata: &ConfigMetadata{
537+
Type: "object",
538+
GoStruct: GoStructConfig{IgnoreDefault: true},
539+
Properties: map[string]*ConfigMetadata{
540+
"timeout": {Type: "string", GoType: "time.Duration"},
541+
},
542+
},
543+
defaultValue: map[string]any{"timeout": "30s"},
544+
},
545+
{
546+
name: "map schema with additional properties does not inspect entries",
547+
metadata: &ConfigMetadata{
548+
Type: "object",
549+
AdditionalProperties: &ConfigMetadata{Type: "string", GoType: "time.Duration"},
550+
},
551+
defaultValue: map[string]any{"timeout": "30s"},
552+
},
553+
{
554+
name: "missing property is ignored",
555+
metadata: &ConfigMetadata{
556+
Type: "object",
557+
Properties: map[string]*ConfigMetadata{},
558+
},
559+
defaultValue: map[string]any{"timeout": "30s"},
560+
},
561+
{
562+
name: "map default imports overridden property types",
563+
metadata: &ConfigMetadata{
564+
Type: "object",
565+
Properties: map[string]*ConfigMetadata{
566+
"timeout": {Type: "string", GoType: "time.Duration"},
567+
},
568+
},
569+
defaultValue: map[string]any{"timeout": "30s"},
570+
expected: []string{"time"},
571+
},
572+
{
573+
name: "array default imports object item property types",
574+
metadata: &ConfigMetadata{
575+
Type: "array",
576+
Items: &ConfigMetadata{
577+
Type: "object",
578+
Properties: map[string]*ConfigMetadata{
579+
"timestamp": {Type: "string", GoType: "time.Time"},
580+
},
581+
},
582+
},
583+
defaultValue: []any{map[string]any{"timestamp": "2026-04-30T00:00:00Z"}},
584+
expected: []string{"time"},
585+
},
586+
{
587+
name: "array default with non-object item does not inspect entries",
588+
metadata: &ConfigMetadata{
589+
Type: "array",
590+
Items: &ConfigMetadata{Type: "string", GoType: "time.Duration"},
591+
},
592+
defaultValue: []any{"30s"},
593+
},
594+
}
595+
596+
for _, tt := range tests {
597+
t.Run(tt.name, func(t *testing.T) {
598+
imports := map[string]bool{}
599+
require.NoError(t, collectCustomDefaultImports(tt.metadata, tt.defaultValue, imports, "", ""))
600+
require.ElementsMatch(t, tt.expected, slices.Collect(maps.Keys(imports)))
601+
})
602+
}
603+
}
604+
521605
func TestExtractImports_InternalResolvedReferenceIncludesNestedImports(t *testing.T) {
522606
md := &ConfigMetadata{
523607
Type: "object",
@@ -1883,6 +1967,13 @@ func TestFormatBaseValue(t *testing.T) {
18831967
}
18841968

18851969
require.Equal(t, `"localhost"`, FormatBaseValue(md, "endpoint", defaultValue("localhost"), "", ""))
1970+
require.Empty(t, FormatBaseValue(
1971+
&ConfigMetadata{Type: "string", GoStruct: GoStructConfig{IgnoreDefault: true}},
1972+
"endpoint",
1973+
defaultValue("localhost"),
1974+
"",
1975+
"",
1976+
))
18861977
}
18871978

18881979
func TestFormatDefaultValue_UnsetDefault(t *testing.T) {

0 commit comments

Comments
 (0)