github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/processor/tablepb/table_test.go (about) 1 // Copyright 2022 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 tablepb 15 16 import ( 17 "encoding/hex" 18 "encoding/json" 19 "strings" 20 "testing" 21 22 "github.com/gogo/protobuf/proto" 23 "github.com/stretchr/testify/require" 24 ) 25 26 func decode(s string) []byte { 27 b, _ := hex.DecodeString(s) 28 return b 29 } 30 31 func TestSpanString(t *testing.T) { 32 t.Parallel() 33 34 span := Span{ 35 TableID: 555555, 36 StartKey: decode("7480000000000009ff89000000f8"), 37 EndKey: decode("748000000000000dffaa5f6980ff"), 38 } 39 require.Equal(t, strings.TrimSpace(` 40 {table_id:555555,start_key:7480000000000009ff89000000f8,end_key:748000000000000dffaa5f6980ff} 41 `), span.String()) 42 43 span = Span{ 44 TableID: 555555, 45 } 46 require.Equal(t, `{table_id:555555}`, span.String()) 47 } 48 49 func TestSpanJSON(t *testing.T) { 50 t.Parallel() 51 52 span := Span{ 53 TableID: 555555, 54 StartKey: decode("7480000000000009ff89000000f8"), 55 EndKey: decode("748000000000000dffaa5f6980ff"), 56 } 57 js, _ := json.Marshal(span) 58 require.Equal(t, strings.TrimSpace(` 59 "{table_id:555555,start_key:7480000000000009ff89000000f8,end_key:748000000000000dffaa5f6980ff}" 60 `), string(js)) 61 62 span = Span{ 63 TableID: 555555, 64 } 65 js, _ = json.Marshal(span) 66 require.Equal(t, `"{table_id:555555}"`, string(js)) 67 } 68 69 func TestSpanProtoMarshalText(t *testing.T) { 70 t.Parallel() 71 72 span := Span{ 73 TableID: 555555, 74 StartKey: decode("7480000000000009ff89000000f8"), 75 EndKey: decode("748000000000000dffaa5f6980ff"), 76 } 77 s := proto.CompactTextString(&span) 78 require.Equal(t, strings.TrimSpace(` 79 {table_id:555555,start_key:7480000000000009ff89000000f8,end_key:748000000000000dffaa5f6980ff} 80 `), s) 81 } 82 83 func TestSpanLess(t *testing.T) { 84 t.Parallel() 85 86 a1 := &Span{TableID: 1, StartKey: []byte("a"), EndKey: []byte("b")} 87 a2 := &Span{TableID: 1, StartKey: []byte("c"), EndKey: []byte("d")} 88 b := &Span{TableID: 2, StartKey: []byte("e"), EndKey: []byte("f")} 89 require.True(t, a1.Less(a2)) 90 require.True(t, a1.Less(b)) 91 require.True(t, a2.Less(b)) 92 } 93 94 func TestSpanEq(t *testing.T) { 95 t.Parallel() 96 97 a := &Span{TableID: 1, StartKey: []byte("a"), EndKey: []byte("b")} 98 b := &Span{TableID: 1, StartKey: []byte("a"), EndKey: []byte("b")} 99 require.True(t, a.Eq(a)) 100 require.True(t, a.Eq(b)) 101 require.True(t, b.Eq(a)) 102 103 c := &Span{TableID: 1, StartKey: []byte("a"), EndKey: []byte("c")} 104 require.False(t, a.Eq(c)) 105 d := &Span{TableID: 2, StartKey: []byte("a"), EndKey: []byte("b")} 106 require.False(t, a.Eq(d)) 107 require.True(t, d.Eq(d)) 108 }