Skip to content

Commit

Permalink
Merge pull request #41 from flovouin/chore/acceptance-tests-card
Browse files Browse the repository at this point in the history
✅ Test the `metabase_card` resource
  • Loading branch information
flovouin authored Mar 30, 2024
2 parents 6c499e5 + 12bcd4c commit 30d33b3
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions internal/provider/card_resource_test.go
Original file line number Diff line number Diff line change
@@ -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"),
),
},
},
})
}

0 comments on commit 30d33b3

Please sign in to comment.