|
| 1 | +package tests |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-testing/helper/acctest" |
| 8 | + "github.com/hashicorp/terraform-plugin-testing/helper/resource" |
| 9 | +) |
| 10 | + |
| 11 | +// TestDashboard_Basic tests creating a dashboard with only the required field (name). |
| 12 | +func TestDashboard_Basic(t *testing.T) { |
| 13 | + skipIfNotAcceptance(t) |
| 14 | + |
| 15 | + rName := acctest.RandomWithPrefix("tf-acc-test") |
| 16 | + |
| 17 | + resource.Test(t, resource.TestCase{ |
| 18 | + PreCheck: func() { testAccPreCheck(t) }, |
| 19 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 20 | + Steps: []resource.TestStep{ |
| 21 | + { |
| 22 | + Config: testAccDashboardBasic(rName), |
| 23 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 24 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "name", rName), |
| 25 | + resource.TestCheckResourceAttrSet("posthog_dashboard.test", "id"), |
| 26 | + resource.TestCheckNoResourceAttr("posthog_dashboard.test", "description"), |
| 27 | + ), |
| 28 | + }, |
| 29 | + }, |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +// TestDashboard_AllFields tests creating a dashboard with all optional fields. |
| 34 | +func TestDashboard_AllFields(t *testing.T) { |
| 35 | + skipIfNotAcceptance(t) |
| 36 | + |
| 37 | + rName := acctest.RandomWithPrefix("tf-acc-test") |
| 38 | + |
| 39 | + resource.Test(t, resource.TestCase{ |
| 40 | + PreCheck: func() { testAccPreCheck(t) }, |
| 41 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 42 | + Steps: []resource.TestStep{ |
| 43 | + { |
| 44 | + Config: testAccDashboardAllFields(rName), |
| 45 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 46 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "name", rName), |
| 47 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "description", "Test dashboard description"), |
| 48 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "pinned", "true"), |
| 49 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "tags.#", "2"), |
| 50 | + resource.TestCheckResourceAttrSet("posthog_dashboard.test", "id"), |
| 51 | + ), |
| 52 | + }, |
| 53 | + }, |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +// TestDashboard_Update tests updating each dashboard field individually. |
| 58 | +func TestDashboard_Update(t *testing.T) { |
| 59 | + skipIfNotAcceptance(t) |
| 60 | + |
| 61 | + rName := acctest.RandomWithPrefix("tf-acc-test") |
| 62 | + |
| 63 | + resource.Test(t, resource.TestCase{ |
| 64 | + PreCheck: func() { testAccPreCheck(t) }, |
| 65 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 66 | + Steps: []resource.TestStep{ |
| 67 | + // Create |
| 68 | + { |
| 69 | + Config: testAccDashboardWithDescription(rName, "Initial description"), |
| 70 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 71 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "name", rName), |
| 72 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "description", "Initial description"), |
| 73 | + ), |
| 74 | + }, |
| 75 | + // Update name |
| 76 | + { |
| 77 | + Config: testAccDashboardWithDescription(rName+"-updated", "Initial description"), |
| 78 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 79 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "name", rName+"-updated"), |
| 80 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "description", "Initial description"), |
| 81 | + ), |
| 82 | + }, |
| 83 | + // Update description |
| 84 | + { |
| 85 | + Config: testAccDashboardWithDescription(rName+"-updated", "Updated description"), |
| 86 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 87 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "name", rName+"-updated"), |
| 88 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "description", "Updated description"), |
| 89 | + ), |
| 90 | + }, |
| 91 | + }, |
| 92 | + }) |
| 93 | +} |
| 94 | + |
| 95 | +// TestDashboard_Tags tests creating, updating, and removing tags. |
| 96 | +func TestDashboard_Tags(t *testing.T) { |
| 97 | + skipIfNotAcceptance(t) |
| 98 | + |
| 99 | + rName := acctest.RandomWithPrefix("tf-acc-test") |
| 100 | + |
| 101 | + resource.Test(t, resource.TestCase{ |
| 102 | + PreCheck: func() { testAccPreCheck(t) }, |
| 103 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 104 | + Steps: []resource.TestStep{ |
| 105 | + // Create with tags |
| 106 | + { |
| 107 | + Config: testAccDashboardWithTags(rName, []string{"tag1", "tag2"}), |
| 108 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 109 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "tags.#", "2"), |
| 110 | + ), |
| 111 | + }, |
| 112 | + // Add more tags |
| 113 | + { |
| 114 | + Config: testAccDashboardWithTags(rName, []string{"tag1", "tag2", "tag3"}), |
| 115 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 116 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "tags.#", "3"), |
| 117 | + ), |
| 118 | + }, |
| 119 | + // Remove tags |
| 120 | + { |
| 121 | + Config: testAccDashboardWithTags(rName, []string{"tag1"}), |
| 122 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 123 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "tags.#", "1"), |
| 124 | + ), |
| 125 | + }, |
| 126 | + }, |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +// TestDashboard_Pinned tests toggling the pinned state. |
| 131 | +func TestDashboard_Pinned(t *testing.T) { |
| 132 | + skipIfNotAcceptance(t) |
| 133 | + |
| 134 | + rName := acctest.RandomWithPrefix("tf-acc-test") |
| 135 | + |
| 136 | + resource.Test(t, resource.TestCase{ |
| 137 | + PreCheck: func() { testAccPreCheck(t) }, |
| 138 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 139 | + Steps: []resource.TestStep{ |
| 140 | + // Create unpinned |
| 141 | + { |
| 142 | + Config: testAccDashboardPinned(rName, false), |
| 143 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 144 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "pinned", "false"), |
| 145 | + ), |
| 146 | + }, |
| 147 | + // Pin it |
| 148 | + { |
| 149 | + Config: testAccDashboardPinned(rName, true), |
| 150 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 151 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "pinned", "true"), |
| 152 | + ), |
| 153 | + }, |
| 154 | + // Unpin it |
| 155 | + { |
| 156 | + Config: testAccDashboardPinned(rName, false), |
| 157 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 158 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "pinned", "false"), |
| 159 | + ), |
| 160 | + }, |
| 161 | + }, |
| 162 | + }) |
| 163 | +} |
| 164 | + |
| 165 | +// TestDashboard_Import tests importing an existing dashboard by ID. |
| 166 | +func TestDashboard_Import(t *testing.T) { |
| 167 | + skipIfNotAcceptance(t) |
| 168 | + |
| 169 | + rName := acctest.RandomWithPrefix("tf-acc-test") |
| 170 | + |
| 171 | + resource.Test(t, resource.TestCase{ |
| 172 | + PreCheck: func() { testAccPreCheck(t) }, |
| 173 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 174 | + Steps: []resource.TestStep{ |
| 175 | + // Create |
| 176 | + { |
| 177 | + Config: testAccDashboardWithDescription(rName, "Import test"), |
| 178 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 179 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "name", rName), |
| 180 | + ), |
| 181 | + }, |
| 182 | + // Import |
| 183 | + { |
| 184 | + ResourceName: "posthog_dashboard.test", |
| 185 | + ImportState: true, |
| 186 | + ImportStateVerify: true, |
| 187 | + }, |
| 188 | + }, |
| 189 | + }) |
| 190 | +} |
| 191 | + |
| 192 | +// TestDashboard_Recreate tests deleting and recreating a dashboard with the same name. |
| 193 | +func TestDashboard_Recreate(t *testing.T) { |
| 194 | + skipIfNotAcceptance(t) |
| 195 | + |
| 196 | + rName := acctest.RandomWithPrefix("tf-acc-test") |
| 197 | + |
| 198 | + resource.Test(t, resource.TestCase{ |
| 199 | + PreCheck: func() { testAccPreCheck(t) }, |
| 200 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 201 | + Steps: []resource.TestStep{ |
| 202 | + // Create first dashboard |
| 203 | + { |
| 204 | + Config: testAccDashboardBasic(rName), |
| 205 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 206 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "name", rName), |
| 207 | + ), |
| 208 | + }, |
| 209 | + // Remove resource from config (destroys it) |
| 210 | + { |
| 211 | + Config: `provider "posthog" {}`, |
| 212 | + }, |
| 213 | + // Recreate |
| 214 | + { |
| 215 | + Config: testAccDashboardWithDescription(rName, "Recreated"), |
| 216 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 217 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "name", rName), |
| 218 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "description", "Recreated"), |
| 219 | + ), |
| 220 | + }, |
| 221 | + }, |
| 222 | + }) |
| 223 | +} |
| 224 | + |
| 225 | +// TestDashboard_EmptyDescription tests handling of empty vs null description. |
| 226 | +func TestDashboard_EmptyDescription(t *testing.T) { |
| 227 | + skipIfNotAcceptance(t) |
| 228 | + |
| 229 | + rName := acctest.RandomWithPrefix("tf-acc-test") |
| 230 | + |
| 231 | + resource.Test(t, resource.TestCase{ |
| 232 | + PreCheck: func() { testAccPreCheck(t) }, |
| 233 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 234 | + Steps: []resource.TestStep{ |
| 235 | + // Create with description |
| 236 | + { |
| 237 | + Config: testAccDashboardWithDescription(rName, "Has description"), |
| 238 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 239 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "description", "Has description"), |
| 240 | + ), |
| 241 | + }, |
| 242 | + // Remove description (set to empty) |
| 243 | + { |
| 244 | + Config: testAccDashboardBasic(rName), |
| 245 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 246 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "name", rName), |
| 247 | + resource.TestCheckResourceAttr("posthog_dashboard.test", "description", ""), |
| 248 | + ), |
| 249 | + }, |
| 250 | + }, |
| 251 | + }) |
| 252 | +} |
| 253 | + |
| 254 | +// Config generators |
| 255 | + |
| 256 | +func testAccDashboardBasic(name string) string { |
| 257 | + return fmt.Sprintf(` |
| 258 | +provider "posthog" {} |
| 259 | +
|
| 260 | +resource "posthog_dashboard" "test" { |
| 261 | + name = %q |
| 262 | +} |
| 263 | +`, name) |
| 264 | +} |
| 265 | + |
| 266 | +func testAccDashboardWithDescription(name, description string) string { |
| 267 | + return fmt.Sprintf(` |
| 268 | +provider "posthog" {} |
| 269 | +
|
| 270 | +resource "posthog_dashboard" "test" { |
| 271 | + name = %q |
| 272 | + description = %q |
| 273 | +} |
| 274 | +`, name, description) |
| 275 | +} |
| 276 | + |
| 277 | +func testAccDashboardAllFields(name string) string { |
| 278 | + return fmt.Sprintf(` |
| 279 | +provider "posthog" {} |
| 280 | +
|
| 281 | +resource "posthog_dashboard" "test" { |
| 282 | + name = %q |
| 283 | + description = "Test dashboard description" |
| 284 | + pinned = true |
| 285 | + tags = ["managed-by:terraform", "env:test"] |
| 286 | +} |
| 287 | +`, name) |
| 288 | +} |
| 289 | + |
| 290 | +func testAccDashboardWithTags(name string, tags []string) string { |
| 291 | + tagsStr := "" |
| 292 | + for i, tag := range tags { |
| 293 | + if i > 0 { |
| 294 | + tagsStr += ", " |
| 295 | + } |
| 296 | + tagsStr += fmt.Sprintf("%q", tag) |
| 297 | + } |
| 298 | + |
| 299 | + return fmt.Sprintf(` |
| 300 | +provider "posthog" {} |
| 301 | +
|
| 302 | +resource "posthog_dashboard" "test" { |
| 303 | + name = %q |
| 304 | + tags = [%s] |
| 305 | +} |
| 306 | +`, name, tagsStr) |
| 307 | +} |
| 308 | + |
| 309 | +func testAccDashboardPinned(name string, pinned bool) string { |
| 310 | + return fmt.Sprintf(` |
| 311 | +provider "posthog" {} |
| 312 | +
|
| 313 | +resource "posthog_dashboard" "test" { |
| 314 | + name = %q |
| 315 | + pinned = %t |
| 316 | +} |
| 317 | +`, name, pinned) |
| 318 | +} |
0 commit comments