|
15 | 15 |
|
16 | 16 | package com.ing.data.cassandra.jdbc.commands;
|
17 | 17 |
|
| 18 | +import com.datastax.oss.driver.api.core.data.CqlDuration; |
| 19 | +import com.datastax.oss.driver.api.core.data.CqlVector; |
| 20 | +import com.datastax.oss.driver.api.querybuilder.QueryBuilder; |
18 | 21 | import com.ing.data.cassandra.jdbc.UsingCassandraContainerTest;
|
| 22 | +import org.apache.commons.collections4.CollectionUtils; |
| 23 | +import org.approvaltests.Approvals; |
| 24 | +import org.approvaltests.core.Options; |
19 | 25 | import org.junit.jupiter.api.BeforeAll;
|
20 | 26 | import org.junit.jupiter.api.Test;
|
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.Arguments; |
| 29 | +import org.junit.jupiter.params.provider.MethodSource; |
21 | 30 | import org.slf4j.Logger;
|
22 | 31 | import org.slf4j.LoggerFactory;
|
23 | 32 |
|
| 33 | +import java.io.File; |
| 34 | +import java.math.BigDecimal; |
| 35 | +import java.math.BigInteger; |
| 36 | +import java.sql.ResultSet; |
24 | 37 | import java.sql.SQLException;
|
| 38 | +import java.sql.Statement; |
| 39 | +import java.util.Arrays; |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.HashSet; |
| 42 | +import java.util.List; |
| 43 | +import java.util.Optional; |
| 44 | +import java.util.UUID; |
| 45 | +import java.util.stream.Stream; |
25 | 46 |
|
| 47 | +import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.function; |
| 48 | +import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal; |
| 49 | +import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.tuple; |
| 50 | +import static com.ing.data.cassandra.jdbc.testing.CopyCommandsTestUtils.COPY_CMD_TEST_ALL_TYPES_TABLE_NAME; |
| 51 | +import static com.ing.data.cassandra.jdbc.testing.CopyCommandsTestUtils.COPY_CMD_TEST_KEYSPACE; |
| 52 | +import static com.ing.data.cassandra.jdbc.testing.CopyCommandsTestUtils.COPY_CMD_TEST_PARTIAL_TABLE_NAME; |
| 53 | +import static com.ing.data.cassandra.jdbc.testing.CopyCommandsTestUtils.COPY_CMD_TEST_TABLE_NAME; |
| 54 | +import static com.ing.data.cassandra.jdbc.testing.CopyCommandsTestUtils.assertCommandResultSet; |
| 55 | +import static org.apache.commons.lang3.StringUtils.EMPTY; |
| 56 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 57 | +import static org.hamcrest.Matchers.containsString; |
26 | 58 | import static org.junit.jupiter.api.Assertions.assertNotNull;
|
| 59 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
27 | 60 |
|
28 | 61 | public class CopyToCommandTest extends UsingCassandraContainerTest {
|
29 | 62 |
|
30 |
| - private static final Logger LOG = LoggerFactory.getLogger(CopyToCommandTest.class); |
31 |
| - private static final String KEYSPACE = "test_keyspace"; |
32 |
| - |
33 | 63 | @BeforeAll
|
34 | 64 | static void finalizeSetUpTests() throws Exception {
|
35 |
| - initConnection(KEYSPACE, "localdatacenter=datacenter1"); |
| 65 | + initConnection(COPY_CMD_TEST_KEYSPACE, "localdatacenter=datacenter1"); |
| 66 | + |
| 67 | + // Populate tables with data to export. |
| 68 | + sqlConnection.createStatement().execute(QueryBuilder.insertInto(COPY_CMD_TEST_TABLE_NAME) |
| 69 | + .value("table_key", literal("key1")) |
| 70 | + .value("bool_val", literal(true)) |
| 71 | + .value("decimal_val", literal(1.5)) |
| 72 | + .asCql() |
| 73 | + ); |
| 74 | + sqlConnection.createStatement().execute(QueryBuilder.insertInto(COPY_CMD_TEST_TABLE_NAME) |
| 75 | + .value("table_key", literal("key2")) |
| 76 | + .value("bool_val", literal(false)) |
| 77 | + .value("decimal_val", literal(4200)) |
| 78 | + .asCql() |
| 79 | + ); |
| 80 | + sqlConnection.createStatement().execute(QueryBuilder.insertInto(COPY_CMD_TEST_TABLE_NAME) |
| 81 | + .value("table_key", literal("key3")) |
| 82 | + .value("bool_val", literal(false)) |
| 83 | + .value("decimal_val", literal(null)) |
| 84 | + .asCql() |
| 85 | + ); |
| 86 | + |
| 87 | + sqlConnection.createStatement().execute(QueryBuilder.insertInto(COPY_CMD_TEST_PARTIAL_TABLE_NAME) |
| 88 | + .value("table_key", literal("key1")) |
| 89 | + .value("int_val", literal(42)) |
| 90 | + .value("str_val", literal("A string containing a character \" to escape")) |
| 91 | + .asCql() |
| 92 | + ); |
| 93 | + |
| 94 | + sqlConnection.createStatement().execute(QueryBuilder.insertInto(COPY_CMD_TEST_ALL_TYPES_TABLE_NAME) |
| 95 | + .value("table_key", literal("key1")) |
| 96 | + .value("ascii_val", literal("abc123")) |
| 97 | + .value("bigint_val", literal(12345678900000L)) |
| 98 | + .value("blob_val", function("textAsBlob", literal("this is a blob"))) |
| 99 | + .value("bool_val", literal(true)) |
| 100 | + .value("date_val", literal("2023-03-25")) |
| 101 | + .value("decimal_val", literal(new BigDecimal("18.97"))) |
| 102 | + .value("double_val", literal(2345.6d)) |
| 103 | + .value("duration_val", literal(CqlDuration.from("3h15m"))) |
| 104 | + .value("float_val", literal(21.3f)) |
| 105 | + .value("inet_val", literal("127.0.0.1")) |
| 106 | + .value("int_val", literal(98)) |
| 107 | + .value("list_val", literal(Arrays.asList(4, 6, 10))) |
| 108 | + .value("map_val", literal(new HashMap<Integer, String>(){{ |
| 109 | + put(3, "three"); |
| 110 | + put(8, "eight"); |
| 111 | + }})) |
| 112 | + .value("smallint_val", literal(2)) |
| 113 | + .value("set_val", literal(new HashSet<Integer>(){{ |
| 114 | + add(2); |
| 115 | + add(3); |
| 116 | + add(5); |
| 117 | + }})) |
| 118 | + .value("time_val", literal("12:30:45.678")) |
| 119 | + .value("ts_val", literal("2023-03-25T12:30:45.789")) |
| 120 | + .value("timeuuid_val", literal(UUID.fromString("1f4b6fe0-f12b-11ef-8b6b-fdf7025e383c"))) |
| 121 | + .value("tinyint_val", literal(12)) |
| 122 | + .value("tuple_val", tuple(literal(5), literal("five"))) |
| 123 | + .value("uuid_val", literal(UUID.fromString("f26f86e6-d8ef-4d30-9d9e-4027d21e02a9"))) |
| 124 | + .value("varchar_val", literal("varchar text")) |
| 125 | + .value("varint_val", literal(new BigInteger("4321"))) |
| 126 | + .value("vector_val", literal(CqlVector.newInstance(1, 3, 8, 6))) |
| 127 | + .asCql() |
| 128 | + ); |
36 | 129 | }
|
37 | 130 |
|
38 |
| - @Test |
39 |
| - void givenTableAndTargetFile_whenExecuteCopyToCommand_generateExpectedCsvFile() throws SQLException { |
| 131 | + private ResultSet executeCopyToCommand(final String sourceTable, final List<String> sourceColumns, |
| 132 | + final String targetCsvFile, final String options) throws SQLException { |
40 | 133 | assertNotNull(sqlConnection);
|
41 |
| - // TODO sqlConnection.createStatement().execute("COPY cf_test1(keyname, t1ivalue) TO 'test_result.csv'"); |
| 134 | + final Statement copyCmdStmt = sqlConnection.createStatement(); |
| 135 | + |
| 136 | + String formattedSourceColumns = EMPTY; |
| 137 | + if (CollectionUtils.isNotEmpty(sourceColumns)) { |
| 138 | + formattedSourceColumns = String.format("(%s)", String.join(",", sourceColumns)); |
| 139 | + } |
| 140 | + |
| 141 | + copyCmdStmt.execute(String.format("COPY %s%s TO '%s' %s", sourceTable, formattedSourceColumns, |
| 142 | + targetCsvFile, Optional.ofNullable(options).orElse(EMPTY))); |
| 143 | + return copyCmdStmt.getResultSet(); |
| 144 | + } |
| 145 | + |
| 146 | + private static Options approvalTestsParameterName(final String csvFile) { |
| 147 | + return Approvals.NAMES.withParameters(csvFile.replace(".csv", EMPTY)); |
| 148 | + } |
| 149 | + |
| 150 | + static Stream<Arguments> buildCopyToCommandVariableParameters() { |
| 151 | + return Stream.of( |
| 152 | + Arguments.of("test_default_options.csv", EMPTY), |
| 153 | + Arguments.of("test_with_header.csv", "WITH HEADER=true"), |
| 154 | + Arguments.of("test_with_format_options.csv", |
| 155 | + "WITH DELIMITER=| AND QUOTE=` AND DECIMALSEP=, AND THOUSANDSSEP=. AND NULLVAL=N/A") |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + @ParameterizedTest |
| 160 | + @MethodSource("buildCopyToCommandVariableParameters") |
| 161 | + void givenTableAndTargetFile_whenExecuteCopyToCommand_generateExpectedCsvFile(final String csvFile, |
| 162 | + final String options) |
| 163 | + throws SQLException { |
| 164 | + final ResultSet resultSet = executeCopyToCommand(COPY_CMD_TEST_TABLE_NAME, null, csvFile, options); |
| 165 | + assertCommandResultSet(resultSet, false, 3, 1); |
| 166 | + Approvals.verify(new File(csvFile), approvalTestsParameterName(csvFile)); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void givenTableAndTargetFile_whenExecuteCopyToCommandWithEscapedChars_generateExpectedCsvFile() |
| 171 | + throws SQLException { |
| 172 | + final String targetCsvFile = "test_with_escape.csv"; |
| 173 | + final ResultSet resultSet = executeCopyToCommand(COPY_CMD_TEST_PARTIAL_TABLE_NAME, null, targetCsvFile, |
| 174 | + "WITH ESCAPE=\""); |
| 175 | + assertCommandResultSet(resultSet, false, 1, 1); |
| 176 | + Approvals.verify(new File(targetCsvFile), approvalTestsParameterName(targetCsvFile)); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + void givenTableWithColumnsAndTargetFile_whenExecuteCopyToCommand_generateExpectedCsvFile() throws SQLException { |
| 181 | + final String targetCsvFile = "test_partial_export.csv"; |
| 182 | + final ResultSet resultSet = executeCopyToCommand(COPY_CMD_TEST_PARTIAL_TABLE_NAME, |
| 183 | + Arrays.asList("table_key", "int_val"), targetCsvFile, EMPTY); |
| 184 | + assertCommandResultSet(resultSet, false, 1, 1); |
| 185 | + Approvals.verify(new File(targetCsvFile), approvalTestsParameterName(targetCsvFile)); |
| 186 | + } |
| 187 | + |
| 188 | + @Test |
| 189 | + void givenTableAndTargetFile_whenExecuteCopyToCommandUnsupportedOptions_throwException() throws SQLException { |
| 190 | + final SQLException sqlException = assertThrows(SQLException.class, () -> |
| 191 | + executeCopyToCommand(COPY_CMD_TEST_TABLE_NAME, null, "test_exception.csv", "WITH BADOPTION=1")); |
| 192 | + assertThat(sqlException.getMessage(), |
| 193 | + containsString("Command COPY used with unknown or unsupported options: [BADOPTION]")); |
| 194 | + } |
| 195 | + |
| 196 | + @Test |
| 197 | + void givenTableWithAllTypesAndTargetFile_whenExecuteCopyFromCommand_executeExpectedStatements() throws Exception { |
| 198 | + final String targetCsvFile = "test_all_types_export.csv"; |
| 199 | + final ResultSet resultSet = executeCopyToCommand(COPY_CMD_TEST_ALL_TYPES_TABLE_NAME, null, targetCsvFile, |
| 200 | + "WITH HEADER=true AND DELIMITER=;"); |
| 201 | + assertCommandResultSet(resultSet, false, 1, 1); |
| 202 | + Approvals.verify(new File(targetCsvFile), approvalTestsParameterName(targetCsvFile)); |
42 | 203 | }
|
43 | 204 |
|
44 | 205 | }
|
0 commit comments