Skip to content

Commit ca78f22

Browse files
committed
Add coverage for script support
1 parent d222abb commit ca78f22

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

schemacrawler-scripting/src/test/java/schemacrawler/test/script/ScriptSupportTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,24 @@
1010

1111
import static org.hamcrest.CoreMatchers.containsString;
1212
import static org.hamcrest.CoreMatchers.is;
13+
import static org.hamcrest.CoreMatchers.nullValue;
1314
import static org.hamcrest.MatcherAssert.assertThat;
1415
import static org.mockito.Mockito.mock;
16+
import static org.mockito.Mockito.spy;
1517
import static org.mockito.Mockito.when;
1618
import static org.mockito.Mockito.withSettings;
19+
import static schemacrawler.test.utility.crawl.LightCatalogUtility.lightCatalog;
1720
import static schemacrawler.test.utility.crawl.LightCatalogUtility.lightNamedObject;
1821

22+
import java.util.Collection;
1923
import java.util.List;
2024
import org.junit.jupiter.api.Test;
25+
import schemacrawler.ermodel.implementation.ERModelBuilder;
26+
import schemacrawler.ermodel.model.ERModel;
27+
import schemacrawler.ermodel.model.Entity;
2128
import schemacrawler.ermodel.model.Relationship;
2229
import schemacrawler.ermodel.model.RelationshipCardinality;
30+
import schemacrawler.schema.Catalog;
2331
import schemacrawler.schema.Column;
2432
import schemacrawler.schema.ColumnReference;
2533
import schemacrawler.schema.ForeignKey;
@@ -30,6 +38,8 @@
3038
import schemacrawler.schema.PrimaryKey;
3139
import schemacrawler.schema.Table;
3240
import schemacrawler.schema.TableReference;
41+
import schemacrawler.schema.TableType;
42+
import schemacrawler.schema.View;
3343
import schemacrawler.test.utility.crawl.LightColumn;
3444
import schemacrawler.test.utility.crawl.LightColumnReference;
3545
import schemacrawler.test.utility.crawl.LightForeignKey;
@@ -224,6 +234,36 @@ public void columnType() {
224234
assertThat(support.columnType(varcharColumn), is("VARCHAR"));
225235
}
226236

237+
@Test
238+
public void entities() {
239+
// No ER model set → empty
240+
assertThat(support.entities().isEmpty(), is(true));
241+
242+
// Strong entity table (table with PK) → entity included in result
243+
final LightTable entityTable = new LightTable("ENTITY_TABLE");
244+
final LightColumn pkCol = entityTable.addColumn("ID");
245+
entityTable.setPrimaryKey(new LightPrimaryKey(pkCol));
246+
final Catalog catalogWithEntity = lightCatalog(entityTable);
247+
final ERModel erModelWithEntity = ERModelBuilder.builder(catalogWithEntity).build();
248+
final ScriptSupport supportWithEntity = new ScriptSupport();
249+
supportWithEntity.setERModel(erModelWithEntity);
250+
final Collection<Entity> entities = supportWithEntity.entities();
251+
assertThat(entities.size(), is(1));
252+
assertThat(entities.iterator().next().getTable(), is(entityTable));
253+
254+
// View table → excluded from result
255+
final View mockView = mock(View.class);
256+
when(mockView.key()).thenReturn(new NamedObjectKey("schema", "MOCK_VIEW"));
257+
when(mockView.getImportedForeignKeys()).thenReturn(List.of());
258+
when(mockView.getColumns()).thenReturn(List.of());
259+
when(mockView.getTableType()).thenReturn(new TableType("VIEW"));
260+
final Catalog catalogWithView = lightCatalog(mockView);
261+
final ERModel erModelWithView = ERModelBuilder.builder(catalogWithView).build();
262+
final ScriptSupport supportWithView = new ScriptSupport();
263+
supportWithView.setERModel(erModelWithView);
264+
assertThat(supportWithView.entities().isEmpty(), is(true));
265+
}
266+
227267
@Test
228268
public void foreignKeyColumns() {
229269
assertThat(support.fkColumns(null), is(""));
@@ -388,6 +428,36 @@ public void stripName() {
388428
assertThat(support.stripName(namedObject), is("abcxyz"));
389429
}
390430

431+
@Test
432+
public void tableReference() {
433+
// Null column → null (isPartial(null) short-circuits to true)
434+
assertThat(support.tableReference(null), is(nullValue()));
435+
436+
// Partial column → null
437+
final Column partialColumn =
438+
mock(Column.class, withSettings().extraInterfaces(PartialDatabaseObject.class));
439+
assertThat(support.tableReference(partialColumn), is(nullValue()));
440+
441+
// LightColumn (isPartOfForeignKey() = false) → null
442+
final LightTable table = new LightTable("t");
443+
final LightColumn notFkCol = table.addColumn("col");
444+
assertThat(support.tableReference(notFkCol), is(nullValue()));
445+
446+
// Column is part of an FK, and the matching FK column reference is found →
447+
// returns the FK
448+
final LightTable pkTable = new LightTable("pk_table");
449+
final LightColumn pkCol = pkTable.addColumn("pk_col");
450+
final LightTable fkTable = spy(new LightTable("fk_table"));
451+
final Column fkCol = mock(Column.class);
452+
when(fkCol.isPartOfForeignKey()).thenReturn(true);
453+
when(fkCol.getParent()).thenReturn(fkTable);
454+
final LightColumnReference colRef = new LightColumnReference(fkCol, pkCol);
455+
final ForeignKey fk = mock(ForeignKey.class);
456+
when(fk.iterator()).thenAnswer(inv -> List.of(colRef).iterator());
457+
when(fkTable.getImportedForeignKeys()).thenReturn(List.of(fk));
458+
assertThat(support.tableReference(fkCol), is(fk));
459+
}
460+
391461
@Test
392462
public void type() {
393463
// getSimpleTypeName(null) returns "unknown", no NPE

0 commit comments

Comments
 (0)