Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.constraint.PostgresConstraintExtractor.getDbTableUniqueConstraint
Source:PostgresConstraintExtractor.java
...25 */26 private static void cannotHandle(String constraintType) {27 SimpleLogger.uniqueWarn("WARNING, EvoMaster cannot extract Postgres constraints with type '" + constraintType);28 }29 private static DbTableUniqueConstraint getDbTableUniqueConstraint(Connection connectionToPostgres, String tableSchema, String tableName, Integer[] columnIds) throws SQLException {30 List<String> uniqueColumnNames = new ArrayList<>();31 for (int columnId : columnIds) {32 String qry = String.format("SELECT att.* " +33 " FROM pg_catalog.pg_attribute att " +34 " INNER JOIN pg_catalog.pg_class rel\n " +35 " ON rel.oid = att.attrelid\n " +36 " INNER JOIN pg_catalog.pg_namespace nsp\n " +37 " ON nsp.oid = rel.relnamespace\n " +38 " WHERE nsp.nspname = '%s'\n" +39 " AND rel.relname = '%s' \n" +40 " AND att.attnum = %s;", tableSchema, tableName, columnId);41 try (Statement stmt = connectionToPostgres.createStatement()) {42 try (ResultSet rs = stmt.executeQuery(qry)) {43 boolean hasRows = rs.next();44 if (!hasRows) {45 throw new IllegalStateException("Unexpected missing pg_catalog.pg_attribute data");46 }47 String uniqueColumnName = rs.getString("attname");48 uniqueColumnNames.add(uniqueColumnName);49 }50 }51 }52 return new DbTableUniqueConstraint(tableName, uniqueColumnNames);53 }54 public List<DbTableConstraint> extract(Connection connectionToPostgres, DbSchemaDto schemaDto) throws SQLException {55 String tableSchema = schemaDto.name;56 List<DbTableConstraint> constraints = new ArrayList<>();57 for (TableDto tableDto : schemaDto.tables) {58 try (Statement statement = connectionToPostgres.createStatement()) {59 String tableName = tableDto.name;60 String query = String.format("SELECT con.*\n" +61 " FROM pg_catalog.pg_constraint con\n" +62 " INNER JOIN pg_catalog.pg_class rel\n" +63 " ON rel.oid = con.conrelid\n" +64 " INNER JOIN pg_catalog.pg_namespace nsp\n" +65 " ON nsp.oid = connamespace\n" +66 " WHERE nsp.nspname = '%s'\n" +67 " AND rel.relname = '%s';", tableSchema, tableName);68 try (ResultSet columns = statement.executeQuery(query)) {69 while (columns.next()) {70 String checkConstraint = columns.getString(CONSRC);71 Array array = columns.getArray(CONKEY);72 String constraintType = columns.getString(CONTYPE);73 DbTableConstraint constraint;74 switch (constraintType) {75 case CONSTRAINT_TYPE_CHECK:76 constraint = new DbTableCheckExpression(tableName, checkConstraint);77 constraints.add(constraint);78 break;79 case CONSTRAINT_TYPE_UNIQUE:80 Integer[] uniqueColumnIds = (Integer[]) array.getArray();81 constraint = getDbTableUniqueConstraint(connectionToPostgres, tableSchema, tableName, uniqueColumnIds);82 constraints.add(constraint);83 break;84 case CONSTRAINT_TYPE_FOREIGN_KEY:85 case CONSTRAINT_TYPE_PRIMARY_KEY:86 /**87 * These types of constraints are already handled by88 * JDBC Metadata89 **/90 break;91 case CONSTRAINT_TYPE_TRIGGER:92 cannotHandle("TRIGGER CONSTRAINT");93 break;94 case CONSTRAINT_TYPE_EXCLUSION:95 cannotHandle("EXCLUSION CONSTRAINT");...
getDbTableUniqueConstraint
Using AI Code Generation
1public class ConstraintExtractorTest {2 public void testGetDbTableUniqueConstraint() {3 try {4 PostgresConstraintExtractor constraintExtractor = new PostgresConstraintExtractor();5 String sql = "SELECT tc.constraint_name, kc.column_name, kc.ordinal_position, tc.table_name, tc.constraint_type, kc.table_schema, kc.table_name FROM information_schema.table_constraints tc JOIN information_schema.key_column_usage kc ON kc.table_name = tc.table_name AND kc.table_schema = tc.table_schema AND kc.constraint_name = tc.constraint_name WHERE tc.constraint_type = 'UNIQUE' AND kc.table_schema = 'public' AND kc.table_name = 'table_name'";6 List<DbTableUniqueConstraint> actual = constraintExtractor.getDbTableUniqueConstraint(sql, "table_name");7 List<DbTableUniqueConstraint> expected = new ArrayList<>();8 expected.add(new DbTableUniqueConstraint("constraint_name", "column_name", 1, "table_name", "UNIQUE", "public", "table_name"));9 assertEquals(expected, actual);10 } catch (SQLException e) {11 e.printStackTrace();12 }13 }14}15public class ConstraintExtractorTest {16 public void testGetDbTableForeignKeyConstraint() {17 try {18 PostgresConstraintExtractor constraintExtractor = new PostgresConstraintExtractor();19 String sql = "SELECT tc.constraint_name, kc.column_name, kc.ordinal_position, tc.table_name, tc.constraint_type, kc.table_schema, kc.table_name FROM information_schema.table_constraints tc JOIN information_schema.key_column_usage kc ON kc.table_name = tc.table_name AND kc.table_schema = tc.table_schema AND kc.constraint_name = tc.constraint_name WHERE tc.constraint_type = 'FOREIGN KEY' AND kc.table_schema = 'public' AND kc.table_name = 'table_name'";20 List<DbTableForeignKeyConstraint> actual = constraintExtractor.getDbTableForeignKeyConstraint(sql, "table_name");21 List<DbTableForeignKeyConstraint> expected = new ArrayList<>();22 expected.add(new DbTableForeignKeyConstraint("constraint_name", "column_name", 1, "table_name", "FOREIGN KEY", "public", "table_name"));23 assertEquals(expected, actual);24 } catch (SQLException e) {25 e.printStackTrace();26 }27 }28}
getDbTableUniqueConstraint
Using AI Code Generation
1public class UniqueConstraintExample {2 public static void main(String[] args) {3 List<TableUniqueConstraint> constraints = PostgresConstraintExtractor.getDbTableUniqueConstraint(connection, "table_name");4 for (TableUniqueConstraint constraint : constraints) {5 System.out.println(constraint);6 }7 }8}9public class ForeignKeyConstraintExample {10 public static void main(String[] args) {11 List<TableForeignKeyConstraint> constraints = PostgresConstraintExtractor.getDbTableForeignKeyConstraint(connection, "table_name");12 for (TableForeignKeyConstraint constraint : constraints) {13 System.out.println(constraint);14 }15 }16}17public class CheckConstraintExample {18 public static void main(String[] args) {19 List<TableCheckConstraint> constraints = PostgresConstraintExtractor.getDbTableCheckConstraint(connection, "table_name");20 for (TableCheckConstraint constraint : constraints) {21 System.out.println(constraint);22 }23 }24}
Check out the latest blogs from LambdaTest on this topic:
I was once asked at a testing summit, “How do you manage a QA team using scrum?” After some consideration, I realized it would make a good article, so here I am. Understand that the idea behind developing software in a scrum environment is for development teams to self-organize.
When it comes to UI components, there are two versatile methods that we can use to build it for your website: either we can use prebuilt components from a well-known library or framework, or we can develop our UI components from scratch.
Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.
QA testers have a unique role and responsibility to serve the customer. Serving the customer in software testing means protecting customers from application defects, failures, and perceived failures from missing or misunderstood requirements. Testing for known requirements based on documentation or discussion is the core of the testing profession. One unique way QA testers can both differentiate themselves and be innovative occurs when senseshaping is used to improve the application user experience.
Joseph, who has been working as a Quality Engineer, was assigned to perform web automation for the company’s website.
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!!