go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/resultdb/internal/exonerations/exoneration_test.go (about)

     1  // Copyright 2020 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 exonerations
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  
    22  	. "go.chromium.org/luci/common/testing/assertions"
    23  	"go.chromium.org/luci/resultdb/internal/invocations"
    24  	"go.chromium.org/luci/resultdb/internal/spanutil"
    25  	"go.chromium.org/luci/resultdb/internal/testutil"
    26  	"go.chromium.org/luci/resultdb/internal/testutil/insert"
    27  	"go.chromium.org/luci/resultdb/pbutil"
    28  	pb "go.chromium.org/luci/resultdb/proto/v1"
    29  	"go.chromium.org/luci/server/span"
    30  )
    31  
    32  func TestRead(t *testing.T) {
    33  	Convey(`Read`, t, func() {
    34  		ctx := testutil.SpannerTestContext(t)
    35  
    36  		invID := invocations.ID("inv")
    37  
    38  		Convey("Full", func() {
    39  			// Insert a TestExoneration.
    40  			testutil.MustApply(ctx,
    41  				insert.Invocation("inv", pb.Invocation_ACTIVE, nil),
    42  				spanutil.InsertMap("TestExonerations", map[string]any{
    43  					"InvocationId":    invID,
    44  					"TestId":          "t t",
    45  					"ExonerationId":   "id",
    46  					"Variant":         pbutil.Variant("k1", "v1", "k2", "v2"),
    47  					"VariantHash":     "deadbeef",
    48  					"ExplanationHTML": spanutil.Compressed("broken"),
    49  					"Reason":          pb.ExonerationReason_OCCURS_ON_OTHER_CLS,
    50  				}))
    51  
    52  			const name = "invocations/inv/tests/t%20t/exonerations/id"
    53  			ex, err := Read(span.Single(ctx), name)
    54  			So(err, ShouldBeNil)
    55  			So(ex, ShouldResembleProto, &pb.TestExoneration{
    56  				Name:            name,
    57  				ExonerationId:   "id",
    58  				TestId:          "t t",
    59  				Variant:         pbutil.Variant("k1", "v1", "k2", "v2"),
    60  				ExplanationHtml: "broken",
    61  				VariantHash:     "deadbeef",
    62  				Reason:          pb.ExonerationReason_OCCURS_ON_OTHER_CLS,
    63  			})
    64  		})
    65  		Convey("Minimal", func() {
    66  			// Insert a TestExoneration without reason. This was only possible
    67  			// prior to May 2022. This test case can be deleted from November 2023.
    68  			testutil.MustApply(ctx,
    69  				insert.Invocation("inv", pb.Invocation_ACTIVE, nil),
    70  				spanutil.InsertMap("TestExonerations", map[string]any{
    71  					"InvocationId":  invID,
    72  					"TestId":        "t t",
    73  					"ExonerationId": "id",
    74  					"Variant":       pbutil.Variant("k1", "v1", "k2", "v2"),
    75  					"VariantHash":   "deadbeef",
    76  					"Reason":        pb.ExonerationReason_EXONERATION_REASON_UNSPECIFIED,
    77  					// Leave ExplanationHTML as null.
    78  				}))
    79  
    80  			const name = "invocations/inv/tests/t%20t/exonerations/id"
    81  			ex, err := Read(span.Single(ctx), name)
    82  			So(err, ShouldBeNil)
    83  			So(ex, ShouldResembleProto, &pb.TestExoneration{
    84  				Name:            name,
    85  				ExonerationId:   "id",
    86  				TestId:          "t t",
    87  				Variant:         pbutil.Variant("k1", "v1", "k2", "v2"),
    88  				ExplanationHtml: "",
    89  				VariantHash:     "deadbeef",
    90  				Reason:          pb.ExonerationReason_EXONERATION_REASON_UNSPECIFIED,
    91  			})
    92  		})
    93  	})
    94  }