66 lines
2.5 KiB
Go
66 lines
2.5 KiB
Go
// Copyright 2024 PingCAP, Inc.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package session
|
|
|
|
import (
|
|
"cmp"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/pingcap/tidb/pkg/ddl"
|
|
"github.com/pingcap/tidb/pkg/meta/metadef"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetStartMode(t *testing.T) {
|
|
require.Equal(t, ddl.Normal, getStartMode(currentBootstrapVersion))
|
|
require.Equal(t, ddl.Normal, getStartMode(currentBootstrapVersion+1))
|
|
require.Equal(t, ddl.Upgrade, getStartMode(currentBootstrapVersion-1))
|
|
require.Equal(t, ddl.Bootstrap, getStartMode(0))
|
|
}
|
|
|
|
func TestDDLTableVersionTables(t *testing.T) {
|
|
require.True(t, slices.IsSortedFunc(ddlTableVersionTables, func(a, b versionedDDLTables) int {
|
|
return cmp.Compare(a.ver, b.ver)
|
|
}), "ddlTableVersionTables should be sorted by version")
|
|
allDDLTables := make([]TableBasicInfo, 0, len(ddlTableVersionTables)*2)
|
|
for _, v := range ddlTableVersionTables {
|
|
allDDLTables = append(allDDLTables, v.tables...)
|
|
}
|
|
testTableBasicInfoSlice(t, allDDLTables)
|
|
}
|
|
|
|
func testTableBasicInfoSlice(t *testing.T, allTables []TableBasicInfo) {
|
|
t.Helper()
|
|
require.True(t, slices.IsSortedFunc(allTables, func(a, b TableBasicInfo) int {
|
|
if a.ID == b.ID {
|
|
t.Errorf("table IDs should be unique, a=%d, b=%d", a.ID, b.ID)
|
|
}
|
|
if a.Name == b.Name {
|
|
t.Errorf("table names should be unique, a=%s, b=%s", a.Name, b.Name)
|
|
}
|
|
return cmp.Compare(b.ID, a.ID)
|
|
}), "tables should be sorted by table ID in descending order")
|
|
for _, vt := range allTables {
|
|
require.Greater(t, vt.ID, metadef.ReservedGlobalIDLowerBound, "table ID should be greater than ReservedGlobalIDLowerBound")
|
|
require.LessOrEqual(t, vt.ID, metadef.ReservedGlobalIDUpperBound, "table ID should be less than or equal to ReservedGlobalIDUpperBound")
|
|
require.Equal(t, strings.ToLower(vt.Name), vt.Name, "table name should be in lower case")
|
|
require.Contains(t, vt.SQL, fmt.Sprintf(" mysql.%s (", vt.Name),
|
|
"table SQL should contain table name and follow the format 'mysql.<table_name> ('")
|
|
}
|
|
}
|