schemacrawler / SchemaCrawler

Free database schema discovery and comprehension tool
http://www.schemacrawler.com/
Other
1.58k stars 200 forks source link

Fluent assertion with assertj #1544

Closed bekoenig closed 2 months ago

bekoenig commented 2 months ago

Feature Request

Writing junit tests with schema crawler is powerful to check the consistency of a data model change. The catalog api is well designed but also very complex. So it's hard to getting in and the resulting assertion code has a lot of boilerplate and looks like a mess:

Table tableEntity1 = catalog.lookupTable(catalog.lookupSchema("EXAMPLE_SCHEMA").orElseThrow(),
            "EXAMPLE_ENTITY1").orElseThrow();
assertThat(tableEntity1.getColumns())
  .anyMatch(x -> x.getName().equals("ENTITY1_ID") && x.getColumnDataType().getName().equals("CHAR") && x.getSize() == 36)
  .anyMatch(x -> x.getName().equals("ATTRIBUTE1") && x.getColumnDataType().getName().equals("VARCHAR") && x.getSize() == 250);
assertThat(tableEntity1.getTableConstraints())
  .anyMatch(x -> x.getName().equals("EXAMPLE_ENTITY1_PK") && x.getConstrainedColumns().stream().anyMatch(y -> y.getName().equals("ENTITY1_ID")));

Possible Alternatives

A new module like "schemacrawler-test" includes an assertion which supports a dsl like:

assertThat(catalog)
  .hasSchema("EXAMPLE_SCHEMA")
  .hasTable("EXAMPLE_ENTITY1")
  .hasColumn(withName("ENTITY1_ID").withColumnDataType("CHAR", 36))
  .hasColumn(withName("ATTRIBUTE1").withColumnDataType("VARCHAR", 250))
  .hasTableConstraint(withName("EXAMPLE_ENTITY1_PK").withConstrainedColumn(withName("ENTITY1_ID")));

Additional Context

If you are interested in such an extension, I can start a first implementation and contribute it!

I really like your project and I'm trying to promote it in my company!

sualeh commented 2 months ago

@bekoenig That is a great idea! Why don't you start your project, and I will link to it from the SchemaCrawler website.