From 3040cb9c65917cdafa025f3fdb497ac4db2a1c49 Mon Sep 17 00:00:00 2001 From: Avner Sorek <53464600+avnerenv0@users.noreply.github.com> Date: Tue, 8 Jun 2021 18:39:53 +0300 Subject: [PATCH] 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 --- env0/data_project_test.go | 109 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 env0/data_project_test.go diff --git a/env0/data_project_test.go b/env0/data_project_test.go new file mode 100644 index 00000000..abf04f8a --- /dev/null +++ b/env0/data_project_test.go @@ -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}), + ) + }) +}