Search in sources :

Example 6 with ColumnMeta

use of io.seata.rm.datasource.sql.struct.ColumnMeta in project seata by seata.

the class MySQLInsertExecutorTest method testGetPkValuesByAuto_GeneratedKeys_HasResult.

@Test
public void testGetPkValuesByAuto_GeneratedKeys_HasResult() throws SQLException {
    doReturn(tableMeta).when(insertExecutor).getTableMeta();
    ColumnMeta columnMeta = mock(ColumnMeta.class);
    Map<String, ColumnMeta> columnMetaMap = new HashMap<>();
    columnMetaMap.put(ID_COLUMN, columnMeta);
    when(columnMeta.isAutoincrement()).thenReturn(true);
    when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap);
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    when(statementProxy.getTargetStatement()).thenReturn(preparedStatement);
    ResultSet resultSet = mock(ResultSet.class);
    when(statementProxy.getGeneratedKeys()).thenReturn(resultSet);
    when(resultSet.next()).thenReturn(true).thenReturn(false);
    when(resultSet.getObject(1)).thenReturn(PK_VALUE);
    List<Object> pkValues = new ArrayList<>();
    pkValues.add(PK_VALUE);
    Map<String, List<Object>> pkValuesList = insertExecutor.getPkValuesByAuto();
    Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValues);
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) MockResultSet(io.seata.rm.datasource.mock.MockResultSet) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.jupiter.api.Test)

Example 7 with ColumnMeta

use of io.seata.rm.datasource.sql.struct.ColumnMeta in project seata by seata.

the class MySQLInsertExecutorTest method testGetPkValuesByAuto_SQLException.

@Test
public void testGetPkValuesByAuto_SQLException() {
    Assertions.assertThrows(SQLException.class, () -> {
        doReturn(tableMeta).when(insertExecutor).getTableMeta();
        ColumnMeta columnMeta = mock(ColumnMeta.class);
        Map<String, ColumnMeta> columnMetaMap = new HashMap<>();
        columnMetaMap.put(ID_COLUMN, columnMeta);
        when(columnMeta.isAutoincrement()).thenReturn(true);
        when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap);
        when(statementProxy.getGeneratedKeys()).thenThrow(new SQLException());
        insertExecutor.getPkValuesByAuto();
    });
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.jupiter.api.Test)

Example 8 with ColumnMeta

use of io.seata.rm.datasource.sql.struct.ColumnMeta in project seata by seata.

the class MySQLInsertExecutorTest method testGetPkValuesByColumn_PkValue_Null.

@Test
public void testGetPkValuesByColumn_PkValue_Null() throws SQLException {
    mockInsertColumns();
    mockInsertRows();
    mockParametersPkWithNull();
    doReturn(tableMeta).when(insertExecutor).getTableMeta();
    when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { ID_COLUMN }));
    ColumnMeta cm = new ColumnMeta();
    cm.setColumnName(ID_COLUMN);
    cm.setIsAutoincrement("YES");
    when(tableMeta.getPrimaryKeyMap()).thenReturn(new HashMap<String, ColumnMeta>() {

        {
            put(ID_COLUMN, cm);
        }
    });
    List<Object> pkValuesAuto = new ArrayList<>();
    pkValuesAuto.add(PK_VALUE);
    // mock getPkValuesByAuto
    doReturn(new HashMap<String, List<Object>>() {

        {
            put(ID_COLUMN, pkValuesAuto);
        }
    }).when(insertExecutor).getPkValuesByAuto();
    doReturn(pkIndexMap).when(insertExecutor).getPkIndex();
    Map<String, List<Object>> pkValuesList = insertExecutor.getPkValuesByColumn();
    // pk value = Null so getPkValuesByAuto
    verify(insertExecutor).getPkValuesByAuto();
    Assertions.assertIterableEquals(pkValuesList.get(ID_COLUMN), pkValuesAuto);
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.jupiter.api.Test)

Example 9 with ColumnMeta

use of io.seata.rm.datasource.sql.struct.ColumnMeta in project seata by seata.

