use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.
the class BaseTransactionalExecutorTest method testBuildLockKey.
@Test
public void testBuildLockKey() {
// build expect data
String tableName = "test_name";
String fieldOne = "1";
String fieldTwo = "2";
String split1 = ":";
String split2 = ",";
String pkColumnName = "id";
// test_name:1,2
String buildLockKeyExpect = tableName + split1 + fieldOne + split2 + fieldTwo;
// mock field
Field field1 = mock(Field.class);
when(field1.getValue()).thenReturn(fieldOne);
Field field2 = mock(Field.class);
when(field2.getValue()).thenReturn(fieldTwo);
List<Map<String, Field>> pkRows = new ArrayList<>();
pkRows.add(Collections.singletonMap(pkColumnName, field1));
pkRows.add(Collections.singletonMap(pkColumnName, field2));
// mock tableMeta
TableMeta tableMeta = mock(TableMeta.class);
when(tableMeta.getTableName()).thenReturn(tableName);
when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { pkColumnName }));
// mock tableRecords
TableRecords tableRecords = mock(TableRecords.class);
when(tableRecords.getTableMeta()).thenReturn(tableMeta);
when(tableRecords.size()).thenReturn(pkRows.size());
when(tableRecords.pkRows()).thenReturn(pkRows);
// mock executor
BaseTransactionalExecutor executor = mock(BaseTransactionalExecutor.class);
when(executor.buildLockKey(tableRecords)).thenCallRealMethod();
when(executor.getTableMeta()).thenReturn(tableMeta);
assertThat(executor.buildLockKey(tableRecords)).isEqualTo(buildLockKeyExpect);
}
use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.
the class BaseTransactionalExecutorTest method testBuildLockKeyWithMultiPk.
@Test
public void testBuildLockKeyWithMultiPk() {
// build expect data
String tableName = "test_name";
String pkOneValue1 = "1";
String pkOneValue2 = "2";
String pkTwoValue1 = "one";
String pkTwoValue2 = "two";
String split1 = ":";
String split2 = ",";
String split3 = "_";
String pkOneColumnName = "id";
String pkTwoColumnName = "userId";
// test_name:1_one,2_two
String buildLockKeyExpect = tableName + split1 + pkOneValue1 + split3 + pkTwoValue1 + split2 + pkOneValue2 + split3 + pkTwoValue2;
// mock field
Field pkOneField1 = mock(Field.class);
when(pkOneField1.getValue()).thenReturn(pkOneValue1);
Field pkOneField2 = mock(Field.class);
when(pkOneField2.getValue()).thenReturn(pkOneValue2);
Field pkTwoField1 = mock(Field.class);
when(pkTwoField1.getValue()).thenReturn(pkTwoValue1);
Field pkTwoField2 = mock(Field.class);
when(pkTwoField2.getValue()).thenReturn(pkTwoValue2);
List<Map<String, Field>> pkRows = new ArrayList<>();
Map<String, Field> row1 = new HashMap<String, Field>() {
{
put(pkOneColumnName, pkOneField1);
put(pkTwoColumnName, pkTwoField1);
}
};
pkRows.add(row1);
Map<String, Field> row2 = new HashMap<String, Field>() {
{
put(pkOneColumnName, pkOneField2);
put(pkTwoColumnName, pkTwoField2);
}
};
pkRows.add(row2);
// mock tableMeta
TableMeta tableMeta = mock(TableMeta.class);
when(tableMeta.getTableName()).thenReturn(tableName);
when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { pkOneColumnName, pkTwoColumnName }));
// mock tableRecords
TableRecords tableRecords = mock(TableRecords.class);
when(tableRecords.getTableMeta()).thenReturn(tableMeta);
when(tableRecords.size()).thenReturn(pkRows.size());
when(tableRecords.pkRows()).thenReturn(pkRows);
// mock executor
BaseTransactionalExecutor executor = mock(BaseTransactionalExecutor.class);
when(executor.buildLockKey(tableRecords)).thenCallRealMethod();
when(executor.getTableMeta()).thenReturn(tableMeta);
assertThat(executor.buildLockKey(tableRecords)).isEqualTo(buildLockKeyExpect);
}
use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.
the class PostgresqlInsertExecutorTest method init.
@BeforeEach
public void init() {
ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
when(connectionProxy.getDbType()).thenReturn(JdbcConstants.POSTGRESQL);
statementProxy = mock(PreparedStatementProxy.class);
when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
StatementCallback statementCallback = mock(StatementCallback.class);
sqlInsertRecognizer = mock(SQLInsertRecognizer.class);
tableMeta = mock(TableMeta.class);
insertExecutor = Mockito.spy(new PostgresqlInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
pkIndexMap = new HashMap<String, Integer>() {
{
put(ID_COLUMN, pkIndex);
}
};
}
use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.
the class MySQLInsertExecutorTest method init.
@BeforeEach
public void init() throws SQLException {
ConnectionProxy connectionProxy = mock(ConnectionProxy.class);
when(connectionProxy.getDbType()).thenReturn(JdbcConstants.MYSQL);
DataSourceProxy dataSourceProxy = new DataSourceProxy(new MockDataSource());
when(connectionProxy.getDataSourceProxy()).thenReturn(dataSourceProxy);
statementProxy = mock(PreparedStatementProxy.class);
when(statementProxy.getConnectionProxy()).thenReturn(connectionProxy);
when(statementProxy.getTargetStatement()).thenReturn(statementProxy);
MockResultSet resultSet = new MockResultSet(statementProxy);
resultSet.mockResultSet(Arrays.asList("Variable_name", "Value"), new Object[][] { { "auto_increment_increment", "1" } });
when(statementProxy.getTargetStatement().executeQuery("SHOW VARIABLES LIKE 'auto_increment_increment'")).thenReturn(resultSet);
StatementCallback statementCallback = mock(StatementCallback.class);
sqlInsertRecognizer = mock(SQLInsertRecognizer.class);
tableMeta = mock(TableMeta.class);
insertExecutor = Mockito.spy(new MySQLInsertExecutor(statementProxy, statementCallback, sqlInsertRecognizer));
pkIndexMap = new HashMap<String, Integer>() {
{
put(ID_COLUMN, pkIndex);
}
};
}
use of io.seata.rm.datasource.sql.struct.TableMeta in project seata by seata.
the class OracleTableMetaCacheTest method getTableMetaTest.
@Test
public void getTableMetaTest() throws SQLException {
MockDriver mockDriver = new MockDriver(columnMetas, indexMetas, pkMetas);
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mock:xxx");
dataSource.setDriver(mockDriver);
DataSourceProxy proxy = new DataSourceProxy(dataSource);
TableMetaCache tableMetaCache = TableMetaCacheFactory.getTableMetaCache(JdbcConstants.ORACLE);
TableMeta tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "t.ot1", proxy.getResourceId());
Assertions.assertNotNull(tableMeta);
tableMeta = tableMetaCache.getTableMeta(proxy.getPlainConnection(), "t.\"ot1\"", proxy.getResourceId());
Assertions.assertNotNull(tableMeta);
}
Aggregations