You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2022/05/10 15:22:22 UTC

[GitHub] [shardingsphere] terrymanu commented on a diff in pull request #17504: Add unit test SingleTableSQLRouter#decorateRouteContext

terrymanu commented on code in PR #17504:
URL: https://github.com/apache/shardingsphere/pull/17504#discussion_r869372779


##########
shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java:
##########
@@ -146,19 +165,19 @@ private LogicSQL createLogicSQL() {
         SQLStatementContext<CreateTableStatement> sqlStatementContext = new CreateTableStatementContext(createTableStatement);
         return new LogicSQL(sqlStatementContext, "create table", parameters);
     }
-    
+
     private Map<String, DataSource> createSingleDataSourceMap() {
         Map<String, DataSource> result = new HashMap<>(2, 1);
         result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
         return result;
     }
-    
+
     private Map<String, DataSource> createReadwriteSplittingDataSourceMap() {
         Map<String, DataSource> result = new HashMap<>(2, 1);
         result.put("readwrite_ds", mock(DataSource.class, RETURNS_DEEP_STUBS));
         return result;
     }
-    
+

Review Comment:
   Please keep the origin indent



##########
shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java:
##########
@@ -146,19 +165,19 @@ private LogicSQL createLogicSQL() {
         SQLStatementContext<CreateTableStatement> sqlStatementContext = new CreateTableStatementContext(createTableStatement);
         return new LogicSQL(sqlStatementContext, "create table", parameters);
     }
-    
+
     private Map<String, DataSource> createSingleDataSourceMap() {
         Map<String, DataSource> result = new HashMap<>(2, 1);
         result.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
         return result;
     }
-    
+

Review Comment:
   Please keep the origin indent



##########
shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java:
##########
@@ -103,29 +103,48 @@ public void assertCreateRouteContextWithMultiDataSource() {
         assertThat(tableMapper.getLogicName(), is("t_order"));
         assertFalse(actual.isFederated());
     }
-    
+
+    @Test
+    public void assertDecorateRouteContext() {
+        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
+                createSingleDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
+        singleTableRule.getSingleTableDataNodes().put("t_order", Collections.singletonList(mockDataNode("ds_0")));
+        ShardingSphereMetaData metaData = mockSingleDataSourceMetaData();
+        RouteContext actual = new RouteContext();
+        new SingleTableSQLRouter().decorateRouteContext(actual, createLogicSQL(), metaData, singleTableRule, new ConfigurationProperties(new Properties()));
+        List<RouteUnit> routeUnits = new ArrayList<>(actual.getRouteUnits());
+        assertThat(actual.getRouteUnits().size(), is(1));
+        assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(), is("ds_0"));
+        assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0"));
+        RouteMapper tableMapper = routeUnits.get(0).getTableMappers().iterator().next();
+        assertThat(tableMapper.getActualName(), is("t_order"));
+        assertThat(tableMapper.getLogicName(), is("t_order"));
+        assertFalse(actual.isFederated());
+    }
+
     private DataNode mockDataNode(final String dataSourceName) {
         DataNode result = new DataNode(dataSourceName, "t_order");
         result.setSchemaName(DefaultDatabase.LOGIC_NAME);
         return result;
     }
-    
+
     private ShardingSphereMetaData mockSingleDataSourceMetaData() {
         ShardingSphereMetaData result = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
         Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
         dataSourceMap.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
         when(result.getResource().getDataSources()).thenReturn(dataSourceMap);
+        when(result.getDatabaseName()).thenReturn(DefaultDatabase.LOGIC_NAME);
         return result;
     }
-    
+
     private ShardingSphereMetaData mockReadwriteSplittingDataSourceMetaData() {
         ShardingSphereMetaData result = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
         Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
         dataSourceMap.put("write_ds", mock(DataSource.class, RETURNS_DEEP_STUBS));
         when(result.getResource().getDataSources()).thenReturn(dataSourceMap);
         return result;
     }
-    
+

Review Comment:
   Please keep the origin indent



##########
shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java:
##########
@@ -103,29 +103,48 @@ public void assertCreateRouteContextWithMultiDataSource() {
         assertThat(tableMapper.getLogicName(), is("t_order"));
         assertFalse(actual.isFederated());
     }
-    
+
+    @Test
+    public void assertDecorateRouteContext() {
+        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
+                createSingleDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
+        singleTableRule.getSingleTableDataNodes().put("t_order", Collections.singletonList(mockDataNode("ds_0")));
+        ShardingSphereMetaData metaData = mockSingleDataSourceMetaData();
+        RouteContext actual = new RouteContext();
+        new SingleTableSQLRouter().decorateRouteContext(actual, createLogicSQL(), metaData, singleTableRule, new ConfigurationProperties(new Properties()));
+        List<RouteUnit> routeUnits = new ArrayList<>(actual.getRouteUnits());
+        assertThat(actual.getRouteUnits().size(), is(1));
+        assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(), is("ds_0"));
+        assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0"));
+        RouteMapper tableMapper = routeUnits.get(0).getTableMappers().iterator().next();
+        assertThat(tableMapper.getActualName(), is("t_order"));
+        assertThat(tableMapper.getLogicName(), is("t_order"));
+        assertFalse(actual.isFederated());
+    }
+
     private DataNode mockDataNode(final String dataSourceName) {
         DataNode result = new DataNode(dataSourceName, "t_order");
         result.setSchemaName(DefaultDatabase.LOGIC_NAME);
         return result;
     }
