add unit test

This commit is contained in:
XudongXie 2024-09-20 12:30:52 +08:00
parent db2701ae44
commit 9cd1066f07
4 changed files with 88 additions and 40 deletions

View File

@ -5,9 +5,10 @@
</component>
<component name="ChangeListManager">
<list default="true" id="d8d12963-4777-48e3-9669-309cf5e41d60" name="更改" comment="add unit test">
<change afterPath="$PROJECT_DIR$/src/test/java/testCommitRollbackExec.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/testCreateTable.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/testAlterTable.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/testAlterTable.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/parser/OracleParser.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/parser/OracleParser.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/testCommitRollbackExec.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/testCommitRollbackExec.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -45,6 +46,8 @@
"Application.Main.executor": "Run",
"JUnit.testAlterTable.testAlterTable.executor": "Run",
"JUnit.testCommitRollbackExec.test.executor": "Run",
"JUnit.testCreateTable.executor": "Run",
"JUnit.testCreateTable.test.executor": "Run",
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"git-widget-placeholder": "master",
@ -56,20 +59,7 @@
"settings.editor.selected.configurable": "preferences.pluginManager"
}
}]]></component>
<component name="RunManager" selected="JUnit.testCommitRollbackExec.test">
<configuration name="App" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="org.example.App" />
<module name="sqlTranslate" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="org.example.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<component name="RunManager" selected="JUnit.testCreateTable">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="sqlTranslate" />
@ -77,22 +67,6 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="AppTest.shouldAnswerWithTrue" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="org.example.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="org.example" />
<option name="MAIN_CLASS_NAME" value="org.example.AppTest" />
<option name="METHOD_NAME" value="shouldAnswerWithTrue" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="testAlterTable.testAlterTable" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<option name="PACKAGE_NAME" value="" />
@ -113,20 +87,39 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="testCreateTable" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="testCreateTable" />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="testCreateTable.test" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="sqlTranslate" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="testCreateTable" />
<option name="METHOD_NAME" value="test" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<list>
<item itemvalue="Application.App" />
<item itemvalue="Application.Main" />
<item itemvalue="JUnit.testAlterTable.testAlterTable" />
<item itemvalue="JUnit.testCommitRollbackExec.test" />
<item itemvalue="JUnit.AppTest.shouldAnswerWithTrue" />
<item itemvalue="JUnit.testCreateTable" />
<item itemvalue="JUnit.testCreateTable.test" />
</list>
<recent_temporary>
<list>
<item itemvalue="JUnit.testCreateTable" />
<item itemvalue="JUnit.testCreateTable.test" />
<item itemvalue="JUnit.testCommitRollbackExec.test" />
<item itemvalue="JUnit.testAlterTable.testAlterTable" />
<item itemvalue="Application.Main" />
<item itemvalue="JUnit.AppTest.shouldAnswerWithTrue" />
<item itemvalue="Application.App" />
</list>
</recent_temporary>
</component>
@ -482,7 +475,7 @@
<option name="project" value="LOCAL" />
<updated>1726633703475</updated>
</task>
<option name="localTasksCounter" value="97" />
<option name="localTasksCounter" value="98" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">

View File

@ -2362,7 +2362,7 @@ public class OracleParser {
ASTNode currentNode = root;
for (int i = 1; i < parseTokens.size(); i++) {
// match declare
if (currentNode == root) {
if (currentNode == root && !(parseTokens.get(i).hasType(Token.TokenType.KEYWORD) && parseTokens.get(i).getValue().equalsIgnoreCase("BEGIN"))) {
for (int j = i; j < parseTokens.size(); j++) {
if (parseTokens.get(j).hasType(Token.TokenType.KEYWORD) && parseTokens.get(j).getValue().equalsIgnoreCase("BEGIN")) {
i = j - 1;

View File

@ -14,11 +14,13 @@ public class testCommitRollbackExec {
@BeforeEach
public void loadData()
{
testSQL.add("BEGIN\n" +
testSQL.add("DECLARE\n" +
"BEGIN\n" +
" INSERT INTO my_table (id, value) VALUES (1, 'test');\n" +
" ROLLBACK;\n" +
"END;");
testSQL.add("BEGIN\n" +
testSQL.add("DECLARE\n" +
"BEGIN\n" +
" INSERT INTO my_table (id, value) VALUES (1, 'test');\n" +
" COMMIT;\n" +
"END;");

View File

@ -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 testCreateTable {
List<String> testSQL = new ArrayList<>();
@BeforeEach
public void loadData()
{
testSQL.add("CREATE TABLE employees (\n" +
" employee_id NUMBER PRIMARY KEY,\n" +
" first_name VARCHAR2(20) Check(first_name > '221'),\n" +
" last_name VARCHAR2(25) Check (last_name != '12813@163.com'),\n" +
" email VARCHAR2(25),\n" +
" hire_date DATE,\n" +
" CONSTRAINT chk_example CHECK (employee_id > 0)" +
");");
System.out.println("===== test of the alter 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 of 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();
}
}
}