github.com/Axway/agent-sdk@v1.1.101/pkg/apic/apiserver/clients/api/v1/index_test.go (about)

     1  package v1
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  )
     7  
     8  func TestIndex(t *testing.T) {
     9  	testCases := []struct {
    10  		name     string
    11  		init     index
    12  		old      []string
    13  		new      []string
    14  		idxVal   string
    15  		expected index
    16  	}{
    17  		{
    18  			"add 2 keys",
    19  			index{},
    20  			[]string{},
    21  			[]string{"idx1", "idx2"},
    22  			"val",
    23  			index{
    24  				"idx1": []string{"val"},
    25  				"idx2": []string{"val"},
    26  			},
    27  		},
    28  		{
    29  			"delete 2 keys",
    30  			index{
    31  				"idx1": []string{"val"},
    32  				"idx2": []string{"val"},
    33  			},
    34  			[]string{"idx1", "idx2"},
    35  			[]string{},
    36  			"val",
    37  			index{
    38  				"idx1": []string{},
    39  				"idx2": []string{},
    40  			},
    41  		},
    42  		{
    43  			"delete 1 key, add one key",
    44  			index{
    45  				"idx1": []string{"val"},
    46  				"idx2": []string{"val"},
    47  			},
    48  			[]string{"idx1", "idx2"},
    49  			[]string{"idx2", "idx3"},
    50  			"val",
    51  			index{
    52  				"idx1": []string{},
    53  				"idx2": []string{"val"},
    54  				"idx3": []string{"val"},
    55  			},
    56  		},
    57  		{
    58  			"delete 1 key, add one key, keep ",
    59  			index{
    60  				"idx1": []string{"otherval", "val"},
    61  				"idx2": []string{"val"},
    62  				"idx3": []string{"otherval"},
    63  			},
    64  			[]string{"idx1", "idx2"},
    65  			[]string{"idx2", "idx3"},
    66  			"val",
    67  			index{
    68  				"idx1": []string{"otherval"},
    69  				"idx2": []string{"val"},
    70  				"idx3": []string{"otherval", "val"},
    71  			},
    72  		},
    73  	}
    74  
    75  	for i := range testCases {
    76  		tc := testCases[i]
    77  
    78  		t.Run(tc.name, func(t *testing.T) {
    79  			tc.init.Update(tc.old, tc.new, tc.idxVal)
    80  
    81  			if !reflect.DeepEqual(tc.init, tc.expected) {
    82  				t.Errorf("got: %+v, expected: %+v", tc.init, tc.expected)
    83  			}
    84  		})
    85  	}
    86  }
    87  
    88  func TestSets(t *testing.T) {
    89  	opIntersection := func(set1 set, set2 set) set {
    90  		return set1.Intersection(set2)
    91  	}
    92  	opUnion := func(set1 set, set2 set) set {
    93  		return set1.Union(set2)
    94  	}
    95  
    96  	testCases := []struct {
    97  		name     string
    98  		set1     set
    99  		set2     set
   100  		op       func(set1 set, set2 set) set
   101  		expected set
   102  	}{{
   103  		"empty intersection with empty",
   104  		set{},
   105  		set{},
   106  		opIntersection,
   107  		set{},
   108  	}, {
   109  		"empty union with empty",
   110  		set{},
   111  		set{},
   112  		opUnion,
   113  		set{},
   114  	}, {
   115  		"empty intersection with one",
   116  		set{},
   117  		newSet("val"),
   118  		opIntersection,
   119  		set{},
   120  	}, {
   121  		"empty union with one",
   122  		set{},
   123  		newSet("val"),
   124  		opUnion,
   125  		newSet("val"),
   126  	}, {
   127  		"identical intersection",
   128  		newSet("val1", "val2"),
   129  		newSet("val1", "val2"),
   130  		opIntersection,
   131  		newSet("val1", "val2"),
   132  	}, {
   133  		"identical union with one",
   134  		newSet("val1", "val2"),
   135  		newSet("val1", "val2"),
   136  		opUnion,
   137  		newSet("val1", "val2"),
   138  	}, {
   139  		"intersection with some common",
   140  		newSet("common1", "common2", "diff_1_1", "diff_1_2"),
   141  		newSet("common1", "common2", "diff_2_1", "diff_2_2"),
   142  		opIntersection,
   143  		newSet("common1", "common2"),
   144  	}, {
   145  		"union with some common",
   146  		newSet("common1", "common2", "diff_1_1", "diff_1_2"),
   147  		newSet("common1", "common2", "diff_2_1", "diff_2_2"),
   148  		opUnion,
   149  		newSet("common1", "common2", "diff_1_1", "diff_1_2", "diff_2_1", "diff_2_2"),
   150  	},
   151  	}
   152  
   153  	for i := range testCases {
   154  		tc := testCases[i]
   155  		t.Run(tc.name, func(t *testing.T) {
   156  			got := tc.op(tc.set1, tc.set2)
   157  
   158  			if !reflect.DeepEqual(got, tc.expected) {
   159  				t.Errorf("got: %+v, expected: %+v", got, tc.expected)
   160  			}
   161  			got = tc.op(tc.set2, tc.set1)
   162  
   163  			if !reflect.DeepEqual(got, tc.expected) {
   164  				t.Errorf("terms inverted: got: %+v, expected: %+v", got, tc.expected)
   165  			}
   166  		})
   167  	}
   168  }