Skip to content

Commit

Permalink
Chore: Add project data source unit tests (#98)
Browse files Browse the repository at this point in the history
* add project data source unit tests

* Splitting scenarios into tests

* PR refactor

* refactoring test a bit

* fixing interface

* one more test case

* finishing up test cases
  • Loading branch information
avnerenv0 committed Jun 8, 2021
1 parent cb9aa88 commit 3040cb9
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions env0/data_project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package env0

import (
"regexp"
"testing"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestProjectDataSource(t *testing.T) {
project := client.Project{
Id: "id0",
Name: "my-project-1",
CreatedBy: "env0",
Role: "role0",
Description: "A project's description",
}

projectDataByName := map[string]interface{}{"name": project.Name}
projectDataById := map[string]interface{}{"id": project.Id}

resourceType := "env0_project"
resourceName := "test_project"
accessor := dataSourceAccessor(resourceType, resourceName)

getValidTestCase := func(input map[string]interface{}) resource.TestCase {
return resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, input),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "id", project.Id),
resource.TestCheckResourceAttr(accessor, "name", project.Name),
resource.TestCheckResourceAttr(accessor, "created_by", project.CreatedBy),
resource.TestCheckResourceAttr(accessor, "role", project.Role),
resource.TestCheckResourceAttr(accessor, "description", project.Description),
),
},
},
}
}

getErrorTestCase := func(input map[string]interface{}, expectedError string) resource.TestCase {
return resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, input),
ExpectError: regexp.MustCompile(expectedError),
},
},
}
}

mockGetProjectCall := func(returnValue client.Project) func(mockFunc *client.MockApiClientInterface) {
return func(mock *client.MockApiClientInterface) {
mock.EXPECT().Project(project.Id).AnyTimes().Return(returnValue, nil)
}
}

mockListProjectsCall := func(returnValue []client.Project) func(mockFunc *client.MockApiClientInterface) {
return func(mock *client.MockApiClientInterface) {
mock.EXPECT().Projects().AnyTimes().Return(returnValue, nil)
}
}

t.Run("By ID", func(t *testing.T) {
runUnitTest(t,
getValidTestCase(projectDataById),
mockGetProjectCall(project),
)
})

t.Run("By Name", func(t *testing.T) {
runUnitTest(t,
getValidTestCase(projectDataByName),
mockListProjectsCall([]client.Project{project}),
)
})

t.Run("Throw error when no name or id is supplied", func(t *testing.T) {
runUnitTest(t,
getErrorTestCase(map[string]interface{}{}, "one of `id,name` must be specified"),
func(mock *client.MockApiClientInterface) {},
)
})

t.Run("Throw error when by name and more than one project exists", func(t *testing.T) {
runUnitTest(t,
getErrorTestCase(projectDataByName, "Found multiple Projects for name"),
mockListProjectsCall([]client.Project{project, project}),
)
})

t.Run("Throw error when by name and no projects found at all", func(t *testing.T) {
runUnitTest(t,
getErrorTestCase(projectDataByName, "Could not find a project with name"),
mockListProjectsCall([]client.Project{}),
)
})

t.Run("Throw error when by name and no projects found with that name", func(t *testing.T) {
projectWithOtherName := map[string]interface{}{"name": "other-name"}
runUnitTest(t,
getErrorTestCase(projectWithOtherName, "Could not find a project with name"),
mockListProjectsCall([]client.Project{project, project}),
)
})
}

0 comments on commit 3040cb9

Please sign in to comment.