From 6b195176430c5ddd726de6f57534db5c3943635e Mon Sep 17 00:00:00 2001 From: Zahiar Ahmed <310030+zahiar@users.noreply.github.com> Date: Tue, 15 Feb 2022 18:25:22 +0000 Subject: [PATCH] GH-73 Replace IF-style test assertion remnants with `stretchr/testify` (#75) --- bitbucket/api/v1/group_members_test.go | 53 ++++++------------ bitbucket/api/v1/group_privileges_test.go | 65 +++++++---------------- bitbucket/api/v1/groups_test.go | 60 ++++++--------------- 3 files changed, 50 insertions(+), 128 deletions(-) diff --git a/bitbucket/api/v1/group_members_test.go b/bitbucket/api/v1/group_members_test.go index 94460e8..f798b95 100644 --- a/bitbucket/api/v1/group_members_test.go +++ b/bitbucket/api/v1/group_members_test.go @@ -3,6 +3,8 @@ package v1 import ( "os" "testing" + + "github.com/stretchr/testify/assert" ) func TestGroupMembers(t *testing.T) { @@ -24,12 +26,10 @@ func TestGroupMembers(t *testing.T) { Name: "tf-bb-group-members-test", }, ) - if group == nil { - t.Error("The Group could not be created.") - } + assert.NotNil(t, group, "The Group could not be created") }) - t.Run("Create", func(t *testing.T) { + t.Run("create", func(t *testing.T) { result, err := c.GroupMembers.Create( &GroupMemberOptions{ OwnerUuid: c.Auth.Username, @@ -38,37 +38,25 @@ func TestGroupMembers(t *testing.T) { }, ) - if err != nil { - t.Error(err) - } - - if result == nil { - t.Error("Expected result") - } else if result.UUID != group.Owner.Uuid { - t.Error("The GroupMember list contains an unexpected member.") - } + assert.NoError(t, err) + assert.NotNil(t, result) + assert.Equal(t, group.Owner.Uuid, result.UUID, "The GroupMember list contains an unexpected member") }) - t.Run("Get", func(t *testing.T) { + t.Run("get", func(t *testing.T) { members, err := c.GroupMembers.Get( &GroupMemberOptions{ OwnerUuid: c.Auth.Username, Slug: group.Slug, }, ) - if err != nil { - t.Error(err) - } - if len(members) != 1 { - t.Error("The GroupMember list contains unexpected members.") - } - if members[0].UUID != group.Owner.Uuid { - t.Error("The GroupMember list contains an unexpected member.") - } + assert.NoError(t, err) + assert.Equal(t, 1, len(members), "The GroupMember list contains unexpected members") + assert.Equal(t, group.Owner.Uuid, members[0].UUID, "The GroupMember list contains an unexpected member") }) - t.Run("Delete", func(t *testing.T) { + t.Run("delete", func(t *testing.T) { err := c.GroupMembers.Delete( &GroupMemberOptions{ OwnerUuid: c.Auth.Username, @@ -76,9 +64,7 @@ func TestGroupMembers(t *testing.T) { UserUuid: group.Owner.Uuid, }, ) - if err != nil { - t.Error(err) - } + assert.NoError(t, err) members, err := c.GroupMembers.Get( &GroupMemberOptions{ @@ -86,13 +72,8 @@ func TestGroupMembers(t *testing.T) { Slug: group.Slug, }, ) - if err != nil { - t.Error(err) - } - - if len(members) != 0 { - t.Error("The GroupMember list contains unexpected members after deleting the member.") - } + assert.NoError(t, err) + assert.Equal(t, 0, len(members), "The GroupMember list contains unexpected members after deleting the member") }) t.Run("teardown", func(t *testing.T) { @@ -101,8 +82,6 @@ func TestGroupMembers(t *testing.T) { Slug: group.Slug, } err := c.Groups.Delete(opt) - if err != nil { - t.Error(err) - } + assert.NoError(t, err) }) } diff --git a/bitbucket/api/v1/group_privileges_test.go b/bitbucket/api/v1/group_privileges_test.go index fcd74ce..dd21177 100644 --- a/bitbucket/api/v1/group_privileges_test.go +++ b/bitbucket/api/v1/group_privileges_test.go @@ -5,6 +5,7 @@ import ( "testing" gobb "github.com/ktrysmt/go-bitbucket" + "github.com/stretchr/testify/assert" ) func TestGroupPrivileges(t *testing.T) { @@ -33,9 +34,7 @@ func TestGroupPrivileges(t *testing.T) { Name: "tf-bb-group-test", }, ) - if group == nil { - t.Error("The Group could not be created.") - } + assert.NotNil(t, group) project, _ = gobbClient.Workspaces.CreateProject( &gobb.ProjectOptions{ @@ -45,9 +44,7 @@ func TestGroupPrivileges(t *testing.T) { IsPrivate: true, }, ) - if project == nil { - t.Error("The Project could not be created.") - } + assert.NotNil(t, project) repo, _ = gobbClient.Repositories.Repository.Create( &gobb.RepositoryOptions{ @@ -58,9 +55,7 @@ func TestGroupPrivileges(t *testing.T) { IsPrivate: "true", }, ) - if repo == nil { - t.Error("The Repository could not be created.") - } + assert.NotNil(t, repo) }) t.Run("create", func(t *testing.T) { @@ -72,19 +67,11 @@ func TestGroupPrivileges(t *testing.T) { Privilege: "write", } groupPrivilege, err := c.GroupPrivileges.Create(opt) - if err != nil { - t.Error(err) - } - if groupPrivilege.Privilege != "write" { - t.Error("The Group Privilege `privilege` attribute does not match the expected value.") - } - if groupPrivilege.Group.Slug != group.Slug { - t.Error("The Group Privilege `group.slug` attribute does not match the expected value.") - } - if groupPrivilege.Repository.Slug != repo.Slug { - t.Error("The Group Privilege `repo.slug` attribute does not match the expected value.") - } + assert.NoError(t, err) + assert.Equal(t, "write", groupPrivilege.Privilege) + assert.Equal(t, group.Slug, groupPrivilege.Group.Slug) + assert.Equal(t, repo.Slug, groupPrivilege.Repository.Slug) }) t.Run("get", func(t *testing.T) { @@ -95,19 +82,11 @@ func TestGroupPrivileges(t *testing.T) { GroupSlug: group.Slug, } groupPrivilege, err := c.GroupPrivileges.Get(opt) - if err != nil { - t.Error(err) - } - if groupPrivilege.Privilege != "write" { - t.Error("The Group Privilege `privilege` attribute does not match the expected value.") - } - if groupPrivilege.Group.Slug != group.Slug { - t.Error("The Group Privilege `group.slug` attribute does not match the expected value.") - } - if groupPrivilege.Repository.Slug != repo.Slug { - t.Error("The Group Privilege `repo.slug` attribute does not match the expected value.") - } + assert.NoError(t, err) + assert.Equal(t, "write", groupPrivilege.Privilege) + assert.Equal(t, group.Slug, groupPrivilege.Group.Slug) + assert.Equal(t, repo.Slug, groupPrivilege.Repository.Slug) }) t.Run("delete", func(t *testing.T) { @@ -119,19 +98,17 @@ func TestGroupPrivileges(t *testing.T) { GroupSlug: group.Slug, }, ) - if err != nil { - t.Error(err) - } + assert.NoError(t, err) + }) - err = c.Groups.Delete( + t.Run("teardown", func(t *testing.T) { + err := c.Groups.Delete( &GroupOptions{ OwnerUuid: c.Auth.Username, Slug: group.Slug, }, ) - if err != nil { - t.Error(err) - } + assert.NoError(t, err) _, err = gobbClient.Repositories.Repository.Delete( &gobb.RepositoryOptions{ @@ -140,9 +117,7 @@ func TestGroupPrivileges(t *testing.T) { Project: project.Key, }, ) - if err != nil { - t.Error(err) - } + assert.NoError(t, err) _, err = gobbClient.Workspaces.DeleteProject( &gobb.ProjectOptions{ @@ -151,8 +126,6 @@ func TestGroupPrivileges(t *testing.T) { Key: project.Key, }, ) - if err != nil { - t.Error(err) - } + assert.NoError(t, err) }) } diff --git a/bitbucket/api/v1/groups_test.go b/bitbucket/api/v1/groups_test.go index f4e1e9d..976288c 100644 --- a/bitbucket/api/v1/groups_test.go +++ b/bitbucket/api/v1/groups_test.go @@ -28,19 +28,11 @@ func TestGroups(t *testing.T) { } group, err := c.Groups.Create(opt) - if err != nil { - t.Error(err) - } - if group.Name != name { - t.Error("The Group `name` attribute does not match the expected value.") - } - if group.AutoAdd != false { - t.Error("The Group `auto_add` attribute does not match the expected value.") - } - if group.Permission != "" { - t.Error("The Group `permission` attribute does not match the expected value.") - } + assert.NoError(t, err) + assert.Equal(t, name, group.Name) + assert.False(t, group.AutoAdd) + assert.Empty(t, group.Permission) groupResourceSlug = group.Slug }) @@ -51,22 +43,12 @@ func TestGroups(t *testing.T) { Slug: groupResourceSlug, } group, err := c.Groups.Get(opt) - if err != nil { - t.Error(err) - } - if group.Name != name { - t.Error("The Group `name` attribute does not match the expected value.") - } - if group.AutoAdd != false { - t.Error("The Group `auto_add` attribute does not match the expected value.") - } - if group.Permission != "" { - t.Error("The Group `permission` attribute does not match the expected value.") - } - if group.Slug != groupResourceSlug { - t.Error("The Group `slug` attribute does not match the expected value.") - } + assert.NoError(t, err) + assert.Equal(t, name, group.Name) + assert.False(t, group.AutoAdd) + assert.Empty(t, group.Permission) + assert.Equal(t, groupResourceSlug, group.Slug) }) t.Run("update", func(t *testing.T) { @@ -77,22 +59,12 @@ func TestGroups(t *testing.T) { Permission: "write", } group, err := c.Groups.Update(opt) - if err != nil { - t.Error(err) - } - if group.Name != name { - t.Error("The Group `name` attribute does not match the expected value.") - } - if group.AutoAdd != true { - t.Error("The Group `auto_add` attribute does not match the expected value.") - } - if group.Permission != "write" { - t.Error("The Group `permission` attribute does not match the expected value.") - } - if group.Slug != groupResourceSlug { - t.Error("The Group `slug` attribute does not match the expected value.") - } + assert.NoError(t, err) + assert.Equal(t, name, group.Name) + assert.True(t, group.AutoAdd) + assert.Equal(t, "write", group.Permission) + assert.Equal(t, groupResourceSlug, group.Slug) }) t.Run("delete", func(t *testing.T) { @@ -101,9 +73,7 @@ func TestGroups(t *testing.T) { Slug: groupResourceSlug, } err := c.Groups.Delete(opt) - if err != nil { - t.Error(err) - } + assert.NoError(t, err) }) }