This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 0a3f4378564 Add test cases on 
ShadowStatementDataSourceMappingsRetriever (#33570)
0a3f4378564 is described below

commit 0a3f43785644975e45e6ba1360549d907b23e2e4
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Nov 6 21:20:10 2024 +0800

    Add test cases on ShadowStatementDataSourceMappingsRetriever (#33570)
    
    * Add test cases on ShadowDeleteStatementDataSourceMappingsRetriever
    
    * Add test cases on ShadowUpdateStatementDataSourceMappingsRetriever
    
    * Add test cases on ShadowUpdateStatementDataSourceMappingsRetriever
    
    * Add test cases on ShadowSelectStatementDataSourceMappingsRetriever
---
 ...UpdateStatementDataSourceMappingsRetriever.java |  4 +-
 ...teStatementDataSourceMappingsRetrieverTest.java | 58 +++++++++++++++
 ...rtStatementDataSourceMappingsRetrieverTest.java |  5 +-
 ...ctStatementDataSourceMappingsRetrieverTest.java | 85 ++++++++++++++++++++++
 ...teStatementDataSourceMappingsRetrieverTest.java | 62 ++++++++++++++++
 5 files changed, 210 insertions(+), 4 deletions(-)

diff --git 
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java
 
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java
index ef143b67fe1..29676d4467d 100644
--- 
a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java
+++ 
b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetriever.java
@@ -22,7 +22,6 @@ import 
org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatem
 import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
 import org.apache.shardingsphere.shadow.route.util.ShadowExtractor;
 import org.apache.shardingsphere.shadow.spi.ShadowOperationType;
-import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.AndPredicate;
 import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
@@ -53,8 +52,7 @@ public final class 
ShadowUpdateStatementDataSourceMappingsRetriever extends Shad
     protected Collection<ShadowColumnCondition> 
getShadowColumnConditions(final String shadowColumnName) {
         Collection<ShadowColumnCondition> result = new LinkedList<>();
         for (ExpressionSegment each : getWhereSegment()) {
-            Collection<ColumnSegment> columns = 
ColumnExtractUtils.extract(each);
-            if (1 != columns.size()) {
+            if (1 != ColumnExtractUtils.extract(each).size()) {
                 continue;
             }
             String tableName = 
sqlStatementContext.getTablesContext().getTableNames().iterator().next();
diff --git 
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowDeleteStatementDataSourceMappingsRetrieverTest.java
 
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowDeleteStatementDataSourceMappingsRetrieverTest.java
new file mode 100644
index 00000000000..5f8fe557b36
--- /dev/null
+++ 
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowDeleteStatementDataSourceMappingsRetrieverTest.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.shadow.route.retriever.dml.table.column;
+
+import 
org.apache.shardingsphere.infra.binder.context.statement.dml.DeleteStatementContext;
+import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
+import org.apache.shardingsphere.shadow.route.util.ShadowExtractor;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
+import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.apache.shardingsphere.test.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+
+import static 
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(ShadowExtractor.class)
+class ShadowDeleteStatementDataSourceMappingsRetrieverTest {
+    
+    @Test
+    void assertRetrieve() {
+        DeleteStatementContext sqlStatementContext = 
mock(DeleteStatementContext.class, RETURNS_DEEP_STUBS);
+        WhereSegment whereSegment = mock(WhereSegment.class);
+        ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+        when(whereSegment.getExpr()).thenReturn(expressionSegment);
+        
when(sqlStatementContext.getWhereSegments()).thenReturn(Collections.singleton(whereSegment));
+        when(ShadowExtractor.extractValues(expressionSegment, 
Collections.singletonList("foo"))).thenReturn(Optional.of(Collections.singleton("foo")));
+        
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("foo_tbl"));
+        ShadowDeleteStatementDataSourceMappingsRetriever retriever = new 
ShadowDeleteStatementDataSourceMappingsRetriever(sqlStatementContext, 
Collections.singletonList("foo"));
+        Collection<ShadowColumnCondition> actual = 
retriever.getShadowColumnConditions("foo_col");
+        Collection<ShadowColumnCondition> expected = 
Collections.singletonList(new ShadowColumnCondition("foo_tbl", "foo_col", 
Collections.singleton("foo")));
+        assertThat(actual, deepEqual(expected));
+    }
+}
diff --git 
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetrieverTest.java
 
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetrieverTest.java
index 5aae3abf9fd..3734294a250 100644
--- 
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetrieverTest.java
+++ 
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowInsertStatementDataSourceMappingsRetrieverTest.java
@@ -24,6 +24,7 @@ import 
org.apache.shardingsphere.shadow.exception.syntax.UnsupportedShadowInsert
 import org.junit.jupiter.api.Test;
 
 import java.util.Arrays;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Optional;
 
@@ -45,7 +46,9 @@ class ShadowInsertStatementDataSourceMappingsRetrieverTest {
         
when(sqlStatementContext.getInsertValueContexts()).thenReturn(Collections.singletonList(insertValueContext));
         
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("foo_tbl"));
         ShadowInsertStatementDataSourceMappingsRetriever retriever = new 
ShadowInsertStatementDataSourceMappingsRetriever(sqlStatementContext);
-        assertThat(retriever.getShadowColumnConditions("foo_col"), 
deepEqual(Collections.singletonList(new ShadowColumnCondition("foo_tbl", 
"foo_col", Collections.singletonList("foo")))));
+        Collection<ShadowColumnCondition> actual = 
retriever.getShadowColumnConditions("foo_col");
+        Collection<ShadowColumnCondition> expected = 
Collections.singletonList(new ShadowColumnCondition("foo_tbl", "foo_col", 
Collections.singletonList("foo")));
+        assertThat(actual, deepEqual(expected));
     }
     
     @Test
diff --git 
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowSelectStatementDataSourceMappingsRetrieverTest.java
 
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowSelectStatementDataSourceMappingsRetrieverTest.java
new file mode 100644
index 00000000000..8a45dd71825
--- /dev/null
+++ 
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowSelectStatementDataSourceMappingsRetrieverTest.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.shadow.route.retriever.dml.table.column;
+
+import 
org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
+import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
+import org.apache.shardingsphere.shadow.route.util.ShadowExtractor;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.generic.OwnerSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.util.ColumnExtractUtils;
+import 
org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue;
+import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.apache.shardingsphere.test.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+
+import static 
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings({ColumnExtractUtils.class, ShadowExtractor.class})
+class ShadowSelectStatementDataSourceMappingsRetrieverTest {
+    
+    @Test
+    void assertRetrieveWithColumnOwner() {
+        SelectStatementContext sqlStatementContext = 
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+        WhereSegment whereSegment = mock(WhereSegment.class);
+        ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+        when(whereSegment.getExpr()).thenReturn(expressionSegment);
+        
when(sqlStatementContext.getWhereSegments()).thenReturn(Arrays.asList(whereSegment,
 mock(WhereSegment.class, RETURNS_DEEP_STUBS)));
+        ColumnSegment columnSegment = mock(ColumnSegment.class);
+        when(columnSegment.getOwner()).thenReturn(Optional.of(new 
OwnerSegment(0, 0, new IdentifierValue("foo"))));
+        
when(ColumnExtractUtils.extract(expressionSegment)).thenReturn(Collections.singleton(columnSegment));
+        when(ShadowExtractor.extractValues(expressionSegment, 
Collections.singletonList("foo"))).thenReturn(Optional.of(Collections.singleton("foo")));
+        
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("foo_tbl"));
+        
when(sqlStatementContext.getTablesContext().getTableAliasNameMap().get("foo")).thenReturn("foo_tbl");
+        ShadowSelectStatementDataSourceMappingsRetriever retriever = new 
ShadowSelectStatementDataSourceMappingsRetriever(sqlStatementContext, 
Collections.singletonList("foo"));
+        Collection<ShadowColumnCondition> actual = 
retriever.getShadowColumnConditions("foo_col");
+        Collection<ShadowColumnCondition> expected = 
Collections.singletonList(new ShadowColumnCondition("foo_tbl", "foo_col", 
Collections.singleton("foo")));
+        assertThat(actual, deepEqual(expected));
+    }
+    
+    @Test
+    void assertRetrieveWithoutColumnOwner() {
+        SelectStatementContext sqlStatementContext = 
mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
+        WhereSegment whereSegment = mock(WhereSegment.class);
+        ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+        when(whereSegment.getExpr()).thenReturn(expressionSegment);
+        
when(sqlStatementContext.getWhereSegments()).thenReturn(Arrays.asList(whereSegment,
 mock(WhereSegment.class, RETURNS_DEEP_STUBS)));
+        ColumnSegment columnSegment = mock(ColumnSegment.class);
+        when(columnSegment.getOwner()).thenReturn(Optional.empty());
+        
when(ColumnExtractUtils.extract(expressionSegment)).thenReturn(Collections.singleton(columnSegment));
+        when(ShadowExtractor.extractValues(expressionSegment, 
Collections.singletonList("foo"))).thenReturn(Optional.of(Collections.singleton("foo")));
+        
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("foo_tbl"));
+        ShadowSelectStatementDataSourceMappingsRetriever retriever = new 
ShadowSelectStatementDataSourceMappingsRetriever(sqlStatementContext, 
Collections.singletonList("foo"));
+        Collection<ShadowColumnCondition> actual = 
retriever.getShadowColumnConditions("foo_col");
+        Collection<ShadowColumnCondition> expected = 
Collections.singletonList(new ShadowColumnCondition("foo_tbl", "foo_col", 
Collections.singleton("foo")));
+        assertThat(actual, deepEqual(expected));
+    }
+}
diff --git 
a/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetrieverTest.java
 
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetrieverTest.java
new file mode 100644
index 00000000000..a7125b66d01
--- /dev/null
+++ 
b/features/shadow/core/src/test/java/org/apache/shardingsphere/shadow/route/retriever/dml/table/column/ShadowUpdateStatementDataSourceMappingsRetrieverTest.java
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.shadow.route.retriever.dml.table.column;
+
+import 
org.apache.shardingsphere.infra.binder.context.statement.dml.UpdateStatementContext;
+import org.apache.shardingsphere.shadow.condition.ShadowColumnCondition;
+import org.apache.shardingsphere.shadow.route.util.ShadowExtractor;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.ExpressionSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.segment.dml.predicate.WhereSegment;
+import 
org.apache.shardingsphere.sql.parser.statement.core.util.ColumnExtractUtils;
+import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.apache.shardingsphere.test.mock.StaticMockSettings;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Optional;
+
+import static 
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings({ColumnExtractUtils.class, ShadowExtractor.class})
+class ShadowUpdateStatementDataSourceMappingsRetrieverTest {
+    
+    @Test
+    void assertRetrieve() {
+        UpdateStatementContext sqlStatementContext = 
mock(UpdateStatementContext.class, RETURNS_DEEP_STUBS);
+        WhereSegment whereSegment = mock(WhereSegment.class);
+        ExpressionSegment expressionSegment = mock(ExpressionSegment.class);
+        when(whereSegment.getExpr()).thenReturn(expressionSegment);
+        
when(sqlStatementContext.getWhereSegments()).thenReturn(Arrays.asList(whereSegment,
 mock(WhereSegment.class, RETURNS_DEEP_STUBS)));
+        
when(ColumnExtractUtils.extract(expressionSegment)).thenReturn(Collections.singleton(mock(ColumnSegment.class)));
+        when(ShadowExtractor.extractValues(expressionSegment, 
Collections.singletonList("foo"))).thenReturn(Optional.of(Collections.singleton("foo")));
+        
when(sqlStatementContext.getTablesContext().getTableNames()).thenReturn(Collections.singleton("foo_tbl"));
+        ShadowUpdateStatementDataSourceMappingsRetriever retriever = new 
ShadowUpdateStatementDataSourceMappingsRetriever(sqlStatementContext, 
Collections.singletonList("foo"));
+        Collection<ShadowColumnCondition> actual = 
retriever.getShadowColumnConditions("foo_col");
+        Collection<ShadowColumnCondition> expected = 
Collections.singletonList(new ShadowColumnCondition("foo_tbl", "foo_col", 
Collections.singleton("foo")));
+        assertThat(actual, deepEqual(expected));
+    }
+}

Reply via email to