package adipe.translate;
import java.util.Comparator;
import org.antlr.v4.runtime.misc.ParseCancellationException;
import org.fest.assertions.api.Assertions;
import org.testng.annotations.Test;
import static org.fest.assertions.api.Assertions.assertThat;
import adipe.translate.Schemas;
import adipe.translate.ra.Schema;
import adipe.translate.sql.IntegerType;
import adipe.translate.sql.NamedColumn;
import adipe.translate.sql.SimpleColumn;
import adipe.translate.sql.Type;
import adipe.translate.sql.VarcharType;
public class SchemasTest {
private static final Comparator<NamedColumn> COMPARE_BY_NAME_AND_TYPE = new Comparator<NamedColumn>() {
@Override
public int compare(NamedColumn o1, NamedColumn o2) {
int namesComparison = o1.name().compareTo(o2.name());
if (namesComparison != 0) {
return namesComparison;
} else {
return o1.type().getClass().getName().compareTo(o2.type().getClass().getName());
}
}
};
private static NamedColumn col(String name, Type type) {
return new NamedColumn(new SimpleColumn(type, name),name.toLowerCase());
}
private static void givenDdlExpectTablesAndColumns(String ddlStatement, String tableName, NamedColumn... expectedColumns)
{
Schema s = Schemas.fromDDL(ddlStatement);
assertThat(s.tableNames())
.containsOnly(tableName);
assertThat(s.schemaTable(tableName).namedColumns())
.usingElementComparator(COMPARE_BY_NAME_AND_TYPE)
.containsExactly(expectedColumns);
}
public static class SingleCreateTableStatement {
@Override
public String toString() {
return "CREATE TABLE";
}
@Test
public void minimal() {
givenDdlExpectTablesAndColumns(
"CREATE TABLE tAb (Aa_i int, aA_s Varchar);",
"tAb",
col("Aa_i", IntegerType.INSTANCE),
col("aA_s", VarcharType.INSTANCE));
}
@Test
public void emptyTable() {
try {
givenDdlExpectTablesAndColumns("CREATE TABLE tab();", "tab" /*, no columns */);
Assertions.failBecauseExceptionWasNotThrown(ParseCancellationException.class);
} catch (ParseCancellationException ex) {
// TODO assert for message or so
}
}
}
// TODO test that duplicate table names are recognized
// TODO test that the insertion order of table names is preserved, from Schemas.fromDDL to Schema.tableNames()
// TODO test that the insertion order of column names is preserved
}