go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/bisection/compilefailureanalysis/heuristic/heuristic_analysis_test.go (about)

     1  // Copyright 2022 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 heuristic
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	. "github.com/smartystreets/goconvey/convey"
    22  	"go.chromium.org/luci/bisection/model"
    23  	pb "go.chromium.org/luci/bisection/proto/v1"
    24  	buildbucketpb "go.chromium.org/luci/buildbucket/proto"
    25  	"go.chromium.org/luci/gae/impl/memory"
    26  	"go.chromium.org/luci/gae/service/datastore"
    27  )
    28  
    29  func TestSaveResultsToDatastore(t *testing.T) {
    30  	t.Parallel()
    31  	c := memory.Use(context.Background())
    32  
    33  	Convey("SaveResultsToDatastore", t, func() {
    34  		heuristicAnalysis := &model.CompileHeuristicAnalysis{}
    35  		err := datastore.Put(c, heuristicAnalysis)
    36  		So(err, ShouldBeNil)
    37  		datastore.GetTestable(c).CatchupIndexes()
    38  
    39  		result := &model.HeuristicAnalysisResult{
    40  			Items: []*model.HeuristicAnalysisResultItem{
    41  				{
    42  					Commit:      "12345",
    43  					ReviewUrl:   "this/is/review/url",
    44  					ReviewTitle: "title",
    45  					Justification: &model.SuspectJustification{
    46  						Items: []*model.SuspectJustificationItem{
    47  							{
    48  								Score:  10,
    49  								Reason: "failure reason",
    50  								Type:   model.JustificationType_FAILURELOG,
    51  							},
    52  						},
    53  					},
    54  				},
    55  			},
    56  		}
    57  
    58  		err = saveResultsToDatastore(c, heuristicAnalysis, result, "host", "proj", "ref")
    59  		So(err, ShouldBeNil)
    60  		datastore.GetTestable(c).CatchupIndexes()
    61  
    62  		suspects := []*model.Suspect{}
    63  		q := datastore.NewQuery("Suspect")
    64  		err = datastore.GetAll(c, q, &suspects)
    65  		So(err, ShouldBeNil)
    66  		So(len(suspects), ShouldEqual, 1)
    67  		So(suspects[0], ShouldResemble, &model.Suspect{
    68  			ParentAnalysis: datastore.KeyForObj(c, heuristicAnalysis),
    69  			Id:             suspects[0].Id,
    70  			ReviewUrl:      "this/is/review/url",
    71  			ReviewTitle:    "title",
    72  			Justification:  "failure reason",
    73  			Score:          10,
    74  			GitilesCommit: buildbucketpb.GitilesCommit{
    75  				Host:    "host",
    76  				Project: "proj",
    77  				Ref:     "ref",
    78  				Id:      "12345",
    79  			},
    80  			VerificationStatus: model.SuspectVerificationStatus_Unverified,
    81  			Type:               model.SuspectType_Heuristic,
    82  			AnalysisType:       pb.AnalysisType_COMPILE_FAILURE_ANALYSIS,
    83  		})
    84  	})
    85  }