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

     1  package stats_test
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     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 TestHistorical(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 historical --service-id=123"),
    28  			api:        mock.API{GetStatsJSONFn: getStatsJSONOK},
    29  			wantOutput: historicalOK,
    30  		},
    31  		{
    32  			args:      args("stats historical --service-id=123"),
    33  			api:       mock.API{GetStatsJSONFn: getStatsJSONError},
    34  			wantError: errTest.Error(),
    35  		},
    36  		{
    37  			args:       args("stats historical --service-id=123 --format=json"),
    38  			api:        mock.API{GetStatsJSONFn: getStatsJSONOK},
    39  			wantOutput: historicalJSONOK,
    40  		},
    41  	}
    42  	for testcaseIdx := range scenarios {
    43  		testcase := &scenarios[testcaseIdx]
    44  		t.Run(strings.Join(testcase.args, " "), func(t *testing.T) {
    45  			var stdout bytes.Buffer
    46  			app.Init = func(_ []string, _ io.Reader) (*global.Data, error) {
    47  				opts := testutil.MockGlobalData(testcase.args, &stdout)
    48  				opts.APIClientFactory = mock.APIClient(testcase.api)
    49  				return opts, nil
    50  			}
    51  			err := app.Run(testcase.args, nil)
    52  			testutil.AssertErrorContains(t, err, testcase.wantError)
    53  			testutil.AssertStringContains(t, stdout.String(), testcase.wantOutput)
    54  		})
    55  	}
    56  }
    57  
    58  var historicalOK = `From: Wed May 15 20:08:35 UTC 2013
    59  To: Thu May 16 20:08:35 UTC 2013
    60  By: day
    61  Region: all
    62  ---
    63  Service ID:                                    123
    64  Start Time:          1970-01-01 00:00:00 +0000 UTC
    65  --------------------------------------------------
    66  Hit Rate:                                    0.00%
    67  Avg Hit Time:                               0.00µs
    68  Avg Miss Time:                              0.00µs
    69  
    70  Request BW:                                      0
    71    Headers:                                       0
    72    Body:                                          0
    73  
    74  Response BW:                                     0
    75    Headers:                                       0
    76    Body:                                          0
    77  
    78  Requests:                                        0
    79    Hit:                                           0
    80    Miss:                                          0
    81    Pass:                                          0
    82    Synth:                                         0
    83    Error:                                         0
    84    Uncacheable:                                   0
    85  `
    86  
    87  var historicalJSONOK = `{"start_time":0}
    88  `
    89  
    90  func getStatsJSONOK(_ *fastly.GetStatsInput, o any) error {
    91  	msg := []byte(`
    92  {
    93    "status": "success",
    94    "meta": {
    95      "to": "Thu May 16 20:08:35 UTC 2013",
    96      "from": "Wed May 15 20:08:35 UTC 2013",
    97      "by": "day",
    98      "region": "all"
    99    },
   100    "msg": null,
   101    "data": [{"start_time": 0}]
   102  }`)
   103  
   104  	return json.Unmarshal(msg, o)
   105  }
   106  
   107  func getStatsJSONError(_ *fastly.GetStatsInput, _ any) error {
   108  	return errTest
   109  }