diff options
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGeneratorTests.java | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/test/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGeneratorTests.java b/src/test/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGeneratorTests.java new file mode 100644 index 0000000..0438baf --- /dev/null +++ b/src/test/java/org/zwobble/hobgoblin/compiler/output/generators/sql/SqlGeneratorTests.java @@ -0,0 +1,69 @@ +package org.zwobble.hobgoblin.compiler.output.generators.sql; + +import org.junit.jupiter.api.Test; +import org.zwobble.hobgoblin.compiler.ast.typed.RelationTable; +import org.zwobble.hobgoblin.compiler.builtins.NativeTypes; +import org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlIdentifier; +import org.zwobble.hobgoblin.compiler.sources.NullSource; +import org.zwobble.hobgoblin.compiler.typechecker.TypesInfoInMemory; +import org.zwobble.hobgoblin.compiler.types.*; +import org.zwobble.hobgoblin.compiler.util.SequencedMaps; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlOrderingTermMatcher.isSqlOrderingTerm; +import static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlRefMatcher.isSqlRef; +import static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlResultColumnMatcher.isSqlResultColumn; +import static org.zwobble.hobgoblin.compiler.output.lang.sql.ast.SqlSelectStatementMatcher.isSqlSelectStatement; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.equalTo; +import static org.zwobble.precisely.Matchers.isSequence; + +public class SqlGeneratorTests { + @Test + public void relationTableIsTranslatedToSelectFromTable() { + var namespaceName = NamespaceName.of("App"); + var databaseSchema = new DatabaseSchema(namespaceName, "blog"); + var table = new Table(databaseSchema, "authors"); + var authorType = new SimpleStructType(namespaceName, "Author"); + var authorIdField = new Field("id", NativeTypes.INT_32, NullSource.INSTANCE); + var authorNameField = new Field("name", NativeTypes.STRING, NullSource.INSTANCE); + + var typesInfo = TypesInfoInMemory.empty(); + var databaseSchemaInfo = new DatabaseSchemaInfo(SequencedMaps.ofEntries( + Map.entry("authors", new TableInfo(table, authorType, authorIdField)) + )); + typesInfo.defineDatabaseSchema(databaseSchema, databaseSchemaInfo); + typesInfo.defineStructType(authorType, Optional.of(List.of(authorIdField, authorNameField))); + + var sqlGenerator = new SqlGenerator(); + + var relation = RelationTable.arbitrary() + .withTable(table) + .build(); + + var statement = sqlGenerator.generateRelation(relation, typesInfo); + + assertThat(statement, isSqlSelectStatement() + .withColumns(isSequence( + isSqlResultColumn() + .withExpression(isSqlRef().withName(equalTo(id("id")))) + .withAlias(equalTo(id("id"))), + isSqlResultColumn() + .withExpression(isSqlRef().withName(equalTo(id("name")))) + .withAlias(equalTo(id("name"))) + )) + .withFrom(equalTo(id("authors"))) + .withOrderBy(isSequence( + isSqlOrderingTerm() + .withExpression(isSqlRef().withName(equalTo(id("id")))) + )) + ); + } + + private SqlIdentifier id(String value) { + return new SqlIdentifier(value); + } +} |
