/* * 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 sql import ( "database/sql" "reflect" "testing" "github.com/golang/mock/gomock" "github.com/seata/seata-go/pkg/common/reflectx" "github.com/seata/seata-go/pkg/datasource/sql/datasource" "github.com/seata/seata-go/pkg/datasource/sql/mock" "github.com/seata/seata-go/pkg/protocol/branch" "github.com/stretchr/testify/assert" ) func initMockResourceManager(t *testing.T, ctrl *gomock.Controller) *mock.MockDataSourceManager { mockResourceMgr := mock.NewMockDataSourceManager(ctrl) datasource.RegisterResourceManager(branch.BranchTypeAT, mockResourceMgr) mockResourceMgr.EXPECT().RegisterResource(gomock.Any()).AnyTimes().Return(nil) mockResourceMgr.EXPECT().CreateTableMetaCache(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, nil) return mockResourceMgr } func Test_seataATDriver_OpenConnector(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() mockMgr := initMockResourceManager(t, ctrl) _ = mockMgr db, err := sql.Open("seata-at-mysql", "root:seata_go@tcp(127.0.0.1:3306)/seata_go_test?multiStatements=true") if err != nil { t.Fatal(err) } defer db.Close() v := reflect.ValueOf(db) if v.Kind() == reflect.Ptr { v = v.Elem() } field := v.FieldByName("connector") fieldVal := reflectx.GetUnexportedField(field) _, ok := fieldVal.(*seataATConnector) assert.True(t, ok, "need return seata at connector") } func Test_seataXADriver_OpenConnector(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() mockMgr := initMockResourceManager(t, ctrl) _ = mockMgr db, err := sql.Open("seata-xa-mysql", "root:seata_go@tcp(127.0.0.1:3306)/seata_go_test?multiStatements=true") if err != nil { t.Fatal(err) } defer db.Close() v := reflect.ValueOf(db) if v.Kind() == reflect.Ptr { v = v.Elem() } field := v.FieldByName("connector") fieldVal := reflectx.GetUnexportedField(field) _, ok := fieldVal.(*seataXAConnector) assert.True(t, ok, "need return seata xa connector") }