|
1 | 1 | package tests |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "testing" |
6 | 7 |
|
@@ -124,13 +125,37 @@ func TestFeatureFlag_FiltersWithRollout(t *testing.T) { |
124 | 125 | Check: resource.ComposeAggregateTestCheckFunc( |
125 | 126 | resource.TestCheckResourceAttr("posthog_feature_flag.test", "key", rKey), |
126 | 127 | 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), |
128 | 129 | ), |
129 | 130 | }, |
130 | 131 | }, |
131 | 132 | }) |
132 | 133 | } |
133 | 134 |
|
| 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 | + |
134 | 159 | // TestFeatureFlag_Update tests updating each field individually. |
135 | 160 | func TestFeatureFlag_Update(t *testing.T) { |
136 | 161 | skipIfNotAcceptance(t) |
|
0 commit comments