|
5 | 5 | "net/http/httptest"
|
6 | 6 | "testing"
|
7 | 7 |
|
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
8 | 9 | "github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
9 | 10 | )
|
10 | 11 |
|
@@ -230,3 +231,297 @@ func TestAccProfilesWarehouseResource(t *testing.T) {
|
230 | 231 | },
|
231 | 232 | })
|
232 | 233 | }
|
| 234 | + |
| 235 | +func TestAccProfilesWarehouseResource_SchemaNameHandling(t *testing.T) { |
| 236 | + // Test the schemaName handling that prevents API failures when the schema |
| 237 | + // name already exists in the warehouse configuration |
| 238 | + t.Parallel() |
| 239 | + |
| 240 | + updateCount := 0 |
| 241 | + fakeServer := httptest.NewServer( |
| 242 | + http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 243 | + w.Header().Set("content-type", "application/json") |
| 244 | + |
| 245 | + payload := "" |
| 246 | + if req.URL.Path == "/spaces/my-space-id/profiles-warehouses" && req.Method == http.MethodPost { |
| 247 | + // Initial create response |
| 248 | + payload = ` |
| 249 | + { |
| 250 | + "data": { |
| 251 | + "profilesWarehouse": { |
| 252 | + "id": "my-warehouse-id", |
| 253 | + "spaceId": "my-space-id", |
| 254 | + "workspaceId": "my-workspace-id", |
| 255 | + "enabled": true, |
| 256 | + "metadata": { |
| 257 | + "id": "my-metadata-id", |
| 258 | + "slug": "snowflake", |
| 259 | + "name": "Snowflake", |
| 260 | + "description": "Data warehouse built for the cloud", |
| 261 | + "logos": { |
| 262 | + "default": "https://cdn.filepicker.io/api/file/JrQWOYvMRRCVvSHp4HL0", |
| 263 | + "mark": "https://cdn.filepicker.io/api/file/OBhrGoCRKaSyvAhDX3fw", |
| 264 | + "alt": "" |
| 265 | + }, |
| 266 | + "options": [] |
| 267 | + }, |
| 268 | + "settings": { |
| 269 | + "name": "My warehouse name", |
| 270 | + "token": "my-token" |
| 271 | + }, |
| 272 | + "schemaName": "my-schema-name" |
| 273 | + } |
| 274 | + } |
| 275 | + } |
| 276 | + ` |
| 277 | + } else if req.URL.Path == "/spaces/my-space-id/profiles-warehouses/my-warehouse-id" && req.Method == http.MethodPatch { |
| 278 | + // Update response - schemaName should only be sent when it changes |
| 279 | + updateCount++ |
| 280 | + payload = ` |
| 281 | + { |
| 282 | + "data": { |
| 283 | + "profilesWarehouse": { |
| 284 | + "id": "my-warehouse-id", |
| 285 | + "spaceId": "my-space-id", |
| 286 | + "workspaceId": "my-workspace-id", |
| 287 | + "enabled": false, |
| 288 | + "metadata": { |
| 289 | + "id": "my-metadata-id", |
| 290 | + "slug": "snowflake", |
| 291 | + "name": "Snowflake", |
| 292 | + "description": "Data warehouse built for the cloud", |
| 293 | + "logos": { |
| 294 | + "default": "https://cdn.filepicker.io/api/file/JrQWOYvMRRCVvSHp4HL0", |
| 295 | + "mark": "https://cdn.filepicker.io/api/file/OBhrGoCRKaSyvAhDX3fw", |
| 296 | + "alt": "" |
| 297 | + }, |
| 298 | + "options": [] |
| 299 | + }, |
| 300 | + "settings": { |
| 301 | + "name": "My new warehouse name", |
| 302 | + "token": "my-other-token" |
| 303 | + }, |
| 304 | + "schemaName": "my-new-schema-name" |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | + ` |
| 309 | + } else if req.URL.Path == "/spaces/my-space-id/profiles-warehouses" && req.Method == http.MethodGet { |
| 310 | + // Read response |
| 311 | + payload = ` |
| 312 | + { |
| 313 | + "data": { |
| 314 | + "profilesWarehouses": [ |
| 315 | + { |
| 316 | + "id": "my-warehouse-id", |
| 317 | + "spaceId": "my-space-id", |
| 318 | + "workspaceId": "my-workspace-id", |
| 319 | + "enabled": true, |
| 320 | + "metadata": { |
| 321 | + "id": "my-metadata-id", |
| 322 | + "slug": "snowflake", |
| 323 | + "name": "Snowflake", |
| 324 | + "description": "Data warehouse built for the cloud", |
| 325 | + "logos": { |
| 326 | + "default": "https://cdn.filepicker.io/api/file/JrQWOYvMRRCVvSHp4HL0", |
| 327 | + "mark": "https://cdn.filepicker.io/api/file/OBhrGoCRKaSyvAhDX3fw", |
| 328 | + "alt": "" |
| 329 | + }, |
| 330 | + "options": [] |
| 331 | + }, |
| 332 | + "settings": { |
| 333 | + "name": "My warehouse name", |
| 334 | + "token": "my-token" |
| 335 | + }, |
| 336 | + "schemaName": "my-schema-name" |
| 337 | + } |
| 338 | + ] |
| 339 | + } |
| 340 | + } |
| 341 | + ` |
| 342 | + } |
| 343 | + |
| 344 | + _, _ = w.Write([]byte(payload)) |
| 345 | + }), |
| 346 | + ) |
| 347 | + defer fakeServer.Close() |
| 348 | + |
| 349 | + providerConfig := ` |
| 350 | + provider "segment" { |
| 351 | + url = "` + fakeServer.URL + `" |
| 352 | + token = "abc123" |
| 353 | + } |
| 354 | + ` |
| 355 | + |
| 356 | + resource.Test(t, resource.TestCase{ |
| 357 | + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
| 358 | + Steps: []resource.TestStep{ |
| 359 | + // Create with schema_name |
| 360 | + { |
| 361 | + Config: providerConfig + ` |
| 362 | + resource "segment_profiles_warehouse" "test" { |
| 363 | + space_id = "my-space-id" |
| 364 | + metadata_id = "my-metadata-id" |
| 365 | + name = "My warehouse name" |
| 366 | + enabled = true |
| 367 | + settings = jsonencode({ |
| 368 | + "token": "my-token" |
| 369 | + }) |
| 370 | + schema_name = "my-schema-name" |
| 371 | + } |
| 372 | + `, |
| 373 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 374 | + resource.TestCheckResourceAttr("segment_profiles_warehouse.test", "schema_name", "my-schema-name"), |
| 375 | + ), |
| 376 | + }, |
| 377 | + // Update with same schema_name - should not send schemaName to API (prevents API failure) |
| 378 | + { |
| 379 | + Config: providerConfig + ` |
| 380 | + resource "segment_profiles_warehouse" "test" { |
| 381 | + space_id = "my-space-id" |
| 382 | + metadata_id = "my-metadata-id" |
| 383 | + name = "My updated warehouse name" |
| 384 | + enabled = false |
| 385 | + settings = jsonencode({ |
| 386 | + "token": "my-other-token" |
| 387 | + }) |
| 388 | + schema_name = "my-schema-name" |
| 389 | + } |
| 390 | + `, |
| 391 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 392 | + resource.TestCheckResourceAttr("segment_profiles_warehouse.test", "schema_name", "my-schema-name"), |
| 393 | + ), |
| 394 | + }, |
| 395 | + // Update with different schema_name - should send schemaName to API (legitimate change) |
| 396 | + { |
| 397 | + Config: providerConfig + ` |
| 398 | + resource "segment_profiles_warehouse" "test" { |
| 399 | + space_id = "my-space-id" |
| 400 | + metadata_id = "my-metadata-id" |
| 401 | + name = "My final warehouse name" |
| 402 | + enabled = true |
| 403 | + settings = jsonencode({ |
| 404 | + "token": "my-final-token" |
| 405 | + }) |
| 406 | + schema_name = "my-new-schema-name" |
| 407 | + } |
| 408 | + `, |
| 409 | + Check: resource.ComposeAggregateTestCheckFunc( |
| 410 | + resource.TestCheckResourceAttr("segment_profiles_warehouse.test", "schema_name", "my-new-schema-name"), |
| 411 | + ), |
| 412 | + }, |
| 413 | + }, |
| 414 | + }) |
| 415 | +} |
| 416 | + |
| 417 | +func TestDetermineSchemaNameForUpdate(t *testing.T) { |
| 418 | + // Test the determineSchemaNameForUpdate function that prevents API failures |
| 419 | + // when the schema name already exists in the warehouse configuration |
| 420 | + tests := []struct { |
| 421 | + name string |
| 422 | + planSchemaName types.String |
| 423 | + stateSchemaName types.String |
| 424 | + expectedResult *string |
| 425 | + description string |
| 426 | + }{ |
| 427 | + { |
| 428 | + name: "both_null", |
| 429 | + planSchemaName: types.StringNull(), |
| 430 | + stateSchemaName: types.StringNull(), |
| 431 | + expectedResult: nil, |
| 432 | + description: "Both null - should not send schemaName to API (prevents API failure)", |
| 433 | + }, |
| 434 | + { |
| 435 | + name: "both_unknown", |
| 436 | + planSchemaName: types.StringUnknown(), |
| 437 | + stateSchemaName: types.StringUnknown(), |
| 438 | + expectedResult: nil, |
| 439 | + description: "Both unknown - should not send schemaName to API (prevents API failure)", |
| 440 | + }, |
| 441 | + { |
| 442 | + name: "both_same_value", |
| 443 | + planSchemaName: types.StringValue("my-schema"), |
| 444 | + stateSchemaName: types.StringValue("my-schema"), |
| 445 | + expectedResult: nil, |
| 446 | + description: "Both have same value - should not send schemaName to API (prevents API failure)", |
| 447 | + }, |
| 448 | + { |
| 449 | + name: "plan_null_state_has_value", |
| 450 | + planSchemaName: types.StringNull(), |
| 451 | + stateSchemaName: types.StringValue("my-schema"), |
| 452 | + expectedResult: nil, // null pointer, not the actual value |
| 453 | + description: "Plan null, state has value - should send schemaName to API", |
| 454 | + }, |
| 455 | + { |
| 456 | + name: "plan_has_value_state_null", |
| 457 | + planSchemaName: types.StringValue("my-schema"), |
| 458 | + stateSchemaName: types.StringNull(), |
| 459 | + expectedResult: stringPtr("my-schema"), |
| 460 | + description: "Plan has value, state null - should send schemaName to API", |
| 461 | + }, |
| 462 | + { |
| 463 | + name: "plan_unknown_state_has_value", |
| 464 | + planSchemaName: types.StringUnknown(), |
| 465 | + stateSchemaName: types.StringValue("my-schema"), |
| 466 | + expectedResult: nil, // unknown becomes null pointer |
| 467 | + description: "Plan unknown, state has value - should send schemaName to API", |
| 468 | + }, |
| 469 | + { |
| 470 | + name: "plan_has_value_state_unknown", |
| 471 | + planSchemaName: types.StringValue("my-schema"), |
| 472 | + stateSchemaName: types.StringUnknown(), |
| 473 | + expectedResult: stringPtr("my-schema"), |
| 474 | + description: "Plan has value, state unknown - should send schemaName to API", |
| 475 | + }, |
| 476 | + { |
| 477 | + name: "different_values", |
| 478 | + planSchemaName: types.StringValue("new-schema"), |
| 479 | + stateSchemaName: types.StringValue("old-schema"), |
| 480 | + expectedResult: stringPtr("new-schema"), |
| 481 | + description: "Different values - should send schemaName to API", |
| 482 | + }, |
| 483 | + { |
| 484 | + name: "empty_string_vs_null", |
| 485 | + planSchemaName: types.StringValue(""), |
| 486 | + stateSchemaName: types.StringNull(), |
| 487 | + expectedResult: stringPtr(""), |
| 488 | + description: "Empty string vs null - should send schemaName to API", |
| 489 | + }, |
| 490 | + { |
| 491 | + name: "null_vs_empty_string", |
| 492 | + planSchemaName: types.StringNull(), |
| 493 | + stateSchemaName: types.StringValue(""), |
| 494 | + expectedResult: nil, |
| 495 | + description: "Null vs empty string - should send schemaName to API", |
| 496 | + }, |
| 497 | + } |
| 498 | + |
| 499 | + for _, tt := range tests { |
| 500 | + t.Run(tt.name, func(t *testing.T) { |
| 501 | + // Test the actual function |
| 502 | + result := determineSchemaNameForUpdate(tt.planSchemaName, tt.stateSchemaName) |
| 503 | + |
| 504 | + // Check if the result matches expected |
| 505 | + if !compareStringPointers(result, tt.expectedResult) { |
| 506 | + t.Errorf("Test case '%s' failed: %s\nExpected: %v, but got: %v", |
| 507 | + tt.name, tt.description, tt.expectedResult, result) |
| 508 | + } |
| 509 | + }) |
| 510 | + } |
| 511 | +} |
| 512 | + |
| 513 | +// Helper function to create string pointers for test cases |
| 514 | +func stringPtr(s string) *string { |
| 515 | + return &s |
| 516 | +} |
| 517 | + |
| 518 | +// Helper function to compare string pointers |
| 519 | +func compareStringPointers(a, b *string) bool { |
| 520 | + if a == nil && b == nil { |
| 521 | + return true |
| 522 | + } |
| 523 | + if a == nil || b == nil { |
| 524 | + return false |
| 525 | + } |
| 526 | + return *a == *b |
| 527 | +} |
0 commit comments