From acaf7f12140aadeb8924fb7d7bb802a6d6c523fc Mon Sep 17 00:00:00 2001 From: XudongXie Date: Fri, 20 Sep 2024 12:57:18 +0800 Subject: [PATCH] add unit test --- sqlTranslate/.idea/workspace.xml | 89 ++++++++++--------- .../src/test/java/testAlterTable.java | 4 +- .../src/test/java/testCommitRollbackExec.java | 4 +- .../src/test/java/testCreateTable.java | 4 +- sqlTranslate/src/test/java/testDelete.java | 51 +++++++++++ sqlTranslate/src/test/java/testDropTable.java | 47 ++++++++++ sqlTranslate/src/test/java/testException.java | 53 +++++++++++ 7 files changed, 204 insertions(+), 48 deletions(-) create mode 100644 sqlTranslate/src/test/java/testDelete.java create mode 100644 sqlTranslate/src/test/java/testDropTable.java create mode 100644 sqlTranslate/src/test/java/testException.java diff --git a/sqlTranslate/.idea/workspace.xml b/sqlTranslate/.idea/workspace.xml index 8304d3f7a..783ee2933 100644 --- a/sqlTranslate/.idea/workspace.xml +++ b/sqlTranslate/.idea/workspace.xml @@ -5,10 +5,10 @@ - + + + - - - - - - - - - - - + - + + + + + + + + + + - - - - + + + + + + + + - - - - @@ -475,7 +480,7 @@ diff --git a/sqlTranslate/src/test/java/testAlterTable.java b/sqlTranslate/src/test/java/testAlterTable.java index 8e19514b1..70c0efb4d 100644 --- a/sqlTranslate/src/test/java/testAlterTable.java +++ b/sqlTranslate/src/test/java/testAlterTable.java @@ -23,7 +23,7 @@ public class testAlterTable { testSQL.add("ALTER TABLE employees ADD CONSTRAINT emp_pk PRIMARY KEY (employee_id);"); testSQL.add("ALTER TABLE employees DROP CONSTRAINT emp_pk;"); testSQL.add("ALTER TABLE employees RENAME TO staff;"); - System.out.println("===== test of the alter table ====="); + System.out.println("===== test of alter table ====="); System.out.println("The source DBMS is: " + CommonConfig.getSourceDB()); System.out.println("The target DBMS is: " + CommonConfig.getTargetDB()); System.out.println(); @@ -34,7 +34,7 @@ public class testAlterTable { { int num = 1; for (String sql : testSQL) { - System.out.println("===== test of the SQL" + num++ + " ====="); + System.out.println("===== test the SQL" + num++ + " ====="); System.out.println("Input SQL: " + sql); OracleLexer lexer = new OracleLexer(sql); lexer.printTokens(); diff --git a/sqlTranslate/src/test/java/testCommitRollbackExec.java b/sqlTranslate/src/test/java/testCommitRollbackExec.java index c825ef5eb..96f4b1934 100644 --- a/sqlTranslate/src/test/java/testCommitRollbackExec.java +++ b/sqlTranslate/src/test/java/testCommitRollbackExec.java @@ -24,7 +24,7 @@ public class testCommitRollbackExec { " INSERT INTO my_table (id, value) VALUES (1, 'test');\n" + " COMMIT;\n" + "END;"); - System.out.println("===== test of the alter table ====="); + System.out.println("===== test of Commit, Rollback,Exec ====="); System.out.println("The source DBMS is: " + CommonConfig.getSourceDB()); System.out.println("The target DBMS is: " + CommonConfig.getTargetDB()); System.out.println(); @@ -35,7 +35,7 @@ public class testCommitRollbackExec { { int num = 1; for (String sql : testSQL) { - System.out.println("===== test of the SQL" + num++ + " ====="); + System.out.println("===== test the SQL" + num++ + " ====="); System.out.println("Input SQL: " + sql); OracleLexer lexer = new OracleLexer(sql); lexer.printTokens(); diff --git a/sqlTranslate/src/test/java/testCreateTable.java b/sqlTranslate/src/test/java/testCreateTable.java index 91d6c1397..11f8e90ec 100644 --- a/sqlTranslate/src/test/java/testCreateTable.java +++ b/sqlTranslate/src/test/java/testCreateTable.java @@ -22,7 +22,7 @@ public class testCreateTable { " hire_date DATE,\n" + " CONSTRAINT chk_example CHECK (employee_id > 0)" + ");"); - System.out.println("===== test of the alter table ====="); + System.out.println("===== test of Create table ====="); System.out.println("The source DBMS is: " + CommonConfig.getSourceDB()); System.out.println("The target DBMS is: " + CommonConfig.getTargetDB()); System.out.println(); @@ -33,7 +33,7 @@ public class testCreateTable { { int num = 1; for (String sql : testSQL) { - System.out.println("===== test of the SQL" + num++ + " ====="); + System.out.println("===== test the SQL" + num++ + " ====="); System.out.println("Input SQL: " + sql); OracleLexer lexer = new OracleLexer(sql); lexer.printTokens(); diff --git a/sqlTranslate/src/test/java/testDelete.java b/sqlTranslate/src/test/java/testDelete.java new file mode 100644 index 000000000..a8f136105 --- /dev/null +++ b/sqlTranslate/src/test/java/testDelete.java @@ -0,0 +1,51 @@ +import config.CommonConfig; +import generator.OpenGaussGenerator; +import lexer.OracleLexer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import parser.OracleParser; +import parser.ast.ASTNode; + +import java.util.ArrayList; +import java.util.List; + +public class testDelete { + List testSQL = new ArrayList<>(); + @BeforeEach + public void loadData() + { + testSQL.add("DELETE FROM employees e\n" + + "WHERE e.department_id IN (\n" + + " SELECT d.department_id\n" + + " FROM departments d\n" + + " WHERE d.department_name = 'Sales'\n" + + ");"); + System.out.println("===== test of Delete ====="); + System.out.println("The source DBMS is: " + CommonConfig.getSourceDB()); + System.out.println("The target DBMS is: " + CommonConfig.getTargetDB()); + System.out.println(); + } + + @Test + public void test() + { + int num = 1; + for (String sql : testSQL) { + System.out.println("===== test the SQL" + num++ + " ====="); + System.out.println("Input SQL: " + sql); + OracleLexer lexer = new OracleLexer(sql); + lexer.printTokens(); + OracleParser parser = new OracleParser(lexer); + ASTNode root = parser.parse(); + System.out.println("The AST of the input SQL: "); + System.out.println(root.getASTString()); + System.out.println("The query String of the AST parsed from the input SQL: "); + System.out.println(root.toQueryString()); + OpenGaussGenerator generator = new OpenGaussGenerator(root); + System.out.println("The converted query String: "); + System.out.println(generator.generate()); + + System.out.println(); + } + } +} diff --git a/sqlTranslate/src/test/java/testDropTable.java b/sqlTranslate/src/test/java/testDropTable.java new file mode 100644 index 000000000..f3618c857 --- /dev/null +++ b/sqlTranslate/src/test/java/testDropTable.java @@ -0,0 +1,47 @@ +import config.CommonConfig; +import generator.OpenGaussGenerator; +import lexer.OracleLexer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import parser.OracleParser; +import parser.ast.ASTNode; + +import java.util.ArrayList; +import java.util.List; + +public class testDropTable { + List testSQL = new ArrayList<>(); + @BeforeEach + public void loadData() + { + testSQL.add("DROP TABLE employees CASCADE CONSTRAINTS;"); + testSQL.add("DROP TABLE employees;"); + System.out.println("===== test of Drop table ====="); + System.out.println("The source DBMS is: " + CommonConfig.getSourceDB()); + System.out.println("The target DBMS is: " + CommonConfig.getTargetDB()); + System.out.println(); + } + + @Test + public void test() + { + int num = 1; + for (String sql : testSQL) { + System.out.println("===== test the SQL" + num++ + " ====="); + System.out.println("Input SQL: " + sql); + OracleLexer lexer = new OracleLexer(sql); + lexer.printTokens(); + OracleParser parser = new OracleParser(lexer); + ASTNode root = parser.parse(); + System.out.println("The AST of the input SQL: "); + System.out.println(root.getASTString()); + System.out.println("The query String of the AST parsed from the input SQL: "); + System.out.println(root.toQueryString()); + OpenGaussGenerator generator = new OpenGaussGenerator(root); + System.out.println("The converted query String: "); + System.out.println(generator.generate()); + + System.out.println(); + } + } +} diff --git a/sqlTranslate/src/test/java/testException.java b/sqlTranslate/src/test/java/testException.java new file mode 100644 index 000000000..a3e66c96b --- /dev/null +++ b/sqlTranslate/src/test/java/testException.java @@ -0,0 +1,53 @@ +import config.CommonConfig; +import generator.OpenGaussGenerator; +import lexer.OracleLexer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import parser.OracleParser; +import parser.ast.ASTNode; + +import java.util.ArrayList; +import java.util.List; + +public class testException { + List testSQL = new ArrayList<>(); + @BeforeEach + public void loadData() + { + testSQL.add("EXCEPTION\n" + + " WHEN e_custom_exception THEN\n" + + " DBMS_OUTPUT.PUT_LINE('Caught an exception: Custom exception raised');\n" + + " WHEN ZERO_DIVIDE THEN\n" + + " DBMS_OUTPUT.PUT_LINE('Caught an exception: Division by zero');\n" + + " WHEN INVALID_NUMBER THEN\n" + + " DBMS_OUTPUT.PUT_LINE('Caught an exception: Invalid number');\n" + + " WHEN OTHERS THEN\n" + + " DBMS_OUTPUT.PUT_LINE('Caught an exception: ' || SQLERRM);"); + System.out.println("===== test of EXCEPTION ====="); + System.out.println("The source DBMS is: " + CommonConfig.getSourceDB()); + System.out.println("The target DBMS is: " + CommonConfig.getTargetDB()); + System.out.println(); + } + + @Test + public void test() + { + int num = 1; + for (String sql : testSQL) { + System.out.println("===== test the SQL" + num++ + " ====="); + System.out.println("Input SQL: " + sql); + OracleLexer lexer = new OracleLexer(sql); + lexer.printTokens(); + ASTNode root = OracleParser.parseException(lexer.getTokens()); + System.out.println("The AST of the input SQL: "); + System.out.println(root.getASTString()); + System.out.println("The query String of the AST parsed from the input SQL: "); + System.out.println(root.toQueryString()); + OpenGaussGenerator generator = new OpenGaussGenerator(root); + System.out.println("The converted query String: "); + System.out.println(generator.generate()); + + System.out.println(); + } + } +}