Settings

See default generators page for more details about default generators.

Packages:

  • guru.mikelue.foxglove.setting - The main package providing DataSetting.

Overview

The setting object is DataSetting, which controls how to decide generators for columns.

Ways to choose generators(ordered by priority)
  1. By ColumnMatcher, which is a Predicate<ColumnMeta>

  2. By type name(as String), which comes from TYPE_NAME of DatabaseMetaData.getColumns(…​)

  3. By java.sql.JDBCType, which comes from DATA_TYPE of DatabaseMetaData.getColumns(…​)

Ways to provide Supplier for a column
  1. A Supplier

  2. A SupplierDecider, which is a function to decide Supplier by ColumnMeta

  3. A Supplier for ValueSpec of Instancio


Versatile example of DataSetting
new DataSetting()
    // Generates 20 rows if no other row number setting on the table
    .setDefaultNumberOfRows(20)
    // Excludes columns with name starting with "audit_"
    .excludeWhen(columnMeta -> columnMeta.name().startsWith("audit_"))
    // Use column matcher
    .columnMatcher(columnMeta -> columnMeta.name().endsWith("_status"))
        // Choose one of the fixed domain values randomly
        .useSupplier(gen().oneOf("ACTIVE", "INACTIVE", "PENDING"))
    // For type name
    .givenType("enum_status")
        // Choose one of the fixed domain values randomly
        .useSupplier(gen().oneOf("ACTIVE", "INACTIVE", "UNKNOWN"))
    // For JDBCType
    .givenType(JDBCType.VARCHAR)
        // Fixed value for all VARCHAR columns
        .useSupplier(() -> "fixed-text")
    .givenType(JDBCType.INTEGER)
        // Fixed range for all INTEGER columns
        .useSpec(() -> gen().ints().range(1000, 1000000))
    // Generates 128 characters for types of TEXT, LONGVARCHAR, etc.
    .largeTextLength(128)
    // Generates null values with 5% odds for all nullable columns
    .generateNull(20);

Global settings

  • You could use DataSetting.default() to get the current global settings.

Modify global settings
DataSetting.defaults()
    // Generates text with 128 characters for types of TEXT, LONGVARCHAR, etc.
    .largeTextLength(128)
    // Generates null values with 10% odds for all nullable columns
    .generateNull(10);

Apply to TableFacet

Apply to DataGenerator

DataGenerator.withSetting
var dataSetting = new DataSetting()
    // Wont' generate value for NULLABLE column automatically
    .notAutoGenerateFor(Property.NULLABLE);

var dataGenerator = new JdbcDataGenerator(getDataSource());
dataGenerator
    .withSetting(dataSetting);

Multi-level settings

Both of TableFacet and DataGenerator are implementing SettingAware, which have their own settings.

The priority of settings(from high to low) are:

  1. TableFacet setting

  2. DataGenerator setting

  3. DataSetting.defaults()

Note

The scalar settings(e.g. generateNull(boolean), largeTextLength(int)) are only applied to current level.

If current level have found(by resolveSupplier(ColumnMeta)) a Supplier for a column, it will use these scalar settings from itself.

Excluding

The DataSetting is a kind of fallback mechanism to provide generators for columns without specific generator defined.

You could use DataSetting.execludeWhen(Predicate<ColumnMeta>) to exclude some columns from applying any generator of this setting.

Excluding example
var dataSetting = new DataSetting()
    // Excludes the column with name ending with "_computed"
    .excludeWhen(meta -> meta.name().endsWith("_computed"));