gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/go-control-plane/pkg/cache/v3/resources_test.go (about)

     1  package cache_test
     2  
     3  import (
     4  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/cache/v3"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"gitee.com/ks-custle/core-gm/go-control-plane/pkg/cache/types"
    10  )
    11  
    12  func TestIndexResourcesByName(t *testing.T) {
    13  	tests := []struct {
    14  		name      string
    15  		resources []types.ResourceWithTTL
    16  		want      map[string]types.ResourceWithTTL
    17  	}{
    18  		{
    19  			name:      "empty",
    20  			resources: nil,
    21  			want:      map[string]types.ResourceWithTTL{},
    22  		},
    23  		{
    24  			name: "more than one",
    25  			resources: []types.ResourceWithTTL{
    26  				{Resource: testEndpoint, TTL: &ttl},
    27  				{Resource: testRoute, TTL: &ttl},
    28  			},
    29  			want: map[string]types.ResourceWithTTL{
    30  				"cluster0": {Resource: testEndpoint, TTL: &ttl},
    31  				"route0":   {Resource: testRoute, TTL: &ttl},
    32  			},
    33  		},
    34  	}
    35  
    36  	for _, tt := range tests {
    37  		t.Run(tt.name, func(t *testing.T) {
    38  			got := cache.IndexResourcesByName(tt.resources)
    39  			assert.Equal(t, tt.want, got)
    40  		})
    41  	}
    42  }
    43  
    44  func TestIndexRawResourceByName(t *testing.T) {
    45  	tests := []struct {
    46  		name      string
    47  		resources []types.Resource
    48  		want      map[string]types.Resource
    49  	}{
    50  		{
    51  			name:      "empty",
    52  			resources: nil,
    53  			want:      map[string]types.Resource{},
    54  		},
    55  		{
    56  			name: "more than one",
    57  			resources: []types.Resource{
    58  				testEndpoint,
    59  				testRoute,
    60  			},
    61  			want: map[string]types.Resource{
    62  				"cluster0": testEndpoint,
    63  				"route0":   testRoute,
    64  			},
    65  		},
    66  	}
    67  
    68  	for _, tt := range tests {
    69  		t.Run(tt.name, func(t *testing.T) {
    70  			got := cache.IndexRawResourcesByName(tt.resources)
    71  			assert.Equal(t, tt.want, got)
    72  		})
    73  	}
    74  }
    75  
    76  func TestNewResources(t *testing.T) {
    77  	resources := cache.NewResources("x", []types.Resource{
    78  		testEndpoint,
    79  		testRoute,
    80  	})
    81  
    82  	assert.NotNil(t, resources.Items)
    83  	assert.Equal(t, "x", resources.Version)
    84  }