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 2023/05/02 23:34:24 UTC

[shardingsphere] branch master updated: Add unit tests for ShardingSphereTable (#25386)

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 b61b87b3cac Add unit tests for ShardingSphereTable (#25386)
b61b87b3cac is described below

commit b61b87b3cace4d4b4773b085c7bf971848dfa5ab
Author: 100paperkite <98...@users.noreply.github.com>
AuthorDate: Wed May 3 08:34:06 2023 +0900

    Add unit tests for ShardingSphereTable (#25386)
    
    * Add unit tests for ShardingSphereTable
    
    * Remove useless blank lines
    
    * Add `assert` prefix to the names of test methods
    
    * Fix checkstyle
    
    - Avoid '.*' form of import
    
    * Remove useless blank line
---
 .../schema/model/ShardingSphereTableTest.java      | 143 +++++++++++++++++++++
 1 file changed, 143 insertions(+)

diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereTableTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereTableTest.java
new file mode 100644
index 00000000000..59efa052e6c
--- /dev/null
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereTableTest.java
@@ -0,0 +1,143 @@
+/*
+ * 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.infra.metadata.database.schema.model;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Types;
+import java.util.Collections;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.hasItems;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+class ShardingSphereTableTest {
+    
+    private ShardingSphereTable shardingSphereTable;
+    
+    @BeforeEach
+    void setUp() {
+        shardingSphereTable = new ShardingSphereTable();
+    }
+    
+    @Test
+    void assertPutColumn() {
+        ShardingSphereColumn column1 = new ShardingSphereColumn("foo_column_1", Types.INTEGER, true, true, false, true, false);
+        ShardingSphereColumn column2 = new ShardingSphereColumn("foo_column_2", Types.INTEGER, false, true, false, true, false);
+        shardingSphereTable.putColumn(column1);
+        shardingSphereTable.putColumn(column2);
+        assertThat(shardingSphereTable.getColumn("foo_column_1"), is(column1));
+        assertThat(shardingSphereTable.getColumn("foo_column_2"), is(column2));
+        assertThat(shardingSphereTable.getColumns(), hasSize(2));
+    }
+    
+    @Test
+    void assertGetColumn() {
+        ShardingSphereColumn column = new ShardingSphereColumn("foo_column", Types.INTEGER, true, true, false, true, false);
+        shardingSphereTable.putColumn(column);
+        assertThat(shardingSphereTable.getColumn("foo_column"), is(column));
+        assertThat(shardingSphereTable.getColumn("FOO_COLUMN"), is(column));
+        assertNull(shardingSphereTable.getColumn("invalid"));
+    }
+    
+    @Test
+    void assertGetColumns() {
+        ShardingSphereColumn column1 = new ShardingSphereColumn("foo_column_1", Types.INTEGER, true, true, false, true, false);
+        ShardingSphereColumn column2 = new ShardingSphereColumn("foo_column_2", Types.INTEGER, false, true, false, true, false);
+        shardingSphereTable.putColumn(column1);
+        shardingSphereTable.putColumn(column2);
+        assertThat(shardingSphereTable.getColumns(), hasItems(column1, column2));
+        assertThat(shardingSphereTable.getColumns(), hasSize(2));
+    }
+    
+    @Test
+    void assertContainsColumn() {
+        ShardingSphereColumn column = new ShardingSphereColumn("foo_column", Types.INTEGER, true, true, false, true, false);
+        shardingSphereTable.putColumn(column);
+        assertTrue(shardingSphereTable.containsColumn("foo_column"));
+        assertFalse(shardingSphereTable.containsColumn("invalid"));
+    }
+    
+    @Test
+    void assertPutIndex() {
+        ShardingSphereIndex index1 = new ShardingSphereIndex("foo_index_1");
+        ShardingSphereIndex index2 = new ShardingSphereIndex("foo_index_2");
+        shardingSphereTable.putIndex(index1);
+        shardingSphereTable.putIndex(index2);
+        assertThat(shardingSphereTable.getIndex("foo_index_1"), is(index1));
+        assertThat(shardingSphereTable.getIndex("foo_index_2"), is(index2));
+        assertNull(shardingSphereTable.getIndex("invalid"));
+        assertThat(shardingSphereTable.getIndexes(), hasSize(2));
+    }
+    
+    @Test
+    void assertGetIndex() {
+        ShardingSphereIndex index = new ShardingSphereIndex("foo_index");
+        shardingSphereTable.putIndex(index);
+        assertThat(shardingSphereTable.getIndex("foo_index"), is(index));
+        assertThat(shardingSphereTable.getIndex("FOO_INDEX"), is(index));
+        assertNull(shardingSphereTable.getIndex("invalid"));
+    }
+    
+    @Test
+    void assertRemoveIndex() {
+        ShardingSphereIndex index1 = new ShardingSphereIndex("foo_index_1");
+        ShardingSphereIndex index2 = new ShardingSphereIndex("foo_index_2");
+        shardingSphereTable.putIndex(index1);
+        shardingSphereTable.putIndex(index2);
+        shardingSphereTable.removeIndex("foo_index_1");
+        assertNull(shardingSphereTable.getIndex("foo_index_1"));
+        shardingSphereTable.removeIndex("invalid");
+        assertThat(shardingSphereTable.getIndex("foo_index_2"), is(index2));
+        assertThat(shardingSphereTable.getIndexes(), hasSize(1));
+    }
+    
+    @Test
+    void assertGetIndexes() {
+        ShardingSphereIndex index1 = new ShardingSphereIndex("foo_index_1");
+        ShardingSphereIndex index2 = new ShardingSphereIndex("foo_index_2");
+        shardingSphereTable.putIndex(index1);
+        shardingSphereTable.putIndex(index2);
+        assertThat(shardingSphereTable.getIndexes(), hasItems(index1, index2));
+        assertThat(shardingSphereTable.getIndexes(), hasSize(2));
+    }
+    
+    @Test
+    void assertContainsIndex() {
+        ShardingSphereIndex index1 = new ShardingSphereIndex("foo_index_1");
+        ShardingSphereIndex index2 = new ShardingSphereIndex("foo_index_2");
+        shardingSphereTable.putIndex(index1);
+        shardingSphereTable.putIndex(index2);
+        assertTrue(shardingSphereTable.containsIndex("foo_index_1"));
+        assertTrue(shardingSphereTable.containsIndex("foo_index_2"));
+        assertFalse(shardingSphereTable.containsIndex("invalid"));
+    }
+    
+    @Test
+    void assertGetConstraints() {
+        ShardingSphereConstraint constraint = new ShardingSphereConstraint("t_order_foreign_key", "t_user");
+        ShardingSphereTable table = new ShardingSphereTable("t_order", Collections.emptyList(), Collections.emptyList(), Collections.singletonList(constraint));
+        assertThat(table.getConstraints(), hasItems(constraint));
+        assertThat(table.getConstraints(), hasSize(1));
+    }
+}