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

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

Source:SqlScriptRunnerTest.java Github

copy

Full Screen

...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}...

Full Screen

Full Screen

QueryResult

Using AI Code Generation

copy

Full Screen

1org.evomaster.client.java.controller.db.QueryResult queryResult = org.evomaster.client.java.controller.db.QueryResult.builder()2 .withColumnNames(Arrays.asList("id", "name", "email"))3 .withDataRows(Arrays.asList(4 Arrays.asList("1", "name1", "email1"),5 Arrays.asList("2", "name2", "email2"),6 Arrays.asList("3", "name3", "email3"),7 Arrays.asList("4", "name4", "email4"),8 Arrays.asList("5", "name5", "email5")9 .build();10org.evomaster.client.java.controller.db.QueryResult queryResult = org.evomaster.client.java.controller.db.QueryResult.builder()11 .withColumnNames(Arrays.asList("id", "name", "email"))12 .withDataRows(Arrays.asList(13 Arrays.asList("1", "name1", "email1"),14 Arrays.asList("2", "name2", "email2"),15 Arrays.asList("3", "name3", "email3"),16 Arrays.asList("4", "name4", "email4"),17 Arrays.asList("5", "name5", "email5")18 .build();19org.evomaster.client.java.controller.db.QueryResult queryResult = org.evomaster.client.java.controller.db.QueryResult.builder()20 .withColumnNames(Arrays.asList("id", "name", "email"))21 .withDataRows(Arrays.asList(22 Arrays.asList("1", "name1", "email1"),23 Arrays.asList("2", "name2", "email2"),24 Arrays.asList("3", "name3", "email3"),25 Arrays.asList("4", "name4", "email4"),26 Arrays.asList("5", "name5", "email5")27 .build();28org.evomaster.client.java.controller.db.QueryResult queryResult = org.evomaster.client.java.controller.db.QueryResult.builder()29 .withColumnNames(Arrays.asList("id", "name", "email"))30 .withDataRows(Arrays.asList(31 Arrays.asList("1", "name1", "email1"),

Full Screen

Full Screen

QueryResult

Using AI Code Generation

copy

Full Screen

1import org.evomaster.client.java.controller.db.QueryResult;2import org.evomaster.client.java.controller.db.SqlScriptRunner;3import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult;4import org.junit.jupiter.api.AfterAll;5import org.junit.jupiter.api.AfterEach;6import org.junit.jupiter.api.BeforeAll;7import org.junit.jupiter.api.BeforeEach;8import org.junit.jupiter.api.Test;9import org.junit.jupiter.api.TestInstance;10import java.util.List;11import java.util.Map;12import static org.junit.jupiter.api.Assertions.*;13@TestInstance(TestInstance.Lifecycle.PER_CLASS)14public class Test_0 {15 public static void initClass() throws Exception {16 ExecutionResult result = runner.run("" +17 "CREATE TABLE IF NOT EXISTS \"public\".\"Student\" (\n" +18 " PRIMARY KEY (\"id\")\n" +19 ");\n" +20 "INSERT INTO \"public\".\"Student\" (\"id\", \"name\", \"age\") VALUES (1, 'John', 25);\n" +21 "INSERT INTO \"public\".\"Student\" (\"id\", \"name\", \"age\") VALUES (2, 'Mary', 20);\n" +22 "INSERT INTO \"public\".\"Student\" (\"id\", \"name\", \"age\") VALUES (3, 'Paul', 22);\n" +23 "INSERT INTO \"public\".\"Student\" (\"id\", \"name\", \"age\") VALUES (4, 'Ann', 21);\n" +24 "INSERT INTO \"public\".\"Student\" (\"id\", \"name\", \"age\") VALUES (5, 'Bob', 23);\n");25 assertTrue(result.success);26 }27 public static void tearDown() throws Exception {28 runner.run("DROP TABLE \"public\".\"Student\";");29 }30 public void initTest() throws Exception {

Full Screen

Full Screen

QueryResult

Using AI Code Generation

copy

Full Screen

1List<Map<String, Object>> rows = queryResult.getRows();2for (Map<String, Object> row : rows) {3 System.out.println(row);4}5List<String> columns = queryResult.getColumns();6for (String column : columns) {7 System.out.println(column);8}9List<List<Object>> rows = queryResult.getRowsAsList();10for (List<Object> row : rows) {11 System.out.println(row);12}13List<String> columns = queryResult.getColumns();14for (String column : columns) {15 System.out.println(column);16}17List<Map<String, Object>> rows = queryResult.getRowsAsMaps();18for (Map<String, Object> row : rows) {19 System.out.println(row);20}21List<String> columns = queryResult.getColumns();22for (String column : columns) {23 System.out.println(column);24}

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.

Run EvoMaster automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful