|
10 | 10 |
|
11 | 11 | import static org.hamcrest.CoreMatchers.containsString; |
12 | 12 | import static org.hamcrest.CoreMatchers.is; |
| 13 | +import static org.hamcrest.CoreMatchers.nullValue; |
13 | 14 | import static org.hamcrest.MatcherAssert.assertThat; |
14 | 15 | import static org.mockito.Mockito.mock; |
| 16 | +import static org.mockito.Mockito.spy; |
15 | 17 | import static org.mockito.Mockito.when; |
16 | 18 | import static org.mockito.Mockito.withSettings; |
| 19 | +import static schemacrawler.test.utility.crawl.LightCatalogUtility.lightCatalog; |
17 | 20 | import static schemacrawler.test.utility.crawl.LightCatalogUtility.lightNamedObject; |
18 | 21 |
|
| 22 | +import java.util.Collection; |
19 | 23 | import java.util.List; |
20 | 24 | import org.junit.jupiter.api.Test; |
| 25 | +import schemacrawler.ermodel.implementation.ERModelBuilder; |
| 26 | +import schemacrawler.ermodel.model.ERModel; |
| 27 | +import schemacrawler.ermodel.model.Entity; |
21 | 28 | import schemacrawler.ermodel.model.Relationship; |
22 | 29 | import schemacrawler.ermodel.model.RelationshipCardinality; |
| 30 | +import schemacrawler.schema.Catalog; |
23 | 31 | import schemacrawler.schema.Column; |
24 | 32 | import schemacrawler.schema.ColumnReference; |
25 | 33 | import schemacrawler.schema.ForeignKey; |
|
30 | 38 | import schemacrawler.schema.PrimaryKey; |
31 | 39 | import schemacrawler.schema.Table; |
32 | 40 | import schemacrawler.schema.TableReference; |
| 41 | +import schemacrawler.schema.TableType; |
| 42 | +import schemacrawler.schema.View; |
33 | 43 | import schemacrawler.test.utility.crawl.LightColumn; |
34 | 44 | import schemacrawler.test.utility.crawl.LightColumnReference; |
35 | 45 | import schemacrawler.test.utility.crawl.LightForeignKey; |
@@ -224,6 +234,36 @@ public void columnType() { |
224 | 234 | assertThat(support.columnType(varcharColumn), is("VARCHAR")); |
225 | 235 | } |
226 | 236 |
|
| 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 | + |
227 | 267 | @Test |
228 | 268 | public void foreignKeyColumns() { |
229 | 269 | assertThat(support.fkColumns(null), is("")); |
@@ -388,6 +428,36 @@ public void stripName() { |
388 | 428 | assertThat(support.stripName(namedObject), is("abcxyz")); |
389 | 429 | } |
390 | 430 |
|
| 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 | + |
391 | 461 | @Test |
392 | 462 | public void type() { |
393 | 463 | // getSimpleTypeName(null) returns "unknown", no NPE |
|
0 commit comments