-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #35: updated to a new vs common version * #35: changed the version because the previous release breaks the interface * #35: removed the KEYS keyword from generated queries because Exasol can't parse it correctly
- Loading branch information
1 parent
f5236e2
commit 6982a40
Showing
4 changed files
with
168 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/test/java/com/exasol/adapter/dialects/SqlGenerationVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package com.exasol.adapter.dialects; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.*; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
|
||
import com.exasol.adapter.AdapterException; | ||
import com.exasol.adapter.AdapterProperties; | ||
import com.exasol.adapter.dialects.dummy.DummySqlDialect; | ||
import com.exasol.adapter.metadata.DataType; | ||
import com.exasol.adapter.sql.*; | ||
|
||
class SqlGenerationVisitorTest { | ||
private static SqlGenerationVisitor sqlGenerationVisitor; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
final Map<String, String> rawProperties = new HashMap<>(); | ||
final AdapterProperties adapterProperties = new AdapterProperties(rawProperties); | ||
final SqlDialect sqlDialect = new DummySqlDialect(null, adapterProperties); | ||
final SqlGenerationContext context = new SqlGenerationContext("", "TEXT_SCHEMA_NAME", false); | ||
sqlGenerationVisitor = new SqlGenerationVisitor(sqlDialect, context); | ||
} | ||
|
||
@Test | ||
void testVisitSqlFunctionScalarCast() throws AdapterException { | ||
final DataType dataType = DataType.createDecimal(18, 0); | ||
final List<SqlNode> arguments = new ArrayList<>(); | ||
arguments.add(new SqlLiteralDouble(20)); | ||
final SqlFunctionScalarCast sqlFunctionScalarCast = new SqlFunctionScalarCast(dataType, arguments); | ||
assertThat(sqlGenerationVisitor.visit(sqlFunctionScalarCast), equalTo("CAST(20.0 AS DECIMAL(18, 0))")); | ||
} | ||
|
||
@Test | ||
void testVisitSqlFunctionScalarExtract() throws AdapterException { | ||
final List<SqlNode> arguments = new ArrayList<>(); | ||
arguments.add(new SqlLiteralTimestamp("2019-02-12 12:07:00")); | ||
final SqlFunctionScalarExtract sqlFunctionScalarExtract = new SqlFunctionScalarExtract("SECOND", arguments); | ||
assertThat(sqlGenerationVisitor.visit(sqlFunctionScalarExtract), | ||
equalTo("EXTRACT(SECOND FROM TIMESTAMP '2019-02-12 12:07:00')")); | ||
} | ||
|
||
@Test | ||
void testVisitSqlFunctionScalarJsonValue() throws AdapterException { | ||
final List<SqlNode> arguments = new ArrayList<>(); | ||
arguments.add(new SqlLiteralString("{\"a\": 1}")); | ||
arguments.add(new SqlLiteralString("$.a")); | ||
final SqlFunctionScalarJsonValue.Behavior emptyBehavior = new SqlFunctionScalarJsonValue.Behavior( | ||
SqlFunctionScalarJsonValue.BehaviorType.DEFAULT, Optional.of(new SqlLiteralString("*** error ***"))); | ||
final SqlFunctionScalarJsonValue.Behavior errorBehavior = new SqlFunctionScalarJsonValue.Behavior( | ||
SqlFunctionScalarJsonValue.BehaviorType.DEFAULT, Optional.of(new SqlLiteralString("*** error ***"))); | ||
final SqlFunctionScalarJsonValue sqlFunctionScalarJsonValue = new SqlFunctionScalarJsonValue( | ||
ScalarFunction.JSON_VALUE, arguments, DataType.createVarChar(1000, DataType.ExaCharset.UTF8), | ||
emptyBehavior, errorBehavior); | ||
assertThat(sqlGenerationVisitor.visit(sqlFunctionScalarJsonValue), | ||
equalTo("JSON_VALUE('{\"a\": 1}', '$.a' RETURNING VARCHAR(1000) UTF8 " | ||
+ "DEFAULT '*** error ***' ON EMPTY DEFAULT '*** error ***' ON ERROR)")); | ||
} | ||
|
||
@Test | ||
void testVisitSqlLimit() { | ||
final SqlLimit sqlLimit = new SqlLimit(5, 10); | ||
assertThat(sqlGenerationVisitor.visit(sqlLimit), equalTo("LIMIT 5 OFFSET 10")); | ||
} | ||
|
||
@Test | ||
void testVisitSqlPredicateIsJson() throws AdapterException { | ||
final SqlNode expressionMock = Mockito.mock(SqlNode.class); | ||
when(expressionMock.accept(sqlGenerationVisitor)).thenReturn("SELECT '{\"a\": 1}'"); | ||
final SqlPredicateIsJson sqlPredicateIsJson = new SqlPredicateIsJson(expressionMock, | ||
AbstractSqlPredicateJson.TypeConstraints.OBJECT, | ||
AbstractSqlPredicateJson.KeyUniquenessConstraint.WITH_UNIQUE_KEYS); | ||
assertThat(sqlGenerationVisitor.visit(sqlPredicateIsJson), | ||
equalTo("SELECT '{\"a\": 1}' IS JSON OBJECT WITH UNIQUE")); | ||
} | ||
|
||
@Test | ||
void testVisitSqlPredicateIsNotJson() throws AdapterException { | ||
final SqlNode expressionMock = Mockito.mock(SqlNode.class); | ||
when(expressionMock.accept(sqlGenerationVisitor)).thenReturn("SELECT '{\"a\": 1}'"); | ||
final SqlPredicateIsNotJson sqlPredicateIsNotJson = new SqlPredicateIsNotJson(expressionMock, | ||
AbstractSqlPredicateJson.TypeConstraints.OBJECT, | ||
AbstractSqlPredicateJson.KeyUniquenessConstraint.WITH_UNIQUE_KEYS); | ||
assertThat(sqlGenerationVisitor.visit(sqlPredicateIsNotJson), | ||
equalTo("SELECT '{\"a\": 1}' IS NOT JSON OBJECT WITH UNIQUE")); | ||
} | ||
|
||
@Test | ||
void testVisitSqlLiteralDate() { | ||
final SqlLiteralDate sqlLiteralDate = new SqlLiteralDate("2015-12-01"); | ||
assertThat(sqlGenerationVisitor.visit(sqlLiteralDate), equalTo("DATE '2015-12-01'")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters