github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/dmlsink/mq/dispatcher/partition/key_test.go (about) 1 // Copyright 2023 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 package partition 14 15 import ( 16 "testing" 17 18 "github.com/pingcap/tiflow/cdc/model" 19 "github.com/stretchr/testify/require" 20 ) 21 22 func TestKeyDispatcher_DispatchRowChangedEvent(t *testing.T) { 23 tests := []struct { 24 name string 25 partitionKey string 26 rowChangedEvt *model.RowChangedEvent 27 wantPartition int32 28 wantKey string 29 }{ 30 { 31 name: "dispatch to partition 0 with partition key 'foo'", 32 partitionKey: "foo", 33 rowChangedEvt: &model.RowChangedEvent{}, 34 wantPartition: 0, 35 wantKey: "foo", 36 }, 37 // Add more test cases here 38 } 39 40 for _, tt := range tests { 41 t.Run(tt.name, func(t *testing.T) { 42 d := NewKeyDispatcher(tt.partitionKey) 43 gotPartition, gotKey, err := d.DispatchRowChangedEvent(tt.rowChangedEvt, 0) 44 require.NoError(t, err) 45 if gotPartition != tt.wantPartition { 46 t.Errorf("DispatchRowChangedEvent() gotPartition = %v, want %v", gotPartition, tt.wantPartition) 47 } 48 if gotKey != tt.wantKey { 49 t.Errorf("DispatchRowChangedEvent() gotKey = %v, want %v", gotKey, tt.wantKey) 50 } 51 }) 52 } 53 }