Skip to content

Commit

Permalink
#103: Added capability test.
Browse files Browse the repository at this point in the history
  • Loading branch information
redcatbear committed Mar 5, 2019
1 parent ca20cc4 commit 42245c6
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 48 deletions.
21 changes: 18 additions & 3 deletions jdbc-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<junit.version>5.4.0</junit.version>
<junit.platform.version>1.3.2</junit.platform.version>
<maven.surefire.version>2.22.1</maven.surefire.version>
</properties>
<distributionManagement>
<repository>
Expand Down Expand Up @@ -62,19 +65,31 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.3.2</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-runner</artifactId>
<version>1.3.2</version>
<version>${junit.platform.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.3.2</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.exasol.adapter.dialects.impl;

import static com.exasol.adapter.capabilities.AggregateFunctionCapability.*;
import static com.exasol.adapter.capabilities.LiteralCapability.*;
import static com.exasol.adapter.capabilities.MainCapability.*;
import static com.exasol.adapter.capabilities.PredicateCapability.*;
import static com.exasol.adapter.capabilities.ScalarFunctionCapability.*;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.exasol.adapter.capabilities.Capabilities;
import com.exasol.adapter.dialects.SqlDialect;

class DB2SqlDialectTest {
SqlDialect dialect;

@BeforeEach
void beforeEach() {
this.dialect = new DB2SqlDialect(null);
}

@Test
void testGetCapabilities() {
final Capabilities capabilities = this.dialect.getCapabilities();
assertAll(() -> assertThat(capabilities.getMainCapabilities(),
containsInAnyOrder(SELECTLIST_PROJECTION, SELECTLIST_EXPRESSIONS, FILTER_EXPRESSIONS,
AGGREGATE_SINGLE_GROUP, AGGREGATE_GROUP_BY_COLUMN, AGGREGATE_GROUP_BY_EXPRESSION,
AGGREGATE_GROUP_BY_TUPLE, AGGREGATE_HAVING, ORDER_BY_COLUMN, ORDER_BY_EXPRESSION, LIMIT)),
() -> assertThat(capabilities.getLiteralCapabilities(),
containsInAnyOrder(NULL, DATE, TIMESTAMP, TIMESTAMP_UTC, DOUBLE, EXACTNUMERIC, STRING,
INTERVAL)),
() -> assertThat(capabilities.getPredicateCapabilities(),
containsInAnyOrder(AND, OR, NOT, EQUAL, NOTEQUAL, LESS, LESSEQUAL, LIKE, LIKE_ESCAPE, BETWEEN,
IN_CONSTLIST, IS_NULL, IS_NOT_NULL)),
() -> assertThat(capabilities.getAggregateFunctionCapabilities(),
containsInAnyOrder(COUNT, COUNT_STAR, COUNT_DISTINCT, GROUP_CONCAT, GROUP_CONCAT_SEPARATOR,
GROUP_CONCAT_ORDER_BY, SUM, SUM_DISTINCT, MIN, MAX, AVG, AVG_DISTINCT, MEDIAN,
FIRST_VALUE, LAST_VALUE, STDDEV, STDDEV_POP, STDDEV_SAMP, VARIANCE, VARIANCE_DISTINCT,
VAR_POP, VAR_SAMP)),
() -> assertThat(capabilities.getScalarFunctionCapabilities(),
containsInAnyOrder(CEIL, DIV, FLOOR, SIGN, ADD, SUB, MULT, FLOAT_DIV, NEG, ABS, ACOS, ASIN,
ATAN, ATAN2, COS, COSH, COT, DEGREES, EXP, GREATEST, LEAST, LN, LOG, MOD, POWER,
RADIANS, SIN, SINH, SQRT, TAN, TANH, ASCII, CHR, INSTR, LENGTH, LOCATE, LOWER, LPAD,
LTRIM, REPEAT, REPLACE, RIGHT, RPAD, RTRIM, SOUNDEX, SUBSTR, TRANSLATE, TRIM, UPPER,
ADD_DAYS, ADD_HOURS, ADD_MINUTES, ADD_MONTHS, ADD_SECONDS, ADD_WEEKS, ADD_YEARS,
CURRENT_DATE, CURRENT_TIMESTAMP, LOCALTIMESTAMP, SYSDATE, SYSTIMESTAMP, CAST, TO_CHAR,
TO_DATE, TO_NUMBER, TO_TIMESTAMP, CASE, CURRENT_SCHEMA, CURRENT_USER, NULLIFZERO,
ZEROIFNULL)));
}
}
Original file line number Diff line number Diff line change
@@ -1,57 +1,60 @@
package com.exasol.adapter.dialects.impl;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import com.exasol.adapter.AdapterException;
import com.exasol.adapter.capabilities.Capabilities;
import com.exasol.adapter.capabilities.PredicateCapability;
import com.exasol.adapter.dialects.SqlDialect;
import com.exasol.adapter.dialects.SqlGenerationContext;
import com.exasol.adapter.dialects.SqlGenerationVisitor;
import com.exasol.adapter.capabilities.*;
import com.exasol.adapter.dialects.*;
import com.exasol.adapter.sql.SqlNode;
import org.junit.Test;
import utils.SqlTestUtil;

import static org.junit.Assert.assertEquals;

public class ExasolSqlDialectTest {
import utils.SqlTestUtil;

@Test
public void testApplyQuoteIfNeeded() {
ExasolSqlDialect dialect = new ExasolSqlDialect(DialectTestData.getExasolDialectContext());
// Regular Identifiers
assertEquals("\"A1\"", dialect.applyQuoteIfNeeded("A1"));
assertEquals("\"A_1\"", dialect.applyQuoteIfNeeded("A_1"));
assertEquals("\"A\"", dialect.applyQuoteIfNeeded("A"));

// Irregular Identifiers
assertEquals("\"A_a_1\"", dialect.applyQuoteIfNeeded("A_a_1"));
assertEquals("\"1\"", dialect.applyQuoteIfNeeded("1"));
assertEquals("\"1a\"", dialect.applyQuoteIfNeeded("1a"));
assertEquals("\"a\"\"b\"", dialect.applyQuoteIfNeeded("a\"b"));
class ExasolSqlDialectTest {
@CsvSource({ "A1, \"A1\"", //
"A_1, \"A_1\"", //
"A,\"A\"", //
"A_a_1, \"A_a_1\"", //
"1, \"1\"", //
"1a, \"1a\", \"1a\"", //
"'a\"b', \"a\"\"b\"" })
@ParameterizedTest
void testApplyQuoteIfNeeded(final String identifier, final String expectedQuotingResult) {
final ExasolSqlDialect dialect = new ExasolSqlDialect(DialectTestData.getExasolDialectContext());
assertThat(dialect.applyQuoteIfNeeded(identifier), equalTo(expectedQuotingResult));
}

@Test
public void testCapabilities() {
// Test if EXASOL dialect really has all capabilities
ExasolSqlDialect dialect = new ExasolSqlDialect(DialectTestData.getExasolDialectContext());
Capabilities caps = dialect.getCapabilities();
assertEquals(PredicateCapability.values().length, caps.getPredicateCapabilities().size());
void testExasolSqlDialectSupportsAllCapabilities() {
final ExasolSqlDialect dialect = new ExasolSqlDialect(DialectTestData.getExasolDialectContext());
final Capabilities capabilities = dialect.getCapabilities();
assertAll(() -> assertThat(capabilities.getMainCapabilities(), containsInAnyOrder(MainCapability.values())),
() -> assertThat(capabilities.getLiteralCapabilities(), containsInAnyOrder(LiteralCapability.values())),
() -> assertThat(capabilities.getPredicateCapabilities(),
containsInAnyOrder(PredicateCapability.values())),
() -> assertThat(capabilities.getScalarFunctionCapabilities(),
containsInAnyOrder(ScalarFunctionCapability.values())),
() -> assertThat(capabilities.getAggregateFunctionCapabilities(),
containsInAnyOrder(AggregateFunctionCapability.values())));
}

@Test
public void testSqlGenerator() throws AdapterException {
SqlNode node = DialectTestData.getTestSqlNode();
String schemaName = "SCHEMA";
String expectedSql = "SELECT \"USER_ID\", COUNT(\"URL\") FROM \"" + schemaName + "\".\"CLICKS\"" +
" WHERE 1 < \"USER_ID\"" +
" GROUP BY \"USER_ID\"" +
" HAVING 1 < COUNT(\"URL\")" +
" ORDER BY \"USER_ID\"" +
" LIMIT 10";
SqlGenerationContext context = new SqlGenerationContext("", schemaName, false, false);
SqlDialect dialect = new ExasolSqlDialect(DialectTestData.getExasolDialectContext());
SqlGenerationVisitor generator = dialect.getSqlGenerationVisitor(context);
String actualSql = node.accept(generator);
assertEquals(SqlTestUtil.normalizeSql(expectedSql), SqlTestUtil.normalizeSql(actualSql));
void testSqlGenerator() throws AdapterException {
final SqlNode node = DialectTestData.getTestSqlNode();
final String schemaName = "SCHEMA";
final String expectedSql = "SELECT \"USER_ID\", COUNT(\"URL\") FROM \"" + schemaName + "\".\"CLICKS\""
+ " WHERE 1 < \"USER_ID\"" + " GROUP BY \"USER_ID\"" + " HAVING 1 < COUNT(\"URL\")"
+ " ORDER BY \"USER_ID\"" + " LIMIT 10";
final SqlGenerationContext context = new SqlGenerationContext("", schemaName, false, false);
final SqlDialect dialect = new ExasolSqlDialect(DialectTestData.getExasolDialectContext());
final SqlGenerationVisitor generator = dialect.getSqlGenerationVisitor(context);
final String actualSql = node.accept(generator);
assertThat(SqlTestUtil.normalizeSql(actualSql), equalTo(SqlTestUtil.normalizeSql(expectedSql)));
}

}
}

0 comments on commit 42245c6

Please sign in to comment.