order_resource_test.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright (c) HashiCorp, Inc.
  2. // SPDX-License-Identifier: MPL-2.0
  3. package provider
  4. import (
  5. "testing"
  6. "github.com/hashicorp/terraform-plugin-testing/helper/resource"
  7. )
  8. // Resource acceptance testing verifies that the entire resource lifecycle,
  9. // such as the Create, Read, Update, and Delete functionality, along with import capabilities.
  10. //
  11. // The testing framework automatically handles destroying test resources
  12. // and returning any errors as a final step,
  13. // regardless of whether there is a destroy step explicitly written.
  14. func TestAccOrderResource(t *testing.T) {
  15. resource.Test(t, resource.TestCase{
  16. ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
  17. Steps: []resource.TestStep{
  18. // Create and Read testing
  19. {
  20. Config: providerConfig + `
  21. resource "hashicups_order" "test" {
  22. items = [
  23. {
  24. coffee = {
  25. id = 1
  26. }
  27. quantity = 2
  28. },
  29. ]
  30. }
  31. `,
  32. Check: resource.ComposeAggregateTestCheckFunc(
  33. // Verify number of items.
  34. resource.TestCheckResourceAttr("hashicups_order.test", "items.#", "1"),
  35. // Verify first order item.
  36. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.quantity", "2"),
  37. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.id", "1"),
  38. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.description", ""),
  39. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.image", "/hashicorp.png"),
  40. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.name", "HCP Aeropress"),
  41. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.price", "200"),
  42. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.teaser", "Automation in a cup"),
  43. // Verify dynamic values have any value set in the state.
  44. resource.TestCheckResourceAttrSet("hashicups_order.test", "id"),
  45. resource.TestCheckResourceAttrSet("hashicups_order.test", "last_updated"),
  46. ),
  47. },
  48. // ImportState testing.
  49. {
  50. // No Config
  51. ResourceName: "hashicups_order.test",
  52. ImportState: true,
  53. ImportStateVerify: true,
  54. // The last_updated attribute does not exist in the HashiCups
  55. // API, therefore there is no value for it during import.
  56. ImportStateVerifyIgnore: []string{"last_updated"},
  57. },
  58. // Update and Read testing.
  59. {
  60. Config: providerConfig + `
  61. resource "hashicups_order" "test" {
  62. items = [
  63. {
  64. coffee = {
  65. id = 2
  66. }
  67. quantity = 2
  68. },
  69. ]
  70. }
  71. `,
  72. Check: resource.ComposeAggregateTestCheckFunc(
  73. // Verify first order item updated (reuse previous state since this is the same test).
  74. resource.TestCheckResourceAttr("hashicups_order.test", "items.#", "1"),
  75. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.quantity", "2"),
  76. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.id", "2"),
  77. // Verify first coffee item has Computed attributes updated.
  78. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.description", ""),
  79. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.image", "/packer.png"),
  80. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.name", "Packer Spiced Latte"),
  81. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.price", "350"),
  82. resource.TestCheckResourceAttr("hashicups_order.test", "items.0.coffee.teaser", "Packed with goodness to spice up your images"),
  83. ),
  84. // Delete testing automatically occurs in TestCase.
  85. },
  86. },
  87. })
  88. }