From 12bcd4ccf395711e41b9207c15a8f94f069dec40 Mon Sep 17 00:00:00 2001 From: Flo Date: Sat, 30 Mar 2024 16:33:25 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20Test=20the=20metabase=5Fcard=20reso?= =?UTF-8?q?urce?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/provider/card_resource_test.go | 116 ++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 internal/provider/card_resource_test.go diff --git a/internal/provider/card_resource_test.go b/internal/provider/card_resource_test.go new file mode 100644 index 0000000..a55d9a3 --- /dev/null +++ b/internal/provider/card_resource_test.go @@ -0,0 +1,116 @@ +package provider + +import ( + "context" + "fmt" + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/terraform" +) + +func testAccCardResource(name string, displayName string) string { + // This references the sample database, which should always have ID 1. + return fmt.Sprintf(` +resource "metabase_card" "%s" { + json = jsonencode({ + name = "%s" + description = "📚" + collection_id = null + collection_position = null + cache_ttl = null + query_type = "query" + dataset_query = { + database = 1 + type = "query" + query = { + source-table = 1 + } + } + parameter_mappings = [] + display = "table" + visualization_settings = {} + parameters = [] + }) +} +`, + name, + displayName, + ) +} + +func testAccCheckCardExists(resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Failed to find resource %s in state.", resourceName) + } + + id, err := strconv.Atoi(rs.Primary.ID) + if err != nil { + return err + } + + response, err := testAccMetabaseClient.GetCardWithResponse(context.Background(), id) + if err != nil { + return err + } + if response.StatusCode() != 200 { + return fmt.Errorf("Received unexpected response from the Metabase API when getting card.") + } + + return nil + } +} + +func testAccCheckCardDestroy(s *terraform.State) error { + for _, rs := range s.RootModule().Resources { + if rs.Type != "metabase_card" { + continue + } + + id, err := strconv.Atoi(rs.Primary.ID) + if err != nil { + return err + } + + response, err := testAccMetabaseClient.GetCardWithResponse(context.Background(), id) + if err != nil { + return err + } + if response.StatusCode() != 404 && !response.JSON200.Archived { + return fmt.Errorf("Card %s still exists.", rs.Primary.ID) + } + } + + return nil +} + +func TestAccCardResource(t *testing.T) { + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + CheckDestroy: testAccCheckCardDestroy, + Steps: []resource.TestStep{ + { + Config: providerConfig + testAccCardResource("test", "🪪"), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckCardExists("metabase_card.test"), + resource.TestCheckResourceAttrSet("metabase_card.test", "id"), + resource.TestCheckResourceAttrSet("metabase_card.test", "json"), + ), + }, + { + ResourceName: "metabase_card.test", + ImportState: true, + }, + { + Config: providerConfig + testAccCardResource("test", "💳"), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttrSet("metabase_card.test", "id"), + resource.TestCheckResourceAttrSet("metabase_card.test", "json"), + ), + }, + }, + }) +}