go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/pbutil/predicate_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  	pb "go.chromium.org/luci/resultdb/proto/v1"
    21  
    22  	. "github.com/smartystreets/goconvey/convey"
    23  	. "go.chromium.org/luci/common/testing/assertions"
    24  )
    25  
    26  func TestValidateTestObjectPredicate(t *testing.T) {
    27  	Convey(`TestValidateTestObjectPredicate`, t, func() {
    28  		Convey(`Empty`, func() {
    29  			err := validateTestObjectPredicate(&pb.TestResultPredicate{})
    30  			So(err, ShouldBeNil)
    31  		})
    32  
    33  		Convey(`TestID`, func() {
    34  			validate := func(TestIdRegexp string) error {
    35  				return validateTestObjectPredicate(&pb.TestResultPredicate{
    36  					TestIdRegexp: TestIdRegexp,
    37  				})
    38  			}
    39  
    40  			Convey(`empty`, func() {
    41  				So(validate(""), ShouldBeNil)
    42  			})
    43  
    44  			Convey(`valid`, func() {
    45  				So(validate("A.+"), ShouldBeNil)
    46  			})
    47  
    48  			Convey(`invalid`, func() {
    49  				So(validate(")"), ShouldErrLike, "test_id_regexp: error parsing regex")
    50  			})
    51  			Convey(`^`, func() {
    52  				So(validate("^a"), ShouldErrLike, "test_id_regexp: must not start with ^")
    53  			})
    54  			Convey(`$`, func() {
    55  				So(validate("a$"), ShouldErrLike, "test_id_regexp: must not end with $")
    56  			})
    57  		})
    58  
    59  		Convey(`Test variant`, func() {
    60  			validVariant := Variant("a", "b")
    61  			invalidVariant := Variant("", "")
    62  
    63  			validate := func(p *pb.VariantPredicate) error {
    64  				return validateTestObjectPredicate(&pb.TestResultPredicate{
    65  					Variant: p,
    66  				})
    67  			}
    68  
    69  			Convey(`Equals`, func() {
    70  				Convey(`Valid`, func() {
    71  					err := validate(&pb.VariantPredicate{
    72  						Predicate: &pb.VariantPredicate_Equals{Equals: validVariant},
    73  					})
    74  					So(err, ShouldBeNil)
    75  				})
    76  				Convey(`Invalid`, func() {
    77  					err := validate(&pb.VariantPredicate{
    78  						Predicate: &pb.VariantPredicate_Equals{Equals: invalidVariant},
    79  					})
    80  					So(err, ShouldErrLike, `variant: equals: "":"": key: unspecified`)
    81  				})
    82  			})
    83  
    84  			Convey(`Contains`, func() {
    85  				Convey(`Valid`, func() {
    86  					err := validate(&pb.VariantPredicate{
    87  						Predicate: &pb.VariantPredicate_Contains{Contains: validVariant},
    88  					})
    89  					So(err, ShouldBeNil)
    90  				})
    91  				Convey(`Invalid`, func() {
    92  					err := validate(&pb.VariantPredicate{
    93  						Predicate: &pb.VariantPredicate_Contains{Contains: invalidVariant},
    94  					})
    95  					So(err, ShouldErrLike, `variant: contains: "":"": key: unspecified`)
    96  				})
    97  			})
    98  
    99  			Convey(`Unspecified`, func() {
   100  				err := validate(&pb.VariantPredicate{})
   101  				So(err, ShouldErrLike, `variant: unspecified`)
   102  			})
   103  		})
   104  	})
   105  }
   106  
   107  func TestValidateTestResultPredicate(t *testing.T) {
   108  	t.Parallel()
   109  	Convey(`ValidateTestResultPredicate`, t, func() {
   110  		Convey(`Expectancy and ExcludeExonerated`, func() {
   111  			err := ValidateTestResultPredicate(&pb.TestResultPredicate{ExcludeExonerated: true})
   112  			So(err, ShouldErrLike, "mutually exclusive")
   113  		})
   114  	})
   115  }