|
18 | 18 | import com.ing.data.cassandra.jdbc.UsingCassandraContainerTest;
|
19 | 19 | import org.junit.jupiter.api.BeforeAll;
|
20 | 20 | import org.junit.jupiter.api.Test;
|
| 21 | +import org.junit.jupiter.params.ParameterizedTest; |
| 22 | +import org.junit.jupiter.params.provider.Arguments; |
| 23 | +import org.junit.jupiter.params.provider.MethodSource; |
21 | 24 | import org.slf4j.Logger;
|
22 | 25 | import org.slf4j.LoggerFactory;
|
23 | 26 |
|
| 27 | +import java.math.BigDecimal; |
24 | 28 | import java.net.URL;
|
25 |
| -import java.sql.PreparedStatement; |
26 |
| -import java.sql.ResultSet; |
27 | 29 | import java.sql.SQLException;
|
| 30 | +import java.sql.Statement; |
| 31 | +import java.util.stream.Stream; |
28 | 32 |
|
29 |
| -import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static com.ing.data.cassandra.jdbc.utils.CopyCommandsTestUtils.COPY_CMD_TEST_KEYSPACE; |
| 34 | +import static com.ing.data.cassandra.jdbc.utils.CopyCommandsTestUtils.COPY_CMD_TEST_PARTIAL_TABLE; |
| 35 | +import static com.ing.data.cassandra.jdbc.utils.CopyCommandsTestUtils.COPY_CMD_TEST_PARTIAL_TABLE_NAME; |
| 36 | +import static com.ing.data.cassandra.jdbc.utils.CopyCommandsTestUtils.COPY_CMD_TEST_TABLE; |
| 37 | +import static com.ing.data.cassandra.jdbc.utils.CopyCommandsTestUtils.COPY_CMD_TEST_TABLE_NAME; |
| 38 | +import static com.ing.data.cassandra.jdbc.utils.CopyCommandsTestUtils.assertRowValues; |
| 39 | +import static org.apache.commons.lang3.StringUtils.EMPTY; |
30 | 40 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
31 |
| -import static org.junit.jupiter.api.Assertions.assertTrue; |
32 | 41 | import static org.junit.jupiter.api.Assertions.fail;
|
33 | 42 |
|
34 | 43 | public class CopyFromCommandTest extends UsingCassandraContainerTest {
|
35 | 44 |
|
36 | 45 | private static final Logger LOG = LoggerFactory.getLogger(CopyFromCommandTest.class);
|
37 |
| - private static final String KEYSPACE = "test_keyspace"; |
38 | 46 |
|
39 | 47 | @BeforeAll
|
40 | 48 | static void finalizeSetUpTests() throws Exception {
|
41 |
| - initConnection(KEYSPACE, "localdatacenter=datacenter1"); |
| 49 | + initConnection(COPY_CMD_TEST_KEYSPACE, "localdatacenter=datacenter1"); |
42 | 50 | }
|
43 | 51 |
|
44 | 52 | static String getTestOriginPath(final String csvFile) {
|
45 |
| - final URL cqlScriptResourceUrl = CopyFromCommandTest.class.getClassLoader().getResource(csvFile); |
| 53 | + final URL cqlScriptResourceUrl = |
| 54 | + CopyFromCommandTest.class.getClassLoader().getResource("copyFromTests/" + csvFile); |
46 | 55 | if (cqlScriptResourceUrl == null) {
|
47 |
| - fail("Could not find the CSV script to import: " + csvFile); |
| 56 | + fail("Could not find the CSV script to import in 'copyFromTests' directory: " + csvFile); |
48 | 57 | }
|
49 | 58 | return cqlScriptResourceUrl.getPath();
|
50 | 59 | }
|
51 | 60 |
|
52 |
| - @Test |
53 |
| - void givenTableAndOriginFile_whenExecuteCopyFromCommand_executeExpectedStatements() throws SQLException { |
| 61 | + private void executeCopyFromCommand(final String targetTable, final String csvSourceFile, |
| 62 | + final String options) throws SQLException { |
54 | 63 | assertNotNull(sqlConnection);
|
55 |
| - sqlConnection.createStatement() |
56 |
| - .execute(String.format("COPY cf_test1 FROM '%s'", getTestOriginPath("test_copy_from_cmd.csv"))); |
57 |
| - |
58 |
| - // Verify the rows to insert as specified in the executed script are effectively present. |
59 |
| - final PreparedStatement verifyStmt = sqlConnection.prepareStatement( |
60 |
| - "SELECT keyname, t1bValue, t1iValue FROM cf_test1 WHERE keyname = ?"); |
61 |
| - verifyStmt.setString(1, "key200"); |
62 |
| - ResultSet verifyRs = verifyStmt.executeQuery(); |
63 |
| - assertTrue(verifyRs.next()); |
64 |
| - assertEquals("key200", verifyRs.getString("keyname")); |
65 |
| - assertTrue(verifyRs.getBoolean("t1bValue")); |
66 |
| - assertEquals(654, verifyRs.getInt("t1iValue")); |
| 64 | + final Statement truncateTableStmt = sqlConnection.createStatement(); |
| 65 | + truncateTableStmt.execute(String.format("TRUNCATE TABLE %s", targetTable)); |
| 66 | + truncateTableStmt.close(); |
| 67 | + sqlConnection.createStatement().execute(String.format("COPY %s FROM '%s' %s", targetTable, |
| 68 | + getTestOriginPath(csvSourceFile), options)); |
| 69 | + } |
| 70 | + |
| 71 | + static Stream<Arguments> buildCopyFromCommandVariableParameters() { |
| 72 | + return Stream.of( |
| 73 | + Arguments.of("test_simple.csv", EMPTY), |
| 74 | + Arguments.of("test_with_headers.csv", "WITH HEADER=true"), |
| 75 | + Arguments.of("test_with_special_format.csv", |
| 76 | + "WITH DELIMITER=| AND QUOTE=` AND DECIMALSEP=, AND THOUSANDSSEP=.") |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + @ParameterizedTest |
| 81 | + @MethodSource("buildCopyFromCommandVariableParameters") |
| 82 | + void givenTableAndOriginFile_whenExecuteCopyFromCommand_executeExpectedStatements(final String csvFile, |
| 83 | + final String options) |
| 84 | + throws SQLException { |
| 85 | + executeCopyFromCommand(COPY_CMD_TEST_TABLE_NAME, csvFile, options); |
| 86 | + assertRowValues(sqlConnection, COPY_CMD_TEST_TABLE, "key1", true, new BigDecimal("654000.7")); |
| 87 | + assertRowValues(sqlConnection, COPY_CMD_TEST_TABLE, "key2", false, new BigDecimal("321000.8")); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void givenTableAndOriginFile_whenExecuteCopyFromCommandWithSkipRows_executeExpectedStatements() |
| 92 | + throws SQLException { |
| 93 | + executeCopyFromCommand(COPY_CMD_TEST_PARTIAL_TABLE_NAME, "test_partial_import.csv", "WITH SKIPROWS=2"); |
| 94 | + assertRowValues(sqlConnection, COPY_CMD_TEST_PARTIAL_TABLE, "key3", 3, "N/A"); |
| 95 | + assertRowValues(sqlConnection, COPY_CMD_TEST_PARTIAL_TABLE, "key4", 4, "test4"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void givenTableAndOriginFile_whenExecuteCopyFromCommandWithMaxRows_executeExpectedStatements() |
| 100 | + throws SQLException { |
| 101 | + executeCopyFromCommand(COPY_CMD_TEST_PARTIAL_TABLE_NAME, "test_partial_import.csv", "WITH MAXROWS=1"); |
| 102 | + assertRowValues(sqlConnection, COPY_CMD_TEST_PARTIAL_TABLE, "key1", 1, "test1"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + void givenTableAndOriginFile_whenExecuteCopyFromCommandWithPartialImportOptions_executeExpectedStatements() |
| 107 | + throws SQLException { |
| 108 | + executeCopyFromCommand(COPY_CMD_TEST_PARTIAL_TABLE_NAME, "test_partial_import.csv", |
| 109 | + "WITH MAXROWS=2 AND SKIPROWS=1 AND SKIPCOLS=str_val"); |
| 110 | + assertRowValues(sqlConnection, COPY_CMD_TEST_PARTIAL_TABLE, "key2", 2, null); |
| 111 | + assertRowValues(sqlConnection, COPY_CMD_TEST_PARTIAL_TABLE, "key3", 3, null); |
| 112 | + } |
| 113 | + |
| 114 | + @Test |
| 115 | + void givenTableAndOriginFile_whenExecuteCopyFromCommandWithNullValueOption_executeExpectedStatements() |
| 116 | + throws SQLException { |
| 117 | + executeCopyFromCommand(COPY_CMD_TEST_PARTIAL_TABLE_NAME, "test_partial_import.csv", "WITH NULLVAL=N/A"); |
| 118 | + assertRowValues(sqlConnection, COPY_CMD_TEST_PARTIAL_TABLE, "key1", 1, "test1"); |
| 119 | + assertRowValues(sqlConnection, COPY_CMD_TEST_PARTIAL_TABLE, "key3", 3, null); |
67 | 120 | }
|
68 | 121 |
|
69 | 122 | }
|
0 commit comments