Skip to content

Commit 030ea5a

Browse files
committed
Improve unit test coverage
Signed-off-by: Matt Lord <mattalord@gmail.com>
1 parent c96807f commit 030ea5a

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
Copyright 2025 The Vitess Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package vdiff
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
25+
"vitess.io/vitess/go/sqltypes"
26+
"vitess.io/vitess/go/stats"
27+
"vitess.io/vitess/go/vt/binlog/binlogplayer"
28+
29+
querypb "vitess.io/vitess/go/vt/proto/query"
30+
tabletmanagerdatapb "vitess.io/vitess/go/vt/proto/tabletmanagerdata"
31+
)
32+
33+
func TestUpdateTableProgress(t *testing.T) {
34+
dr := &DiffReport{
35+
TableName: "test",
36+
ProcessedRows: 1e9,
37+
}
38+
queryTemplate := `update _vt.vdiff_table set rows_compared = 1000000000, lastpk = '%s', report = '{"TableName":"test","ProcessedRows":1000000000,"MatchingRows":0,"MismatchedRows":0,"ExtraRowsSource":0,"ExtraRowsTarget":0}' where vdiff_id = 1 and table_name = 'test'`
39+
40+
testCases := []struct {
41+
name string
42+
fields []*querypb.Field
43+
pkCols []int
44+
sourcePkCols []int
45+
lastRow []sqltypes.Value
46+
expectedLastPK string
47+
wantErr bool
48+
}{
49+
{
50+
name: "identical PKs",
51+
fields: []*querypb.Field{
52+
{
53+
Name: "a", Type: sqltypes.Int64,
54+
},
55+
{
56+
Name: "b", Type: sqltypes.Int64,
57+
},
58+
},
59+
pkCols: []int{0, 1},
60+
sourcePkCols: []int{0, 1},
61+
lastRow: []sqltypes.Value{sqltypes.NewInt64(1), sqltypes.NewInt64(2)},
62+
expectedLastPK: `target:{fields:{name:"a" type:INT64} fields:{name:"b" type:INT64} rows:{lengths:1 lengths:1 values:"12"}}`,
63+
},
64+
{
65+
name: "more PK cols on target",
66+
fields: []*querypb.Field{
67+
{
68+
Name: "a", Type: sqltypes.Int64,
69+
},
70+
{
71+
Name: "b", Type: sqltypes.Int64,
72+
},
73+
},
74+
pkCols: []int{0, 1},
75+
sourcePkCols: []int{0},
76+
lastRow: []sqltypes.Value{sqltypes.NewInt64(1), sqltypes.NewInt64(2)},
77+
expectedLastPK: `target:{fields:{name:"a" type:INT64} fields:{name:"b" type:INT64} rows:{lengths:1 lengths:1 values:"12"}} source:{fields:{name:"a" type:INT64} rows:{lengths:1 values:"1"}}`,
78+
},
79+
{
80+
name: "more PK cols on source",
81+
fields: []*querypb.Field{
82+
{
83+
Name: "a", Type: sqltypes.Int64,
84+
},
85+
{
86+
Name: "b", Type: sqltypes.Int64,
87+
},
88+
},
89+
pkCols: []int{0},
90+
sourcePkCols: []int{0, 1},
91+
lastRow: []sqltypes.Value{sqltypes.NewInt64(1), sqltypes.NewInt64(2)},
92+
expectedLastPK: `target:{fields:{name:"a" type:INT64} rows:{lengths:1 values:"1"}} source:{fields:{name:"a" type:INT64} fields:{name:"b" type:INT64} rows:{lengths:1 lengths:1 values:"12"}}`,
93+
},
94+
}
95+
96+
for _, tc := range testCases {
97+
t.Run(tc.name, func(t *testing.T) {
98+
dbc := binlogplayer.NewMockDBClient(t)
99+
dbc.ExpectRequest(fmt.Sprintf(queryTemplate, tc.expectedLastPK), &sqltypes.Result{}, nil)
100+
td := &tableDiffer{
101+
wd: &workflowDiffer{
102+
ct: &controller{
103+
id: 1,
104+
TableDiffRowCounts: stats.NewCountersWithSingleLabel("", "", "Rows"),
105+
},
106+
opts: &tabletmanagerdatapb.VDiffOptions{
107+
CoreOptions: &tabletmanagerdatapb.VDiffCoreOptions{},
108+
},
109+
},
110+
table: &tabletmanagerdatapb.TableDefinition{
111+
Name: "test",
112+
},
113+
tablePlan: &tablePlan{
114+
pkCols: tc.pkCols,
115+
sourcePkCols: tc.sourcePkCols,
116+
table: &tabletmanagerdatapb.TableDefinition{
117+
Fields: tc.fields,
118+
},
119+
},
120+
}
121+
if err := td.updateTableProgress(dbc, dr, tc.lastRow); (err != nil) != tc.wantErr {
122+
require.FailNow(t, "tableDiffer.updateTableProgress() error = %v, wantErr %v",
123+
err, tc.wantErr)
124+
}
125+
})
126+
}
127+
}

0 commit comments

Comments
 (0)