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/05/12 10:29:27 UTC
[shardingsphere] branch master updated: add integration test for ddl generator (#17609)
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 a18abb1a323 add integration test for ddl generator (#17609)
a18abb1a323 is described below
commit a18abb1a323cdaf8cb39b2c0e1729101b92d87fe
Author: Chuxin Chen <ch...@qq.com>
AuthorDate: Thu May 12 18:29:13 2022 +0800
add integration test for ddl generator (#17609)
* add integration test for ddl generator
* add integration test for ddl generator
* add integration test for ddl generator
---
.../cases/entity/DDLGeneratorAssertionEntity.java | 45 +++++++
.../entity/DDLGeneratorAssertionsRootEntity.java | 36 ++++++
.../cases/entity/DDLGeneratorInputEntity.java | 41 +++++++
.../cases/entity/DDLGeneratorOutputEntity.java | 41 +++++++
.../cases/postgresql/PostgreSQLDDLGeneratorIT.java | 135 +++++++++++++++++++++
.../postgresql/ddlgenerator/ddlgenerator.xml | 116 ++++++++++++++++++
6 files changed, 414 insertions(+)
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorAssertionEntity.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorAssertionEntity.java
new file mode 100644
index 00000000000..f4f28eed31b
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorAssertionEntity.java
@@ -0,0 +1,45 @@
+/*
+ * 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.integration.data.pipeline.cases.entity;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+import java.util.Collection;
+
+/**
+ * DDL generator entity for JAXB.
+ */
+@XmlAccessorType(XmlAccessType.FIELD)
+@Getter
+@Setter
+public final class DDLGeneratorAssertionEntity {
+
+ @XmlAttribute(required = true)
+ private String id;
+
+ @XmlElement(required = true)
+ private DDLGeneratorInputEntity input;
+
+ @XmlElement(required = true, name = "output")
+ private Collection<DDLGeneratorOutputEntity> outputs;
+}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorAssertionsRootEntity.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorAssertionsRootEntity.java
new file mode 100644
index 00000000000..f6f285299e2
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorAssertionsRootEntity.java
@@ -0,0 +1,36 @@
+/*
+ * 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.integration.data.pipeline.cases.entity;
+
+import lombok.Getter;
+
+import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import java.util.Collection;
+import java.util.LinkedList;
+
+/**
+ * DDL generator root entity for JAXB.
+ */
+@XmlRootElement(name = "ddl-generator-assertions")
+@Getter
+public final class DDLGeneratorAssertionsRootEntity {
+
+ @XmlElement(name = "ddl-generator-assertion")
+ private final Collection<DDLGeneratorAssertionEntity> assertions = new LinkedList<>();
+}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorInputEntity.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorInputEntity.java
new file mode 100644
index 00000000000..a730d267e70
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorInputEntity.java
@@ -0,0 +1,41 @@
+/*
+ * 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.integration.data.pipeline.cases.entity;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+
+/**
+ * DDL generator input entity for JAXB.
+ */
+@XmlAccessorType(XmlAccessType.FIELD)
+@Getter
+@Setter
+public final class DDLGeneratorInputEntity {
+
+ @XmlAttribute
+ private String table;
+
+ @XmlElement
+ private String sql;
+}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorOutputEntity.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorOutputEntity.java
new file mode 100644
index 00000000000..09be6d6ad86
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/entity/DDLGeneratorOutputEntity.java
@@ -0,0 +1,41 @@
+/*
+ * 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.integration.data.pipeline.cases.entity;
+
+import lombok.Getter;
+import lombok.Setter;
+
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlElement;
+
+/**
+ * DDL generator output entity for JAXB.
+ */
+@XmlAccessorType(XmlAccessType.FIELD)
+@Getter
+@Setter
+public final class DDLGeneratorOutputEntity {
+
+ @XmlAttribute
+ private String version;
+
+ @XmlElement
+ private String sql;
+}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/postgresql/PostgreSQLDDLGeneratorIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/postgresql/PostgreSQLDDLGeneratorIT.java
new file mode 100644
index 00000000000..25bf61e5c78
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/java/org/apache/shardingsphere/integration/data/pipeline/cases/postgresql/PostgreSQLDDLGeneratorIT.java
@@ -0,0 +1,135 @@
+/*
+ * 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.integration.data.pipeline.cases.postgresql;
+
+import com.google.common.base.Strings;
+import com.zaxxer.hikari.HikariDataSource;
+import org.apache.shardingsphere.data.pipeline.spi.ddlgenerator.DialectDDLSQLGeneratorFactory;
+import org.apache.shardingsphere.infra.database.type.dialect.PostgreSQLDatabaseType;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import org.apache.shardingsphere.integration.data.pipeline.cases.entity.DDLGeneratorAssertionEntity;
+import org.apache.shardingsphere.integration.data.pipeline.cases.entity.DDLGeneratorAssertionsRootEntity;
+import org.apache.shardingsphere.integration.data.pipeline.cases.entity.DDLGeneratorOutputEntity;
+import org.apache.shardingsphere.integration.data.pipeline.env.IntegrationTestEnvironment;
+import org.apache.shardingsphere.integration.data.pipeline.factory.DatabaseContainerFactory;
+import org.apache.shardingsphere.integration.data.pipeline.framework.container.database.DockerDatabaseContainer;
+import org.apache.shardingsphere.integration.data.pipeline.framework.param.ScalingParameterized;
+import org.apache.shardingsphere.test.integration.env.DataSourceEnvironment;
+import org.junit.After;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+import javax.sql.DataSource;
+import javax.xml.bind.JAXB;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Objects;
+import java.util.regex.Pattern;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+@RunWith(Parameterized.class)
+public final class PostgreSQLDDLGeneratorIT {
+
+ private static final String CASE_FILE_PATH = "/ddlgenerator.xml";
+
+ private static final String PARENT_PATH = "env/scenario/manual/postgresql/ddlgenerator";
+
+ private static final String DEFAULT_SCHEMA = "public";
+
+ private static final Pattern REPLACE_LINE_SPACE = Pattern.compile("\\s*|\t|\r|\n");
+
+ private static final IntegrationTestEnvironment ENV = IntegrationTestEnvironment.getInstance();
+
+ private final DockerDatabaseContainer dockerDatabaseContainer;
+
+ private final ScalingParameterized parameterized;
+
+ private final DDLGeneratorAssertionsRootEntity rootEntity;
+
+ public PostgreSQLDDLGeneratorIT(final ScalingParameterized parameterized) {
+ this.parameterized = parameterized;
+ this.rootEntity = JAXB.unmarshal(Objects.requireNonNull(PostgreSQLDDLGeneratorIT.class.getClassLoader().getResource(parameterized.getParentPath() + CASE_FILE_PATH)),
+ DDLGeneratorAssertionsRootEntity.class);
+ this.dockerDatabaseContainer = DatabaseContainerFactory.newInstance(parameterized.getDatabaseType(), parameterized.getDockerImageName());
+ dockerDatabaseContainer.start();
+ }
+
+ @Parameters(name = "{0}")
+ public static Collection<ScalingParameterized> getParameters() {
+ Collection<ScalingParameterized> result = new LinkedList<>();
+ for (String each : ENV.getPostgresVersions()) {
+ if (Strings.isNullOrEmpty(each)) {
+ continue;
+ }
+ result.add(new ScalingParameterized(new PostgreSQLDatabaseType(), each, PARENT_PATH));
+ }
+ return result;
+ }
+
+ @Test
+ public void assertGenerateDDLSQL() throws SQLException {
+ DataSource dataSource = createDataSource();
+ try (
+ Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ int majorVersion = connection.getMetaData().getDatabaseMajorVersion();
+ for (DDLGeneratorAssertionEntity each : rootEntity.getAssertions()) {
+ statement.execute(each.getInput().getSql());
+ String sql = DialectDDLSQLGeneratorFactory.findInstance(parameterized.getDatabaseType()).orElseThrow(() -> new ShardingSphereException("Failed to get dialect ddl sql generator"))
+ .generateDDLSQL(each.getInput().getTable(), DEFAULT_SCHEMA, dataSource);
+ assertThat(REPLACE_LINE_SPACE.matcher(sql).replaceAll(""), is(REPLACE_LINE_SPACE.matcher(getVersionOutput(each.getOutputs(), majorVersion)).replaceAll("")));
+ }
+ }
+ }
+
+ private String getVersionOutput(final Collection<DDLGeneratorOutputEntity> outputs, final int majorVersion) {
+ String result = "";
+ for (DDLGeneratorOutputEntity each : outputs) {
+ if ("default".equals(each.getVersion())) {
+ result = each.getSql();
+ }
+ if (String.valueOf(majorVersion).equals(each.getVersion())) {
+ return each.getSql();
+ }
+ }
+ return result;
+ }
+
+ private DataSource createDataSource() {
+ HikariDataSource result = new HikariDataSource();
+ result.setDriverClassName(DataSourceEnvironment.getDriverClassName(dockerDatabaseContainer.getDatabaseType()));
+ result.setJdbcUrl(dockerDatabaseContainer.getJdbcUrl(dockerDatabaseContainer.getHost(), dockerDatabaseContainer.getFirstMappedPort(), "postgres"));
+ result.setUsername("root");
+ result.setPassword("root");
+ result.setMaximumPoolSize(2);
+ result.setTransactionIsolation("TRANSACTION_READ_COMMITTED");
+ return result;
+ }
+
+ @After
+ public void stopContainer() {
+ dockerDatabaseContainer.stop();
+ }
+}
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/resources/env/scenario/manual/postgresql/ddlgenerator/ddlgenerator.xml b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/resources/env/scenario/manual/postgresql/ddlgenerator/ddlgenerator.xml
new file mode 100644
index 00000000000..a70bb9f8ca6
--- /dev/null
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-integration-test-scaling/src/test/resources/env/scenario/manual/postgresql/ddlgenerator/ddlgenerator.xml
@@ -0,0 +1,116 @@
+<!--
+ ~ 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.
+ -->
+
+<ddl-generator-assertions>
+ <ddl-generator-assertion id="t_order">
+ <input table="t_order">
+ <sql>
+ CREATE TABLE t_order (order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id));
+ </sql>
+ </input>
+ <output version="default">
+ <sql>
+ CREATE TABLE IF NOT EXISTS public.t_order
+ (
+ order_id integer NOT NULL,
+ user_id integer NOT NULL,
+ status character varying(45) COLLATE pg_catalog."default",
+ CONSTRAINT t_order_pkey PRIMARY KEY (
+ order_id)
+ )
+ TABLESPACE pg_default;
+ </sql>
+ </output>
+ <output version="10">
+ <sql>
+ CREATE TABLE IF NOT EXISTS public.t_order
+ (
+ order_id integer NOT NULL,
+ user_id integer NOT NULL,
+ status character varying(45) COLLATE pg_catalog."default",
+ CONSTRAINT t_order_pkey PRIMARY KEY (order_id))
+ WITH (OIDS = FALSE)
+ TABLESPACE pg_default;
+ </sql>
+ </output>
+ <output version="11">
+ <sql>
+ CREATE TABLE IF NOT EXISTS public.t_order
+ (
+ order_id integer NOT NULL,
+ user_id integer NOT NULL,
+ status character varying(45) COLLATE pg_catalog."default",
+ CONSTRAINT t_order_pkey PRIMARY KEY (order_id))
+ WITH (OIDS = FALSE)
+ TABLESPACE pg_default;
+ </sql>
+ </output>
+ </ddl-generator-assertion>
+
+ <ddl-generator-assertion id="t_order_item">
+ <input table="t_order_item">
+ <sql>
+ CREATE TABLE t_order_item (item_id INT NOT NULL, order_id INT NOT NULL, user_id INT NOT NULL, status VARCHAR(45) NULL, creation_date DATE, PRIMARY KEY (item_id));
+ </sql>
+ </input>
+ <output version="default">
+ <sql>
+ CREATE TABLE IF NOT EXISTS public.t_order_item
+ (
+ item_id integer NOT NULL,
+ order_id integer NOT NULL,
+ user_id integer NOT NULL,
+ status character varying(45) COLLATE pg_catalog."default",
+ creation_date date,
+ CONSTRAINT t_order_item_pkey PRIMARY KEY (
+ item_id)
+ )
+ TABLESPACE pg_default;
+ </sql>
+ </output>
+ <output version="10">
+ <sql>
+ CREATE TABLE IF NOT EXISTS public.t_order_item
+ (
+ item_id integer NOT NULL,
+ order_id integer NOT NULL,
+ user_id integer NOT NULL,
+ status character varying(45) COLLATE pg_catalog."default",
+ creation_date date,
+ CONSTRAINT t_order_item_pkey PRIMARY KEY (
+ item_id))
+ WITH (OIDS = FALSE)
+ TABLESPACE pg_default;
+ </sql>
+ </output>
+ <output version="11">
+ <sql>
+ CREATE TABLE IF NOT EXISTS public.t_order_item
+ (
+ item_id integer NOT NULL,
+ order_id integer NOT NULL,
+ user_id integer NOT NULL,
+ status character varying(45) COLLATE pg_catalog."default",
+ creation_date date,
+ CONSTRAINT t_order_item_pkey PRIMARY KEY (
+ item_id))
+ WITH (OIDS = FALSE)
+ TABLESPACE pg_default;
+ </sql>
+ </output>
+ </ddl-generator-assertion>
+</ddl-generator-assertions>