-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Add project data source unit tests (#98)
* 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
Showing
1 changed file
with
109 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,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}), | ||
) | ||
}) | ||
} |