Best Testcontainers-java code snippet using org.testcontainers.ext.ScriptSplittingTest.splitAndCompare
Source:ScriptSplittingTest.java
...14 "SELECT 'foo `bar`'",15 "SELECT 'foo -- `bar`'",16 "SELECT 'foo /* `bar`'"17 );18 splitAndCompare(script, expected);19 }20 @Test21 public void testIssue1547Case1() {22 String script = "create database if not exists ttt;\n" +23 "\n" +24 "use ttt;\n" +25 "\n" +26 "create table aaa\n" +27 "(\n" +28 " id bigint auto_increment primary key,\n" +29 " end_time datetime null COMMENT 'end_time',\n" +30 " data_status varchar(16) not null\n" +31 ") comment 'aaa';\n" +32 "\n" +33 "create table bbb\n" +34 "(\n" +35 " id bigint auto_increment primary key\n" +36 ") comment 'bbb';";37 List<String> expected = asList(38 "create database if not exists ttt",39 "use ttt",40 "create table aaa ( id bigint auto_increment primary key, end_time datetime null COMMENT 'end_time', data_status varchar(16) not null ) comment 'aaa'",41 "create table bbb ( id bigint auto_increment primary key ) comment 'bbb'"42 );43 splitAndCompare(script, expected);44 }45 @Test46 public void testIssue1547Case2() {47 String script = "CREATE TABLE bar (\n" +48 " end_time VARCHAR(255)\n" +49 ");\n" +50 "CREATE TABLE bar (\n" +51 " end_time VARCHAR(255)\n" +52 ");";53 List<String> expected = asList(54 "CREATE TABLE bar ( end_time VARCHAR(255) )",55 "CREATE TABLE bar ( end_time VARCHAR(255) )"56 );57 splitAndCompare(script, expected);58 }59 @Test60 public void testUnusualSemicolonPlacement() {61 String script = "SELECT 1;;;;;SELECT 2;\n;SELECT 3\n; SELECT 4;\n SELECT 5";62 List<String> expected = asList(63 "SELECT 1",64 "SELECT 2",65 "SELECT 3",66 "SELECT 4",67 "SELECT 5"68 );69 splitAndCompare(script, expected);70 }71 @Test72 public void testCommentedSemicolon() {73 String script = "CREATE TABLE bar (\n" +74 " foo VARCHAR(255)\n" +75 "); \nDROP PROCEDURE IF EXISTS -- ;\n" +76 " count_foo";77 List<String> expected = asList(78 "CREATE TABLE bar ( foo VARCHAR(255) )",79 "DROP PROCEDURE IF EXISTS count_foo"80 );81 splitAndCompare(script, expected);82 }83 @Test84 public void testStringEscaping() {85 String script = "SELECT \"a /* string literal containing comment characters like -- here\";\n" +86 "SELECT \"a 'quoting' \\\"scenario ` involving BEGIN keyword\\\" here\";\n" +87 "SELECT * from `bar`;";88 List<String> expected = asList(89 "SELECT \"a /* string literal containing comment characters like -- here\"",90 "SELECT \"a 'quoting' \\\"scenario ` involving BEGIN keyword\\\" here\"",91 "SELECT * from `bar`"92 );93 splitAndCompare(script, expected);94 }95 @Test96 public void testBlockCommentExclusion() {97 String script = "INSERT INTO bar (foo) /* ; */ VALUES ('hello world');";98 List<String> expected = asList(99 "INSERT INTO bar (foo) VALUES ('hello world')"100 );101 splitAndCompare(script, expected);102 }103 @Test104 public void testBeginEndKeywordCorrectDetection() {105 String script = "INSERT INTO something_end (begin_with_the_token, another_field) /*end*/ VALUES /* end */ (' begin ', `end`)-- begin\n;";106 List<String> expected = asList(107 "INSERT INTO something_end (begin_with_the_token, another_field) VALUES (' begin ', `end`)"108 );109 splitAndCompare(script, expected);110 }111 @Test112 public void testCommentInStrings() {113 String script = "CREATE TABLE bar (foo VARCHAR(255));\n" +114 "\n" +115 "/* Insert Values */\n" +116 "INSERT INTO bar (foo) values ('--1');\n" +117 "INSERT INTO bar (foo) values ('--2');\n" +118 "INSERT INTO bar (foo) values ('/* something */');\n" +119 "/* INSERT INTO bar (foo) values (' */'); -- '*/;\n" + // purposefully broken, to see if it breaks our splitting120 "INSERT INTO bar (foo) values ('foo');";121 List<String> expected = asList(122 "CREATE TABLE bar (foo VARCHAR(255))",123 "INSERT INTO bar (foo) values ('--1')",124 "INSERT INTO bar (foo) values ('--2')",125 "INSERT INTO bar (foo) values ('/* something */')",126 "'); -- '*/",127 "INSERT INTO bar (foo) values ('foo')"128 );129 splitAndCompare(script, expected);130 }131 @Test132 public void testMultipleBeginEndDetection() {133 String script = "CREATE TABLE bar (foo VARCHAR(255));\n" +134 "\n" +135 "CREATE TABLE gender (gender VARCHAR(255));\n" +136 "CREATE TABLE ending (ending VARCHAR(255));\n" +137 "CREATE TABLE end2 (end2 VARCHAR(255));\n" +138 "CREATE TABLE end_2 (end2 VARCHAR(255));\n" +139 "\n" +140 "BEGIN\n" +141 " INSERT INTO ending values ('ending');\n" +142 "END;\n" +143 "\n" +144 "BEGIN\n" +145 " INSERT INTO ending values ('ending');\n" +146 "END/*hello*/;\n" +147 "\n" +148 "BEGIN--Hello\n" +149 " INSERT INTO ending values ('ending');\n" +150 "END;\n" +151 "\n" +152 "/*Hello*/BEGIN\n" +153 " INSERT INTO ending values ('ending');\n" +154 "END;\n" +155 "\n" +156 "CREATE TABLE foo (bar VARCHAR(255));";157 List<String> expected = asList(158 "CREATE TABLE bar (foo VARCHAR(255))",159 "CREATE TABLE gender (gender VARCHAR(255))",160 "CREATE TABLE ending (ending VARCHAR(255))",161 "CREATE TABLE end2 (end2 VARCHAR(255))",162 "CREATE TABLE end_2 (end2 VARCHAR(255))",163 "BEGIN\n" +164 " INSERT INTO ending values ('ending');\n" +165 "END",166 "BEGIN\n" +167 " INSERT INTO ending values ('ending');\n" +168 "END",169 "BEGIN--Hello\n" +170 " INSERT INTO ending values ('ending');\n" +171 "END",172 "BEGIN\n" +173 " INSERT INTO ending values ('ending');\n" +174 "END",175 "CREATE TABLE foo (bar VARCHAR(255))"176 );177 splitAndCompare(script, expected);178 }179 @Test180 public void testProcedureBlock() {181 String script = "CREATE PROCEDURE count_foo()\n" +182 " BEGIN\n" +183 "\n" +184 " BEGIN\n" +185 " SELECT *\n" +186 " FROM bar;\n" +187 " SELECT 1\n" +188 " FROM dual;\n" +189 " END;\n" +190 "\n" +191 " BEGIN\n" +192 " select * from bar;\n" +193 " END;\n" +194 "\n" +195 " -- we can do comments\n" +196 "\n" +197 " /* including block\n" +198 " comments\n" +199 " */\n" +200 "\n" +201 " /* what if BEGIN appears inside a comment? */\n" +202 "\n" +203 " select \"or what if BEGIN appears inside a literal?\";\n" +204 "\n" +205 " END /*; */;";206 List<String> expected = asList(207 "CREATE PROCEDURE count_foo() BEGIN\n" +208 "\n" +209 " BEGIN\n" +210 " SELECT *\n" +211 " FROM bar;\n" +212 " SELECT 1\n" +213 " FROM dual;\n" +214 " END;\n" +215 "\n" +216 " BEGIN\n" +217 " select * from bar;\n" +218 " END;\n" +219 "\n" +220 " -- we can do comments\n" +221 "\n" +222 " /* including block\n" +223 " comments\n" +224 " */\n" +225 "\n" +226 " /* what if BEGIN appears inside a comment? */\n" +227 "\n" +228 " select \"or what if BEGIN appears inside a literal?\";\n" +229 "\n" +230 " END"231 );232 splitAndCompare(script, expected);233 }234 @Test235 public void testUnclosedBlockComment() {236 String script = "SELECT 'foo `bar`'; /*";237 try {238 doSplit(script);239 fail("Should have thrown!");240 } catch (ScriptUtils.ScriptParseException expected) {241 // ignore expected exception242 }243 }244 @Test245 public void testIssue1452Case() {246 String script = "create table test (text VARCHAR(255));\n" +247 "\n" +248 "/* some comment */\n" +249 "insert into `test` (`text`) values ('a b');";250 List<String> expected = asList(251 "create table test (text VARCHAR(255))",252 "insert into `test` (`text`) values ('a b')"253 );254 splitAndCompare(script, expected);255 }256 private void splitAndCompare(String script, List<String> expected) {257 final List<String> statements = doSplit(script);258 Assertions.assertThat(statements).isEqualTo(expected);259 }260 private List<String> doSplit(String script) {261 final List<String> statements = new ArrayList<>();262 ScriptUtils.splitSqlScript("ignored", script, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_COMMENT_PREFIX, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER, statements);263 return statements;264 }265}...
splitAndCompare
Using AI Code Generation
1import org.testcontainers.containers.PostgreSQLContainer;2import org.testcontainers.ext.ScriptSplittingTest;3import org.testcontainers.utility.MountableFile;4import java.io.IOException;5import java.nio.file.Path;6import java.nio.file.Paths;7import java.util.List;8public class SplitPostgresScriptTest {9 public static void main(String[] args) throws IOException {10 PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:10.5")11 .withDatabaseName("test")12 .withUsername("test")13 .withPassword("test");14 postgres.start();15 Path path = Paths.get("/home/rafael/Downloads/test.sql");16 List<String> splitStatements = ScriptSplittingTest.splitAndCompare(path);17 splitStatements.forEach(System.out::println);18 postgres.copyFileToContainer(MountableFile.forHostPath(path), "/test.sql");19 postgres.execInContainer("psql", "-U", "test", "-d", "test", "-f", "/test.sql");20 }21}22import org.testcontainers.containers.PostgreSQLContainer;23import org.testcontainers.ext.ScriptSplittingTest;24import org.testcontainers.utility.MountableFile;25import java.io.IOException;26import java.nio.file.Path;27import java.nio.file.Paths;28import java.util.List;29public class SplitPostgresScriptTest {30 public static void main(String[] args) throws IOException {31 PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:10.5")32 .withDatabaseName("test")33 .withUsername("test")34 .withPassword("test");35 postgres.start();36 Path path = Paths.get("/home/rafael/Downloads/test.sql");37 List<String> splitStatements = ScriptSplittingTest.splitAndCompare(path);38 splitStatements.forEach(System.out::println);39 postgres.copyFileToContainer(MountableFile.forHostPath(path), "/test.sql");40 postgres.execInContainer("psql", "-U", "test", "-d", "test", "-f", "/test.sql");41 }42}43SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';
splitAndCompare
Using AI Code Generation
1import org.testcontainers.ext.ScriptSplittingTest2import org.testcontainers.ext.ScriptUtils3import org.testcontainers.ext.ScriptUtils.ScriptResult4import org.testcontainers.ext.ScriptUtils.ScriptResult.Status5import org.testcontainers.jdbc.JdbcDatabaseDelegate6import org.testcontainers.jdbc.JdbcDatabaseContainer7import org.testcontainers.jdbc.JdbcDatabaseDelegate8String script = "CREATE TABLE IF NOT EXISTS person (id int, name varchar(255));\n" +9 "INSERT INTO person VALUES (1, 'John');\n" +10 "INSERT INTO person VALUES (2, 'Paul');\n" +11 "INSERT INTO person VALUES (3, 'George');\n" +12 "INSERT INTO person VALUES (4, 'Ringo');\n" +13 "SELECT * FROM person;\n" +14 "UPDATE person SET name = 'John Lennon' WHERE id = 1;\n" +15 "SELECT * FROM person;\n" +16 "DROP TABLE person;\n" +17 "SELECT * FROM person;\n" +18 "INSERT INTO person VALUES (5, 'Pete');\n" +19 "SELECT * FROM person;\n" +20 "CREATE TABLE IF NOT EXISTS person (id int, name varchar(255));\n" +21 "INSERT INTO person VALUES (1, 'John');\n" +22 "INSERT INTO person VALUES (2, 'Paul');\n" +23 "INSERT INTO person VALUES (3, 'George');\n" +24 "INSERT INTO person VALUES (4, 'Ringo');\n" +25 "SELECT * FROM person;\n"26def splitAndCompare(String script, JdbcDatabaseContainer container) {27 def jdbcDatabaseDelegate = new JdbcDatabaseDelegate(container)28 def scriptResult = ScriptUtils.runInitScript(jdbcDatabaseDelegate, script)29 def statements = ScriptSplittingTest.splitScript(script)30 def expectedStatements = statements.collect { new ScriptResult(it, Status.SUCCESS, null) }31}32splitAndCompare(script, container)
splitAndCompare
Using AI Code Generation
1public class ScriptSplittingTest {2 public static void main(String[] args) throws IOException {3 splitAndCompare("script.sql");4 splitAndCompare("scriptWithComments.sql");5 }6 public static void splitAndCompare(String fileName) throws IOException {7 String script = readScript(fileName);8 List<String> statements = splitScript(script);9 String joinedStatements = joinStatements(statements);10 System.out.println("File: " + fileName);11 System.out.println("Split and joined statements are equal: " + script.equals(joinedStatements));12 System.out.println("Statements count: " + statements.size());13 System.out.println("Statements: " + statements);14 System.out.println("Joined statements: " + joinedStatements);15 System.out.println("16");17 }18 public static List<String> splitScript(String script) {19 return Arrays.stream(script.split("(?<=(;|\\r?\\n))"))20 .map(String::trim)21 .filter(s -> !s.isEmpty())22 .collect(Collectors.toList());23 }24 public static String joinStatements(List<String> statements) {25 return statements.stream()26 .map(s -> s + System.lineSeparator())27 .collect(Collectors.joining());28 }29 public static String readScript(String fileName) throws IOException {30 return new String(Files.readAllBytes(Paths.get(fileName)));31 }32}33Statements: [CREATE TABLE test (id int), INSERT INTO test VALUES (1)]34Joined statements: CREATE TABLE test (id int)35INSERT INTO test VALUES (1)
splitAndCompare
Using AI Code Generation
1public void testSplitAndExecuteScript() throws IOException {2 String script = "src/test/resources/test.sql";3 ScriptSplittingTest scriptSplittingTest = new ScriptSplittingTest();4 List<String> scripts = scriptSplittingTest.splitAndCompare(script);5 for (String s : scripts) {6 System.out.println(s);7 }8}
splitAndCompare
Using AI Code Generation
1import org.testcontainers.ext.ScriptSplittingTest2def scriptSplittingTest = new ScriptSplittingTest()3def script1 = "CREATE TABLE test_table (id INT, name VARCHAR(255));"4def script2 = "CREATE TABLE test_table (id INT, name VARCHAR(255));"5def result = scriptSplittingTest.splitAndCompare(script1, script1Name, script2, script2Name)6import org.testcontainers.ext.ScriptSplittingTest7def scriptSplittingTest = new ScriptSplittingTest()8def script1 = "CREATE TABLE test_table (id INT, name VARCHAR(255));"9def script2 = "CREATE TABLE test_table (id INT, name VARCHAR(255));"10def result = scriptSplittingTest.splitAndCompare(script1, script1Name, script2, script2Name)11import org.testcontainers.ext.ScriptSplittingTest12def scriptSplittingTest = new ScriptSplittingTest()13def script1 = "CREATE TABLE test_table (id INT, name VARCHAR(255));"14def script2 = "CREATE TABLE test_table (id INT, name VARCHAR(255));"15def result = scriptSplittingTest.splitAndCompare(script1, script1Name, script2, script2Name)16import org.testcontainers.ext.ScriptSplittingTest17def scriptSplittingTest = new ScriptSplittingTest()
splitAndCompare
Using AI Code Generation
1import org.testcontainers.containers.JdbcDatabaseContainer2import org.testcontainers.containers.MySQLContainerProvider3import org.testcontainers.containers.PostgreSQLContainerProvider4import org.testcontainers.containers.OracleContainerProvider5import org.testcontainers.containers.MSSQLServerContainerProvider6import org.testcontainers.containers.JdbcDatabaseContainerProvider7import org.testcontainers.jdbc.ContainerDatabaseDriver8import org.testcontainers.ext.ScriptSplittingTest9import org.testcontainers.ext.ScriptUtils10import java.util.ServiceLoader11import java.util.regex.Pattern12String dbContainer = System.getProperty("db.container", "mysql")
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!!