From 57c51b0420fcbbbf53d2a2b7b4838c8a495aef13 Mon Sep 17 00:00:00 2001 From: David Dansby <39511285+DataDavD@users.noreply.github.com> Date: Thu, 29 Jul 2021 23:33:28 -0700 Subject: [PATCH] Create test for DeleteBranch and DeleteTag and update Refs test 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. --- tests/repository_test.go | 110 +++++++++++++++++++++++++++++++++++---- 1 file changed, 100 insertions(+), 10 deletions(-) diff --git a/tests/repository_test.go b/tests/repository_test.go index 263c6d3..c4890d2 100644 --- a/tests/repository_test.go +++ b/tests/repository_test.go @@ -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") @@ -300,7 +302,7 @@ 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) @@ -308,39 +310,127 @@ func TestGetRepositoryRefs(t *testing.T) { 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") + } + } + } }