go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/pbutil/variant_test.go (about)

     1  // Copyright 2019 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package pbutil
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  	. "go.chromium.org/luci/common/testing/assertions"
    22  	pb "go.chromium.org/luci/resultdb/proto/v1"
    23  )
    24  
    25  func TestValidateVariant(t *testing.T) {
    26  	t.Parallel()
    27  	Convey(`TestValidateVariant`, t, func() {
    28  		Convey(`empty`, func() {
    29  			err := ValidateVariant(Variant())
    30  			So(err, ShouldBeNil)
    31  		})
    32  
    33  		Convey(`invalid`, func() {
    34  			err := ValidateVariant(Variant("1", "b"))
    35  			So(err, ShouldErrLike, `key: does not match`)
    36  		})
    37  	})
    38  }
    39  
    40  func TestVariantUtils(t *testing.T) {
    41  	t.Parallel()
    42  
    43  	Convey(`Conversion to pair strings works`, t, func() {
    44  		v := Variant(
    45  			"key/with/part/k3", "v3",
    46  			"k1", "v1",
    47  			"key/k2", "v2",
    48  		)
    49  		So(VariantToStrings(v), ShouldResemble, []string{
    50  			"k1:v1", "key/k2:v2", "key/with/part/k3:v3",
    51  		})
    52  	})
    53  
    54  	Convey(`Conversion from pair strings works`, t, func() {
    55  		Convey(`for valid pairs`, func() {
    56  			vr, err := VariantFromStrings([]string{"k1:v1", "key/k2:v2", "key/with/part/k3:v3"})
    57  			So(err, ShouldBeNil)
    58  			So(vr, ShouldResembleProto, Variant(
    59  				"k1", "v1",
    60  				"key/k2", "v2",
    61  				"key/with/part/k3", "v3",
    62  			))
    63  		})
    64  
    65  		Convey(`for empty list returns nil`, func() {
    66  			vr, err := VariantFromStrings([]string{})
    67  			So(vr, ShouldBeNil)
    68  			So(err, ShouldBeNil)
    69  		})
    70  	})
    71  
    72  	Convey(`Key sorting works`, t, func() {
    73  		vr := Variant(
    74  			"k2", "v2",
    75  			"k3", "v3",
    76  			"k1", "v1",
    77  		)
    78  		So(SortedVariantKeys(vr), ShouldResemble, []string{"k1", "k2", "k3"})
    79  	})
    80  
    81  	Convey(`VariantToStringPairs`, t, func() {
    82  		pairs := []string{
    83  			"k1", "v1",
    84  			"k2", "v2",
    85  			"k3", "v3",
    86  		}
    87  		vr := Variant(pairs...)
    88  		So(VariantToStringPairs(vr), ShouldResemble, StringPairs(pairs...))
    89  	})
    90  
    91  	Convey(`CombineVariant`, t, func() {
    92  		baseVariant := Variant(
    93  			"key/with/part/k3", "v3",
    94  		)
    95  		additionalVariant := Variant(
    96  			// Overwrite the value for the duplicate key.
    97  			"key/with/part/k3", "v4",
    98  			"k1", "v1",
    99  			"key/k2", "v2",
   100  		)
   101  		expectedVariant := Variant(
   102  			"key/with/part/k3", "v4",
   103  			"k1", "v1",
   104  			"key/k2", "v2",
   105  		)
   106  		So(CombineVariant(baseVariant, additionalVariant), ShouldResemble, expectedVariant)
   107  	})
   108  
   109  	Convey(`CombineVariant with nil variant`, t, func() {
   110  		var baseVariant *pb.Variant
   111  		var additionalVariant *pb.Variant
   112  		var expectedVariant *pb.Variant
   113  		v1, v2 := Variant("key/with/part/k3", "v3"), Variant("key/with/part/k4", "v4")
   114  
   115  		// (nil, nil)
   116  		baseVariant, additionalVariant, expectedVariant = nil, nil, nil
   117  		So(CombineVariant(baseVariant, additionalVariant), ShouldResemble, expectedVariant)
   118  
   119  		// (variant, nil)
   120  		baseVariant, additionalVariant, expectedVariant = v1, nil, v1
   121  		So(CombineVariant(baseVariant, additionalVariant), ShouldResemble, expectedVariant)
   122  
   123  		// (nil, variant)
   124  		baseVariant, additionalVariant, expectedVariant = nil, v2, v2
   125  		So(CombineVariant(baseVariant, additionalVariant), ShouldResemble, expectedVariant)
   126  	})
   127  }