Skip to content

Commit

Permalink
Create test for DeleteBranch and DeleteTag and update Refs test
Browse files Browse the repository at this point in the history
This commit tests the DeleteBranch and DeleteTag repo methods. It also
updates the ListRefs part of the tests to ensure that it also properly
lists tags as well as branches.
  • Loading branch information
DataDavD committed Dec 8, 2021
1 parent 2dd2dbb commit 57c51b0
Showing 1 changed file with 100 additions and 10 deletions.
110 changes: 100 additions & 10 deletions tests/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,10 @@ func TestDeleteRepositoryPipelineVariables(t *testing.T) {
}
}

// This test tests the CreateBranch, CreateTag, ListRefs, DeleteBranch, and DeleteTag repo methods
func TestGetRepositoryRefs(t *testing.T) {

// Create Branch, List Refs and search for Branch that was created,
// then test successful Branch deletion
user := os.Getenv("BITBUCKET_TEST_USERNAME")
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
owner := os.Getenv("BITBUCKET_TEST_OWNER")
Expand All @@ -300,47 +302,135 @@ func TestGetRepositoryRefs(t *testing.T) {
Owner: owner,
RepoSlug: repo,
Name: "TestGetRepoRefsBranch",
Target: bitbucket.RepositoryBranchTarget{Hash: "master"},
Target: bitbucket.RepositoryRefTarget{Hash: "master"},
}

_, err := c.Repositories.Repository.CreateBranch(opt)
if err != nil {
t.Error("Could not create new branch", err)
}

refOpts := &bitbucket.RepositoryRefOptions{
refBranchOpts := &bitbucket.RepositoryRefOptions{
Owner: owner,
RepoSlug: repo,
}

resRefs, err := c.Repositories.Repository.ListRefs(refOpts)
resBranchRefs, err := c.Repositories.Repository.ListRefs(refBranchOpts)
if err != nil {
t.Error("The refs is not found.")
t.Error("The refs/branch is not found.")
}

expected := struct {
branchExpected := struct {
n string
t string
}{}

for _, ref := range resRefs.Refs {
for _, ref := range resBranchRefs.Refs {
for k, v := range ref {
// kCopy := k
vCopy := v
if val, ok := vCopy.(string); ok {
if k == "name" && val == "TestGetRepoRefsBranch" {
expected.n = val
branchExpected.n = val
}
}
if val, ok := vCopy.(string); ok {
if k == "type" && val == "branch" {
expected.t = val
branchExpected.t = val
}
}
}
}

if !(expected.n == "TestGetRepoRefsBranch" && expected.t == "branch") {
if !(branchExpected.n == "TestGetRepoRefsBranch" && branchExpected.t == "branch") {
t.Error("Could not list refs/branch that was created in test setup")
}

delBranchOpt := &bitbucket.RepositoryBranchOptions{
Owner: owner,
RepoSlug: repo,
Name: "TestGetRepoRefsBranch",
}
resBranchDel, err := c.Repositories.Repository.DeleteBranch(delBranchOpt)
if err != nil {
t.Error("Could not delete branch")
}

if mp, ok := resBranchDel.(map[string]interface{}); ok {
for k, v := range mp {
if k == "type" && v == "error" {
t.Error("Delete branch returned an error, when it should have successfully" +
"delete the test branch created during test setup")
}
}
}

// Create Tag, List Refs and search for Tag that was created,
// then test successful Tag deletion
tagOpt := &bitbucket.RepositoryTagCreationOptions{
Owner: owner,
RepoSlug: repo,
Name: "TestGetRepoRefsTag",
Target: bitbucket.RepositoryRefTarget{Hash: "master"},
}

_, err = c.Repositories.Repository.CreateTag(tagOpt)
if err != nil {
t.Error("Could not create new tag", err)
}

refTagOpts := &bitbucket.RepositoryRefOptions{
Owner: owner,
RepoSlug: repo,
}

resTagRefs, err := c.Repositories.Repository.ListRefs(refTagOpts)
if err != nil {
t.Error("The ref/tag is not found.")
}

tagExpected := struct {
n string
t string
}{}

for _, ref := range resTagRefs.Refs {
for k, v := range ref {
// kCopy := k
vCopy := v
if val, ok := vCopy.(string); ok {
if k == "name" && val == "TestGetRepoRefsTag" {
tagExpected.n = val
}
}
if val, ok := vCopy.(string); ok {
if k == "type" && val == "tag" {
tagExpected.t = val
}
}
}
}

if !(tagExpected.n == "TestGetRepoRefsTag" && tagExpected.t == "tag") {
t.Error("Could not list refs/tag that was created in test setup")
}

delTagOpt := &bitbucket.RepositoryTagOptions{
Owner: owner,
RepoSlug: repo,
Name: "TestGetRepoRefsTag",
}
resTagDel, err := c.Repositories.Repository.DeleteTag(delTagOpt)
if err != nil {
t.Error("Could not delete tag")
}

if mp, ok := resTagDel.(map[string]interface{}); ok {
for k, v := range mp {
if k == "type" && v == "error" {
t.Error("Delete tag returned an error, when it should have successfully" +
"delete the test tag created during test setup")
}
}
}
}

0 comments on commit 57c51b0

Please sign in to comment.