How to use cleanDataInTables method of org.evomaster.client.java.controller.db.DbCleaner class

Best EvoMaster code snippet using org.evomaster.client.java.controller.db.DbCleaner.cleanDataInTables

Source:DbCleaner.java Github

copy

Full Screen

...36 For H2, we have to delete tables one at a time... but, to avoid issues37 with FKs, we must temporarily disable the integrity checks38 */39 disableReferentialIntegrity(statement, type);40 cleanDataInTables(tablesToSkip, statement, type, schemaName, isSingleCleanCommand(type), doDropTable);41 resetSequences(statement, type, schemaName);42 enableReferentialIntegrity(statement, type);43 statement.close();44 } catch (Exception e) {45 /*46 this could happen if there is a current transaction with a lock on any table.47 We could check the content of INFORMATION_SCHEMA.LOCKS, or simply look at error message48 */49 String msg = e.getMessage();50 if(msg != null && msg.toLowerCase().contains("timeout")){51 if(retries > 0) {52 SimpleLogger.warn("Timeout issue with cleaning DB. Trying again.");53 //let's just wait a bit, and retry54 try {55 Thread.sleep(2000);56 } catch (InterruptedException interruptedException) {57 }58 retries--;59 clearDatabase(retries, connection, schemaName, tablesToSkip, type, doDropTable);60 } else {61 SimpleLogger.error("Giving up cleaning the DB. There are still timeouts.");62 }63 }64 throw new RuntimeException(e);65 }66 }67 public static void clearDatabase(Connection connection, List<String> tablesToSkip, DatabaseType type){68 clearDatabase(connection, getDefaultSchema(type), tablesToSkip, type);69 }70 public static void clearDatabase(Connection connection, String schemaName, List<String> tablesToSkip, DatabaseType type){71 clearDatabase(getDefaultReties(type), connection, schemaName, tablesToSkip, type, false);72 }73 public static void dropDatabaseTables(Connection connection, String schemaName, List<String> tablesToSkip, DatabaseType type){74 if (type != DatabaseType.MYSQL && type != DatabaseType.MARIADB)75 throw new IllegalArgumentException("Dropping tables are not supported by "+type);76 clearDatabase(getDefaultReties(type), connection, schemaName, tablesToSkip, type, true);77 }78 public static void clearDatabase_Postgres(Connection connection, String schemaName, List<String> tablesToSkip ) {79 clearDatabase(getDefaultReties(DatabaseType.POSTGRES), connection, schemaName, tablesToSkip, DatabaseType.POSTGRES, false);80 }81 /**82 *83 * @param tablesToSkip are tables to be skipped84 * @param statement is to execute the SQL command85 * @param schema specify the schema of data to clean. if [schema] is empty, all data will be cleaned.86 * @param singleCommand specify whether to execute the SQL commands (e.g., truncate table/tables) by single command87 * @param doDropTable specify whether to drop tables which is only for MySQL and MariaDB now.88 * @throws SQLException are exceptions during sql execution89 */90 private static void cleanDataInTables(List<String> tablesToSkip, Statement statement, DatabaseType type, String schema, boolean singleCommand, boolean doDropTable) throws SQLException {91 // Find all tables and truncate them92 Set<String> tables = new HashSet<>();93 ResultSet rs = statement.executeQuery(getAllTableCommand(type, schema));94 while (rs.next()) {95 tables.add(rs.getString(1));96 }97 rs.close();98 if (tables.isEmpty()) {99 throw new IllegalStateException("Could not find any table");100 }101 if (tablesToSkip != null) {102 for (String skip : tablesToSkip) {103 if (!tables.stream().anyMatch(t -> t.equalsIgnoreCase(skip))) {104 String msg = "Asked to skip table '" + skip + "', but it does not exist.";...

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