Skip to content

Commit

Permalink
CLOUD-250122: [CLI] NPE when org list is empty (#2968)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo committed May 23, 2024
1 parent 9c22e9a commit 98b65fb
Showing 1 changed file with 63 additions and 12 deletions.
75 changes: 63 additions & 12 deletions internal/cli/organizations/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,79 @@ import (
"github.com/golang/mock/gomock"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115014/admin"
)

func TestList_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockOrganizationLister(ctrl)
tests := []struct {
name string
expected *atlasv2.PaginatedOrganization
returnErr error
}{
{
name: "non-nil result",
expected: &atlasv2.PaginatedOrganization{
Results: &[]atlasv2.AtlasOrganization{{}, {}, {}},
},
},
{
name: "nil result",
expected: &atlasv2.PaginatedOrganization{
Results: nil,
},
},
{
name: "no results",
expected: &atlasv2.PaginatedOrganization{},
},
{
name: "no results",
expected: &atlasv2.PaginatedOrganization{
Results: &[]atlasv2.AtlasOrganization{},
},
},
{
name: "with results",
expected: &atlasv2.PaginatedOrganization{
Results: &[]atlasv2.AtlasOrganization{
{
Id: pointer.Get("test"),
Name: "test",
},
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockStore := mocks.NewMockOrganizationLister(ctrl)
listOpts := &ListOpts{store: mockStore}

expected := &atlasv2.PaginatedOrganization{}
mockStore.
EXPECT().
Organizations(listOpts.newOrganizationListOptions()).
Return(tt.expected, nil).
Times(1)

listOpts := &ListOpts{store: mockStore}
if err := listOpts.Run(); err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}

mockStore.
EXPECT().
Organizations(listOpts.newOrganizationListOptions()).
Return(expected, nil).
Times(1)
err := listOpts.Print(tt.expected)
if err != nil {
t.Fatalf("Print() unexpected error: %v", err)
}

if err := listOpts.Run(); err != nil {
t.Fatalf("Run() unexpected error: %v", err)
test.VerifyOutputTemplate(t, listTemplate, tt.expected)
})
}
test.VerifyOutputTemplate(t, listTemplate, expected)
}

func TestListBuilder(t *testing.T) {
Expand Down

0 comments on commit 98b65fb

Please sign in to comment.