Skip to content

Commit 64823f3

Browse files
committed
add test for unknown duration
1 parent 3d07048 commit 64823f3

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

internal/provider/convert.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ func FromStringMap(m map[string]string) types.Map {
5050

5151
// ToStringList converts a types.List to Settings_StringList and handles diagnostics internally
5252
func ToStringList(ctx context.Context, dst **pb.Settings_StringList, list types.List, diagnostics *diag.Diagnostics) {
53-
// Handle null list case first
5453
if list.IsNull() {
5554
*dst = nil
5655
return
@@ -91,7 +90,7 @@ func ToStringSlice(ctx context.Context, dst *[]string, list types.List, diagnost
9190

9291
// ToDuration converts a types.String containing a duration to a durationpb.Duration and handles diagnostics internally
9392
func ToDuration(dst **durationpb.Duration, src timetypes.GoDuration, diagnostics *diag.Diagnostics) {
94-
if src.IsNull() {
93+
if src.IsNull() || src.IsUnknown() {
9594
*dst = nil
9695
return
9796
}

internal/provider/convert_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,56 @@ func TestFromDurationP(t *testing.T) {
8686
}
8787
}
8888

89+
func TestToDuration(t *testing.T) {
90+
tests := []struct {
91+
name string
92+
input timetypes.GoDuration
93+
expected *durationpb.Duration
94+
expectError bool
95+
}{
96+
{
97+
name: "null duration",
98+
input: timetypes.NewGoDurationNull(),
99+
expected: nil,
100+
},
101+
{
102+
name: "unknown duration",
103+
input: timetypes.NewGoDurationUnknown(),
104+
expected: nil,
105+
},
106+
{
107+
name: "zero duration",
108+
input: timetypes.NewGoDurationValueFromStringMust("0s"),
109+
expected: durationpb.New(0),
110+
},
111+
{
112+
name: "normal duration",
113+
input: timetypes.NewGoDurationValueFromStringMust("1h1m0s"),
114+
expected: durationpb.New(time.Hour + time.Minute),
115+
},
116+
}
117+
118+
for _, tt := range tests {
119+
t.Run(tt.name, func(t *testing.T) {
120+
var result *durationpb.Duration
121+
diagnostics := diag.Diagnostics{}
122+
provider.ToDuration(&result, tt.input, &diagnostics)
123+
124+
if tt.expectError {
125+
assert.True(t, diagnostics.HasError())
126+
return
127+
}
128+
129+
assert.False(t, diagnostics.HasError())
130+
if tt.expected == nil {
131+
assert.Nil(t, result)
132+
} else {
133+
assert.Equal(t, tt.expected.AsDuration(), result.AsDuration())
134+
}
135+
})
136+
}
137+
}
138+
89139
func TestToStringList(t *testing.T) {
90140
ctx := context.Background()
91141
tests := []struct {

0 commit comments

Comments
 (0)