Best EvoMaster code snippet using org.evomaster.client.java.controller.db.SqlScriptRunner.execInsert
Source:SqlScriptRunnerTest.java
...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 @Test...
execInsert
Using AI Code Generation
1import org.evomaster.client.java.controller.db.SqlScriptRunner;2import org.evomaster.client.java.controller.db.SqlScriptRunnerConfig;3public class SqlScriptRunnerTest {4 public static void main(String[] args) {5 SqlScriptRunnerConfig config = new SqlScriptRunnerConfig();6 config.setDatabaseDriver("org.postgresql.Driver");7 config.setUsername("postgres");8 config.setPassword("postgres");9 config.setSqlScript("create_table.sql");10 SqlScriptRunner sqlScriptRunner = new SqlScriptRunner(config);11 sqlScriptRunner.execInsert();12 }13}
execInsert
Using AI Code Generation
1import org.evomaster.client.java.controller.db.SqlScriptRunner;2import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult;3import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ExecutionStatus;4import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultType;5import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue;6import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueType;7import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueString;8import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueNumber;9import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueBoolean;10import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueDate;11import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueTime;12import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueTimestamp;13import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueNull;14import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueBlob;15import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueClob;16import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueArray;17import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueObject;18import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueRef;19import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueStruct;20import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueRowId;21import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueXml;22import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueOther;23import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueUnknown;24import org.evomaster.client.java.controller.db.SqlScriptRunner.ExecutionResult.ResultValue.ResultValueUnsupported;25import org.evomaster.client.java.controller.db.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!!