github.com/fastly/cli@v1.7.2-0.20240304164155-9d0f1d77c3bf/pkg/commands/stats/regions_test.go (about)

     1  package stats_test
     2  
     3  import (
     4  	"bytes"
     5  	"errors"
     6  	"io"
     7  	"strings"
     8  	"testing"
     9  
    10  	"github.com/fastly/go-fastly/v9/fastly"
    11  
    12  	"github.com/fastly/cli/pkg/app"
    13  	"github.com/fastly/cli/pkg/global"
    14  	"github.com/fastly/cli/pkg/mock"
    15  	"github.com/fastly/cli/pkg/testutil"
    16  )
    17  
    18  func TestRegions(t *testing.T) {
    19  	args := testutil.Args
    20  	scenarios := []struct {
    21  		args       []string
    22  		api        mock.API
    23  		wantError  string
    24  		wantOutput string
    25  	}{
    26  		{
    27  			args:       args("stats regions"),
    28  			api:        mock.API{GetRegionsFn: getRegionsOK},
    29  			wantOutput: "foo\nbar\nbaz\n",
    30  		},
    31  		{
    32  			args:      args("stats regions"),
    33  			api:       mock.API{GetRegionsFn: getRegionsError},
    34  			wantError: errTest.Error(),
    35  		},
    36  	}
    37  	for testcaseIdx := range scenarios {
    38  		testcase := &scenarios[testcaseIdx]
    39  		t.Run(strings.Join(testcase.args, " "), func(t *testing.T) {
    40  			var stdout bytes.Buffer
    41  			app.Init = func(_ []string, _ io.Reader) (*global.Data, error) {
    42  				opts := testutil.MockGlobalData(testcase.args, &stdout)
    43  				opts.APIClientFactory = mock.APIClient(testcase.api)
    44  				return opts, nil
    45  			}
    46  			err := app.Run(testcase.args, nil)
    47  			testutil.AssertErrorContains(t, err, testcase.wantError)
    48  			testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput)
    49  		})
    50  	}
    51  }
    52  
    53  func getRegionsOK() (*fastly.RegionsResponse, error) {
    54  	return &fastly.RegionsResponse{
    55  		Data: []string{"foo", "bar", "baz"},
    56  	}, nil
    57  }
    58  
    59  var errTest = errors.New("fixture error")
    60  
    61  func getRegionsError() (*fastly.RegionsResponse, error) {
    62  	return nil, errTest
    63  }