go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gae/service/datastore/raw_interface_test.go (about)

     1  // Copyright 2015 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 datastore
    16  
    17  import (
    18  	"testing"
    19  
    20  	. "github.com/smartystreets/goconvey/convey"
    21  )
    22  
    23  func TestMultiMetaGetter(t *testing.T) {
    24  	t.Parallel()
    25  
    26  	Convey("Test MultiMetaGetter", t, func() {
    27  		Convey("nil", func() {
    28  			mmg := NewMultiMetaGetter(nil)
    29  			val, ok := mmg.GetMeta(7, "hi")
    30  			So(ok, ShouldBeFalse)
    31  			So(val, ShouldBeNil)
    32  
    33  			So(GetMetaDefault(mmg.GetSingle(7), "hi", "value"), ShouldEqual, "value")
    34  
    35  			m := mmg.GetSingle(10)
    36  			val, ok = m.GetMeta("hi")
    37  			So(ok, ShouldBeFalse)
    38  			So(val, ShouldBeNil)
    39  
    40  			So(GetMetaDefault(m, "hi", "value"), ShouldEqual, "value")
    41  		})
    42  
    43  		Convey("stuff", func() {
    44  			pmaps := []PropertyMap{{}, nil, {}}
    45  			So(pmaps[0].SetMeta("hi", "thing"), ShouldBeTrue)
    46  			So(pmaps[2].SetMeta("key", 100), ShouldBeTrue)
    47  			mmg := NewMultiMetaGetter(pmaps)
    48  
    49  			// oob is OK
    50  			So(GetMetaDefault(mmg.GetSingle(7), "hi", "value"), ShouldEqual, "value")
    51  
    52  			// nil is OK
    53  			So(GetMetaDefault(mmg.GetSingle(1), "key", true), ShouldEqual, true)
    54  
    55  			val, ok := mmg.GetMeta(0, "hi")
    56  			So(ok, ShouldBeTrue)
    57  			So(val, ShouldEqual, "thing")
    58  
    59  			So(GetMetaDefault(mmg.GetSingle(2), "key", 20), ShouldEqual, 100)
    60  		})
    61  	})
    62  }