Search in sources :

Example 26 with TableRecords

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);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.jupiter.api.Test)

Example 27 with 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);
    }
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Row(io.seata.rm.datasource.sql.struct.Row) SQLException(java.sql.SQLException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) Test(org.junit.jupiter.api.Test)

Example 28 with TableRecords

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());
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(io.seata.rm.datasource.sql.struct.Field) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row) Test(org.junit.jupiter.api.Test)

Example 29 with TableRecords

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));
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException) Test(org.junit.jupiter.api.Test)

Example 30 with TableRecords

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);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(io.seata.rm.datasource.sql.struct.Field) Row(io.seata.rm.datasource.sql.struct.Row) Test(org.junit.jupiter.api.Test)

Aggregations

TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)52 Row (io.seata.rm.datasource.sql.struct.Row)29 Test (org.junit.jupiter.api.Test)25 ArrayList (java.util.ArrayList)23 Field (io.seata.rm.datasource.sql.struct.Field)21 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)19 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)14 List (java.util.List)13 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)12 SQLException (java.sql.SQLException)9 Collectors (java.util.stream.Collectors)7 CollectionUtils (io.seata.common.util.CollectionUtils)6 ColumnUtils (io.seata.rm.datasource.ColumnUtils)6 AbstractUndoExecutor (io.seata.rm.datasource.undo.AbstractUndoExecutor)6 JdbcConstants (io.seata.sqlparser.util.JdbcConstants)6 SQLRecognizer (io.seata.sqlparser.SQLRecognizer)4 BeforeAll (org.junit.jupiter.api.BeforeAll)4 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)3 SqlGenerateUtils (io.seata.rm.datasource.SqlGenerateUtils)3 UndoExecutorTest (io.seata.rm.datasource.undo.UndoExecutorTest)3