-    
+
     private ShardingSphereMetaData mockSingleDataSourceMetaData() {
         ShardingSphereMetaData result = mock(ShardingSphereMetaData.class, RETURNS_DEEP_STUBS);
         Map<String, DataSource> dataSourceMap = new HashMap<>(2, 1);
         dataSourceMap.put("ds_0", mock(DataSource.class, RETURNS_DEEP_STUBS));
         when(result.getResource().getDataSources()).thenReturn(dataSourceMap);
+        when(result.getDatabaseName()).thenReturn(DefaultDatabase.LOGIC_NAME);
         return result;
     }
-    
+

Review Comment:
   Please keep the origin indent



##########
shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java:
##########
@@ -146,19 +165,19 @@ private LogicSQL createLogicSQL() {
         SQLStatementContext<CreateTableStatement> sqlStatementContext = new CreateTableStatementContext(createTableStatement);
         return new LogicSQL(sqlStatementContext, "create table", parameters);
     }
-    
+

Review Comment:
   Please keep the origin indent



##########
shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java:
##########
@@ -135,7 +154,7 @@ private ShardingSphereMetaData mockMultiDataSourceMetaData() {
         when(result.getDatabaseName()).thenReturn(DefaultDatabase.LOGIC_NAME);
         return result;
     }
-    
+

Review Comment:
   Please keep the origin indent



##########
shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java:
##########
@@ -103,29 +103,48 @@ public void assertCreateRouteContextWithMultiDataSource() {
         assertThat(tableMapper.getLogicName(), is("t_order"));
         assertFalse(actual.isFederated());
     }
-    
+
+    @Test
+    public void assertDecorateRouteContext() {
+        SingleTableRule singleTableRule = new SingleTableRule(new SingleTableRuleConfiguration(), DefaultDatabase.LOGIC_NAME, mock(DatabaseType.class),
+                createSingleDataSourceMap(), Collections.emptyList(), new ConfigurationProperties(new Properties()));
+        singleTableRule.getSingleTableDataNodes().put("t_order", Collections.singletonList(mockDataNode("ds_0")));
+        ShardingSphereMetaData metaData = mockSingleDataSourceMetaData();
+        RouteContext actual = new RouteContext();
+        new SingleTableSQLRouter().decorateRouteContext(actual, createLogicSQL(), metaData, singleTableRule, new ConfigurationProperties(new Properties()));
+        List<RouteUnit> routeUnits = new ArrayList<>(actual.getRouteUnits());
+        assertThat(actual.getRouteUnits().size(), is(1));
+        assertThat(routeUnits.get(0).getDataSourceMapper().getLogicName(), is("ds_0"));
+        assertThat(routeUnits.get(0).getDataSourceMapper().getActualName(), is("ds_0"));
+        RouteMapper tableMapper = routeUnits.get(0).getTableMappers().iterator().next();
+        assertThat(tableMapper.getActualName(), is("t_order"));
+        assertThat(tableMapper.getLogicName(), is("t_order"));
+        assertFalse(actual.isFederated());
+    }
+
     private DataNode mockDataNode(final String dataSourceName) {
         DataNode result = new DataNode(dataSourceName, "t_order");
         result.setSchemaName(DefaultDatabase.LOGIC_NAME);
         return result;
     }
-    
+

Review Comment:
   Please keep the origin indent



##########
shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java:
##########
@@ -55,7 +55,7 @@
 import static org.mockito.Mockito.when;
 
 public final class SingleTableSQLRouterTest {
-    
+

Review Comment:
   Please keep the origin indent



##########
shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java:
##########
@@ -85,7 +85,7 @@ public void assertCreateRouteContextWithReadwriteSplittingDataSource() {
         assertTrue(routeUnits.get(0).getTableMappers().isEmpty());
         assertFalse(actual.isFederated());
     }
-    
+

Review Comment:
   Please keep the origin indent



##########
shardingsphere-kernel/shardingsphere-single-table/shardingsphere-single-table-core/src/test/java/org/apache/shardingsphere/singletable/route/SingleTableSQLRouterTest.java:
##########
@@ -70,7 +70,7 @@ public void assertCreateRouteContextWithSingleDataSource() {
         assertTrue(routeUnits.get(0).getTableMappers().isEmpty());
         assertFalse(actual.isFederated());
     }
-    
+

Review Comment:
   Please keep the origin indent



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org