Best EvoMaster code snippet using org.evomaster.dbconstraint.TableConstraint.TableConstraint
Source:SqlConditionTranslator.java
...25import java.util.List;26import java.util.Set;27import java.util.stream.Collectors;28import static org.evomaster.dbconstraint.ast.SqlComparisonOperator.EQUALS_TO;29public class SqlConditionTranslator implements SqlConditionVisitor<TableConstraint, Void> {30 private static final String THIS_METHOD_SHOULD_NOT_BE_INVOKED = "This method should not be directly called";31 public static final String UNEXPECTED_COMPARISON_OPERATOR_MESSAGE = "Unexpected comparison operator ";32 private final TranslationContext translationContext;33 public SqlConditionTranslator(TranslationContext translationContext) {34 this.translationContext = translationContext;35 }36 /**37 * FIXME38 * temporary workaround before major refactoring.39 * Recall that Column.getTable() is not reliable40 */41 private String getTableName(SqlColumn column) {42 String tableName = column.getTableName();43 if (tableName != null) {44 return tableName;45 } else {46 return this.translationContext.getCurrentTableName();47 }48 }49 @Override50 public TableConstraint visit(SqlAndCondition andExpression, Void argument) {51 TableConstraint left = andExpression.getLeftExpr().accept(this, null);52 TableConstraint right = andExpression.getRightExpr().accept(this, null);53 return new AndConstraint(translationContext.getCurrentTableName(), left, right);54 }55 @Override56 public TableConstraint visit(SqlComparisonCondition e, Void argument) {57 SqlCondition left = e.getLeftOperand();58 SqlCondition right = e.getRightOperand();59 if (left instanceof SqlLiteralValue && right instanceof SqlColumn) {60 SqlLiteralValue leftLiteral = (SqlLiteralValue) left;61 SqlColumn rightColumn = (SqlColumn) right;62 return visit(leftLiteral, e, rightColumn);63 } else if (left instanceof SqlColumn && right instanceof SqlLiteralValue) {64 SqlColumn leftColumn = (SqlColumn) left;65 SqlLiteralValue rightLiteral = (SqlLiteralValue) right;66 return visit(leftColumn, e, rightLiteral);67 } else if (left instanceof SqlCondition && right instanceof SqlCondition) {68 TableConstraint leftTableConstraint = e.getLeftOperand().accept(this, null);69 TableConstraint rightTableConstraint = e.getRightOperand().accept(this, null);70 if (e.getSqlComparisonOperator().equals(EQUALS_TO)) {71 return new IffConstraint(translationContext.getCurrentTableName(), leftTableConstraint, rightTableConstraint);72 }73 }74 // TODO This translation should be implemented75 throw new SqlCannotBeTranslatedException(e.toSql() + " cannot be translated yet");76 }77 private TableConstraint visit(SqlColumn leftColumn, SqlComparisonCondition e, SqlLiteralValue rightLiteral) {78 final String tableName = getTableName(leftColumn);79 final String columnName = leftColumn.getColumnName();80 if (rightLiteral instanceof SqlBigIntegerLiteralValue) {81 long value = ((SqlBigIntegerLiteralValue) rightLiteral).getBigInteger().longValue();82 switch (e.getSqlComparisonOperator()) {83 case EQUALS_TO:84 return new RangeConstraint(tableName, columnName, value, value);85 case GREATER_THAN:86 return new LowerBoundConstraint(tableName, columnName, value + 1);87 case GREATER_THAN_OR_EQUAL:88 return new LowerBoundConstraint(tableName, columnName, value);89 case LESS_THAN:90 return new UpperBoundConstraint(tableName, columnName, value - 1);91 case LESS_THAN_OR_EQUAL:92 return new UpperBoundConstraint(tableName, columnName, value);93 default:94 throw new UnsupportedOperationException(UNEXPECTED_COMPARISON_OPERATOR_MESSAGE + e.getSqlComparisonOperator());95 }96 } else if (rightLiteral instanceof SqlStringLiteralValue) {97 SqlStringLiteralValue stringLiteralValue = (SqlStringLiteralValue) rightLiteral;98 if (e.getSqlComparisonOperator().equals(EQUALS_TO)) {99 return new EnumConstraint(tableName, columnName, Collections.singletonList(stringLiteralValue.getStringValue()));100 } else {101 throw new UnsupportedOperationException(UNEXPECTED_COMPARISON_OPERATOR_MESSAGE + e.getSqlComparisonOperator());102 }103 } else {104 throw new UnsupportedOperationException("Unsupported literal " + rightLiteral);105 }106 }107 private TableConstraint visit(SqlLiteralValue leftLiteral, SqlComparisonCondition e, SqlColumn rightColumn) {108 if (leftLiteral instanceof SqlBigIntegerLiteralValue) {109 long value = ((SqlBigIntegerLiteralValue) leftLiteral).getBigInteger().longValue();110 final String tableName = getTableName(rightColumn);111 final String columnName = rightColumn.getColumnName();112 switch (e.getSqlComparisonOperator()) {113 case EQUALS_TO:114 return new RangeConstraint(tableName, columnName, value, value);115 case GREATER_THAN:116 return new UpperBoundConstraint(tableName, columnName, value - 1);117 case GREATER_THAN_OR_EQUAL:118 return new UpperBoundConstraint(tableName, columnName, value);119 case LESS_THAN:120 return new LowerBoundConstraint(tableName, columnName, value + 1);121 case LESS_THAN_OR_EQUAL:122 return new LowerBoundConstraint(tableName, columnName, value);123 default:124 throw new UnsupportedOperationException(UNEXPECTED_COMPARISON_OPERATOR_MESSAGE + e.getSqlComparisonOperator());125 }126 } else {127 throw new UnsupportedOperationException("Unsupported literal " + e.getSqlComparisonOperator());128 }129 }130 @Override131 public TableConstraint visit(SqlInCondition inExpression, Void argument) {132 SqlColumn column = inExpression.getSqlColumn();133 String tableName = getTableName(column);134 String columnName = column.getColumnName();135 SqlConditionList rightItemsList = inExpression.getLiteralList();136 List<String> stringValues = new ArrayList<>();137 for (SqlCondition expressionValue : rightItemsList.getSqlConditionExpressions()) {138 final String stringValue;139 if (expressionValue instanceof SqlStringLiteralValue) {140 stringValue = new StringValue(expressionValue.toSql()).getNotExcapedValue();141 } else {142 stringValue = expressionValue.toSql();143 }144 stringValues.add(stringValue);145 }146 return new EnumConstraint(tableName, columnName, stringValues);147 }148 @Override149 public TableConstraint visit(SqlNullLiteralValue e, Void argument) {150 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);151 }152 @Override153 public TableConstraint visit(SqlStringLiteralValue e, Void argument) {154 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);155 }156 @Override157 public TableConstraint visit(SqlConditionList e, Void argument) {158 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);159 }160 @Override161 public TableConstraint visit(SqlIsNotNullCondition e, Void argument) {162 String tableName = getTableName(e.getColumn());163 String columnName = e.getColumn().getColumnName();164 return new IsNotNullConstraint(tableName, columnName);165 }166 @Override167 public TableConstraint visit(SqlBinaryDataLiteralValue e, Void argument) {168 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);169 }170 @Override171 public TableConstraint visit(SqlSimilarToCondition e, Void argument) {172 String tableName = getTableName(e.getColumn());173 final String pattern = e.getPattern().getStringValue();174 return new SimilarToConstraint(tableName, e.getColumn().getColumnName(), pattern, translationContext.getDatabaseType());175 }176 @Override177 public TableConstraint visit(SqlIsNullCondition e, Void argument) {178 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);179 }180 @Override181 public TableConstraint visit(SqlLikeCondition e, Void argument) {182 String tableName = getTableName(e.getColumnName());183 String columnName = e.getColumnName().getColumnName();184 String pattern = e.getPattern().getStringValue();185 return new LikeConstraint(tableName, columnName, pattern, translationContext.getDatabaseType());186 }187 @Override188 public TableConstraint visit(SqlOrCondition e, Void argument) {189 List<TableConstraint> orConstraints = e.getOrConditions().stream().map(c -> c.accept(this, null)).collect(Collectors.toList());190 if (orConstraints.stream().allMatch(c -> c instanceof LikeConstraint)) {191 // all like constraints should have the same tablename, column and database type192 Set<String> columnNames = orConstraints193 .stream()194 .map(c -> (LikeConstraint) c)195 .map(c -> c.getColumnName())196 .collect(Collectors.toSet());197 Set<String> tableNames = orConstraints198 .stream()199 .map(c -> (LikeConstraint) c)200 .map(c -> c.getTableName())201 .collect(Collectors.toSet());202 Set<ConstraintDatabaseType> databaseTypes = orConstraints203 .stream().map(c -> (LikeConstraint) c)204 .map(c -> c.getDatabaseType())205 .collect(Collectors.toSet());206 if (columnNames.size() == 1 && tableNames.size() == 1 && databaseTypes.size() == 1) {207 String tableName = tableNames.iterator().next();208 String columnName = columnNames.iterator().next();209 ConstraintDatabaseType databaseType = databaseTypes.iterator().next();210 List<String> patterns = orConstraints211 .stream()212 .map(c -> (LikeConstraint) c)213 .map(c -> c.getPatterns())214 .flatMap(List::stream)215 .collect(Collectors.toList());216 return new LikeConstraint(tableName,columnName,patterns,databaseType);217 }218 }219 return new OrConstraint(translationContext.getCurrentTableName(), orConstraints.toArray(new TableConstraint[]{}));220 }221 @Override222 public TableConstraint visit(SqlBigDecimalLiteralValue e, Void argument) {223 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);224 }225 @Override226 public TableConstraint visit(SqlBigIntegerLiteralValue e, Void argument) {227 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);228 }229 @Override230 public TableConstraint visit(SqlBooleanLiteralValue e, Void argument) {231 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);232 }233 @Override234 public TableConstraint visit(SqlColumn e, Void argument) {235 throw new UnsupportedOperationException(THIS_METHOD_SHOULD_NOT_BE_INVOKED);236 }237}...
TableConstraint
Using AI Code Generation
1package org.evomaster.client.java.instrumentation.example.db;2import org.evomaster.client.java.instrumentation.example.db.constraint.TableConstraint;3import java.util.List;4public class DbExampleDTOMapper {5 public static DbExampleDTO toDto(DbExample dbExample) {6 DbExampleDTO dto = new DbExampleDTO();7 dto.setId(dbExample.getId());8 dto.setName(dbExample.getName());9 dto.setAge(dbExample.getAge());10 dto.setSalary(dbExample.getSalary());11 return dto;12 }13 public static DbExample toEntity(DbExampleDTO dto) {14 DbExample dbExample = new DbExample();15 dbExample.setId(dto.getId());16 dbExample.setName(dto.getName());17 dbExample.setAge(dto.getAge());18 dbExample.setSalary(dto.getSalary());19 return dbExample;20 }21 public static List<DbExample> toEntity(List<DbExampleDTO> dtoList) {22 List<DbExample> dbExampleList = new ArrayList<>();23 for (DbExampleDTO dto : dtoList) {24 dbExampleList.add(toEntity(dto));25 }26 return dbExampleList;27 }28 public static List<DbExampleDTO> toDto(List<DbExample> entityList) {29 List<DbExampleDTO> dtoList = new ArrayList<>();30 for (DbExample entity : entityList) {31 dtoList.add(toDto(entity));32 }33 return dtoList;34 }35 public static List<DbExample> toEntity(List<DbExampleDTO> dtoList, TableConstraint tableConstraint) {36 List<DbExample> dbExampleList = new ArrayList<>();37 for (DbExampleDTO dto : dtoList) {38 dbExampleList.add(toEntity(dto, tableConstraint));39 }40 return dbExampleList;41 }42 public static DbExample toEntity(DbExampleDTO dto, TableConstraint tableConstraint) {43 DbExample dbExample = new DbExample();44 dbExample.setId(dto.getId());45 dbExample.setName(dto.getName());46 dbExample.setAge(dto.getAge());47 dbExample.setSalary(dto.getSalary());48 tableConstraint.apply(dbExample);49 return dbExample;50 }51}52package org.evomaster.client.java.instrumentation.example.db;
TableConstraint
Using AI Code Generation
1TableConstraint tableConstraint = new TableConstraint("tableName");2tableConstraint.addPrimaryKeyConstraint("column1");3tableConstraint.addNotNullConstraint("column2");4tableConstraint.addUniqueConstraint("column3", "column4");5tableConstraint.addCheckConstraint("column5", "column5 > 0");6tableConstraint.addForeignKeyConstraint("column6", "tableName2", "column7");7TableConstraint tableConstraint = new TableConstraint("tableName");8tableConstraint.addPrimaryKeyConstraint("column1");9tableConstraint.addNotNullConstraint("column2");10tableConstraint.addUniqueConstraint("column3", "column4");11tableConstraint.addCheckConstraint("column5", "column5 > 0");12tableConstraint.addForeignKeyConstraint("column6", "tableName2", "column7");13TableConstraint tableConstraint = new TableConstraint("tableName");14tableConstraint.addPrimaryKeyConstraint("column1");15tableConstraint.addNotNullConstraint("column2");16tableConstraint.addUniqueConstraint("column3", "column4");17tableConstraint.addCheckConstraint("column5", "column5 > 0");18tableConstraint.addForeignKeyConstraint("column6", "tableName2", "column7");19TableConstraint tableConstraint = new TableConstraint("tableName");20tableConstraint.addPrimaryKeyConstraint("column1");21tableConstraint.addNotNullConstraint("column2");22tableConstraint.addUniqueConstraint("column3", "column4");23tableConstraint.addCheckConstraint("column5", "column5 > 0");24tableConstraint.addForeignKeyConstraint("column6", "tableName2", "column7");
TableConstraint
Using AI Code Generation
1TableConstraint tc = new TableConstraint();2String dbPath = "jdbc:h2:mem:test;MODE=MYSQL;DB_CLOSE_DELAY=-1";3String dbDriver = "org.h2.Driver";4String tableName = "table_name";5String primaryKey = "id";6String foreignKey = "id";7String foreignKeyTable = "foreign_table";8String foreignKeyColumn = "id";9String foreignKeyTable2 = "foreign_table2";10String foreignKeyColumn2 = "id";11String foreignKeyTable3 = "foreign_table3";12String foreignKeyColumn3 = "id";13String foreignKeyTable4 = "foreign_table4";14String foreignKeyColumn4 = "id";15String foreignKeyTable5 = "foreign_table5";16String foreignKeyColumn5 = "id";17String foreignKeyTable6 = "foreign_table6";18String foreignKeyColumn6 = "id";19String foreignKeyTable7 = "foreign_table7";20String foreignKeyColumn7 = "id";21String foreignKeyTable8 = "foreign_table8";22String foreignKeyColumn8 = "id";23String foreignKeyTable9 = "foreign_table9";24String foreignKeyColumn9 = "id";
TableConstraint
Using AI Code Generation
1 private List<TableConstraint> generateTableConstraints(List<List<String>> rows, Table table) {2 List<TableConstraint> tableConstraints = new ArrayList<>();3 Map<String, List<String>> columnValues = new HashMap<>();4 for (Column column : table.getColumns()) {5 List<String> values = new ArrayList<>();6 columnValues.put(column.getName(), values);7 }8 for (List<String> row : rows) {9 int i = 0;10 for (Column column : table.getColumns()) {11 columnValues.get(column.getName()).add(row.get(i));12 i++;13 }14 }15 for (Column column : table.getColumns()) {16 TableConstraint tableConstraint = new TableConstraint(column.getName());17 tableConstraint.setConstraints(TableConstraint.getTableConstraint(columnValues.get(column.getName())));18 tableConstraints.add(tableConstraint);19 }20 return tableConstraints;21 }22 public List<DatabaseConstraint> generateConstraints() {23 List<DatabaseConstraint> databaseConstraints = new ArrayList<>();24 for (Table table : database.getTables()) {25 List<List<String>> rows = database.getRows(table);26 if (rows.size() > 0) {27 DatabaseConstraint databaseConstraint = new DatabaseConstraint(table.getName());28 databaseConstraint.setTableConstraints(generateTableConstraints(rows, table));29 databaseConstraints.add(databaseConstraint);30 }31 }32 return databaseConstraints;33 }34 package org.evomaster.dbconstraint;35 import java.util.ArrayList;36 import java.util.List;37 public class TableConstraint {38 private String column;39 private List<Constraint> constraints;40 public TableConstraint(String column) {41 this.column = column;42 this.constraints = new ArrayList<>();43 }44 public String getColumn() {45 return column;46 }47 public void setColumn(String column) {48 this.column = column;49 }50 public List<Constraint> getConstraints() {51 return constraints;52 }53 public void setConstraints(List<Constraint> constraints) {54 this.constraints = constraints;55 }
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!!