This is an automated email from the ASF dual-hosted git repository.
yx9o pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new d70536804da Add more test cases on
QualifiedReadwriteSplittingPrimaryDataSourceRouterTest (#33599)
d70536804da is described below
commit d70536804dadb529a541bd133b3d17a839b915bd
Author: Liang Zhang <[email protected]>
AuthorDate: Fri Nov 8 17:29:26 2024 +0800
Add more test cases on
QualifiedReadwriteSplittingPrimaryDataSourceRouterTest (#33599)
* Add more test cases on
QualifiedReadwriteSplittingTransactionalDataSourceRouter
* Add more test cases on
QualifiedReadwriteSplittingPrimaryDataSourceRouterTest
---
...dwriteSplittingPrimaryDataSourceRouterTest.java | 61 +++++++++++++++++-----
1 file changed, 48 insertions(+), 13 deletions(-)
diff --git
a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/qualified/type/QualifiedReadwriteSplittingPrimaryDataSourceRouterTest.java
b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/qualified/type/QualifiedReadwriteSplittingPrimaryDataSourceRouterTest.java
index b998cd54b76..ab152137697 100644
---
a/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/qualified/type/QualifiedReadwriteSplittingPrimaryDataSourceRouterTest.java
+++
b/features/readwrite-splitting/core/src/test/java/org/apache/shardingsphere/readwritesplitting/route/qualified/type/QualifiedReadwriteSplittingPrimaryDataSourceRouterTest.java
@@ -17,12 +17,14 @@
package org.apache.shardingsphere.readwritesplitting.route.qualified.type;
-import
org.apache.shardingsphere.infra.binder.context.statement.CommonSQLStatementContext;
+import
org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
+import
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.infra.hint.HintValueContext;
+import
org.apache.shardingsphere.readwritesplitting.rule.ReadwriteSplittingDataSourceGroupRule;
import
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.LockSegment;
import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
-import
org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLSelectStatement;
-import
org.apache.shardingsphere.sql.parser.statement.mysql.dml.MySQLUpdateStatement;
+import
org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
@@ -30,8 +32,11 @@ import org.mockito.junit.jupiter.MockitoExtension;
import java.util.Optional;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -39,27 +44,57 @@ import static org.mockito.Mockito.when;
class QualifiedReadwriteSplittingPrimaryDataSourceRouterTest {
@Mock
- private CommonSQLStatementContext sqlStatementContext;
+ private SQLStatementContext sqlStatementContext;
- @Mock
- private HintValueContext hintValueContext;
+ private final HintValueContext hintValueContext = new HintValueContext();
@Test
- void assertWriteRouteStatement() {
- MySQLSelectStatement selectStatement =
mock(MySQLSelectStatement.class);
+ void assertIsQualifiedWithSelect() {
+ SelectStatement selectStatement = mock(SelectStatement.class);
when(selectStatement.getLock()).thenReturn(Optional.of(new
LockSegment(0, 1)));
when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement);
assertTrue(new
QualifiedReadwriteSplittingPrimaryDataSourceRouter().isQualified(sqlStatementContext,
null, hintValueContext));
-
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(MySQLUpdateStatement.class));
+ }
+
+ @Test
+ void assertIsQualifiedWithSelectAndContainsLastInsertIdProjection() {
+ SelectStatementContext sqlStatementContext =
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+
when(sqlStatementContext.getProjectionsContext().isContainsLastInsertIdProjection()).thenReturn(true);
assertTrue(new
QualifiedReadwriteSplittingPrimaryDataSourceRouter().isQualified(sqlStatementContext,
null, hintValueContext));
}
@Test
- void assertHintRouteWriteOnly() {
+ void assertIsQualifiedWithUpdate() {
+
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(UpdateStatement.class));
+ assertTrue(new
QualifiedReadwriteSplittingPrimaryDataSourceRouter().isQualified(sqlStatementContext,
null, hintValueContext));
+ }
+
+ @Test
+ void assertIsQualifiedWithHintManager() {
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(SelectStatement.class));
- when(hintValueContext.isWriteRouteOnly()).thenReturn(false);
- assertFalse(new
QualifiedReadwriteSplittingPrimaryDataSourceRouter().isQualified(sqlStatementContext,
null, hintValueContext));
- when(hintValueContext.isWriteRouteOnly()).thenReturn(true);
+ try (HintManager hintManager = HintManager.getInstance()) {
+ hintManager.setWriteRouteOnly();
+ assertTrue(new
QualifiedReadwriteSplittingPrimaryDataSourceRouter().isQualified(sqlStatementContext,
null, hintValueContext));
+ }
+ }
+
+ @Test
+ void assertIsQualifiedWithHintValue() {
+
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(SelectStatement.class));
+ hintValueContext.setWriteRouteOnly(true);
assertTrue(new
QualifiedReadwriteSplittingPrimaryDataSourceRouter().isQualified(sqlStatementContext,
null, hintValueContext));
}
+
+ @Test
+ void assertIsNotQualifiedWithHint() {
+
when(sqlStatementContext.getSqlStatement()).thenReturn(mock(SelectStatement.class));
+ assertFalse(new
QualifiedReadwriteSplittingPrimaryDataSourceRouter().isQualified(sqlStatementContext,
null, hintValueContext));
+ }
+
+ @Test
+ void assertRoute() {
+ ReadwriteSplittingDataSourceGroupRule rule =
mock(ReadwriteSplittingDataSourceGroupRule.class);
+ when(rule.getWriteDataSource()).thenReturn("write_ds");
+ assertThat(new
QualifiedReadwriteSplittingPrimaryDataSourceRouter().route(rule),
is("write_ds"));
+ }
}