You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

driver_test.go 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package sql
  18. import (
  19. "database/sql"
  20. "reflect"
  21. "testing"
  22. "github.com/golang/mock/gomock"
  23. "github.com/seata/seata-go/pkg/datasource/sql/datasource"
  24. "github.com/seata/seata-go/pkg/datasource/sql/mock"
  25. "github.com/seata/seata-go/pkg/protocol/branch"
  26. "github.com/stretchr/testify/assert"
  27. )
  28. func initMockResourceManager(t *testing.T, ctrl *gomock.Controller) *mock.MockDataSourceManager {
  29. mockResourceMgr := mock.NewMockDataSourceManager(ctrl)
  30. datasource.RegisterResourceManager(branch.BranchTypeAT, mockResourceMgr)
  31. mockResourceMgr.EXPECT().RegisterResource(gomock.Any()).AnyTimes().Return(nil)
  32. mockResourceMgr.EXPECT().CreateTableMetaCache(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes().Return(nil, nil)
  33. return mockResourceMgr
  34. }
  35. func Test_seataATDriver_OpenConnector(t *testing.T) {
  36. ctrl := gomock.NewController(t)
  37. defer ctrl.Finish()
  38. mockMgr := initMockResourceManager(t, ctrl)
  39. _ = mockMgr
  40. db, err := sql.Open("seata-at-mysql", "root:seata_go@tcp(127.0.0.1:3306)/seata_go_test?multiStatements=true")
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. defer db.Close()
  45. v := reflect.ValueOf(db)
  46. if v.Kind() == reflect.Ptr {
  47. v = v.Elem()
  48. }
  49. field := v.FieldByName("connector")
  50. fieldVal := GetUnexportedField(field)
  51. _, ok := fieldVal.(*seataATConnector)
  52. assert.True(t, ok, "need return seata at connector")
  53. }
  54. func Test_seataXADriver_OpenConnector(t *testing.T) {
  55. ctrl := gomock.NewController(t)
  56. defer ctrl.Finish()
  57. mockMgr := initMockResourceManager(t, ctrl)
  58. _ = mockMgr
  59. db, err := sql.Open("seata-xa-mysql", "root:seata_go@tcp(127.0.0.1:3306)/seata_go_test?multiStatements=true")
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. defer db.Close()
  64. v := reflect.ValueOf(db)
  65. if v.Kind() == reflect.Ptr {
  66. v = v.Elem()
  67. }
  68. field := v.FieldByName("connector")
  69. fieldVal := GetUnexportedField(field)
  70. _, ok := fieldVal.(*seataXAConnector)
  71. assert.True(t, ok, "need return seata xa connector")
  72. }