Skip to content

Commit

Permalink
DRY tests
Browse files Browse the repository at this point in the history
  • Loading branch information
razbensimon committed Jun 2, 2021
1 parent 20f7ef7 commit a1a0f56
Showing 1 changed file with 11 additions and 27 deletions.
38 changes: 11 additions & 27 deletions env0/data_sshkey_test.go
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
package env0

import (
"encoding/json"
"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"testing"
)

func TestUnitSshKeyDataSourceById(t *testing.T) {
resourceType := "env0_ssh_key"
resourceName := "test"
resourceFullName := dataSourceAccessor(resourceType, resourceName)
sshKey := client.SshKey{
Id: "id0",
Name: "name0",
Value: "Key🔑",
}

testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]string{
"id": sshKey.Id,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceFullName, "id", sshKey.Id),
resource.TestCheckResourceAttr(resourceFullName, "name", sshKey.Name),
),
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
mock.EXPECT().SshKeys().AnyTimes().Return([]client.SshKey{sshKey}, nil)
})
testUnitSshKeyDataSource(t, "id")
}

func TestUnitSshKeyDataSourceByName(t *testing.T) {
testUnitSshKeyDataSource(t, "name")
}

func testUnitSshKeyDataSource(t *testing.T, byKey string) {
resourceType := "env0_ssh_key"
resourceName := "test"
resourceFullName := dataSourceAccessor(resourceType, resourceName)
Expand All @@ -45,11 +25,15 @@ func TestUnitSshKeyDataSourceByName(t *testing.T) {
Value: "Key🔑",
}

sshKeyAsJson, _ := json.Marshal(sshKey)
var jsonData map[string]string
_ = json.Unmarshal(sshKeyAsJson, &jsonData)

testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]string{
"name": sshKey.Name,
byKey: jsonData[byKey],
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceFullName, "id", sshKey.Id),
Expand Down

0 comments on commit a1a0f56

Please sign in to comment.