Best EvoMaster code snippet using org.evomaster.client.java.controller.internal.db.h2.H2SchemaExtractorTest.getConnection
Source:H2SchemaExtractorTest.java
...15import static org.junit.jupiter.api.Assertions.*;16public class H2SchemaExtractorTest extends DatabaseH2TestInit implements DatabaseTestTemplate {17 @Test18 public void testBasic() throws Exception {19 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(x INT)");20 DbSchemaDto schema = SchemaExtractor.extract(getConnection());21 assertNotNull(schema);22 assertAll(() -> assertEquals("public", schema.name.toLowerCase()),23 () -> assertEquals(DatabaseType.H2, schema.databaseType),24 () -> assertEquals(1, schema.tables.size()),25 () -> assertEquals("foo", schema.tables.get(0).name.toLowerCase()),26 () -> assertEquals(1, schema.tables.get(0).columns.size())27 );28 }29 @Test30 public void testTwoTables() throws Exception {31 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(x INT); CREATE TABLE Bar(y INT)");32 DbSchemaDto schema = SchemaExtractor.extract(getConnection());33 assertNotNull(schema);34 assertEquals(2, schema.tables.size());35 assertTrue(schema.tables.stream().map(t -> t.name.toLowerCase()).anyMatch(n -> n.equals("foo")));36 assertTrue(schema.tables.stream().map(t -> t.name.toLowerCase()).anyMatch(n -> n.equals("bar")));37 }38 @Test39 public void testIdentity() throws Exception {40 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(" +41 " id bigint generated by default as identity " +42 ", x int" +43 ", primary key (id) " +44 ");");45 DbSchemaDto schema = SchemaExtractor.extract(getConnection());46 TableDto table = schema.tables.get(0);47 assertEquals(2, table.columns.size());48 ColumnDto id = table.columns.stream()49 .filter(c -> c.name.equalsIgnoreCase("id"))50 .findAny().get();51 ColumnDto x = table.columns.stream()52 .filter(c -> c.name.equalsIgnoreCase("x"))53 .findAny().get();54 assertEquals("integer", x.type.toLowerCase());55 assertEquals("bigint", id.type.toLowerCase());56 assertFalse(x.autoIncrement);57 assertTrue(id.autoIncrement);58 }59 @Test60 public void testBasicConstraints() throws Exception {61 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(" +62 " id bigint generated by default as identity " +63 ", name varchar(128) not null " +64 ", surname varchar(255) " +65 ", primary key (id) " +66 ");");67 DbSchemaDto schema = SchemaExtractor.extract(getConnection());68 TableDto table = schema.tables.get(0);69 assertEquals(3, table.columns.size());70 ColumnDto id = table.columns.stream()71 .filter(c -> c.name.equalsIgnoreCase("id"))72 .findAny().get();73 assertTrue(id.autoIncrement);74 ColumnDto name = table.columns.stream()75 .filter(c -> c.name.equalsIgnoreCase("name"))76 .findAny().get();77 assertFalse(name.autoIncrement);78 assertFalse(name.nullable);79 assertEquals(128, name.size);80 ColumnDto surname = table.columns.stream()81 .filter(c -> c.name.equalsIgnoreCase("surname"))82 .findAny().get();83 assertFalse(surname.autoIncrement);84 assertTrue(surname.nullable);85 assertEquals(255, surname.size);86 }87 @Test88 public void testBasicForeignKey() throws Exception {89 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(" +90 " id bigint generated by default as identity " +91 ", barId bigint not null " +92 ");" +93 " CREATE TABLE Bar(id bigint generated by default as identity);" +94 " ALTER TABLE Foo add constraint barIdKey foreign key (barId) references Bar;\n"95 );96 DbSchemaDto schema = SchemaExtractor.extract(getConnection());97 assertEquals(2, schema.tables.size());98 TableDto bar = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Bar")).findAny().get();99 TableDto foo = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();100 assertEquals(0, bar.foreignKeys.size());101 assertEquals(1, foo.foreignKeys.size());102 ForeignKeyDto foreignKey = foo.foreignKeys.get(0);103 assertEquals(1, foreignKey.sourceColumns.size());104 assertTrue(foreignKey.sourceColumns.stream().anyMatch(c -> c.equalsIgnoreCase("barId")));105 assertTrue(foreignKey.targetTable.equalsIgnoreCase("Bar"));106 }107 @Test108 public void testQuizGame() throws Exception {109 SqlScriptRunner.runScriptFromResourceFile(getConnection(), "/db_schemas/quizgame.sql");110 DbSchemaDto schema = SchemaExtractor.extract(getConnection());111 assertEquals(6, schema.tables.size());112 //TODO test all of its content113 }114 @Test115 public void testRetrieveSchema() throws Exception {116 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(x INT); CREATE TABLE Bar(y INT)");117 InstrumentedSutStarter starter = getInstrumentedSutStarter();118 String url = start(starter);119 given().accept(ContentType.JSON)120 .get(url + BASE_PATH + INFO_SUT_PATH)121 .then()122 .statusCode(200)123 .body("data.sqlSchemaDto.tables.size()", is(2));124 }125 @Test126 public void testColumnUpperBoundConstraint() throws Exception {127 String sqlCommand = "CREATE TABLE FOO (fooId INT, age_max integer check (age_max<=100));";128 SqlScriptRunner.execCommand(getConnection(), sqlCommand);129 DbSchemaDto schema = SchemaExtractor.extract(getConnection());130 assertEquals(1, schema.tables.size());131 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();132 assertEquals(2, fooTable.columns.size());133 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("fooId")));134 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("age_max")));135 // TODO check that the column constraint is actually extracted136 ColumnDto columnDto = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("age_max")).findFirst().orElse(null);137 assertEquals("INTEGER", columnDto.type);138 assertEquals(1, fooTable.tableCheckExpressions.size());139 assertEquals("(AGE_MAX <= 100)", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);140 }141 @Test142 public void testTableConstraint() throws Exception {143 String sqlCommand = "CREATE TABLE FOO (fooId INT, age_max integer);"144 + "ALTER TABLE FOO ADD CONSTRAINT CHK_AGE_MAX CHECK (age_max<=100);";145 SqlScriptRunner.execCommand(getConnection(), sqlCommand);146 DbSchemaDto schema = SchemaExtractor.extract(getConnection());147 assertEquals(1, schema.tables.size());148 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();149 assertEquals(2, fooTable.columns.size());150 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("fooId")));151 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("age_max")));152 assertEquals(1, fooTable.tableCheckExpressions.size());153 assertEquals("(AGE_MAX <= 100)", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);154 }155 @Test156 public void testPrimaryKey() throws Exception {157 String sqlCommand = "CREATE TABLE FOO (id INT, "158 + "primary key (id));";159 SqlScriptRunner.execCommand(getConnection(), sqlCommand);160 DbSchemaDto schema = SchemaExtractor.extract(getConnection());161 assertEquals(1, schema.tables.size());162 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();163 assertEquals(1, fooTable.columns.size());164 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("id")));165 assertEquals(true, fooTable.columns.get(0).primaryKey);166 assertEquals(false, fooTable.columns.get(0).unique);167 }168 @Test169 public void testEnumStringConstraint() throws Exception {170 String sqlCommand = "CREATE TABLE FOO (fooId INT, status varchar(1));"171 + "ALTER TABLE FOO ADD CONSTRAINT CHK_STATUS CHECK (status in ('A', 'B'));";172 SqlScriptRunner.execCommand(getConnection(), sqlCommand);173 DbSchemaDto schema = SchemaExtractor.extract(getConnection());174 assertEquals(1, schema.tables.size());175 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();176 assertEquals(2, fooTable.columns.size());177 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("fooId")));178 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("status")));179 ColumnDto statusColumn = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("status")).findFirst().get();180 assertEquals(1, fooTable.tableCheckExpressions.size());181 assertEquals("(STATUS IN('A', 'B'))", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);182 }183 @Test184 public void testEnumBooleanConstraint() throws Exception {185 String sqlCommand = "CREATE TABLE FOO (status BOOLEAN);\n" +186 " ALTER TABLE FOO ADD CONSTRAINT CHK_STATUS CHECK (status in (true, false));";187 SqlScriptRunner.execCommand(getConnection(), sqlCommand);188 DbSchemaDto schema = SchemaExtractor.extract(getConnection());189 assertEquals(1, schema.tables.size());190 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();191 assertEquals(1, fooTable.columns.size());192 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("status")));193 ColumnDto statusColumn = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("status")).findFirst().get();194 assertEquals(1, fooTable.tableCheckExpressions.size());195 assertEquals("(STATUS IN(TRUE, FALSE))", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);196 }197 @Test198 public void testEnumIntegerConstraint() throws Exception {199 String sqlCommand = "CREATE TABLE FOO (status INT);\n" +200 " ALTER TABLE FOO ADD CONSTRAINT CHK_STATUS CHECK (status in (42, 77));";201 SqlScriptRunner.execCommand(getConnection(), sqlCommand);202 DbSchemaDto schema = SchemaExtractor.extract(getConnection());203 assertEquals(1, schema.tables.size());204 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();205 assertEquals(1, fooTable.columns.size());206 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("status")));207 ColumnDto statusColumn = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("status")).findFirst().get();208 assertEquals(1, fooTable.tableCheckExpressions.size());209 assertEquals("(STATUS IN(42, 77))", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);210 }211 @Test212 public void testEnumTinyIntConstraint() throws Exception {213 String sqlCommand = "CREATE TABLE FOO (status TINYINT);\n" +214 " ALTER TABLE FOO ADD CONSTRAINT CHK_STATUS CHECK (status in (42, 77));";215 SqlScriptRunner.execCommand(getConnection(), sqlCommand);216 DbSchemaDto schema = SchemaExtractor.extract(getConnection());217 assertEquals(1, schema.tables.size());218 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();219 assertEquals(1, fooTable.columns.size());220 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("status")));221 ColumnDto statusColumn = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("status")).findFirst().get();222 assertEquals(1, fooTable.tableCheckExpressions.size());223 assertEquals("(STATUS IN(42, 77))", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);224 }225 @Test226 public void testEnumSmallIntConstraint() throws Exception {227 String sqlCommand = "CREATE TABLE FOO (status SMALLINT);\n" +228 " ALTER TABLE FOO ADD CONSTRAINT CHK_STATUS CHECK (status in (42, 77));";229 SqlScriptRunner.execCommand(getConnection(), sqlCommand);230 DbSchemaDto schema = SchemaExtractor.extract(getConnection());231 assertEquals(1, schema.tables.size());232 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();233 assertEquals(1, fooTable.columns.size());234 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("status")));235 ColumnDto statusColumn = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("status")).findFirst().get();236 assertEquals(1, fooTable.tableCheckExpressions.size());237 assertEquals("(STATUS IN(42, 77))", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);238 }239 @Test240 public void testEnumBigIntConstraint() throws Exception {241 String sqlCommand = "CREATE TABLE FOO (status BIGINT);\n" +242 " ALTER TABLE FOO ADD CONSTRAINT CHK_STATUS CHECK (status in (42, 77));";243 SqlScriptRunner.execCommand(getConnection(), sqlCommand);244 DbSchemaDto schema = SchemaExtractor.extract(getConnection());245 assertEquals(1, schema.tables.size());246 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();247 assertEquals(1, fooTable.columns.size());248 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("status")));249 ColumnDto statusColumn = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("status")).findFirst().get();250 assertEquals(1, fooTable.tableCheckExpressions.size());251 assertEquals("(STATUS IN(42, 77))", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);252 }253 @Test254 public void testEnumDoubleConstraint() throws Exception {255 String sqlCommand = "CREATE TABLE FOO (status DOUBLE);\n" +256 " ALTER TABLE FOO ADD CONSTRAINT CHK_STATUS CHECK (status in (1.0, 2.5));";257 SqlScriptRunner.execCommand(getConnection(), sqlCommand);258 DbSchemaDto schema = SchemaExtractor.extract(getConnection());259 assertEquals(1, schema.tables.size());260 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();261 assertEquals(1, fooTable.columns.size());262 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("status")));263 ColumnDto statusColumn = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("status")).findFirst().get();264 assertEquals(1, fooTable.tableCheckExpressions.size());265 assertEquals("(STATUS IN(1.0, 2.5))", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);266 }267 @Test268 public void testEnumRealConstraint() throws Exception {269 String sqlCommand = "CREATE TABLE FOO (status REAL);\n" +270 " ALTER TABLE FOO ADD CONSTRAINT CHK_STATUS CHECK (status in (1.0, 2.5));";271 SqlScriptRunner.execCommand(getConnection(), sqlCommand);272 DbSchemaDto schema = SchemaExtractor.extract(getConnection());273 assertEquals(1, schema.tables.size());274 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();275 assertEquals(1, fooTable.columns.size());276 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("status")));277 ColumnDto statusColumn = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("status")).findFirst().get();278 assertEquals(1, fooTable.tableCheckExpressions.size());279 assertEquals("(STATUS IN(1.0, 2.5))", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);280 }281 @Test282 public void testEnumDecimalConstraint() throws Exception {283 String sqlCommand = "CREATE TABLE FOO (status DECIMAL);\n" +284 " ALTER TABLE FOO ADD CONSTRAINT CHK_STATUS CHECK (status in (1.0, 2.5));";285 SqlScriptRunner.execCommand(getConnection(), sqlCommand);286 DbSchemaDto schema = SchemaExtractor.extract(getConnection());287 assertEquals(1, schema.tables.size());288 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();289 assertEquals(1, fooTable.columns.size());290 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("status")));291 ColumnDto statusColumn = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("status")).findFirst().get();292 assertEquals(1, fooTable.tableCheckExpressions.size());293 assertEquals("(STATUS IN(1.0, 2.5))", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);294 }295 @Test296 public void testEnumCharConstraint() throws Exception {297 String sqlCommand = "CREATE TABLE FOO (status CHAR);"298 + "ALTER TABLE FOO ADD CONSTRAINT CHK_STATUS CHECK (status in ('A', 'B'));";299 SqlScriptRunner.execCommand(getConnection(), sqlCommand);300 DbSchemaDto schema = SchemaExtractor.extract(getConnection());301 assertEquals(1, schema.tables.size());302 TableDto fooTable = schema.tables.stream().filter(t -> t.name.equalsIgnoreCase("Foo")).findAny().get();303 assertEquals(1, fooTable.columns.size());304 assertTrue(fooTable.columns.stream().anyMatch(c -> c.name.equalsIgnoreCase("status")));305 ColumnDto statusColumn = fooTable.columns.stream().filter(c -> c.name.equalsIgnoreCase("status")).findFirst().get();306 assertEquals(1, fooTable.tableCheckExpressions.size());307 assertEquals("(STATUS IN('A', 'B'))", fooTable.tableCheckExpressions.get(0).sqlCheckExpression);308 }309 @Override310 public Connection getConnection() {311 return connection;312 }313 @Override314 public SutController getSutController() {315 return new DatabaseFakeH2SutController(connection);316 }317}...
getConnection
Using AI Code Generation
1Connection connection = H2SchemaExtractor.getConnection();2String schema = H2SchemaExtractor.getSchema(connection);3List<String> tableNames = H2SchemaExtractor.getTableNames(connection);4List<String> primaryKeys = H2SchemaExtractor.getPrimaryKeys(connection, tableNames.get(0));5List<String> columnNames = H2SchemaExtractor.getColumnNames(connection, tableNames.get(0));6List<String> columnTypes = H2SchemaExtractor.getColumnTypes(connection, tableNames.get(0));7List<Integer> columnSizes = H2SchemaExtractor.getColumnSizes(connection, tableNames.get(0));8List<String> columnNullability = H2SchemaExtractor.getColumnNullability(connection, tableNames.get(0));9List<String> columnDefaultValues = H2SchemaExtractor.getColumnDefaultValues(connection, tableNames.get(0));10List<String> columnAutoincrementValues = H2SchemaExtractor.getColumnAutoincrementValues(connection, tableNames.get(0));11List<String> columnForeignKeyValues = H2SchemaExtractor.getColumnForeignKeyValues(connection, tableNames.get(0));12List<String> columnForeignKeyValues = H2SchemaExtractor.getColumnForeignKeyValues(connection, tableNames.get(0));13List<String> columnForeignKeyTableValues = H2SchemaExtractor.getColumnForeignKeyTableValues(connection, tableNames.get(0));14List<String> columnForeignKeyTableValues = H2SchemaExtractor.getColumnForeignKeyTableValues(connection, tableNames.get(0));15List<String> columnForeignKeyTableValues = H2SchemaExtractor.getColumnForeignKeyTableValues(connection, tableNames.get(0));16List<String> columnForeignKeyTableValues = H2SchemaExtractor.getColumnForeignKeyTableValues(connection, tableNames.get(0));17List<String> columnForeignKeyTableValues = H2SchemaExtractor.getColumnForeignKeyTableValues(connection, tableNames.get(0));18List<String> columnForeignKeyTableValues = H2SchemaExtractor.getColumnForeignKeyTableValues(connection,
getConnection
Using AI Code Generation
1public Connection getConnection() throws SQLException {2 return DriverManager.getConnection("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", "");3}4public void testGetConnection() throws SQLException {5 H2SchemaExtractorTest h2SchemaExtractorTest0 = new H2SchemaExtractorTest();6 Connection connection0 = h2SchemaExtractorTest0.getConnection();7 assertNotNull(connection0);8}9public void testGetConnection_0_0() throws SQLException {10 H2SchemaExtractorTest h2SchemaExtractorTest0 = new H2SchemaExtractorTest();11 Connection connection0 = h2SchemaExtractorTest0.getConnection();12 assertNotNull(connection0);13 assertNotNull(connection0);14}15[INFO] --- evomaster-client-java:0.95.0-SNAPSHOT:generate-tests (default) @ evomaster-client-java ---
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!!