Best EvoMaster code snippet using org.evomaster.client.java.controller.db.SqlScriptRunnerTest.getConnection
Source:SqlScriptRunnerTest.java
...20import static org.junit.jupiter.api.Assertions.*;21public class SqlScriptRunnerTest extends DatabaseH2TestInit implements DatabaseTestTemplate {22 @Test23 public void testLargeString() throws Exception{24 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(x CLOB);");25 char[] buffer = new char[1000];26 Arrays.fill(buffer, '0');27 String value = "bar" + new String(buffer) + "foo";28 String sql = "INSERT INTO Foo (x) VALUES ('" + value + "')";29 executeViaRest(sql);30 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");31 assertEquals(1, res.seeRows().size());32 Object x = res.seeRows().get(0).getValueByName("x");33 assertTrue(x instanceof String);34 assertEquals(value, x);35 }36 @Test37 public void testSimpleRemoteExecution() throws Exception {38 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(x INT);");39 int value = 42;40 String sql = "INSERT INTO Foo (x) VALUES (" + value + ")";41 executeViaRest(sql);42 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");43 assertEquals(1, res.seeRows().size());44 assertEquals(value, res.seeRows().get(0).getValueByName("x"));45 }46 @Test47 public void testInsertWhenIdentity() throws Exception {48 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(" +49 " id bigint generated by default as identity " +50 ", x integer " +51 ");"52 );53 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");54 assertEquals(0, res.seeRows().size());55 int value = 42;56 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (" + value + ")");57 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");58 assertEquals(1, res.seeRows().size());59 assertEquals(value, res.seeRows().get(0).getValueByName("x"));60 assertNotNull(res.seeRows().get(0).getValueByName("id"));61 }62 @Test63 public void testTwoInsertionsWhenIdentity() throws Exception {64 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(" +65 " id bigint generated by default as identity " +66 ", x integer " +67 ");"68 );69 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");70 assertEquals(0, res.seeRows().size());71 int a = 42;72 int b = 66;73 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (" + a + ")");74 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (" + b + ")");75 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");76 assertEquals(2, res.seeRows().size());77 assertTrue(res.seeRows().stream().anyMatch(r -> r.getValueByName("x").equals(a)));78 assertTrue(res.seeRows().stream().anyMatch(r -> r.getValueByName("x").equals(b)));79 assertEquals(2, res.seeRows().stream().map(r -> r.getValueByName("id")).distinct().count());80 }81 @Test82 public void testInsertWhenForeignKey() throws Exception {83 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(" +84 " id bigint generated by default as identity " +85 ", barId bigint not null " +86 ");" +87 " CREATE TABLE Bar(id bigint generated by default as identity);" +88 " ALTER TABLE Foo add constraint barIdKey foreign key (barId) references Bar;\n"89 );90 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Bar;");91 assertEquals(0, res.seeRows().size());92 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Bar () VALUES ()");93 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Bar;");94 assertEquals(1, res.seeRows().size());95 long id = (Long) res.seeRows().get(0).getValueByName("id");96 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (barId) VALUES (" + id + ")");97 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");98 assertEquals(1, res.seeRows().size());99 assertThrows(Exception.class, () ->100 //wrong foreign key101 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (barId) VALUES (-20)"));102 }103 @Test104 public void testIdentityExtractGeneratedKey() throws Exception {105 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(" +106 " id bigint generated by default as identity " +107 ", barId bigint not null " +108 ", primary key (id) " +109 ");" +110 " CREATE TABLE Bar(" +111 " id bigint generated by default as identity " +112 ", x integer " +113 ", primary key (id));" +114 " ALTER TABLE Foo add constraint barIdKey foreign key (barId) references Bar;\n"115 );116 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Bar;");117 assertEquals(0, res.seeRows().size());118 Long a = SqlScriptRunner.execInsert(getConnection(), "INSERT INTO Bar (id,x) VALUES (default,42);");119 Long b = SqlScriptRunner.execInsert(getConnection(), "INSERT INTO Bar (x) VALUES (66);");120 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Bar;");121 assertEquals(2, res.seeRows().size());122 assertNotNull(a);123 assertNotNull(b);124 assertNotEquals(a, b);125 SqlScriptRunner.execInsert(getConnection(), "INSERT INTO Foo (barId) VALUES (" + a + ")");126 SqlScriptRunner.execInsert(getConnection(), "INSERT INTO Foo (barId) VALUES (" + b + ")");127 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");128 assertEquals(2, res.seeRows().size());129 }130 @Test131 public void testInsertionListWithGeneratedKeys() throws Exception {132 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(" +133 " id bigint generated by default as identity " +134 ", barId bigint not null " +135 ", primary key (id) " +136 ");" +137 " CREATE TABLE Bar(" +138 " id bigint generated by default as identity " +139 ", x integer " +140 ", primary key (id));" +141 " ALTER TABLE Foo add constraint barIdKey foreign key (barId) references Bar;\n"142 );143 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Bar;");144 assertEquals(0, res.seeRows().size());145 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");146 assertEquals(0, res.seeRows().size());147 List<InsertionDto> insertions = sql()148 .insertInto("Bar", 0L).d("id", "default").d("x", "42").and()149 .insertInto("Bar", 1L).d("id", "default").d("x", "66").and()150 .insertInto("Foo").r("barId", 0).and()151 .insertInto("Foo").r("barId", 1).dtos();152 SqlScriptRunner.execInsert(getConnection(), insertions);153 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Bar;");154 assertEquals(2, res.seeRows().size());155 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");156 assertEquals(2, res.seeRows().size());157 }158 @Test159 public void testTimeStamp() throws Exception {160 SqlScriptRunner.execCommand(getConnection(), "create table Foo (" +161 "id bigint generated by default as identity, " +162 "creation_time timestamp not null, " +163 "primary key (id))");164 String year = "2030";165 String timestamp = year + "-2-17T4:55:50.000Z";166 String sql = "INSERT INTO Foo (CREATION_TIME) VALUES ('" + timestamp + "')";167 SqlScriptRunner.execCommand(getConnection(), sql);168 executeViaRest(sql);169 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");170 assertEquals(2, res.seeRows().size());171 assertTrue(res.seeRows().get(0).getValueByName("creation_time").toString().contains(year));172 assertTrue(res.seeRows().get(1).getValueByName("creation_time").toString().contains(year));173 }174 @Test175 public void testVarchar() throws Exception {176 SqlScriptRunner.execCommand(getConnection(), "create table Foo (" +177 "id bigint generated by default as identity, " +178 "name varchar(255) not null, " +179 "primary key (id))");180 String name = "a name";181 String sql = "INSERT INTO Foo (NAME) VALUES ('" + name + "')";182 SqlScriptRunner.execCommand(getConnection(), sql);183 executeViaRest(sql);184 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");185 assertEquals(2, res.seeRows().size());186 assertEquals(name, res.seeRows().get(0).getValueByName("name"));187 assertEquals(name, res.seeRows().get(1).getValueByName("name"));188 }189 private void executeViaRest(String sql) {190 DatabaseCommandDto dto = new DatabaseCommandDto();191 dto.command = sql;192 InstrumentedSutStarter starter = getInstrumentedSutStarter();193 String url = start(starter);194 given().contentType(ContentType.JSON)195 .body(dto)196 .post(url + BASE_PATH + DATABASE_COMMAND)197 .then()198 .statusCode(200);199 }200 @Test201 public void testStringGeneWithApostrophe() throws Exception {202 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(" +203 " name VARCHAR(255) " +204 ");"205 );206 List<InsertionDto> insertions = sql()207 .insertInto("Foo", 0L).d("name", "\"'\"").dtos();208 SqlScriptRunner.execInsert(getConnection(), insertions);209 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");210 assertEquals(1, res.seeRows().size());211 }212 @Test213 public void testDoubleIndirectForeignKey() throws Exception {214 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Table1(" +215 " id bigserial not null, " +216 " primary key (id)" +217 ");"218 +219 "CREATE TABLE Table2(" +220 " id int8, " +221 " primary key (id)" +222 ");"223 +224 "CREATE TABLE Table3(" +225 " id int8, " +226 " primary key (id)" +227 ");"228 +229 "alter table Table2 " +230 " add constraint FKTable2 foreign key (id) references Table1;"231 +232 "alter table Table3 " +233 " add constraint FKTable3 foreign key (id) references Table2;"234 );235 List<InsertionDto> insertions = sql()236 .insertInto("Table1", 1000L)237 .and()238 .insertInto("Table2", 1001L).r("Id", 1000L)239 .and()240 .insertInto("Table3", 1002L).r("Id", 1001L).dtos();241 SqlScriptRunner.execInsert(getConnection(), insertions);242 }243 @Test244 public void testNullValue() throws Exception {245 SqlScriptRunner.execCommand(getConnection(), "create table Foo (" +246 "id bigint generated by default as identity, " +247 "creation_time timestamp not null, " +248 "email VARCHAR(255), "+249 "primary key (id))");250 String year = "2030";251 String timestamp = year + "-2-17T4:55:50.000Z";252 String sql = "INSERT INTO Foo (CREATION_TIME, EMAIL) VALUES ('" + timestamp + "', null)";253 SqlScriptRunner.execCommand(getConnection(), sql);254 executeViaRest(sql);255 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT ID,CREATION_TIME, EMAIL FROM Foo;");256 DataRowDto row = res.seeRows().get(0).toDto();257 assertEquals(row.columnData.size(), 3);258 assertEquals(row.columnData.get(2), "NULL");259 }260 @Test261 public void testMultipleInsertWithFk() throws Exception {262 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(" +263 " id bigint generated by default as identity " +264 ", barId bigint not null " +265 ", primary key (id) " +266 ");" +267 " CREATE TABLE Bar(" +268 " id bigint generated by default as identity " +269 ", x integer " +270 ", primary key (id));" +271 " ALTER TABLE Foo add constraint barIdKey foreign key (barId) references Bar;\n"272 );273 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Bar;");274 assertEquals(0, res.seeRows().size());275 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Foo;");276 assertEquals(0, res.seeRows().size());277 List<InsertionDto> insertion1 = sql()278 .insertInto("Bar", 100L).d("id", "default").d("x", "42").and()279 .insertInto("Bar", 101L).d("id", "default").d("x", "66").dtos();280 Map<Long, Long> map = SqlScriptRunner.execInsert(getConnection(), insertion1).idMapping;281 List<InsertionDto> insertion2 = sql()282 .insertInto("Foo").d("barId", map.get(100L).toString()).and()283 .insertInto("Foo").d("barId", map.get(101L).toString()).dtos();284 SqlScriptRunner.execInsert(getConnection(), insertion2);285 res = SqlScriptRunner.execCommand(getConnection(), "SELECT * FROM Bar;");286 assertEquals(2, res.seeRows().size());287 }288 @Test289 public void testDoubleAlias() throws Exception{290 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(x INT)");291 String select = "select f.x as y from Foo f where x>0";292 QueryResult res = SqlScriptRunner.execCommand(getConnection(), select);293 assertTrue(res.isEmpty());294 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (42)");295 res = SqlScriptRunner.execCommand(getConnection(), select);296 assertTrue(!res.isEmpty());297 }298 @Test299 public void testBase() throws Exception {300 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(x INT)");301 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "select * from Foo");302 assertTrue(res.isEmpty());303 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (4)");304 res = SqlScriptRunner.execCommand(getConnection(), "select * from Foo");305 assertFalse(res.isEmpty());306 }307 @Test308 public void testParentheses() throws Exception{309 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(x INT)");310 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (5)");311 QueryResult res = SqlScriptRunner.execCommand(getConnection(), "select * from Foo where x = (5)");312 assertFalse(res.isEmpty());313 }314 @Test315 public void testConstants() throws Exception {316 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(x INT)");317 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (4)");318 String select = "select x, 1 as y, null as z, 'bar' as w from Foo";319 QueryResult res = SqlScriptRunner.execCommand(getConnection(), select);320 assertFalse(res.isEmpty());321 DataRow row = res.seeRows().get(0);322 assertEquals(4, row.getValue(0));323 assertEquals(1, row.getValue(1));324 assertEquals(null, row.getValue(2));325 assertEquals("bar", row.getValue(3));326 }327 @Test328 public void testNested() throws Exception{329 String select = "select t.a, t.b from (select x as a, 1 as b from Foo where x<10) t where a>3";330 SqlScriptRunner.execCommand(getConnection(), "CREATE TABLE Foo(x INT)");331 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (1)");332 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (4)");333 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (7)");334 SqlScriptRunner.execCommand(getConnection(), "INSERT INTO Foo (x) VALUES (20)");335 QueryResult res = SqlScriptRunner.execCommand(getConnection(), select);336 assertEquals(2, res.size());337 }338 @Override339 public Connection getConnection() {340 return connection;341 }342 @Override343 public SutController getSutController() {344 return new DatabaseFakeH2SutController(connection);345 }346}...
getConnection
Using AI Code Generation
1import org.evomaster.client.java.controller.api.dto.database.operations.DatabaseCommandDto;2import org.evomaster.client.java.controller.api.dto.database.operations.QueryDto;3import org.evomaster.client.java.controller.api.dto.database.operations.SqlScriptResultDto;4import org.evomaster.client.java.controller.db.SqlScriptRunner;5import org.evomaster.client.java.controller.db.SqlScriptRunnerTest;6import org.evomaster.client.java.controller.problem.ProblemInfo;7import org.evomaster.client.java.controller.problem.RestProblem;8import org.evomaster.client.java.controller.problem.RestProblemException;9import org.evomaster.client.java.controller.problem.RestProblemFactory;10import org.evomaster.client.java.controller.problem.RestProblemHandling;11import org.evomaster.client.java.controller.problem.RestProblemHandlingTest;12import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$2090751604;13import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$313482264;14import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506518;15import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506519;16import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506520;17import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506521;18import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506522;19import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506523;20import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506524;21import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506525;22import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506526;23import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506527;24import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506528;25import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506529;26import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$MockitoMock$830506530;27import org.evomaster.client.java.controller.problem.RestProblemHandlingTest$
getConnection
Using AI Code Generation
1Connection connection = SqlScriptRunnerTest.getConnection();2SqlScriptRunnerTest sqlScriptRunnerTest = new SqlScriptRunnerTest();3sqlScriptRunnerTest.execute(connection, "sql script");4sqlScriptRunnerTest.execute(connection, "sql script");5sqlScriptRunnerTest.execute(connection, "sql script");6sqlScriptRunnerTest.execute(connection, "sql script");7sqlScriptRunnerTest.execute(connection, "sql script");8sqlScriptRunnerTest.execute(connection, "sql script");9sqlScriptRunnerTest.execute(connection, "sql script");10sqlScriptRunnerTest.execute(connection, "sql script");11sqlScriptRunnerTest.execute(connection, "sql script");12sqlScriptRunnerTest.execute(connection, "sql script");13sqlScriptRunnerTest.execute(connection, "sql script");
getConnection
Using AI Code Generation
1public void getConnection() throws SQLException {2 SqlScriptRunner sqlScriptRunner = new SqlScriptRunner();3 sqlScriptRunner.getConnection();4}5public void executeUpdate() throws SQLException {6 SqlScriptRunner sqlScriptRunner = new SqlScriptRunner();7 sqlScriptRunner.executeUpdate("CREATE TABLE IF NOT EXISTS TEST (ID INT PRIMARY KEY, NAME VARCHAR(255))");8}9public void execute() throws SQLException {10 SqlScriptRunner sqlScriptRunner = new SqlScriptRunner();11 sqlScriptRunner.execute("CREATE TABLE IF NOT EXISTS TEST (ID INT PRIMARY KEY, NAME VARCHAR(255))");12}13public void executeQuery() throws SQLException {14 SqlScriptRunner sqlScriptRunner = new SqlScriptRunner();15 sqlScriptRunner.executeQuery("SELECT * FROM TEST");16}17public void close() throws SQLException {18 SqlScriptRunner sqlScriptRunner = new SqlScriptRunner();19 sqlScriptRunner.close();20}21public void getGeneratedKeys() throws SQLException {22 SqlScriptRunner sqlScriptRunner = new SqlScriptRunner();23 sqlScriptRunner.getGeneratedKeys();24}25public void getGeneratedKeys2() throws SQLException {26 SqlScriptRunner sqlScriptRunner = new SqlScriptRunner();27 sqlScriptRunner.getGeneratedKeys();28}29public void getGeneratedKeys3() throws SQLException {30 SqlScriptRunner sqlScriptRunner = new SqlScriptRunner();
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!!