You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/04/09 13:22:03 UTC

[shardingsphere] branch master updated: add test (#16693)

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

duanzhengqiang 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 f71d7ad7e53 add test (#16693)
f71d7ad7e53 is described below

commit f71d7ad7e5301306271de13da62f79bc945dc44f
Author: weihubeats <we...@163.com>
AuthorDate: Sat Apr 9 21:21:55 2022 +0800

    add test (#16693)
    
    * add test
    
    * add test
    
    * add test
---
 .../schema/util/IndexMetaDataUtilTest.java         | 52 +++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/util/IndexMetaDataUtilTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/util/IndexMetaDataUtilTest.java
index 845c96cad54..e2560c521d4 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/util/IndexMetaDataUtilTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/metadata/schema/util/IndexMetaDataUtilTest.java
@@ -17,13 +17,40 @@
 
 package org.apache.shardingsphere.infra.metadata.schema.util;
 
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Lists;
+import lombok.SneakyThrows;
+import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData;
+import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.ddl.index.IndexSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
+import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Collections;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
+@RunWith(MockitoJUnitRunner.class)
 public final class IndexMetaDataUtilTest {
-    
+
+    private static final String TABLE = "table";
+
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private DataSource dataSource;
+
     @Test
     public void assertGetLogicIndexNameWithIndexNameSuffix() {
         assertThat(IndexMetaDataUtil.getLogicIndexName("order_index_t_order", "t_order"), is("order_index"));
@@ -48,4 +75,27 @@ public final class IndexMetaDataUtilTest {
     public void assertGetActualIndexNameWithoutActualTableName() {
         assertThat(IndexMetaDataUtil.getActualIndexName("order_index", null), is("order_index"));
     }
+
+    @Test
+    public void assertGetGeneratedLogicIndexName() {
+        ColumnSegment columnSegment = new ColumnSegment(0, 0, new IdentifierValue("user_id"));
+        ColumnSegment columnSegment1 = new ColumnSegment(0, 0, new IdentifierValue("user_name"));
+        assertThat(IndexMetaDataUtil.getGeneratedLogicIndexName(Lists.newArrayList(columnSegment, columnSegment1)), is("user_id_user_name_idx"));
+    }
+
+    @Test
+    @SneakyThrows(SQLException.class)
+    public void assertGetTableNamesFromMetaData() {
+        Connection connection = mock(Connection.class, RETURNS_DEEP_STUBS);
+        when(dataSource.getConnection()).thenReturn(connection);
+        ShardingSphereSchema schema = buildSchema();
+        IndexSegment indexSegment1 = new IndexSegment(0, 0, new IdentifierValue(TABLE));
+        assertThat(IndexMetaDataUtil.getTableNamesFromMetaData(schema, Lists.newArrayList(indexSegment1)), is(Collections.singletonList(TABLE)));
+    }
+
+    private ShardingSphereSchema buildSchema() {
+        IndexMetaData indexMetaData = new IndexMetaData(TABLE);
+        TableMetaData tableMetaData = new TableMetaData(TABLE, Collections.emptyList(), Collections.singletonList(indexMetaData), Collections.emptyList());
+        return new ShardingSphereSchema(ImmutableMap.of(TABLE, tableMetaData));
+    }
 }