the class MySQLInsertExecutorTest method testGetPkValuesByAuto_GeneratedKeys_NoResult.

@Test
public void testGetPkValuesByAuto_GeneratedKeys_NoResult() throws SQLException {
    doReturn(tableMeta).when(insertExecutor).getTableMeta();
    ColumnMeta columnMeta = mock(ColumnMeta.class);
    Map<String, ColumnMeta> columnMetaMap = new HashMap<>();
    columnMetaMap.put(ID_COLUMN, columnMeta);
    when(columnMeta.isAutoincrement()).thenReturn(true);
    when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap);
    PreparedStatement preparedStatement = mock(PreparedStatement.class);
    when(statementProxy.getTargetStatement()).thenReturn(preparedStatement);
    ResultSet resultSet = mock(ResultSet.class);
    when(statementProxy.getGeneratedKeys()).thenReturn(resultSet);
    when(resultSet.next()).thenReturn(false);
    when(resultSet.getObject(1)).thenReturn(PK_VALUE);
    Map<String, List<Object>> pkValues = insertExecutor.getPkValuesByAuto();
    Assertions.assertEquals(pkValues.get(ID_COLUMN).size(), 0);
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) MockResultSet(io.seata.rm.datasource.mock.MockResultSet) PreparedStatement(java.sql.PreparedStatement) ArrayList(java.util.ArrayList) List(java.util.List) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.jupiter.api.Test)

Example 10 with ColumnMeta

use of io.seata.rm.datasource.sql.struct.ColumnMeta in project seata by seata.

the class MySQLInsertExecutorTest method testGetPkValuesByAuto_ShouldNeverHappenException.

@Test
public void testGetPkValuesByAuto_ShouldNeverHappenException() {
    Assertions.assertThrows(ShouldNeverHappenException.class, () -> {
        doReturn(tableMeta).when(insertExecutor).getTableMeta();
        PreparedStatement preparedStatement = mock(PreparedStatement.class);
        when(statementProxy.getTargetStatement()).thenReturn(preparedStatement);
        when(preparedStatement.getGeneratedKeys()).thenReturn(mock(ResultSet.class));
        Map<String, ColumnMeta> columnMetaMap = new HashMap<>();
        ColumnMeta columnMeta = mock(ColumnMeta.class);
        columnMetaMap.put(ID_COLUMN, columnMeta);
        when(columnMeta.isAutoincrement()).thenReturn(false);
        when(tableMeta.getPrimaryKeyMap()).thenReturn(columnMetaMap);
        insertExecutor.getPkValuesByAuto();
    });
}
Also used : ColumnMeta(io.seata.rm.datasource.sql.struct.ColumnMeta) HashMap(java.util.HashMap) ResultSet(java.sql.ResultSet) MockResultSet(io.seata.rm.datasource.mock.MockResultSet) PreparedStatement(java.sql.PreparedStatement) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.jupiter.api.Test)

Aggregations

ColumnMeta (io.seata.rm.datasource.sql.struct.ColumnMeta)18 ResultSet (java.sql.ResultSet)10 HashMap (java.util.HashMap)9 Test (org.junit.jupiter.api.Test)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Mockito.anyString (org.mockito.Mockito.anyString)7 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)5 MockResultSet (io.seata.rm.datasource.mock.MockResultSet)5 PreparedStatement (java.sql.PreparedStatement)5 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)4 SQLException (java.sql.SQLException)4 IndexMeta (io.seata.rm.datasource.sql.struct.IndexMeta)3 Map (java.util.Map)2 PreparedStatementProxy (io.seata.rm.datasource.PreparedStatementProxy)1 StatementProxy (io.seata.rm.datasource.StatementProxy)1 OracleInsertExecutor (io.seata.rm.datasource.exec.oracle.OracleInsertExecutor)1 SQLInsertRecognizer (io.seata.sqlparser.SQLInsertRecognizer)1 SqlSequenceExpr (io.seata.sqlparser.struct.SqlSequenceExpr)1 BigDecimal (java.math.BigDecimal)1