go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/appengine/model/utils_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 model 16 17 import ( 18 "context" 19 "testing" 20 21 "go.chromium.org/luci/gae/impl/memory" 22 "go.chromium.org/luci/gae/service/datastore" 23 24 . "github.com/smartystreets/goconvey/convey" 25 ) 26 27 type testEntity struct { 28 ID int `gae:"$id"` 29 Val int 30 } 31 32 func TestGetIgnoreMissing(t *testing.T) { 33 t.Parallel() 34 35 Convey("With datastore", t, func() { 36 ctx := memory.Use(context.Background()) 37 datastore.GetTestable(ctx).AutoIndex(true) 38 39 for i := 1; i < 5; i++ { 40 So(datastore.Put(ctx, &testEntity{ID: i, Val: i}), ShouldBeNil) 41 } 42 43 Convey("No missing", func() { 44 exists := testEntity{ID: 1} 45 So(GetIgnoreMissing(ctx, &exists), ShouldBeNil) 46 So(exists.Val, ShouldEqual, 1) 47 }) 48 49 Convey("Scalar args", func() { 50 exists1 := testEntity{ID: 1} 51 exists2 := testEntity{ID: 2} 52 missing := testEntity{ID: 1000} 53 54 Convey("One", func() { 55 So(GetIgnoreMissing(ctx, &exists1), ShouldBeNil) 56 So(exists1.Val, ShouldEqual, 1) 57 58 So(GetIgnoreMissing(ctx, &missing), ShouldBeNil) 59 So(missing.Val, ShouldEqual, 0) 60 }) 61 62 Convey("Many", func() { 63 So(GetIgnoreMissing(ctx, &exists1, &missing, &exists2), ShouldBeNil) 64 So(exists1.Val, ShouldEqual, 1) 65 So(missing.Val, ShouldEqual, 0) 66 So(exists2.Val, ShouldEqual, 2) 67 }) 68 }) 69 70 Convey("Vector args", func() { 71 Convey("One", func() { 72 ents := []testEntity{{ID: 1}, {ID: 1000}, {ID: 2}} 73 74 So(GetIgnoreMissing(ctx, ents), ShouldBeNil) 75 So(ents[0].Val, ShouldEqual, 1) 76 So(ents[1].Val, ShouldEqual, 0) 77 So(ents[2].Val, ShouldEqual, 2) 78 }) 79 80 Convey("Many", func() { 81 ents1 := []testEntity{{ID: 1}, {ID: 1000}, {ID: 2}} 82 ents2 := []testEntity{{ID: 3}, {ID: 1001}, {ID: 4}} 83 84 So(GetIgnoreMissing(ctx, ents1, ents2), ShouldBeNil) 85 So(ents1[0].Val, ShouldEqual, 1) 86 So(ents1[1].Val, ShouldEqual, 0) 87 So(ents1[2].Val, ShouldEqual, 2) 88 So(ents2[0].Val, ShouldEqual, 3) 89 So(ents2[1].Val, ShouldEqual, 0) 90 So(ents2[2].Val, ShouldEqual, 4) 91 }) 92 }) 93 94 Convey("Mixed args", func() { 95 exists := testEntity{ID: 1} 96 missing := testEntity{ID: 1000} 97 ents := []testEntity{{ID: 3}, {ID: 1001}, {ID: 4}} 98 99 So(GetIgnoreMissing(ctx, &exists, &missing, ents), ShouldBeNil) 100 So(exists.Val, ShouldEqual, 1) 101 So(missing.Val, ShouldEqual, 0) 102 So(ents[0].Val, ShouldEqual, 3) 103 So(ents[1].Val, ShouldEqual, 0) 104 So(ents[2].Val, ShouldEqual, 4) 105 }) 106 }) 107 }