Skip to content

chore adding extra details to unit test scenarios #2051

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/test/java/net/sf/jsqlparser/statement/delete/DeleteTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package net.sf.jsqlparser.statement.delete;

import java.io.StringReader;
import java.util.List;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
Expand All @@ -22,6 +24,9 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import net.sf.jsqlparser.statement.select.SelectItem;
import net.sf.jsqlparser.statement.select.WithItem;
import org.junit.jupiter.api.Test;

public class DeleteTest {
Expand Down Expand Up @@ -118,8 +123,18 @@ public void testWith() throws JSQLParserException {
+ "DELETE FROM cfe.instrument_ref\n"
+ "WHERE id_instrument_ref = (SELECT id_instrument_ref\n"
+ " FROM a)";

assertSqlCanBeParsedAndDeparsed(statement, true);
Delete delete = (Delete) assertSqlCanBeParsedAndDeparsed(statement, true);
List<WithItem> withItems = delete.getWithItemsList();
assertEquals("cfe.instrument_ref", delete.getTable().getFullyQualifiedName());
assertEquals(2, withItems.size());
SelectItem selectItem1 = withItems.get(0).getSelect().getPlainSelect().getSelectItems().get(0);
assertEquals("1", selectItem1.getExpression().toString());
assertEquals(" id_instrument_ref", selectItem1.getAlias().toString());
assertEquals(" a", withItems.get(0).getAlias().toString());
SelectItem selectItem2 = withItems.get(1).getSelect().getPlainSelect().getSelectItems().get(0);
assertEquals("1", selectItem2.getExpression().toString());
assertEquals(" id_instrument_ref", selectItem2.getAlias().toString());
assertEquals(" b", withItems.get(1).getAlias().toString());
}

@Test
Expand Down
94 changes: 71 additions & 23 deletions src/test/java/net/sf/jsqlparser/statement/insert/InsertTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@
import net.sf.jsqlparser.expression.JdbcParameter;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.relational.ExistsExpression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.AllColumns;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.Values;
import net.sf.jsqlparser.statement.select.*;
import net.sf.jsqlparser.statement.update.UpdateSet;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import java.io.StringReader;
import java.util.List;

import static junit.framework.Assert.assertNull;
import static net.sf.jsqlparser.test.TestUtils.assertDeparse;
import static net.sf.jsqlparser.test.TestUtils.assertOracleHintExists;
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
Expand Down Expand Up @@ -277,12 +277,26 @@ public void testInsertSelect() throws JSQLParserException {

@Test
public void testInsertWithSelect() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"INSERT INTO mytable (mycolumn) WITH a AS (SELECT mycolumn FROM mytable) SELECT mycolumn FROM a",
true);
assertSqlCanBeParsedAndDeparsed(
"INSERT INTO mytable (mycolumn) (WITH a AS (SELECT mycolumn FROM mytable) SELECT mycolumn FROM a)",
true);
String sqlStr1 = "INSERT INTO mytable (mycolumn) WITH a AS (SELECT mycolumn FROM mytable) SELECT mycolumn FROM a";
Insert insert1 = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr1, true);
List<WithItem> insertWithItems1 = insert1.getWithItemsList();
List<WithItem> selectWithItems1 = insert1.getSelect().getWithItemsList();
assertEquals("mytable", insert1.getTable().getFullyQualifiedName());
assertNull(insertWithItems1);
assertEquals(1, selectWithItems1.size());
assertEquals("SELECT mycolumn FROM mytable", selectWithItems1.get(0).getSelect().getPlainSelect().toString());
assertEquals(" a", selectWithItems1.get(0).getAlias().toString());

String sqlStr2 = "INSERT INTO mytable (mycolumn) (WITH a AS (SELECT mycolumn FROM mytable) SELECT mycolumn FROM a)";
Insert insert2 = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr2, true);
List<WithItem> insertWithItems2 = insert2.getWithItemsList();
assertEquals("mytable", insert2.getTable().getFullyQualifiedName());
assertNull(insertWithItems2);
ParenthesedSelect select = (ParenthesedSelect) insert2.getSelect();
List<WithItem> selectWithItems2 = select.getSelect().getWithItemsList();
assertEquals(1, selectWithItems2.size());
assertEquals("SELECT mycolumn FROM mytable", selectWithItems2.get(0).getSelect().getPlainSelect().toString());
assertEquals(" a", selectWithItems2.get(0).getAlias().toString());
}

@Test
Expand Down Expand Up @@ -348,9 +362,15 @@ public void testKeywordPrecisionIssue363() throws JSQLParserException {

@Test
public void testWithDeparsingIssue406() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"insert into mytab3 (a,b,c) select a,b,c from mytab where exists(with t as (select * from mytab2) select * from t)",
true);
String sqlStr = "insert into mytab3 (a,b,c) select a,b,c from mytab where exists(with t as (select * from mytab2) select * from t)";
Insert insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
List<WithItem> insertWithItems = insert.getWithItemsList();
List<WithItem> selectWithItems = insert.getSelect().getWithItemsList();
assertEquals("mytab3", insert.getTable().getFullyQualifiedName());
assertNull(insertWithItems);
assertNull(selectWithItems);
ExistsExpression exists = (ExistsExpression) insert.getPlainSelect().getWhere();
assertEquals("(WITH t AS (SELECT * FROM mytab2) SELECT * FROM t)", exists.getRightExpression().toString());
}

