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 6d5cf990255 Refactor test cases on YamlSchemaSwapper (#33246)
6d5cf990255 is described below

commit 6d5cf9902559a79fe8dda6d2fa81517a499a8274
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Oct 15 00:24:47 2024 +0800

    Refactor test cases on YamlSchemaSwapper (#33246)
    
    * Add test cases on YamlViewSwapper
    
    * Refactor test cases on YamlSchemaSwapper
---
 .../database/schema/model/ShardingSphereView.java  |  7 +-
 .../yaml/schema/swapper/YamlSchemaSwapperTest.java | 96 ++++++++--------------
 ...schema-without-table.yaml => empty-schema.yaml} |  2 +-
 .../src/test/resources/yaml/schema/schema.yaml     | 30 +++----
 4 files changed, 51 insertions(+), 84 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereView.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereView.java
index 0d53b3e9516..e03d1a3918d 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereView.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/schema/model/ShardingSphereView.java
@@ -19,11 +19,13 @@ package 
org.apache.shardingsphere.infra.metadata.database.schema.model;
 
 import lombok.EqualsAndHashCode;
 import lombok.Getter;
+import lombok.RequiredArgsConstructor;
 import lombok.ToString;
 
 /**
  * ShardingSphere view.
  */
+@RequiredArgsConstructor
 @Getter
 @EqualsAndHashCode
 @ToString
@@ -32,9 +34,4 @@ public final class ShardingSphereView {
     private final String name;
     
     private final String viewDefinition;
-    
-    public ShardingSphereView(final String name, final String viewDefinition) {
-        this.name = name;
-        this.viewDefinition = viewDefinition;
-    }
 }
diff --git 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlSchemaSwapperTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlSchemaSwapperTest.java
index b39d7828f85..d7610814cb1 100644
--- 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlSchemaSwapperTest.java
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/yaml/schema/swapper/YamlSchemaSwapperTest.java
@@ -19,12 +19,13 @@ package org.apache.shardingsphere.infra.yaml.schema.swapper;
 
 import lombok.SneakyThrows;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereConstraint;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereIndex;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
 import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
+import 
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereView;
 import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
-import 
org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereColumn;
 import 
org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereSchema;
-import 
org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereTable;
 import org.junit.jupiter.api.Test;
 
 import java.io.IOException;
@@ -32,86 +33,53 @@ import java.net.URISyntaxException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.Collections;
-import java.util.Map;
 import java.util.stream.Collectors;
 
-import static org.hamcrest.CoreMatchers.is;
+import static 
org.apache.shardingsphere.test.matcher.ShardingSphereAssertionMatchers.deepEqual;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertFalse;
-import static org.junit.jupiter.api.Assertions.assertTrue;
 
 class YamlSchemaSwapperTest {
     
-    private static final String YAML = "yaml/schema/schema.yaml";
+    private static final String YAML_FILE = "yaml/schema/schema.yaml";
     
-    private static final String YAML_WITHOUT_TABLE = 
"yaml/schema/schema-without-table.yaml";
+    private static final String EMPTY_YAML_FILE = 
"yaml/schema/empty-schema.yaml";
     
-    @Test
-    void assertSwapToYamlSchema() {
-        ShardingSphereSchema schema = new 
YamlSchemaSwapper().swapToObject(YamlEngine.unmarshal(readYAML(YAML), 
YamlShardingSphereSchema.class));
-        YamlShardingSphereSchema yamlSchema = new 
YamlSchemaSwapper().swapToYamlConfiguration(schema);
-        assertThat(yamlSchema.getTables().keySet(), 
is(Collections.singleton("t_order")));
-        YamlShardingSphereTable yamlTableMetaData = 
yamlSchema.getTables().get("t_order");
-        assertThat(yamlTableMetaData.getIndexes().keySet(), 
is(Collections.singleton("primary")));
-        assertYamlColumn(yamlTableMetaData.getColumns());
-    }
-    
-    private void assertYamlColumn(final Map<String, YamlShardingSphereColumn> 
columns) {
-        assertThat(columns.size(), is(2));
-        YamlShardingSphereColumn idColumn = columns.get("id");
-        assertThat(idColumn.getName(), is("id"));
-        assertFalse(idColumn.isCaseSensitive());
-        assertThat(idColumn.getDataType(), is(0));
-        assertFalse(idColumn.isGenerated());
-        assertTrue(idColumn.isPrimaryKey());
-        assertFalse(idColumn.isUnsigned());
-        assertFalse(idColumn.isNullable());
-        assertTrue(idColumn.isVisible());
-        YamlShardingSphereColumn nameColumn = columns.get("name");
-        assertThat(nameColumn.getName(), is("name"));
-        assertTrue(nameColumn.isCaseSensitive());
-        assertThat(nameColumn.getDataType(), is(10));
-        assertTrue(nameColumn.isGenerated());
-        assertFalse(nameColumn.isPrimaryKey());
-        assertTrue(nameColumn.isUnsigned());
-        assertFalse(nameColumn.isNullable());
-        assertFalse(nameColumn.isVisible());
-    }
+    private final YamlSchemaSwapper swapper = new YamlSchemaSwapper();
     
     @Test
-    void assertSwapToShardingSphereSchema() {
-        YamlShardingSphereSchema yamlSchema = 
YamlEngine.unmarshal(readYAML(YAML), YamlShardingSphereSchema.class);
-        ShardingSphereSchema actualSchema = new 
YamlSchemaSwapper().swapToObject(yamlSchema);
-        assertThat(actualSchema.getAllTableNames(), 
is(Collections.singleton("t_order")));
-        ShardingSphereTable actualTable = actualSchema.getTable("t_order");
-        assertColumn(actualTable);
-        assertThat(actualTable.getIndexValues().size(), is(1));
-        assertThat(actualTable.getIndexValues().iterator().next().getName(), 
is("PRIMARY"));
-        assertThat(actualSchema.getAllColumnNames("t_order").size(), is(2));
-        assertTrue(actualSchema.containsColumn("t_order", "id"));
-        assertTrue(actualSchema.containsColumn("t_order", "name"));
-    }
-    
-    private void assertColumn(final ShardingSphereTable table) {
-        assertThat(table.getColumnValues().size(), is(2));
-        assertThat(table.getColumn("id"), is(new ShardingSphereColumn("id", 0, 
true, false, false, true, false, false)));
-        assertThat(table.getColumn("name"), is(new 
ShardingSphereColumn("name", 10, false, true, true, false, true, false)));
+    void assertSwapToYamlConfiguration() {
+        ShardingSphereSchema schema = createShardingSphereSchema();
+        YamlShardingSphereSchema actual = 
swapper.swapToYamlConfiguration(schema);
+        YamlShardingSphereSchema expected = unmarshal(YAML_FILE);
+        assertThat(actual, deepEqual(expected));
     }
     
     @Test
-    void assertSwapToYamlSchemaWithoutTable() {
-        ShardingSphereSchema schema = new 
YamlSchemaSwapper().swapToObject(YamlEngine.unmarshal(readYAML(YAML_WITHOUT_TABLE),
 YamlShardingSphereSchema.class));
-        assertTrue(new 
YamlSchemaSwapper().swapToYamlConfiguration(schema).getTables().isEmpty());
+    void assertSwapToObject() {
+        ShardingSphereSchema actual = 
swapper.swapToObject(unmarshal(YAML_FILE));
+        ShardingSphereSchema expected = createShardingSphereSchema();
+        assertThat(actual, deepEqual(expected));
     }
     
     @Test
-    void assertSwapToShardingSphereSchemaWithoutTable() {
-        YamlShardingSphereSchema yamlSchema = 
YamlEngine.unmarshal(readYAML(YAML_WITHOUT_TABLE), 
YamlShardingSphereSchema.class);
-        assertTrue(new 
YamlSchemaSwapper().swapToObject(yamlSchema).getAllTableNames().isEmpty());
+    void assertSwapToObjectWithEmptySchema() {
+        ShardingSphereSchema actual = 
swapper.swapToObject(unmarshal(EMPTY_YAML_FILE));
+        ShardingSphereSchema expected = new ShardingSphereSchema("foo_schema");
+        assertThat(actual, deepEqual(expected));
     }
     
     @SneakyThrows({URISyntaxException.class, IOException.class})
-    private String readYAML(final String yamlFile) {
-        return 
Files.readAllLines(Paths.get(ClassLoader.getSystemResource(yamlFile).toURI())).stream().collect(Collectors.joining(System.lineSeparator()));
+    private YamlShardingSphereSchema unmarshal(final String yamlFile) {
+        String yamlContent = 
Files.readAllLines(Paths.get(ClassLoader.getSystemResource(yamlFile).toURI())).stream().collect(Collectors.joining(System.lineSeparator()));
+        return YamlEngine.unmarshal(yamlContent, 
YamlShardingSphereSchema.class);
+    }
+    
+    private ShardingSphereSchema createShardingSphereSchema() {
+        ShardingSphereTable table = new ShardingSphereTable(null,
+                Collections.singleton(new ShardingSphereColumn("foo_col", 0, 
true, false, false, true, false, false)),
+                Collections.singleton(new ShardingSphereIndex("PRIMARY")),
+                Collections.singleton(new 
ShardingSphereConstraint("foo_constraint", "foo_tbl")), null);
+        ShardingSphereView view = new ShardingSphereView("foo_view", "SELECT 
1");
+        return new ShardingSphereSchema("foo_schema", 
Collections.singletonMap("foo_tbl", table), 
Collections.singletonMap("foo_view", view));
     }
 }
diff --git 
a/infra/common/src/test/resources/yaml/schema/schema-without-table.yaml 
b/infra/common/src/test/resources/yaml/schema/empty-schema.yaml
similarity index 97%
rename from 
infra/common/src/test/resources/yaml/schema/schema-without-table.yaml
rename to infra/common/src/test/resources/yaml/schema/empty-schema.yaml
index 4c456c05991..c4d034348b0 100644
--- a/infra/common/src/test/resources/yaml/schema/schema-without-table.yaml
+++ b/infra/common/src/test/resources/yaml/schema/empty-schema.yaml
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-tables:
+name: foo_schema
diff --git a/infra/common/src/test/resources/yaml/schema/schema.yaml 
b/infra/common/src/test/resources/yaml/schema/schema.yaml
index 35bb4429961..ba3b22b9f2d 100644
--- a/infra/common/src/test/resources/yaml/schema/schema.yaml
+++ b/infra/common/src/test/resources/yaml/schema/schema.yaml
@@ -15,25 +15,27 @@
 # limitations under the License.
 #
 
+name: foo_schema
 tables:
-  t_order:
+  foo_tbl:
     columns:
-      id:
-        caseSensitive: false
+      foo_col:
+        name: foo_col
         dataType: 0
-        generated: false
-        name: id
         primaryKey: true
-        unsigned: false
+        generated: false
+        caseSensitive: false
         visible: true
-      name:
-        caseSensitive: true
-        dataType: 10
-        generated: true
-        name: name
-        primaryKey: false
-        unsigned: true
-        visible: false
+        unsigned: false
+        nullable: false
     indexes:
        primary:
           name: PRIMARY
+    constraints:
+      foo_constraint:
+          name: foo_constraint
+          referencedTableName: foo_tbl
+views:
+  foo_view:
+    name: foo_view
+    viewDefinition: SELECT 1

Reply via email to