|
| 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 | + |
| 18 | +package factor |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/stretchr/testify/assert" |
| 24 | + |
| 25 | + "seata.apache.org/seata-go/pkg/datasource/sql/types" |
| 26 | + "seata.apache.org/seata-go/pkg/datasource/sql/undo" |
| 27 | +) |
| 28 | + |
| 29 | +func TestGetUndoExecutor_Insert(t *testing.T) { |
| 30 | + // Test getting INSERT undo executor for MySQL |
| 31 | + sqlUndoLog := undo.SQLUndoLog{ |
| 32 | + SQLType: types.SQLTypeInsert, |
| 33 | + TableName: "test_table", |
| 34 | + } |
| 35 | + |
| 36 | + executor, err := GetUndoExecutor(types.DBTypeMySQL, sqlUndoLog) |
| 37 | + |
| 38 | + assert.NoError(t, err) |
| 39 | + assert.NotNil(t, executor) |
| 40 | +} |
| 41 | + |
| 42 | +func TestGetUndoExecutor_Update(t *testing.T) { |
| 43 | + // Test getting UPDATE undo executor for MySQL |
| 44 | + sqlUndoLog := undo.SQLUndoLog{ |
| 45 | + SQLType: types.SQLTypeUpdate, |
| 46 | + TableName: "test_table", |
| 47 | + } |
| 48 | + |
| 49 | + executor, err := GetUndoExecutor(types.DBTypeMySQL, sqlUndoLog) |
| 50 | + |
| 51 | + assert.NoError(t, err) |
| 52 | + assert.NotNil(t, executor) |
| 53 | +} |
| 54 | + |
| 55 | +func TestGetUndoExecutor_Delete(t *testing.T) { |
| 56 | + // Test getting DELETE undo executor for MySQL |
| 57 | + sqlUndoLog := undo.SQLUndoLog{ |
| 58 | + SQLType: types.SQLTypeDelete, |
| 59 | + TableName: "test_table", |
| 60 | + } |
| 61 | + |
| 62 | + executor, err := GetUndoExecutor(types.DBTypeMySQL, sqlUndoLog) |
| 63 | + |
| 64 | + assert.NoError(t, err) |
| 65 | + assert.NotNil(t, executor) |
| 66 | +} |
| 67 | + |
| 68 | +func TestGetUndoExecutor_UnsupportedSQLType(t *testing.T) { |
| 69 | + // Test unsupported SQL types |
| 70 | + testCases := []struct { |
| 71 | + name string |
| 72 | + sqlType types.SQLType |
| 73 | + }{ |
| 74 | + {"SELECT", types.SQLTypeSelect}, |
| 75 | + {"CREATE", types.SQLTypeCreate}, |
| 76 | + {"DROP", types.SQLTypeDrop}, |
| 77 | + {"ALTER", types.SQLTypeAlter}, |
| 78 | + {"TRUNCATE", types.SQLTypeTruncate}, |
| 79 | + } |
| 80 | + |
| 81 | + for _, tc := range testCases { |
| 82 | + t.Run(tc.name, func(t *testing.T) { |
| 83 | + sqlUndoLog := undo.SQLUndoLog{ |
| 84 | + SQLType: tc.sqlType, |
| 85 | + TableName: "test_table", |
| 86 | + } |
| 87 | + |
| 88 | + executor, err := GetUndoExecutor(types.DBTypeMySQL, sqlUndoLog) |
| 89 | + |
| 90 | + assert.Error(t, err) |
| 91 | + assert.Nil(t, executor) |
| 92 | + assert.Contains(t, err.Error(), "not support") |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestGetUndoExecutor_UnsupportedDBType(t *testing.T) { |
| 98 | + // Test with unsupported database type |
| 99 | + sqlUndoLog := undo.SQLUndoLog{ |
| 100 | + SQLType: types.SQLTypeInsert, |
| 101 | + TableName: "test_table", |
| 102 | + } |
| 103 | + |
| 104 | + executor, err := GetUndoExecutor(types.DBTypePostgreSQL, sqlUndoLog) |
| 105 | + |
| 106 | + assert.Error(t, err) |
| 107 | + assert.Nil(t, executor) |
| 108 | + assert.Equal(t, ErrNotImplDBType, err) |
| 109 | +} |
| 110 | + |
| 111 | +func TestGetUndoExecutor_AllSupportedSQLTypes(t *testing.T) { |
| 112 | + // Test all supported SQL types with MySQL |
| 113 | + testCases := []struct { |
| 114 | + name string |
| 115 | + sqlType types.SQLType |
| 116 | + }{ |
| 117 | + {"Insert", types.SQLTypeInsert}, |
| 118 | + {"Update", types.SQLTypeUpdate}, |
| 119 | + {"Delete", types.SQLTypeDelete}, |
| 120 | + } |
| 121 | + |
| 122 | + for _, tc := range testCases { |
| 123 | + t.Run(tc.name, func(t *testing.T) { |
| 124 | + sqlUndoLog := undo.SQLUndoLog{ |
| 125 | + SQLType: tc.sqlType, |
| 126 | + TableName: "test_table", |
| 127 | + } |
| 128 | + |
| 129 | + executor, err := GetUndoExecutor(types.DBTypeMySQL, sqlUndoLog) |
| 130 | + |
| 131 | + assert.NoError(t, err) |
| 132 | + assert.NotNil(t, executor) |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestGetUndoExecutor_WithBeforeAndAfterImages(t *testing.T) { |
| 138 | + // Test with complete undo log including before and after images |
| 139 | + sqlUndoLog := undo.SQLUndoLog{ |
| 140 | + SQLType: types.SQLTypeUpdate, |
| 141 | + TableName: "users", |
| 142 | + BeforeImage: &types.RecordImage{ |
| 143 | + TableName: "users", |
| 144 | + Rows: []types.RowImage{}, |
| 145 | + }, |
| 146 | + AfterImage: &types.RecordImage{ |
| 147 | + TableName: "users", |
| 148 | + Rows: []types.RowImage{}, |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + executor, err := GetUndoExecutor(types.DBTypeMySQL, sqlUndoLog) |
| 153 | + |
| 154 | + assert.NoError(t, err) |
| 155 | + assert.NotNil(t, executor) |
| 156 | +} |
| 157 | + |
| 158 | +func TestGetUndoExecutor_InvalidDBTypeValue(t *testing.T) { |
| 159 | + // Test with invalid/custom DB type value |
| 160 | + sqlUndoLog := undo.SQLUndoLog{ |
| 161 | + SQLType: types.SQLTypeInsert, |
| 162 | + TableName: "test_table", |
| 163 | + } |
| 164 | + |
| 165 | + invalidDBType := types.DBType(999) |
| 166 | + executor, err := GetUndoExecutor(invalidDBType, sqlUndoLog) |
| 167 | + |
| 168 | + assert.Error(t, err) |
| 169 | + assert.Nil(t, executor) |
| 170 | +} |
| 171 | + |
| 172 | +func TestGetUndoExecutor_ErrorPropagation(t *testing.T) { |
| 173 | + // Test that errors from GetUndoExecutorHolder are properly propagated |
| 174 | + sqlUndoLog := undo.SQLUndoLog{ |
| 175 | + SQLType: types.SQLTypeInsert, |
| 176 | + TableName: "test_table", |
| 177 | + } |
| 178 | + |
| 179 | + executor, err := GetUndoExecutor(types.DBTypeOracle, sqlUndoLog) |
| 180 | + |
| 181 | + assert.Error(t, err) |
| 182 | + assert.Nil(t, executor) |
| 183 | + assert.Equal(t, ErrNotImplDBType, err) |
| 184 | +} |
| 185 | + |
| 186 | +func TestGetUndoExecutor_MultipleCallsSameType(t *testing.T) { |
| 187 | + // Test multiple calls with the same SQL type return valid executors |
| 188 | + sqlUndoLog := undo.SQLUndoLog{ |
| 189 | + SQLType: types.SQLTypeInsert, |
| 190 | + TableName: "test_table", |
| 191 | + } |
| 192 | + |
| 193 | + executor1, err1 := GetUndoExecutor(types.DBTypeMySQL, sqlUndoLog) |
| 194 | + assert.NoError(t, err1) |
| 195 | + assert.NotNil(t, executor1) |
| 196 | + |
| 197 | + executor2, err2 := GetUndoExecutor(types.DBTypeMySQL, sqlUndoLog) |
| 198 | + assert.NoError(t, err2) |
| 199 | + assert.NotNil(t, executor2) |
| 200 | + |
| 201 | + assert.IsType(t, executor1, executor2) |
| 202 | +} |
| 203 | + |
| 204 | +func TestGetUndoExecutor_EmptyTableName(t *testing.T) { |
| 205 | + // Test with empty table name (should still work as validation happens elsewhere) |
| 206 | + sqlUndoLog := undo.SQLUndoLog{ |
| 207 | + SQLType: types.SQLTypeInsert, |
| 208 | + TableName: "", |
| 209 | + } |
| 210 | + |
| 211 | + executor, err := GetUndoExecutor(types.DBTypeMySQL, sqlUndoLog) |
| 212 | + |
| 213 | + assert.NoError(t, err) |
| 214 | + assert.NotNil(t, executor) |
| 215 | +} |
0 commit comments