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.

delete_executor_test.go 3.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. "database/sql/driver"
  20. "testing"
  21. "github.com/stretchr/testify/assert"
  22. "seata.apache.org/seata-go/pkg/datasource/sql/exec"
  23. "seata.apache.org/seata-go/pkg/datasource/sql/parser"
  24. "seata.apache.org/seata-go/pkg/datasource/sql/types"
  25. "seata.apache.org/seata-go/pkg/datasource/sql/util"
  26. )
  27. func TestNewDeleteExecutor(t *testing.T) {
  28. executor := NewDeleteExecutor(nil, nil, nil)
  29. _, ok := executor.(*deleteExecutor)
  30. assert.Equalf(t, true, ok, "should be *deleteExecutor")
  31. }
  32. func Test_deleteExecutor_buildBeforeImageSQL(t *testing.T) {
  33. tests := []struct {
  34. name string
  35. sourceQuery string
  36. sourceQueryArgs []driver.Value
  37. expectQuery string
  38. expectQueryArgs []driver.Value
  39. }{
  40. {
  41. sourceQuery: "delete from t_user where id = ?",
  42. sourceQueryArgs: []driver.Value{100},
  43. expectQuery: "SELECT SQL_NO_CACHE * FROM t_user WHERE id=? FOR UPDATE",
  44. expectQueryArgs: []driver.Value{100},
  45. },
  46. {
  47. sourceQuery: "delete from t_user where id = ? and name = 'Jack' and age between ? and ?",
  48. sourceQueryArgs: []driver.Value{100, 18, 28},
  49. expectQuery: "SELECT SQL_NO_CACHE * FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age BETWEEN ? AND ? FOR UPDATE",
  50. expectQueryArgs: []driver.Value{100, 18, 28},
  51. },
  52. {
  53. sourceQuery: "delete from t_user where id = ? and name = 'Jack' and age in (?,?)",
  54. sourceQueryArgs: []driver.Value{100, 18, 28},
  55. expectQuery: "SELECT SQL_NO_CACHE * FROM t_user WHERE id=? AND name=_UTF8MB4Jack AND age IN (?,?) FOR UPDATE",
  56. expectQueryArgs: []driver.Value{100, 18, 28},
  57. },
  58. {
  59. sourceQuery: "delete from t_user where kk between ? and ? and id = ? and addr in(?,?) and age > ? order by name desc limit ?",
  60. sourceQueryArgs: []driver.Value{10, 20, 17, "Beijing", "Guangzhou", 18, 2},
  61. expectQuery: "SELECT SQL_NO_CACHE * FROM t_user WHERE kk BETWEEN ? AND ? AND id=? AND addr IN (?,?) AND age>? ORDER BY name DESC LIMIT ? FOR UPDATE",
  62. expectQueryArgs: []driver.Value{10, 20, 17, "Beijing", "Guangzhou", 18, 2},
  63. },
  64. }
  65. for _, tt := range tests {
  66. t.Run(tt.name, func(t *testing.T) {
  67. c, err := parser.DoParser(tt.sourceQuery)
  68. assert.Nil(t, err)
  69. executor := NewDeleteExecutor(c, &types.ExecContext{Values: tt.sourceQueryArgs, NamedValues: util.ValueToNamedValue(tt.sourceQueryArgs)}, []exec.SQLHook{})
  70. query, args, err := executor.(*deleteExecutor).buildBeforeImageSQL(tt.sourceQuery, util.ValueToNamedValue(tt.sourceQueryArgs))
  71. assert.Nil(t, err)
  72. assert.Equal(t, tt.expectQuery, query)
  73. assert.Equal(t, tt.expectQueryArgs, util.NamedValueToValue(args))
  74. })
  75. }
  76. }