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.

plain_executor_test.go 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 at
  18. import (
  19. "context"
  20. "database/sql/driver"
  21. "fmt"
  22. "testing"
  23. "github.com/DATA-DOG/go-sqlmock"
  24. "github.com/stretchr/testify/assert"
  25. "seata.apache.org/seata-go/pkg/datasource/sql/exec"
  26. "seata.apache.org/seata-go/pkg/datasource/sql/mock"
  27. "seata.apache.org/seata-go/pkg/datasource/sql/types"
  28. )
  29. func TestNewPlainExecutor(t *testing.T) {
  30. executor := NewPlainExecutor(nil, nil)
  31. _, ok := executor.(*plainExecutor)
  32. assert.Equalf(t, true, ok, "should be *plainExecutor")
  33. }
  34. func TestPlainExecutor_ExecContext(t *testing.T) {
  35. tests := []struct {
  36. name string
  37. f exec.CallbackWithNamedValue
  38. wantVal types.ExecResult
  39. wantErr error
  40. }{
  41. {
  42. name: "test1",
  43. f: func(ctx context.Context, query string, args []driver.NamedValue) (types.ExecResult, error) {
  44. return NewMockInsertResult(int64(1), int64(2)), nil
  45. },
  46. wantVal: NewMockInsertResult(int64(1), int64(2)),
  47. wantErr: nil,
  48. },
  49. {
  50. name: "test2",
  51. f: func(ctx context.Context, query string, args []driver.NamedValue) (types.ExecResult, error) {
  52. return nil, fmt.Errorf("test error")
  53. },
  54. wantVal: nil,
  55. wantErr: fmt.Errorf("test error"),
  56. },
  57. }
  58. for _, tt := range tests {
  59. t.Run(tt.name, func(t *testing.T) {
  60. u := &plainExecutor{execContext: &types.ExecContext{}}
  61. val, err := u.ExecContext(context.Background(), tt.f)
  62. assert.Equalf(t, tt.wantVal, val, "")
  63. assert.Equalf(t, tt.wantErr, err, "")
  64. })
  65. }
  66. }
  67. type mockInsertResult struct {
  68. lastInsertID int64
  69. rowsAffected int64
  70. }
  71. func NewMockInsertResult(lastInsertID int64, rowsAffected int64) mockInsertResult {
  72. return mockInsertResult{
  73. lastInsertID: lastInsertID,
  74. rowsAffected: rowsAffected,
  75. }
  76. }
  77. func (m mockInsertResult) GetRows() driver.Rows {
  78. return &mock.MockTestDriverRows{}
  79. }
  80. func (m mockInsertResult) GetResult() driver.Result {
  81. return sqlmock.NewResult(m.lastInsertID, m.rowsAffected)
  82. }