How to use addUniqueConstraintToColumn method of org.evomaster.client.java.controller.internal.db.SchemaExtractor class

Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.SchemaExtractor.addUniqueConstraintToColumn

Source:SchemaExtractor.java Github

copy

Full Screen

...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 {204 throw new RuntimeException("Unknown constraint type " + constraint.getClass().getName());205 }206 }207 }208 private static void handleTableEntry(DbSchemaDto schemaDto, DatabaseMetaData md, ResultSet tables, Set<String> tableNames) throws SQLException {209 TableDto tableDto = new TableDto();210 schemaDto.tables.add(tableDto);211 tableDto.name = tables.getString("TABLE_NAME");212 if (tableNames.contains(tableDto.name)) {213 /*214 * Perhaps we should throw a more specific exception than IllegalArgumentException215 */...

Full Screen

Full Screen

addUniqueConstraintToColumn

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType;2import org.evomaster.client.java.controller.api.dto.database.schema.DbActionDto;3import org.evomaster.client.java.controller.api.dto.database.schema.DbActionTransformer;4import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;5import org.evomaster.client.java.controller.internal.db.SchemaExtractor;6import org.evomaster.client.java.controller.internal.db.schema.Column;7import org.evomaster.client.java.controller.internal.db.schema.Table;8import org.junit.jupiter.api.Test;9import java.sql.SQLException;10import java.util.ArrayList;11import java.util.Arrays;12import java.util.List;13import static org.junit.jupiter.api.Assertions.assertEquals;14import static org.junit.jupiter.api.Assertions.assertTrue;15public class SchemaExtractorTest {16 public void testAddUniqueConstraintToColumn() throws SQLException {17 List<TableDto> tables = new ArrayList<>();18 TableDto table = new TableDto();19 table.setDatabaseType(DatabaseType.H2);20 table.setName("table");21 table.setColumns(Arrays.asList("column1", "column2"));22 table.setPrimaryKeyColumns(Arrays.asList("column1"));23 table.setForeignKeyColumns(Arrays.asList("column1"));24 table.setUniqueConstraintColumns(Arrays.asList("column1"));25 tables.add(table);26 List<DbActionDto> actions = new ArrayList<>();27 DbActionDto action = new DbActionDto();28 action.setTable("table");29 action.setDatabaseType(DatabaseType.H2);30 action.setSql("INSERT INTO table(column1, column2) VALUES(1, 'a')");31 actions.add(action);32 List<Table> tableList = DbActionTransformer.transformToTables(tables, actions);33 SchemaExtractor.addUniqueConstraintToColumn(tableList);34 assertEquals(1, tableList.size());35 Table t = tableList.get(0);36 assertEquals(1, t.getColumns().size());37 Column c = t.getColumns().get(0);38 assertTrue(c.isUnique());39 }40}

Full Screen

Full Screen

addUniqueConstraintToColumn

Using AI Code Generation

copy

Full Screen

1package org.evomaster.client.java.controller.internal.db;2import org.evomaster.client.java.controller.api.dto.database.schema.DbActionDto;3import org.evomaster.client.java.controller.api.dto.database.schema.DbSchemaDto;4import org.evomaster.client.java.controller.api.dto.database.schema.TableDto;5import java.util.ArrayList;6import java.util.List;7public class SchemaExtractor {8 public static DbSchemaDto addUniqueConstraintToColumn(DbSchemaDto schema, String tableName, String columnName) {9 if (schema == null) {10 return null;11 }12 if (tableName == null || tableName.isBlank()) {13 return null;14 }15 if (columnName == null || columnName.isBlank()) {16 return null;17 }18 TableDto table = schema.tables.stream()19 .filter(t -> t.name.equals(tableName))20 .findFirst()21 .orElse(null);22 if (table == null) {23 return null;24 }25 List<DbActionDto> newActions = new ArrayList<>();26 for (DbActionDto action : schema.actions) {27 if (action.table.equals(tableName)) {28 if (action.columnNames == null) {29 continue;30 }31 if (action.columnNames.contains(columnName)) {32 if (action.indexes == null) {33 action.indexes = new ArrayList<>();34 }35 if (action.indexes.stream().noneMatch(i -> i.equals(columnName))) {36 action.indexes.add(columnName);37 }38 }39 }40 newActions.add(action);41 }42 schema.actions = newActions;43 return schema;44 }45}46The following is the code for the test class that uses the above code to add a unique constraint to a column. The test class is in the following file: [UniqueConstraintTest.java](

Full Screen

Full Screen

addUniqueConstraintToColumn

Using AI Code Generation

copy

Full Screen

1addUniqueConstraintToColumn("public", "users", "name");2addUniqueConstraintToColumn("public", "users", "email");3loadSchema("schema.sql");4extractSchema();5saveSchema("schema.sql");6loadSchema("schema.json");7saveSchema("schema.json");8saveSchemaSQL("schema.sql");9saveSchemaSQL("schema.sql");10addTable("public", "users");11addColumn("public", "users", "id", "integer", false);

Full Screen

Full Screen

addUniqueConstraintToColumn

Using AI Code Generation

copy

Full Screen

1 private void addUniqueConstraintToColumn(String tableName, String columnName, boolean isNullable){2 try{3 String sql = String.format("SELECT DISTINCT %s FROM %s", columnName, tableName);4 try(Statement stmt = getStatement();5 ResultSet rs = stmt.executeQuery(sql)){6 Set<Object> values = new HashSet<>();7 while (rs.next()){8 Object value = rs.getObject(columnName);9 values.add(value);10 }11 if(values.size() == 1){12 if(isNullable){13 if(values.contains(null)){14 uniqueValuesForNullableColumns.put(tableName+"."+columnName, values);15 }16 } else {17 uniqueValuesForNotNullColumns.put(tableName+"."+columnName, values);18 }19 }20 }21 } catch (SQLException e){22 log.warn("Could not get unique values for column {}.{}", tableName, columnName);23 }24 }25 private void addUniqueConstraintToColumn(String tableName, String columnName, boolean isNullable){26 try{27 String sql = String.format("SELECT DISTINCT %s FROM %s", columnName, tableName);28 try(Statement stmt = getStatement();29 ResultSet rs = stmt.executeQuery(sql)){30 Set<Object> values = new HashSet<>();31 while (rs.next()){32 Object value = rs.getObject(columnName);33 values.add(value);34 }35 if(values.size() == 1){36 if(isNullable){37 if(values.contains(null)){38 uniqueValuesForNullableColumns.put(tableName+"."+columnName, values);39 }40 } else {41 uniqueValuesForNotNullColumns.put(tableName+"."+columnName, values);42 }43 }44 }45 } catch (SQLException e){46 log.warn("Could not get unique values for column {}.{}", tableName, columnName);47 }48 }49 private void addUniqueConstraintToColumn(String tableName, String columnName, boolean isNullable){50 try{51 String sql = String.format("SELECT DISTINCT %s FROM %s", columnName, tableName);52 try(Statement stmt = getStatement();53 ResultSet rs = stmt.executeQuery(sql)){54 Set<Object> values = new HashSet<>();55 while (rs.next()){56 Object value = rs.getObject(columnName);57 values.add(value);58 }59 if(values.size() == 1){

Full Screen

Full Screen

addUniqueConstraintToColumn

Using AI Code Generation

copy

Full Screen

1import org.junit.jupiter.api.Assertions.*2import org.junit.jupiter.api.Test3class UniqueConstraintTest {4 fun test0() {5 val schema = SchemaExtractor.extractSchema("jdbc:h2:mem:test", "sa", "")6 val table = schema.findTable("PUBLIC", "PERSON")7 val column = table?.findColumn("ID")8 val uniqueConstraint = UniqueConstraint("PUBLIC", "PERSON", "ID", "PUBLIC_PERSON_ID", 0)9 val result = SchemaExtractor.addUniqueConstraintToColumn(table, column, uniqueConstraint)10 assertTrue(result)11 }12}13import org.junit.jupiter.api.Assertions.*14import org.junit.jupiter.api.Test15class ForeignKeyConstraintTest {16 fun test0() {17 val schema = SchemaExtractor.extractSchema("jdbc:h2:mem:test", "sa", "")18 val table = schema.findTable("PUBLIC", "PERSON")19 val column = table?.findColumn("ID")20 val foreignKeyConstraint = ForeignKeyConstraint("PUBLIC", "PERSON", "ID", "PUBLIC_PERSON_ID", 0, "PUBLIC", "PERSON", "ID")21 val result = SchemaExtractor.addForeignKeyConstraintToColumn(table, column, foreignKeyConstraint)22 assertTrue(result)23 }24}25import org.junit

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful