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);
}
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();
});
}
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);
}
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);
}
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();
});
}
Aggregations