github.com/esnet/gdg@v0.6.1-0.20240412190737-6b6eba9c14d8/cli/test/conections_test.go (about)

     1  package test
     2  
     3  import (
     4  	"github.com/esnet/gdg/cli"
     5  	"github.com/esnet/gdg/cli/support"
     6  	"github.com/esnet/gdg/internal/service"
     7  	"github.com/esnet/gdg/internal/service/mocks"
     8  	"github.com/grafana/grafana-openapi-client-go/models"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/mock"
    11  	"io"
    12  	"strings"
    13  	"testing"
    14  )
    15  
    16  func TestConnectionCommand(t *testing.T) {
    17  	testSvc := new(mocks.GrafanaService)
    18  	getMockSvc := func() service.GrafanaService {
    19  		return testSvc
    20  	}
    21  	resp := []models.DataSourceListItemDTO{
    22  		{
    23  			ID:        5,
    24  			Name:      "Hello",
    25  			UID:       "magicUid",
    26  			Type:      "elasticsearch",
    27  			IsDefault: false,
    28  		},
    29  	}
    30  
    31  	testSvc.EXPECT().InitOrganizations().Return()
    32  	testSvc.EXPECT().ListConnections(mock.Anything).Return(resp)
    33  
    34  	optionMockSvc := func() support.RootOption {
    35  		return func(response *support.RootCommand) {
    36  			response.GrafanaSvc = getMockSvc
    37  		}
    38  	}
    39  	r, w, cleanup := InterceptStdout()
    40  
    41  	err := cli.Execute("testing.yml", []string{"backup", "connections", "list"}, optionMockSvc())
    42  	assert.Nil(t, err)
    43  	defer cleanup()
    44  	w.Close()
    45  
    46  	out, _ := io.ReadAll(r)
    47  	outStr := string(out)
    48  	assert.True(t, strings.Contains(outStr, "magicUid"))
    49  	assert.True(t, strings.Contains(outStr, "Hello"))
    50  }