order_resource_test.go 3.4 KB

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