Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Create test for ListRefs
This commit creates a test case for ListRefs based on setting up the
test repo with a new branch and then listing the refs and making sure
properly created the test repo. So, this test case technically also
tests a portion of the functionality of the CreateBranch function.
  • Loading branch information
codeaucafe committed Jul 28, 2021
commit 6dd40ec84ebb1048676f1e36c85909f048f8a4e6
71 changes: 71 additions & 0 deletions tests/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,74 @@ func TestDeleteRepositoryPipelineVariables(t *testing.T) {
t.Error(err)
}
}

func TestGetRepositoryRefs(t *testing.T) {

user := os.Getenv("BITBUCKET_TEST_USERNAME")
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
owner := os.Getenv("BITBUCKET_TEST_OWNER")
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")

if user == "" {
t.Error("BITBUCKET_TEST_USERNAME is empty.")
}
if pass == "" {
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
}
if owner == "" {
t.Error("BITBUCKET_TEST_OWNER is empty.")
}
if repo == "" {
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
}

c := bitbucket.NewBasicAuth(user, pass)

opt := &bitbucket.RepositoryBranchCreationOptions{
Owner: owner,
RepoSlug: repo,
Name: "TestGetRepoRefsBranch",
Target: bitbucket.RepositoryBranchTarget{Hash: "master"},
}

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

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

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

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

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

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