github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/kv/kv_test.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package kv
    15  
    16  import (
    17  	"bytes"
    18  	"strings"
    19  	"testing"
    20  
    21  	. "github.com/pingcap/check"
    22  	"github.com/pingcap/tidb/types"
    23  	"go.uber.org/zap"
    24  	"go.uber.org/zap/zapcore"
    25  )
    26  
    27  type rowSuite struct{}
    28  
    29  var _ = Suite(&rowSuite{})
    30  
    31  func TestRow(t *testing.T) {
    32  	TestingT(t)
    33  }
    34  
    35  func (s *rowSuite) TestMarshal(c *C) {
    36  	dats := make([]types.Datum, 4)
    37  	dats[0].SetInt64(1)
    38  	dats[1].SetNull()
    39  	dats[2] = types.MaxValueDatum()
    40  	dats[3] = types.MinNotNullDatum()
    41  
    42  	encoder := zapcore.NewConsoleEncoder(zapcore.EncoderConfig{})
    43  	out, err := encoder.EncodeEntry(zapcore.Entry{}, []zap.Field{zapRow("row", dats)})
    44  	c.Assert(err, IsNil)
    45  	c.Assert(strings.TrimRight(out.String(), "\n"), Equals,
    46  		`{"row": ["kind: int64, val: 1", "kind: null, val: NULL", "kind: max, val: +inf", "kind: min, val: -inf"]}`)
    47  }
    48  
    49  func (s *kvSuite) TestSimplePairIter(c *C) {
    50  	pairs := []Pair{
    51  		{Key: []byte("1"), Val: []byte("a")},
    52  		{Key: []byte("2"), Val: []byte("b")},
    53  		{Key: []byte("3"), Val: []byte("c")},
    54  		{Key: []byte("5"), Val: []byte("d")},
    55  	}
    56  	expectCount := 4
    57  	iter := newSimpleKVIter(pairs)
    58  	count := 0
    59  	for iter.Next() {
    60  		count++
    61  	}
    62  	c.Assert(count, Equals, expectCount)
    63  
    64  	c.Assert(iter.First(), IsTrue)
    65  	c.Assert(iter.Last(), IsTrue)
    66  
    67  	c.Assert(iter.Seek([]byte("1")), IsTrue)
    68  	c.Assert(bytes.Equal(iter.Key(), []byte("1")), IsTrue)
    69  	c.Assert(bytes.Equal(iter.Value(), []byte("a")), IsTrue)
    70  	c.Assert(iter.Valid(), IsTrue)
    71  
    72  	c.Assert(iter.Seek([]byte("2")), IsTrue)
    73  	c.Assert(bytes.Equal(iter.Key(), []byte("2")), IsTrue)
    74  	c.Assert(bytes.Equal(iter.Value(), []byte("b")), IsTrue)
    75  	c.Assert(iter.Valid(), IsTrue)
    76  
    77  	c.Assert(iter.Seek([]byte("3")), IsTrue)
    78  	c.Assert(bytes.Equal(iter.Key(), []byte("3")), IsTrue)
    79  	c.Assert(bytes.Equal(iter.Value(), []byte("c")), IsTrue)
    80  	c.Assert(iter.Valid(), IsTrue)
    81  
    82  	// 4 not exists, so seek position will move to 5.
    83  	c.Assert(iter.Seek([]byte("4")), IsTrue)
    84  	c.Assert(bytes.Equal(iter.Key(), []byte("5")), IsTrue)
    85  	c.Assert(bytes.Equal(iter.Value(), []byte("d")), IsTrue)
    86  	c.Assert(iter.Valid(), IsTrue)
    87  
    88  	// 6 not exists, so seek position will not valid.
    89  	c.Assert(iter.Seek([]byte("6")), IsFalse)
    90  	c.Assert(iter.Valid(), IsFalse)
    91  }