Skip to content

Commit

Permalink
Refactor the code to address the issue of duplicate definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Code-Fight committed Dec 16, 2024
1 parent 84b8079 commit a54a9d9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 66 deletions.
34 changes: 3 additions & 31 deletions pkg/datasource/sql/datasource/base/meta_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/go-sql-driver/mysql"
"github.com/stretchr/testify/assert"
"seata.apache.org/seata-go/pkg/datasource/sql/types"
"seata.apache.org/seata-go/testdata"
)

var (
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestBaseTableMetaCache_refresh(t *testing.T) {
size: 0,
expireDuration: EexpireTime,
cache: map[string]*entry{
"test": &entry{
"test": {
value: types.TableMeta{},
lastAccess: time.Now(),
},
Expand All @@ -86,36 +87,7 @@ func TestBaseTableMetaCache_refresh(t *testing.T) {
cfg: &mysql.Config{},
db: &sql.DB{},
}, args: args{ctx: ctx},
want: types.TableMeta{
TableName: "test",
Columns: map[string]types.ColumnMeta{
"id": {
ColumnName: "id",
},
"name": {
ColumnName: "name",
},
},
Indexs: map[string]types.IndexMeta{
"": {
ColumnName: "id",
IType: types.IndexTypePrimaryKey,
Columns: []types.ColumnMeta{
{
ColumnName: "id",
DatabaseType: types.GetSqlDataType("BIGINT"),
},
{
ColumnName: "id",
},
},
},
},
ColumnNames: []string{
"id",
"name",
},
}},
want: testdata.MockWantTypesMeta()},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
42 changes: 7 additions & 35 deletions pkg/datasource/sql/datasource/mysql/trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/agiledragon/gomonkey/v2"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"seata.apache.org/seata-go/testdata"

"seata.apache.org/seata-go/pkg/datasource/sql/mock"
"seata.apache.org/seata-go/pkg/datasource/sql/types"
Expand Down Expand Up @@ -75,7 +76,7 @@ func initGetColumnMetasStub(m *mysqlTrigger, columnMeta []types.ColumnMeta) *gom
}

func Test_mysqlTrigger_LoadOne(t *testing.T) {

wantTableMeta := testdata.MockWantTypesMeta()
type args struct {
ctx context.Context
dbName string
Expand All @@ -90,40 +91,11 @@ func Test_mysqlTrigger_LoadOne(t *testing.T) {
wantTableMeta *types.TableMeta
}{
{
name: "1",
args: args{ctx: context.Background(), dbName: "dbName", tableName: "test", conn: nil},
indexMeta: initMockIndexMeta(),
columnMeta: initMockColumnMeta(),
wantTableMeta: &types.TableMeta{
TableName: "test",
Columns: map[string]types.ColumnMeta{
"id": {
ColumnName: "id",
},
"name": {
ColumnName: "name",
},
},
Indexs: map[string]types.IndexMeta{
"": {
ColumnName: "id",
IType: types.IndexTypePrimaryKey,
Columns: []types.ColumnMeta{
{
ColumnName: "id",
DatabaseType: int32(types.JDBCTypeBigInt),
},
{
ColumnName: "id",
},
},
},
},
ColumnNames: []string{
"id",
"name",
},
},
name: "1",
args: args{ctx: context.Background(), dbName: "dbName", tableName: "test", conn: nil},
indexMeta: initMockIndexMeta(),
columnMeta: initMockColumnMeta(),
wantTableMeta: &wantTableMeta,
},
}
for _, tt := range tests {
Expand Down
53 changes: 53 additions & 0 deletions testdata/meta_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 testdata

import "seata.apache.org/seata-go/pkg/datasource/sql/types"

func MockWantTypesMeta() types.TableMeta {
return types.TableMeta{
TableName: "test",
Columns: map[string]types.ColumnMeta{
"id": {
ColumnName: "id",
},
"name": {
ColumnName: "name",
},
},
Indexs: map[string]types.IndexMeta{
"": {
ColumnName: "id",
IType: types.IndexTypePrimaryKey,
Columns: []types.ColumnMeta{
{
ColumnName: "id",
DatabaseType: types.GetSqlDataType("BIGINT"),
},
{
ColumnName: "id",
},
},
},
},
ColumnNames: []string{
"id",
"name",
},
}
}

0 comments on commit a54a9d9

Please sign in to comment.