Skip to content

Commit 3042606

Browse files
committed
chore: Fixes in the tests to cover more cases
1 parent e924d3f commit 3042606

7 files changed

Lines changed: 89 additions & 9 deletions

File tree

internal/resource/core/helpers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,14 @@ func Int32SetPreserveEmpty(ctx context.Context, values []int32, currentModel typ
8080
}
8181
return types.SetNull(types.Int32Type), diags
8282
}
83+
84+
// ShouldClearString returns true if the plan value is null but state has a value,
85+
// indicating the user wants to clear the field by sending an empty string.
86+
func ShouldClearString(plan, state types.String) bool {
87+
return plan.IsNull() && !state.IsNull()
88+
}
89+
90+
// StringPtr returns a pointer to the given string.
91+
func StringPtr(s string) *string {
92+
return &s
93+
}

internal/resource/core/helpers_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,44 @@ func TestInt32SetPreserveEmpty(t *testing.T) {
318318
})
319319
}
320320
}
321+
322+
func TestShouldClearString(t *testing.T) {
323+
tests := map[string]struct {
324+
plan types.String
325+
state types.String
326+
want bool
327+
}{
328+
"plan null, state has value - should clear": {
329+
plan: types.StringNull(),
330+
state: types.StringValue("existing"),
331+
want: true,
332+
},
333+
"plan null, state null - nothing to clear": {
334+
plan: types.StringNull(),
335+
state: types.StringNull(),
336+
want: false,
337+
},
338+
"plan has value, state has value - no clearing needed": {
339+
plan: types.StringValue("new"),
340+
state: types.StringValue("old"),
341+
want: false,
342+
},
343+
"plan has value, state null - no clearing needed": {
344+
plan: types.StringValue("new"),
345+
state: types.StringNull(),
346+
want: false,
347+
},
348+
"plan unknown, state has value - unknown handled elsewhere": {
349+
plan: types.StringUnknown(),
350+
state: types.StringValue("existing"),
351+
want: false,
352+
},
353+
}
354+
355+
for name, tc := range tests {
356+
t.Run(name, func(t *testing.T) {
357+
got := ShouldClearString(tc.plan, tc.state)
358+
assert.Equal(t, tc.want, got)
359+
})
360+
}
361+
}

internal/resource/dashboard.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ func (o DashboardOps) BuildCreateRequest(ctx context.Context, model DashboardRes
107107
func (o DashboardOps) BuildUpdateRequest(ctx context.Context, plan, state DashboardResourceTFModel) (httpclient.DashboardRequest, diag.Diagnostics) {
108108
req, diags := o.BuildCreateRequest(ctx, plan)
109109

110-
if plan.Description.IsNull() && !state.Description.IsNull() {
111-
empty := ""
112-
req.Description = &empty
110+
// Clear description if removed from config
111+
if core.ShouldClearString(plan.Description, state.Description) {
112+
req.Description = core.StringPtr("")
113113
}
114114

115115
if !plan.Deleted.IsNull() {

internal/resource/insight.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,14 @@ func (o InsightOps) BuildCreateRequest(ctx context.Context, model InsightResourc
161161
return input, diags
162162
}
163163

164-
func (o InsightOps) BuildUpdateRequest(ctx context.Context, plan, _ InsightResourceTFModel) (httpclient.InsightRequest, diag.Diagnostics) {
164+
func (o InsightOps) BuildUpdateRequest(ctx context.Context, plan, state InsightResourceTFModel) (httpclient.InsightRequest, diag.Diagnostics) {
165165
req, diags := o.BuildCreateRequest(ctx, plan)
166166

167+
// Clear description if removed from config
168+
if core.ShouldClearString(plan.Description, state.Description) {
169+
req.Description = core.StringPtr("")
170+
}
171+
167172
if !plan.Deleted.IsNull() {
168173
deleted := plan.Deleted.ValueBool()
169174
req.Deleted = &deleted

tests/dashboard_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,15 +244,13 @@ func TestDashboard_EmptyDescription(t *testing.T) {
244244
Config: testAccDashboardBasic(rName),
245245
Check: resource.ComposeAggregateTestCheckFunc(
246246
resource.TestCheckResourceAttr("posthog_dashboard.test", "name", rName),
247-
resource.TestCheckResourceAttr("posthog_dashboard.test", "description", ""),
247+
resource.TestCheckNoResourceAttr("posthog_dashboard.test", "description"),
248248
),
249249
},
250250
},
251251
})
252252
}
253253

254-
// Config generators
255-
256254
func testAccDashboardBasic(name string) string {
257255
return fmt.Sprintf(`
258256
provider "posthog" {}

tests/feature_flag_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tests
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"testing"
67

@@ -124,13 +125,37 @@ func TestFeatureFlag_FiltersWithRollout(t *testing.T) {
124125
Check: resource.ComposeAggregateTestCheckFunc(
125126
resource.TestCheckResourceAttr("posthog_feature_flag.test", "key", rKey),
126127
resource.TestCheckResourceAttrSet("posthog_feature_flag.test", "filters"),
127-
resource.TestCheckResourceAttr("posthog_feature_flag.test", "filters.groups[0].rollout_percentage", "75"),
128+
testCheckFiltersRolloutPercentage("posthog_feature_flag.test", 0, 75),
128129
),
129130
},
130131
},
131132
})
132133
}
133134

135+
// testCheckFiltersRolloutPercentage verifies the rollout_percentage in a filters JSON attribute.
136+
func testCheckFiltersRolloutPercentage(resourceName string, groupIndex int, expected float64) resource.TestCheckFunc {
137+
return resource.TestCheckResourceAttrWith(resourceName, "filters", func(value string) error {
138+
var filters struct {
139+
Groups []struct {
140+
RolloutPercentage *float64 `json:"rollout_percentage"`
141+
} `json:"groups"`
142+
}
143+
if err := json.Unmarshal([]byte(value), &filters); err != nil {
144+
return fmt.Errorf("failed to parse filters JSON: %w", err)
145+
}
146+
if groupIndex >= len(filters.Groups) {
147+
return fmt.Errorf("group index %d out of range (have %d groups)", groupIndex, len(filters.Groups))
148+
}
149+
if filters.Groups[groupIndex].RolloutPercentage == nil {
150+
return fmt.Errorf("rollout_percentage is nil for group %d", groupIndex)
151+
}
152+
if *filters.Groups[groupIndex].RolloutPercentage != expected {
153+
return fmt.Errorf("expected rollout_percentage %v, got %v", expected, *filters.Groups[groupIndex].RolloutPercentage)
154+
}
155+
return nil
156+
})
157+
}
158+
134159
// TestFeatureFlag_Update tests updating each field individually.
135160
func TestFeatureFlag_Update(t *testing.T) {
136161
skipIfNotAcceptance(t)

tests/insight_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func TestInsight_EmptyDescription(t *testing.T) {
325325
Config: testAccInsightBasic(rName),
326326
Check: resource.ComposeAggregateTestCheckFunc(
327327
resource.TestCheckResourceAttr("posthog_insight.test", "name", rName),
328-
resource.TestCheckNoResourceAttr("posthog_dashboard.test", "description"),
328+
resource.TestCheckNoResourceAttr("posthog_insight.test", "description"),
329329
),
330330
},
331331
},

0 commit comments

Comments
 (0)