Skip to content

Commit

Permalink
Add test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sualeh committed Jul 21, 2023
1 parent a618970 commit a395c61
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@
public class DatabaseObjectDescriptionFunctionParameters implements FunctionParameters {

public enum DatabaseObjectsScope {
NONE, SEQUENCES, SYNONYMS, ROUTINES,;
NONE,
SEQUENCES,
SYNONYMS,
ROUTINES,
;
}

@JsonPropertyDescription("Name of database object to describe.")
private String databaseObjectName;

@JsonPropertyDescription("Indicates what details of database objects to show - sequences, synonyms, or routines (that is, stored procedures or functions).")
@JsonPropertyDescription(
"Indicates what details of database objects to show - sequences, synonyms, or routines (that is, stored procedures or functions).")
private DatabaseObjectsScope databaseObjectsScope;

public String getDatabaseObjectName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@
public class DatabaseObjectListFunctionParameters implements FunctionParameters {

public enum DatabaseObjectType {
ALL, TABLES, ROUTINES, SEQUENCES, SYNONYMS;
ALL,
TABLES,
ROUTINES,
SEQUENCES,
SYNONYMS;
}

@JsonPropertyDescription("Type of database object to list, like tables, routines (that is, functions and stored procedures), schemas (that is, catalogs), sequences, or synonyms.")
@JsonPropertyDescription(
"Type of database object to list, like tables, routines (that is, functions and stored procedures), schemas (that is, catalogs), sequences, or synonyms.")
private DatabaseObjectType databaseObjectType;

public DatabaseObjectType getDatabaseObjectType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@
public class TableDecriptionFunctionParameters implements FunctionParameters {

public enum TableDescriptionScope {
DEFAULT, COLUMNS, PRIMARY_KEY, INDEXES, FOREIGN_KEYS, TRIGGERS;
DEFAULT,
COLUMNS,
PRIMARY_KEY,
INDEXES,
FOREIGN_KEYS,
TRIGGERS;
}

@JsonPropertyDescription("Name of database table or view to describe.")
private String tableName;

@JsonPropertyDescription("Indicates what details of the database table or view to show - columns, primary key, indexes, foreign keys, or triggers.")
@JsonPropertyDescription(
"Indicates what details of the database table or view to show - columns, primary key, indexes, foreign keys, or triggers.")
private TableDescriptionScope descriptionScope;

public TableDescriptionScope getDescriptionScope() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@
public class TableReferencesFunctionReturn implements FunctionReturn {

private static final String NEW_LINE = String.format("%n");
private static final Identifiers identifiers = IdentifiersBuilder.builder()
.withIdentifierQuotingStrategy(IdentifierQuotingStrategy.quote_all).toOptions();
private static final Identifiers identifiers =
IdentifiersBuilder.builder()
.withIdentifierQuotingStrategy(IdentifierQuotingStrategy.quote_all)
.toOptions();
private final Table table;
private final TableReferenceType tableReferenceType;

Expand All @@ -62,7 +64,8 @@ public String render() {
case ALL:
// Fall-through
default:
return renderTableRelationships(TableReferenceType.PARENT) + NEW_LINE
return renderTableRelationships(TableReferenceType.PARENT)
+ NEW_LINE
+ renderTableRelationships(TableReferenceType.CHILD);
}
}
Expand All @@ -74,8 +77,12 @@ private String noData(final TableReferenceType tableReferenceType) {
} else {
buffer.append("Table ");
}
buffer.append(identifiers.quoteFullName(table)).append(" has no ")
.append(tableReferenceType.name().toLowerCase()).append(" relationships.").append(NEW_LINE);
buffer
.append(identifiers.quoteFullName(table))
.append(" has no ")
.append(tableReferenceType.name().toLowerCase())
.append(" relationships.")
.append(NEW_LINE);
return buffer.toString();
}

Expand All @@ -102,7 +109,8 @@ private String renderTableRelationships(final TableReferenceType tableReferenceT
tableName(buffer, tableReferenceType);

for (final Table referencedTable : referencedTables) {
buffer.append(String.format("- %s", identifiers.quoteFullName(referencedTable)))
buffer
.append(String.format("- %s", identifiers.quoteFullName(referencedTable)))
.append(NEW_LINE);
}
return buffer.toString();
Expand All @@ -115,7 +123,11 @@ private void tableName(final StringBuilder buffer, final TableReferenceType tabl
} else {
buffer.append("Table ");
}
buffer.append(identifiers.quoteFullName(table)).append(" has the following ")
.append(tableReferenceType.name().toLowerCase()).append(" tables:").append(NEW_LINE);
buffer
.append(identifiers.quoteFullName(table))
.append(" has the following ")
.append(tableReferenceType.name().toLowerCase())
.append(" tables:")
.append(NEW_LINE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

package schemacrawler.tools.command.chatgpt.test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static schemacrawler.test.utility.DatabaseTestUtility.getCatalog;
import static schemacrawler.test.utility.FileHasContent.classpathResource;
Expand Down Expand Up @@ -136,6 +137,16 @@ public void loadCatalog(final Connection connection) throws Exception {
catalog = getCatalog(connection, schemaRetrievalOptions, schemaCrawlerOptions);
}

@Test
public void parameters(final TestContext testContext) throws Exception {
final DatabaseObjectDescriptionFunctionParameters args =
new DatabaseObjectDescriptionFunctionParameters();
args.setDatabaseObjectsScope(ROUTINES);
assertThat(
args.toString(),
is("{\"database-object-name\":null,\"database-objects-scope\":\"ROUTINES\"}"));
}

private void describeDatabaseObject(
final TestContext testContext, final DatabaseObjectDescriptionFunctionParameters args)
throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

package schemacrawler.tools.command.chatgpt.test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static schemacrawler.test.utility.DatabaseTestUtility.getCatalog;
import static schemacrawler.test.utility.FileHasContent.classpathResource;
Expand Down Expand Up @@ -94,6 +95,13 @@ public void loadCatalog(final Connection connection) throws Exception {
catalog = getCatalog(connection, schemaRetrievalOptions, schemaCrawlerOptions);
}

@Test
public void parameters(final TestContext testContext) throws Exception {
final DatabaseObjectListFunctionParameters args = new DatabaseObjectListFunctionParameters();
args.setDatabaseObjectType(ALL);
assertThat(args.toString(), is("{\"database-object-type\":\"ALL\"}"));
}

@Test
public void routines(final TestContext testContext) throws Exception {
final DatabaseObjectListFunctionParameters args = new DatabaseObjectListFunctionParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

package schemacrawler.tools.command.chatgpt.test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static schemacrawler.test.utility.DatabaseTestUtility.getCatalog;
import static schemacrawler.test.utility.FileHasContent.classpathResource;
Expand Down Expand Up @@ -150,6 +151,14 @@ public void loadCatalog(final Connection connection) throws Exception {
catalog = getCatalog(connection, schemaRetrievalOptions, schemaCrawlerOptions);
}

@Test
public void parameters(final TestContext testContext) throws Exception {
final TableDecriptionFunctionParameters args = new TableDecriptionFunctionParameters();
args.setTableName("AUTHORS");
assertThat(
args.toString(), is("{\"table-name\":\"AUTHORS\",\"description-scope\":\"DEFAULT\"}"));
}

private void describeTable(
final TestContext testContext, final TableDecriptionFunctionParameters args)
throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

package schemacrawler.tools.command.chatgpt.test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static schemacrawler.test.utility.DatabaseTestUtility.getCatalog;
import static schemacrawler.test.utility.FileHasContent.classpathResource;
Expand Down Expand Up @@ -91,6 +92,15 @@ public void loadCatalog(final Connection connection) throws Exception {
catalog = getCatalog(connection, schemaRetrievalOptions, schemaCrawlerOptions);
}

@Test
public void parameters(final TestContext testContext) throws Exception {
final TableReferencesFunctionParameters args = new TableReferencesFunctionParameters();
args.setTableName("BOOKS");
args.setTableReferenceType(TableReferenceType.CHILD);
assertThat(
args.toString(), is("{\"table-name\":\"BOOKS\",\"table-reference-type\":\"CHILD\"}"));
}

@Test
public void parentsForTable(final TestContext testContext) throws Exception {
final TableReferencesFunctionParameters args = new TableReferencesFunctionParameters();
Expand Down

0 comments on commit a395c61

Please sign in to comment.