Skip to content
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

Enhancement/25 unit tests part 2 #31

Merged
merged 4 commits into from
Feb 13, 2019
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
17 changes: 6 additions & 11 deletions src/main/java/com/exasol/adapter/sql/SqlFunctionScalarCast.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.util.Collections;
import java.util.List;


public class SqlFunctionScalarCast extends SqlNode {
private final DataType dataType;
private final List<SqlNode> arguments;
Expand All @@ -21,23 +20,19 @@ public SqlFunctionScalarCast(final DataType dataType, final List<SqlNode> argume
}

public DataType getDataType() {
return dataType;
return this.dataType;
}


public List<SqlNode> getArguments() {
if (arguments == null) {
return Collections.emptyList();
} else {
return Collections.unmodifiableList(arguments);
}
return Collections.unmodifiableList(this.arguments);
}

@Override
public String toSimpleSql() {
assert arguments != null;
assert arguments.size() == 1 && arguments.get(0) != null;
return "CAST (" + arguments.get(0).toSimpleSql() + " AS " + getDataType().toString() + ")";
assert this.arguments != null;
assert this.arguments.size() == 1 && this.arguments.get(0) != null;
return "CAST (" + this.arguments.get(0).toSimpleSql() + " AS " + getDataType().toString() +
")";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public class SqlFunctionScalarExtract extends SqlNode {
private final List<SqlNode> arguments;

public SqlFunctionScalarExtract(final String toExtract, final List<SqlNode> arguments) {
SqlArgumentValidator.validateSqlFunctionArguments(arguments, SqlFunctionScalarExtract.class);
SqlArgumentValidator
.validateSqlFunctionArguments(arguments, SqlFunctionScalarExtract.class);
this.arguments = arguments;
this.toExtract = toExtract;
for (final SqlNode node : this.arguments) {
Expand All @@ -20,21 +21,17 @@ public SqlFunctionScalarExtract(final String toExtract, final List<SqlNode> argu
}

public String getToExtract() {
return toExtract;
return this.toExtract;
}

public List<SqlNode> getArguments() {
if (arguments == null) {
return Collections.emptyList();
} else {
return Collections.unmodifiableList(arguments);
}
return Collections.unmodifiableList(this.arguments);
}

@Override
public String toSimpleSql() {
assert arguments.size() == 1 && arguments.get(0) != null;
return "EXTRACT (" + toExtract + " FROM " + arguments.get(0).toSimpleSql() + ")";
assert this.arguments.size() == 1 && this.arguments.get(0) != null;
return "EXTRACT (" + this.toExtract + " FROM " + this.arguments.get(0).toSimpleSql() + ")";
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ public SqlPredicateIsNotNull(final SqlNode expression) {
}

public SqlNode getExpression() {
return expression;
return this.expression;
}

@Override
public String toSimpleSql() {
return expression.toSimpleSql() + " IS NOT NULL";
return this.expression.toSimpleSql() + " IS NOT NULL";
}

@Override
public SqlNodeType getType() {
return SqlNodeType.PREDICATE_IS_NULL;
return SqlNodeType.PREDICATE_IS_NOT_NULL;
AnastasiiaSergienko marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/com/exasol/adapter/sql/SqlColumnTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.exasol.adapter.sql;

import com.exasol.adapter.AdapterException;
import com.exasol.adapter.metadata.ColumnMetadata;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
@RunWith(JUnitPlatform.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class SqlColumnTest {
private static final int ID = 1;
private static final String TABLE_NAME = "table_name";
private static final String METADATA_TEST_NAME = "metadata_test_name";
private SqlColumn sqlColumn;
@Mock private ColumnMetadata columnMetadata;

@BeforeEach
void setUp() {
when(this.columnMetadata.getName()).thenReturn(METADATA_TEST_NAME);
this.sqlColumn = new SqlColumn(ID, this.columnMetadata, TABLE_NAME);
}

@Test
void testToSimpleSql() {
assertThat(this.sqlColumn.toSimpleSql(), equalTo("\"" + METADATA_TEST_NAME + "\""));
}

@Test
void testGetType() {
assertThat(this.sqlColumn.getType(), equalTo(SqlNodeType.COLUMN));
}

@Test
void testAccept() throws AdapterException {
final SqlNodeVisitor<SqlColumn> visitor = mock(SqlNodeVisitor.class);
when(visitor.visit(this.sqlColumn)).thenReturn(this.sqlColumn);
assertThat(this.sqlColumn.accept(visitor), equalTo(this.sqlColumn));
}

@Test
void testGetId() {
assertThat(this.sqlColumn.getId(), equalTo(ID));
}

@Test
void testGetMetadata() {
assertThat(this.sqlColumn.getMetadata(), equalTo(this.columnMetadata));
}

@Test
void testGetName() {
assertThat(this.sqlColumn.getName(), equalTo(METADATA_TEST_NAME));
}

@Test
void testGetTableName() {
assertThat(this.sqlColumn.getTableName(), equalTo(TABLE_NAME));
}
}
76 changes: 76 additions & 0 deletions src/test/java/com/exasol/adapter/sql/SqlFunctionAggregateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.exasol.adapter.sql;

import com.exasol.adapter.AdapterException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class SqlFunctionAggregateTest {
private static final boolean TEST_DISTINCT = true;
public static final String TEST_NAME = "test string";
private SqlFunctionAggregate sqlFunctionAggregate;
private List<SqlNode> arguments;
private AggregateFunction aggregateFunction;

@BeforeEach
void setUp() {
this.aggregateFunction = AggregateFunction.AVG;
this.arguments = new ArrayList<>();
this.arguments.add(new SqlLiteralString(TEST_NAME));
this.sqlFunctionAggregate =
new SqlFunctionAggregate(this.aggregateFunction, this.arguments, TEST_DISTINCT);
}

@Test
void testGetArguments() {
assertThat(this.sqlFunctionAggregate.getArguments(), equalTo(this.arguments));
}

@Test
void testGetArgumentsReturnsEmptyList() {
assertThat(
new SqlFunctionAggregate(this.aggregateFunction, null, TEST_DISTINCT).getArguments(),
equalTo(Collections.emptyList()));
}

@Test
void testToSimpleSql() {
assertThat(this.sqlFunctionAggregate.toSimpleSql(),
equalTo("AVG(DISTINCT '" + TEST_NAME + "')"));
}

@Test
void testGetType() {
assertThat(this.sqlFunctionAggregate.getType(), equalTo(SqlNodeType.FUNCTION_AGGREGATE));
}

@Test
void testGetFunction() {
assertThat(this.sqlFunctionAggregate.getFunction(), equalTo(this.aggregateFunction));
}

@Test
void getFunctionName() {
assertThat(this.sqlFunctionAggregate.getFunctionName(), equalTo("AVG"));
}

@Test
void testAccept() throws AdapterException {
final SqlNodeVisitor<SqlFunctionAggregate> visitor = mock(SqlNodeVisitor.class);
when(visitor.visit(this.sqlFunctionAggregate)).thenReturn(this.sqlFunctionAggregate);
assertThat(this.sqlFunctionAggregate.accept(visitor), equalTo(this.sqlFunctionAggregate));
}

@Test
void testHasDistinct() {
assertThat(this.sqlFunctionAggregate.hasDistinct(), equalTo(TEST_DISTINCT));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.exasol.adapter.sql;

import com.exasol.adapter.AdapterException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -9,6 +10,8 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class SqlFunctionScalarCaseTest {
private SqlFunctionScalarCase sqlFunctionScalarCase;
Expand Down Expand Up @@ -69,4 +72,11 @@ void testGetType() {
void testGetFunction() {
assertThat(this.sqlFunctionScalarCase.getFunction(), equalTo(ScalarFunction.CASE));
}

@Test
void testAccept() throws AdapterException {
final SqlNodeVisitor<SqlFunctionScalarCase> visitor = mock(SqlNodeVisitor.class);
when(visitor.visit(this.sqlFunctionScalarCase)).thenReturn(this.sqlFunctionScalarCase);
assertThat(this.sqlFunctionScalarCase.accept(visitor), equalTo(this.sqlFunctionScalarCase));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.exasol.adapter.sql;

import com.exasol.adapter.AdapterException;
import com.exasol.adapter.metadata.DataType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class SqlFunctionScalarCastTest {
private SqlFunctionScalarCast sqlFunctionScalarCast;
private List<SqlNode> arguments;
private DataType dataType;
private SqlNode sqlNode;

@BeforeEach
void setUp() {
this.dataType = DataType.createDecimal(18, 0);
this.arguments = new ArrayList<>();
this.arguments.add(new SqlLiteralDouble(20));
this.sqlFunctionScalarCast = new SqlFunctionScalarCast(this.dataType, this.arguments);
}

@Test
void testGetArguments() {
assertThat(this.sqlFunctionScalarCast.getArguments(), equalTo(this.arguments));
}

@Test
void testGetArgumentsWithNullAsArgumentList() {
assertThrows(IllegalArgumentException.class,
() -> this.sqlFunctionScalarCast = new SqlFunctionScalarCast(this.dataType, null));
}

@Test
void testGetDataType() {
assertThat(this.sqlFunctionScalarCast.getDataType(), equalTo(this.dataType));
}

@Test
void testToSimpleSql() {
assertThat(this.sqlFunctionScalarCast.toSimpleSql(), equalTo("CAST (20.0 AS DECIMAL(18, 0))"));
}

@Test
void testGetType() {
assertThat(this.sqlFunctionScalarCast.getType(), equalTo(SqlNodeType.FUNCTION_SCALAR_CAST));
}

@Test
void testGetFunction() {
assertThat(this.sqlFunctionScalarCast.getFunction(), equalTo(ScalarFunction.CAST));
}

@Test
void testAccept() throws AdapterException {
AnastasiiaSergienko marked this conversation as resolved.
Show resolved Hide resolved
final SqlNodeVisitor<SqlFunctionScalarCast> visitor = mock(SqlNodeVisitor.class);
when(visitor.visit(this.sqlFunctionScalarCast)).thenReturn(this.sqlFunctionScalarCast);
assertThat(this.sqlFunctionScalarCast.accept(visitor), equalTo(this.sqlFunctionScalarCast));
}
}
Loading