generated from hashicorp/terraform-provider-scaffolding-framework
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from flovouin/chore/acceptance-tests-card
✅ Test the `metabase_card` resource
- Loading branch information
Showing
1 changed file
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |