Skip to content

Commit

Permalink
update test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Udit committed Nov 23, 2020
1 parent c7118b7 commit 94c06b9
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 378 deletions.
136 changes: 59 additions & 77 deletions github/data_source_github_repository_milestone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,73 @@ package github

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"regexp"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccGithubRepositoryMilestoneDataSource_noMatchReturnsError(t *testing.T) {
repo := "nonExistentRepo"
owner := "no-user"
number := "1"
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubRepositoryMilestoneDataSourceNonExistentConfig(repo, owner, number),
ExpectError: regexp.MustCompile(`Not Found`),
},
},
})
}
func TestAccGithubRepositoryMilestoneDataSource(t *testing.T) {

func TestAccGithubRepositoryMilestoneDataSource_existing(t *testing.T) {
repo := acctest.RandomWithPrefix("tf-acc-test")
title := acctest.RandomWithPrefix("ms")
description := acctest.RandomWithPrefix("tf-acc-test-desc")
dueDate := time.Now().UTC().Format(layoutISO)

rn := "github_repository_milestone.test"
dataSource := "data.github_repository_milestone.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckGithubRepositoryMilestoneDataSourceConfig(repo, title, description, dueDate),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSource, "title", rn, "title"),
resource.TestCheckResourceAttrPair(dataSource, "description", rn, "description"),
resource.TestCheckResourceAttrPair(dataSource, "due_date", rn, "due_date"),
resource.TestCheckResourceAttrPair(dataSource, "state", rn, "state"),
resource.TestCheckResourceAttrPair(dataSource, "number", rn, "number"),
resource.TestCheckResourceAttrPair(dataSource, "owner", rn, "owner"),
resource.TestCheckResourceAttrPair(dataSource, "repository", rn, "repository"),
),
},
},
})
}
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

func testAccCheckGithubRepositoryMilestoneDataSourceNonExistentConfig(owner, repo, number string) string {
return fmt.Sprintf(`
data "github_repository_milestone" "test" {
owner = "%s"
repository = "%s"
number = "%s"
}
`, owner, repo, number)
}
t.Run("queries a repository milestone", func(t *testing.T) {

func testAccCheckGithubRepositoryMilestoneDataSourceConfig(repo, title, description, dueDate string) string {
return fmt.Sprintf(`
resource "github_repository" "test" {
name = "%s"
}
config := fmt.Sprintf(`
resource "github_repository_milestone" "test" {
owner = split("/", "${github_repository.test.full_name}")[0]
repository = github_repository.test.name
title = "%s"
description = "%s"
due_date = "%s"
}
resource "github_repository" "test" {
name = "tf-acc-test-%s"
}
data "github_repository_milestone" "test" {
owner = github_repository_milestone.test.owner
repository = github_repository_milestone.test.repository
number = github_repository_milestone.test.number
}
`, repo, title, description, dueDate)
resource "github_repository_milestone" "test" {
owner = split("/", "${github_repository.test.full_name}")[0]
repository = github_repository.test.name
title = "v1.0.0"
description = "General Availability"
due_date = "2020-11-22"
state = "closed"
}
data "github_repository_milestone" "test" {
owner = github_repository_milestone.test.owner
repository = github_repository_milestone.test.repository
number = github_repository_milestone.test.number
}
`, randomID)

check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_repository_milestone.test", "state",
"closed",
),
)

testCase := func(t *testing.T, mode string) {
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessMode(t, mode) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
}

t.Run("with an anonymous account", func(t *testing.T) {
t.Skip("anonymous account not supported for this operation")
})

t.Run("with an individual account", func(t *testing.T) {
testCase(t, individual)
})

t.Run("with an organization account", func(t *testing.T) {
testCase(t, organization)
})

})
}
2 changes: 1 addition & 1 deletion github/resource_github_repository_milestone.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func resourceGithubRepositoryMilestoneCreate(d *schema.ResourceData, meta interf
if err != nil {
return err
}
date := time.Date(dueDate.Year(), dueDate.Month(), dueDate.Day(), 7, 0, 0, 0, time.UTC)
date := time.Date(dueDate.Year(), dueDate.Month(), dueDate.Day(), 23, 39, 0, 0, time.UTC)
milestone.DueOn = &date
}
if v, ok := d.GetOk("state"); ok && len(v.(string)) > 0 {
Expand Down
Loading

0 comments on commit 94c06b9

Please sign in to comment.