vitess.io/vitess@v0.16.2/go/vt/vtadmin/testutil/proto_compare.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testutil
    18  
    19  import (
    20  	"fmt"
    21  	"sort"
    22  	"testing"
    23  
    24  	"vitess.io/vitess/go/test/utils"
    25  
    26  	vtadminpb "vitess.io/vitess/go/vt/proto/vtadmin"
    27  )
    28  
    29  // AssertKeyspaceSlicesEqual is a convenience function to assert that two
    30  // []*vtadminpb.Keyspaces slices are equal
    31  func AssertKeyspaceSlicesEqual(t *testing.T, expected []*vtadminpb.Keyspace, actual []*vtadminpb.Keyspace) {
    32  	t.Helper()
    33  	for _, ks := range [][]*vtadminpb.Keyspace{expected, actual} {
    34  		for _, k := range ks {
    35  			if k.Shards != nil {
    36  				for _, ss := range k.Shards {
    37  					ss.Shard.KeyRange = nil
    38  				}
    39  			}
    40  		}
    41  	}
    42  	sort.Slice(expected, func(i, j int) bool {
    43  		return fmt.Sprintf("%v", expected[i]) < fmt.Sprintf("%v", expected[j])
    44  	})
    45  	sort.Slice(actual, func(i, j int) bool {
    46  		return fmt.Sprintf("%v", actual[i]) < fmt.Sprintf("%v", actual[j])
    47  	})
    48  	utils.MustMatch(t, expected, actual)
    49  }
    50  
    51  // AssertSchemaSlicesEqual is a convenience function to assert that two
    52  // []*vtadminpb.Schema slices are equal
    53  func AssertSchemaSlicesEqual(t *testing.T, expected []*vtadminpb.Schema, actual []*vtadminpb.Schema) {
    54  	t.Helper()
    55  	sort.Slice(expected, func(i, j int) bool {
    56  		return fmt.Sprintf("%v", expected[i]) < fmt.Sprintf("%v", expected[j])
    57  	})
    58  	sort.Slice(actual, func(i, j int) bool {
    59  		return fmt.Sprintf("%v", actual[i]) < fmt.Sprintf("%v", actual[j])
    60  	})
    61  	utils.MustMatch(t, expected, actual)
    62  }
    63  
    64  // AssertSrvVSchemaSlicesEqual is a convenience function to assert that two
    65  // []*vtadminpb.SrvVSchema slices are equal
    66  func AssertSrvVSchemaSlicesEqual(t *testing.T, expected []*vtadminpb.SrvVSchema, actual []*vtadminpb.SrvVSchema) {
    67  	t.Helper()
    68  
    69  	sort.Slice(expected, func(i, j int) bool {
    70  		return fmt.Sprintf("%v", expected[i]) < fmt.Sprintf("%v", expected[j])
    71  	})
    72  	sort.Slice(actual, func(i, j int) bool {
    73  		return fmt.Sprintf("%v", actual[i]) < fmt.Sprintf("%v", actual[j])
    74  	})
    75  	utils.MustMatch(t, expected, actual)
    76  }
    77  
    78  // AssertTabletSlicesEqual is a convenience function to assert that two
    79  // []*vtadminpb.Tablet slices are equal
    80  func AssertTabletSlicesEqual(t *testing.T, expected []*vtadminpb.Tablet, actual []*vtadminpb.Tablet) {
    81  	t.Helper()
    82  	sort.Slice(expected, func(i, j int) bool {
    83  		return fmt.Sprintf("%v", expected[i]) < fmt.Sprintf("%v", expected[j])
    84  	})
    85  	sort.Slice(actual, func(i, j int) bool {
    86  		return fmt.Sprintf("%v", actual[i]) < fmt.Sprintf("%v", actual[j])
    87  	})
    88  	utils.MustMatch(t, expected, actual)
    89  }