@Test
Expand Down Expand Up @@ -390,9 +410,16 @@ public void testInsertKeyWordIntervalIssue682() throws JSQLParserException {

@Test
public void testWithAtFront() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"WITH foo AS ( SELECT attr FROM bar ) INSERT INTO lalelu (attr) SELECT attr FROM foo",
true);
String sqlStr = "WITH foo AS ( SELECT attr FROM bar ) INSERT INTO lalelu (attr) SELECT attr FROM foo";
Insert insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
List<WithItem> insertWithItems = insert.getWithItemsList();
assertEquals("lalelu", insert.getTable().getFullyQualifiedName());
assertEquals(1, insertWithItems.size());
assertEquals("SELECT attr FROM bar", insertWithItems.get(0).getSelect().getPlainSelect().toString());
assertEquals(" foo", insertWithItems.get(0).getAlias().toString());
assertEquals("SELECT attr FROM foo", insert.getSelect().toString());
assertEquals("foo", insert.getSelect().getPlainSelect().getFromItem().toString());
assertEquals("[attr]", insert.getSelect().getPlainSelect().getSelectItems().toString());
}

@Test
Expand Down Expand Up @@ -427,8 +454,16 @@ public void testDisableKeywordIssue945() throws JSQLParserException {

@Test
public void testWithListIssue282() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed(
"WITH myctl AS (SELECT a, b FROM mytable) INSERT INTO mytable SELECT a, b FROM myctl");
String sqlStr = "WITH myctl AS (SELECT a, b FROM mytable) INSERT INTO mytable SELECT a, b FROM myctl";
Insert insert = (Insert) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
List<WithItem> insertWithItems = insert.getWithItemsList();
assertEquals("mytable", insert.getTable().getFullyQualifiedName());
assertEquals(1, insertWithItems.size());
assertEquals("SELECT a, b FROM mytable", insertWithItems.get(0).getSelect().getPlainSelect().toString());
assertEquals(" myctl", insertWithItems.get(0).getAlias().toString());
assertEquals("SELECT a, b FROM myctl", insert.getSelect().toString());
assertEquals("myctl", insert.getSelect().getPlainSelect().getFromItem().toString());
assertEquals("[a, b]", insert.getSelect().getPlainSelect().getSelectItems().toString());
}

@Test
Expand Down Expand Up @@ -468,8 +503,18 @@ public void testInsertUnionSelectIssue1491() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("insert into table1 (tf1,tf2,tf2)\n"
+ "((select sf1,sf2,sf3 from s1)" + "union " + "(select rf1,rf2,rf2 from r1))",
true);
}

assertSqlCanBeParsedAndDeparsed("(with a as (select * from dual) select * from a)", true);
@Test
public void testWithSelectFromDual() throws JSQLParserException {
String sqlStr = "(with a as (select * from dual) select * from a)";
ParenthesedSelect parenthesedSelect = (ParenthesedSelect) assertSqlCanBeParsedAndDeparsed(sqlStr, true);
List<WithItem> withItems = parenthesedSelect.getSelect().getWithItemsList();
assertEquals(1, withItems.size());
assertEquals("SELECT * FROM dual", withItems.get(0).getSelect().getPlainSelect().toString());
assertEquals(" a", withItems.get(0).getAlias().toString());
assertEquals("a", parenthesedSelect.getPlainSelect().getFromItem().toString());
assertEquals("[*]", parenthesedSelect.getPlainSelect().getSelectItems().toString());
}

@Test
Expand Down Expand Up @@ -528,6 +573,11 @@ public void insertOnConflictObjectsTest() throws JSQLParserException {
String sqlStr = "WITH a ( a, b , c ) \n" + "AS (SELECT 1 , 2 , 3 )\n"
+ "insert into test\n" + "select * from a";
Insert insert = (Insert) CCJSqlParserUtil.parse(sqlStr);
List<WithItem> withItems = insert.getWithItemsList();
assertEquals("test", insert.getTable().getFullyQualifiedName());
assertEquals(1, withItems.size());
assertEquals("[1, 2, 3]", withItems.get(0).getSelect().getPlainSelect().getSelectItems().toString());
assertEquals(" a", withItems.get(0).getAlias().toString());

Expression whereExpression = CCJSqlParserUtil.parseExpression("a=1", false);
Expression valueExpression = CCJSqlParserUtil.parseExpression("b/2", false);
Expand Down Expand Up @@ -608,8 +658,7 @@ public void testDefaultValues() throws JSQLParserException {
@Test
public void testDefaultValuesWithAlias() throws JSQLParserException {
String statement = "INSERT INTO mytable x DEFAULT VALUES";
assertSqlCanBeParsedAndDeparsed(statement);
Insert insert = (Insert) parserManager.parse(new StringReader(statement));
Insert insert = (Insert) assertSqlCanBeParsedAndDeparsed(statement);
assertEquals("mytable", insert.getTable().getFullyQualifiedName());
assertEquals("INSERT INTO MYTABLE X DEFAULT VALUES", insert.toString().toUpperCase());
assertEquals("x", insert.getTable().getAlias().getName());
Expand All @@ -623,8 +672,7 @@ public void testDefaultValuesWithAlias() throws JSQLParserException {
@Test
public void testDefaultValuesWithAliasAndAs() throws JSQLParserException {
String statement = "INSERT INTO mytable AS x DEFAULT VALUES";
assertSqlCanBeParsedAndDeparsed(statement);
Insert insert = (Insert) parserManager.parse(new StringReader(statement));
Insert insert = (Insert) assertSqlCanBeParsedAndDeparsed(statement);
assertEquals("mytable", insert.getTable().getFullyQualifiedName());
assertEquals("INSERT INTO MYTABLE AS X DEFAULT VALUES", insert.toString().toUpperCase());
assertEquals("x", insert.getTable().getAlias().getName());
Expand Down
Loading
Loading