Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.SchemaExtractor.addConstraints
Source:SchemaExtractor.java
...153 JDBC MetaData is quite limited.154 To check constraints, we need to do SQL queries on the system tables.155 Unfortunately, this is database-dependent156 */157 addConstraints(connection, dt, schemaDto);158 assert validate(schemaDto);159 return schemaDto;160 }161 /**162 * Adds a unique constraint to the corresponding ColumnDTO for the selected table.column pair.163 * Requires the ColumnDTO to be contained in the TableDTO.164 * If the column DTO is not contained, a IllegalArgumentException is thrown.165 *166 * @param tableDto the DTO of the table167 * @param columnName the name of the column to add the unique constraint on168 **/169 public static void addUniqueConstraintToColumn(TableDto tableDto, String columnName) {170 ColumnDto columnDto = tableDto.columns.stream()171 .filter(c -> c.name.equals(columnName)).findAny().orElse(null);172 if (columnDto == null) {173 throw new IllegalArgumentException("Missing column DTO for column:" + tableDto.name + "." + columnName);174 }175 columnDto.unique = true;176 }177 /**178 * Appends constraints that are database specific.179 */180 private static void addConstraints(Connection connection, DatabaseType dt, DbSchemaDto schemaDto) throws SQLException {181 TableConstraintExtractor constraintExtractor = TableConstraintExtractorFactory.buildConstraintExtractor(dt);182 if (constraintExtractor != null) {183 final List<DbTableConstraint> dbTableConstraints = constraintExtractor.extract(connection, schemaDto);184 addConstraints(schemaDto, dbTableConstraints);185 } else {186 SimpleLogger.uniqueWarn("WARNING: EvoMaster cannot extract constraints from database " + dt);187 }188 }189 private static void addConstraints(DbSchemaDto schemaDto, List<DbTableConstraint> constraintList) {190 for (DbTableConstraint constraint : constraintList) {191 String tableName = constraint.getTableName();192 TableDto tableDto = schemaDto.tables.stream().filter(t -> t.name.equalsIgnoreCase(tableName)).findFirst().orElse(null);193 if (constraint instanceof DbTableCheckExpression) {194 TableCheckExpressionDto constraintDto = new TableCheckExpressionDto();195 final DbTableCheckExpression tableCheckExpression = (DbTableCheckExpression) constraint;196 constraintDto.sqlCheckExpression = tableCheckExpression.getSqlCheckExpression();197 tableDto.tableCheckExpressions.add(constraintDto);198 } else if (constraint instanceof DbTableUniqueConstraint) {199 DbTableUniqueConstraint tableUniqueConstraint = (DbTableUniqueConstraint) constraint;200 for (String columnName : tableUniqueConstraint.getUniqueColumnNames()) {201 addUniqueConstraintToColumn(tableDto, columnName);202 }203 } else {...
addConstraints
Using AI Code Generation
1package org.evomaster.client.java.controller.internal.db;2import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;3import org.evomaster.client.java.controller.api.dto.database.schema.DbSchemaDto;4import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;5import org.evomaster.client.java.controller.api.dto.database.schema.TableIndexDto;6import org.evomaster.client.java.controller.api.dto.database.schema.TableIndexType;7import org.evomaster.client.java.controller.api.dto.database.schema.TableIndexColumnDto;8import org.evomaster.client.java.controller.api.dto.database.schema.ColumnDto;9import org.evomaster.client.java.controller.api.dto.database.schema.ColumnDataType;10import org.evomaster.client.java.controller.api.dto.database.schema.ColumnForeignKeyDto;11public class SchemaExtractor {12 public static void main(String[] args) {13 DbSchemaDto dbSchemaDto = new DbSchemaDto();14 dbSchemaDto.setDatabaseType(DatabaseType.H2);15 dbSchemaDto.setSchemaName("PUBLIC");16 TableDto tableDto = new TableDto();17 tableDto.setTableName("CUSTOMER");18 tableDto.setSchemaName("PUBLIC");19 tableDto.setDatabaseType(DatabaseType.H2);20 TableIndexDto tableIndexDto = new TableIndexDto();21 tableIndexDto.setIndexName("PRIMARY_KEY_9");22 tableIndexDto.setIndexType(TableIndexType.PRIMARY_KEY);23 tableIndexDto.setUnique(true);24 tableIndexDto.setTableSchema("PUBLIC");25 tableIndexDto.setTableName("CUSTOMER");26 TableIndexColumnDto tableIndexColumnDto = new TableIndexColumnDto();27 tableIndexColumnDto.setColumnName("ID");28 tableIndexColumnDto.setIndexName("PRIMARY_KEY_9");29 tableIndexColumnDto.setTableName("CUSTOMER");30 tableIndexColumnDto.setTableSchema("PUBLIC");31 tableIndexColumnDto.setOrdinalPosition(1);32 tableIndexColumnDto.setTableCatalog("PUBLIC");33 tableIndexColumnDto.setIndexCatalog("PUBLIC");34 tableIndexColumnDto.setIndexSchema("PUBLIC");35 ColumnDto columnDto = new ColumnDto();36 columnDto.setColumnName("ID");37 columnDto.setDataType(ColumnDataType.INTEGER);38 columnDto.setNullable(false);39 columnDto.setAutoIncrement(true);40 columnDto.setPrimaryKey(true);41 columnDto.setTableSchema("PUBLIC");42 columnDto.setTableName("CUSTOMER");43 columnDto.setTableCatalog("PUBLIC");44 columnDto.setOrdinalPosition(1
addConstraints
Using AI Code Generation
1public static void addConstraints(Schema schema) {2 Set<Table> tables = schema.getTables();3 for (Table table : tables) {4 List<ForeignKeyConstraint> foreignKeyConstraints = table.getForeignKeyConstraints();5 for (ForeignKeyConstraint foreignKeyConstraint : foreignKeyConstraints) {6 Table foreignTable = foreignKeyConstraint.getForeignTable();7 List<Column> foreignColumns = foreignKeyConstraint.getForeignColumns();8 List<Column> localColumns = foreignKeyConstraint.getLocalColumns();9 for (int i = 0; i < foreignColumns.size(); i++) {10 Column foreignColumn = foreignColumns.get(i);11 Column localColumn = localColumns.get(i);12 String foreignColumnName = foreignColumn.getName();13 String localColumnName = localColumn.getName();14 String foreignTableName = foreignTable.getName();15 String localTableName = table.getName();16 String constraintName = foreignKeyConstraint.getName();17 Constraint constraint = new Constraint(localTableName, localColumnName, foreignTableName, foreignColumnName, constraintName);18 table.addConstraint(constraint);19 }20 }21 }22 }23public void addConstraints(List<Constraint> constraints) {24 for (Constraint constraint : constraints) {25 String localTableName = constraint.getLocalTableName();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!