github.com/m-lab/locate@v0.17.6/api/v2/scanner_test.go (about)

     1  package v2
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/go-test/deep"
     8  	"github.com/gomodule/redigo/redis"
     9  	"github.com/m-lab/go/testingx"
    10  )
    11  
    12  var tests = []struct {
    13  	name     string
    14  	receiver redis.Scanner
    15  	scanObj  interface{}
    16  }{
    17  	{
    18  		name:     "registration-success",
    19  		receiver: &Registration{},
    20  		scanObj: &Registration{
    21  			City:          "New York",
    22  			CountryCode:   "US",
    23  			ContinentCode: "NA",
    24  			Experiment:    "ndt",
    25  			Hostname:      "ndt-mlab1-lga0t.mlab-sandbox.measurement-lab.org",
    26  			Latitude:      40.7667,
    27  			Longitude:     -73.8667,
    28  			Machine:       "mlab1",
    29  			Metro:         "lga",
    30  			Project:       "mlab-sandbox",
    31  			Site:          "lga0t",
    32  			Type:          "physical",
    33  			Uplink:        "10g",
    34  			Services: map[string][]string{
    35  				"ndt/ndt7": {
    36  					"ws://ndt/v7/upload",
    37  					"ws://ndt/v7/download",
    38  					"wss://ndt/v7/upload",
    39  					"wss://ndt/v7/download",
    40  				},
    41  			},
    42  		},
    43  	},
    44  	{
    45  		name:     "health-success",
    46  		receiver: &Health{},
    47  		scanObj: &Health{
    48  			Score: 1.0,
    49  		},
    50  	},
    51  	{
    52  		name:     "prometheus-success",
    53  		receiver: &Prometheus{},
    54  		scanObj: &Prometheus{
    55  			Health: true,
    56  		},
    57  	},
    58  }
    59  
    60  func TestRedisScan_Success(t *testing.T) {
    61  	for _, tt := range tests {
    62  		t.Run(tt.name, func(t *testing.T) {
    63  			b, err := json.Marshal(tt.scanObj)
    64  			testingx.Must(t, err, "failed to marshal obj")
    65  
    66  			err = tt.receiver.RedisScan(b)
    67  			if err != nil {
    68  				t.Fatalf("RedisScan() error: %+v, want: nil", err)
    69  			}
    70  
    71  			if diff := deep.Equal(tt.receiver, tt.scanObj); diff != nil {
    72  				t.Errorf("RedisScan() failed to scan object; got: %+v. want: %+v", tt.receiver,
    73  					tt.scanObj)
    74  			}
    75  		})
    76  	}
    77  }
    78  
    79  func TestRedisScan_Failure(t *testing.T) {
    80  	for _, tt := range tests {
    81  		t.Run(tt.name, func(t *testing.T) {
    82  			err := tt.receiver.RedisScan(tt.scanObj)
    83  			if err == nil {
    84  				t.Error("RedisScan() error: nil, want: err")
    85  			}
    86  		})
    87  	}
    88  }