use of io.seata.rm.datasource.sql.struct.TableRecords in project seata by seata.
the class MySQLInsertExecutorTest method testAfterImage_ByAuto.
@Test
public void testAfterImage_ByAuto() throws SQLException {
doReturn(false).when(insertExecutor).containsPK();
doReturn(true).when(insertExecutor).containsColumns();
Map<String, List<Object>> pkValuesMap = new HashMap<>();
pkValuesMap.put("id", Arrays.asList(new Object[] { PK_VALUE }));
doReturn(pkValuesMap).when(insertExecutor).getPkValuesByAuto();
TableRecords tableRecords = new TableRecords();
doReturn(tableRecords).when(insertExecutor).buildTableRecords(pkValuesMap);
doReturn(tableMeta).when(insertExecutor).getTableMeta();
when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { ID_COLUMN }));
TableRecords resultTableRecords = insertExecutor.afterImage(new TableRecords());
Assertions.assertEquals(resultTableRecords, tableRecords);
}
use of io.seata.rm.datasource.sql.struct.TableRecords in project seata by seata.
the class MySQLInsertExecutorTest method testBeforeImage.
@Test
public void testBeforeImage() throws SQLException {
doReturn(tableMeta).when(insertExecutor).getTableMeta();
TableRecords tableRecords = insertExecutor.beforeImage();
Assertions.assertEquals(tableRecords.size(), 0);
try {
tableRecords.add(new Row());
} catch (Exception e) {
Assertions.assertTrue(e instanceof UnsupportedOperationException);
}
try {
tableRecords.getTableMeta();
} catch (Exception e) {
Assertions.assertTrue(e instanceof UnsupportedOperationException);
}
}
use of io.seata.rm.datasource.sql.struct.TableRecords in project seata by seata.
the class TestUndoExecutor method testParsePK.
@Test
public void testParsePK() {
TableMeta tableMeta = Mockito.mock(TableMeta.class);
Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "id" }));
Mockito.when(tableMeta.getTableName()).thenReturn("table_name");
TableRecords beforeImage = new TableRecords();
beforeImage.setTableName("table_name");
beforeImage.setTableMeta(tableMeta);
List<Row> beforeRows = new ArrayList<>();
Row row0 = new Row();
Field field01 = addField(row0, "id", 1, "12345");
Field field02 = addField(row0, "age", 1, "2");
beforeRows.add(row0);
Row row1 = new Row();
Field field11 = addField(row1, "id", 1, "12346");
Field field12 = addField(row1, "age", 1, "2");
beforeRows.add(row1);
beforeImage.setRows(beforeRows);
SQLUndoLog sqlUndoLog = new SQLUndoLog();
sqlUndoLog.setSqlType(SQLType.UPDATE);
sqlUndoLog.setTableMeta(tableMeta);
sqlUndoLog.setTableName("table_name");
sqlUndoLog.setBeforeImage(beforeImage);
sqlUndoLog.setAfterImage(null);
TestUndoExecutor executor = new TestUndoExecutor(sqlUndoLog, true);
Map<String, List<Field>> pkValues = executor.parsePkValues(beforeImage);
Assertions.assertEquals(2, pkValues.get("id").size());
}
use of io.seata.rm.datasource.sql.struct.TableRecords in project seata by seata.
the class TestUndoExecutor method dataValidationInsert.
@Test
public void dataValidationInsert() throws SQLException {
TableRecords beforeImage = execQuery(tableMeta, "SELECT * FROM table_name WHERE id IN (12345, 12346);");
execSQL("INSERT INTO table_name(id, name) VALUES (12345,'aaa');");
execSQL("INSERT INTO table_name(id, name) VALUES (12346,'aaa');");
TableRecords afterImage = execQuery(tableMeta, "SELECT * FROM table_name WHERE id IN (12345, 12346);");
SQLUndoLog sqlUndoLog = new SQLUndoLog();
sqlUndoLog.setSqlType(SQLType.INSERT);
sqlUndoLog.setTableMeta(tableMeta);
sqlUndoLog.setTableName("table_name");
sqlUndoLog.setBeforeImage(beforeImage);
sqlUndoLog.setAfterImage(afterImage);
TestUndoExecutor spy = new TestUndoExecutor(sqlUndoLog, false);
// case1: normal case before:0 -> after:2 -> current:2
Assertions.assertTrue(spy.dataValidationAndGoOn(connection));
// case2: dirty data before:0 -> after:2 -> current:2'
execSQL("update table_name set name = 'yyy' where id in (12345, 12346);");
try {
Assertions.assertTrue(spy.dataValidationAndGoOn(connection));
Assertions.fail();
} catch (Exception e) {
Assertions.assertTrue(e instanceof SQLException);
}
// case3: before == current before:0 -> after:2 -> current:0
execSQL("delete from table_name where id in (12345, 12346);");
Assertions.assertFalse(spy.dataValidationAndGoOn(connection));
// case 4: before == after before:0 -> after:0
afterImage = execQuery(tableMeta, "SELECT * FROM table_name WHERE id IN (12345, 12346);");
sqlUndoLog.setAfterImage(afterImage);
Assertions.assertFalse(spy.dataValidationAndGoOn(connection));
}
use of io.seata.rm.datasource.sql.struct.TableRecords in project seata by seata.
the class UndoExecutorTest method testInsert.
/**
* Test insert.
*/
@Test
public void testInsert() throws SQLException {
SQLUndoLog SQLUndoLog = new SQLUndoLog();
SQLUndoLog.setTableName("my_test_table");
SQLUndoLog.setSqlType(SQLType.INSERT);
TableRecords beforeImage = TableRecords.empty(new MockTableMeta("product", "id"));
TableRecords afterImage = new TableRecords(new MockTableMeta("product", "id"));
Row afterRow1 = new Row();
Field pkField = new Field();
pkField.setKeyType(KeyType.PRIMARY_KEY);
pkField.setName("id");
pkField.setType(Types.INTEGER);
pkField.setValue(213);
afterRow1.add(pkField);
Field name = new Field();
name.setName("name");
name.setType(Types.VARCHAR);
name.setValue("SEATA");
afterRow1.add(name);
Field since = new Field();
since.setName("since");
since.setType(Types.VARCHAR);
since.setValue("2014");
afterRow1.add(since);
Row afterRow = new Row();
Field pkField1 = new Field();
pkField1.setKeyType(KeyType.PRIMARY_KEY);
pkField1.setName("id");
pkField1.setType(Types.INTEGER);
pkField1.setValue(214);
afterRow.add(pkField1);
Field name1 = new Field();
name1.setName("name");
name1.setType(Types.VARCHAR);
name1.setValue("GTS");
afterRow.add(name1);
Field since1 = new Field();
since1.setName("since");
since1.setType(Types.VARCHAR);
since1.setValue("2016");
afterRow.add(since1);
afterImage.add(afterRow1);
afterImage.add(afterRow);
SQLUndoLog.setBeforeImage(beforeImage);
SQLUndoLog.setAfterImage(afterImage);
AbstractUndoExecutor executor = UndoExecutorFactory.getUndoExecutor(JdbcConstants.MYSQL, SQLUndoLog);
MockConnection connection = new MockConnection();
AbstractUndoExecutor spy = Mockito.spy(executor);
// skip data validation
Mockito.doReturn(true).when(spy).dataValidationAndGoOn(connection);
Mockito.doReturn(JdbcConstants.MYSQL).when(spy).getDbType(connection);
spy.executeOn(connection);
}
Aggregations