26
26
27
27
import java .math .BigDecimal ;
28
28
import java .net .URL ;
29
+ import java .sql .ResultSet ;
29
30
import java .sql .SQLException ;
30
31
import java .sql .Statement ;
32
+ import java .util .Optional ;
31
33
import java .util .stream .Stream ;
32
34
33
35
import static com .ing .data .cassandra .jdbc .utils .CopyCommandsTestUtils .COPY_CMD_TEST_KEYSPACE ;
34
36
import static com .ing .data .cassandra .jdbc .utils .CopyCommandsTestUtils .COPY_CMD_TEST_PARTIAL_TABLE ;
35
37
import static com .ing .data .cassandra .jdbc .utils .CopyCommandsTestUtils .COPY_CMD_TEST_PARTIAL_TABLE_NAME ;
36
38
import static com .ing .data .cassandra .jdbc .utils .CopyCommandsTestUtils .COPY_CMD_TEST_TABLE ;
37
39
import static com .ing .data .cassandra .jdbc .utils .CopyCommandsTestUtils .COPY_CMD_TEST_TABLE_NAME ;
40
+ import static com .ing .data .cassandra .jdbc .utils .CopyCommandsTestUtils .assertCommandResultSet ;
38
41
import static com .ing .data .cassandra .jdbc .utils .CopyCommandsTestUtils .assertRowValues ;
39
42
import static org .apache .commons .lang3 .StringUtils .EMPTY ;
43
+ import static org .hamcrest .MatcherAssert .assertThat ;
44
+ import static org .hamcrest .Matchers .containsString ;
40
45
import static org .junit .jupiter .api .Assertions .assertNotNull ;
46
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
41
47
import static org .junit .jupiter .api .Assertions .fail ;
42
48
43
49
public class CopyFromCommandTest extends UsingCassandraContainerTest {
@@ -49,23 +55,35 @@ static void finalizeSetUpTests() throws Exception {
49
55
initConnection (COPY_CMD_TEST_KEYSPACE , "localdatacenter=datacenter1" );
50
56
}
51
57
52
- static String getTestOriginPath (final String csvFile ) {
58
+ static String getTestOriginPath (final String csvFile , final boolean allowInvalidFile ) {
53
59
final URL cqlScriptResourceUrl =
54
60
CopyFromCommandTest .class .getClassLoader ().getResource ("copyFromTests/" + csvFile );
55
61
if (cqlScriptResourceUrl == null ) {
62
+ if (allowInvalidFile ) {
63
+ return csvFile ;
64
+ }
56
65
fail ("Could not find the CSV script to import in 'copyFromTests' directory: " + csvFile );
57
66
}
58
67
return cqlScriptResourceUrl .getPath ();
59
68
}
60
69
61
- private void executeCopyFromCommand (final String targetTable , final String csvSourceFile ,
62
- final String options ) throws SQLException {
70
+ private ResultSet executeCopyFromCommand (final String targetTable , final String csvSourceFile ,
71
+ final String options ) throws SQLException {
72
+ return executeCopyFromCommand (targetTable , csvSourceFile , options , false );
73
+ }
74
+
75
+ private ResultSet executeCopyFromCommand (final String targetTable , final String csvSourceFile ,
76
+ final String options , final boolean allowInvalidFile ) throws SQLException {
63
77
assertNotNull (sqlConnection );
78
+ // First, truncate target table to ensure the data of any test previously executed is removed.
64
79
final Statement truncateTableStmt = sqlConnection .createStatement ();
65
80
truncateTableStmt .execute (String .format ("TRUNCATE TABLE %s" , targetTable ));
66
81
truncateTableStmt .close ();
67
- sqlConnection .createStatement ().execute (String .format ("COPY %s FROM '%s' %s" , targetTable ,
68
- getTestOriginPath (csvSourceFile ), options ));
82
+
83
+ final Statement copyCmdStmt = sqlConnection .createStatement ();
84
+ copyCmdStmt .execute (String .format ("COPY %s FROM '%s' %s" , targetTable ,
85
+ getTestOriginPath (csvSourceFile , allowInvalidFile ), Optional .ofNullable (options ).orElse (EMPTY )));
86
+ return copyCmdStmt .getResultSet ();
69
87
}
70
88
71
89
static Stream <Arguments > buildCopyFromCommandVariableParameters () {
@@ -82,41 +100,91 @@ static Stream<Arguments> buildCopyFromCommandVariableParameters() {
82
100
void givenTableAndOriginFile_whenExecuteCopyFromCommand_executeExpectedStatements (final String csvFile ,
83
101
final String options )
84
102
throws SQLException {
85
- executeCopyFromCommand (COPY_CMD_TEST_TABLE_NAME , csvFile , options );
103
+ final ResultSet resultSet = executeCopyFromCommand (COPY_CMD_TEST_TABLE_NAME , csvFile , options );
104
+ assertCommandResultSet (resultSet , true , 2 , 1 , 0 );
86
105
assertRowValues (sqlConnection , COPY_CMD_TEST_TABLE , "key1" , true , new BigDecimal ("654000.7" ));
87
106
assertRowValues (sqlConnection , COPY_CMD_TEST_TABLE , "key2" , false , new BigDecimal ("321000.8" ));
88
107
}
89
108
90
109
@ Test
91
110
void givenTableAndOriginFile_whenExecuteCopyFromCommandWithSkipRows_executeExpectedStatements ()
92
111
throws SQLException {
93
- executeCopyFromCommand (COPY_CMD_TEST_PARTIAL_TABLE_NAME , "test_partial_import.csv" , "WITH SKIPROWS=2" );
112
+ final ResultSet resultSet = executeCopyFromCommand (COPY_CMD_TEST_PARTIAL_TABLE_NAME ,
113
+ "test_partial_import.csv" , "WITH SKIPROWS=2" );
114
+ assertCommandResultSet (resultSet , true , 2 , 1 , 2 );
94
115
assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key3" , 3 , "N/A" );
95
116
assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key4" , 4 , "test4" );
96
117
}
97
118
98
119
@ Test
99
120
void givenTableAndOriginFile_whenExecuteCopyFromCommandWithMaxRows_executeExpectedStatements ()
100
121
throws SQLException {
101
- executeCopyFromCommand (COPY_CMD_TEST_PARTIAL_TABLE_NAME , "test_partial_import.csv" , "WITH MAXROWS=1" );
122
+ final ResultSet resultSet = executeCopyFromCommand (COPY_CMD_TEST_PARTIAL_TABLE_NAME ,
123
+ "test_partial_import.csv" , "WITH MAXROWS=1" );
124
+ assertCommandResultSet (resultSet , true , 1 , 1 , 3 );
102
125
assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key1" , 1 , "test1" );
103
126
}
104
127
105
128
@ Test
106
129
void givenTableAndOriginFile_whenExecuteCopyFromCommandWithPartialImportOptions_executeExpectedStatements ()
107
130
throws SQLException {
108
- executeCopyFromCommand (COPY_CMD_TEST_PARTIAL_TABLE_NAME , "test_partial_import.csv" ,
131
+ final ResultSet resultSet = executeCopyFromCommand (COPY_CMD_TEST_PARTIAL_TABLE_NAME , "test_partial_import.csv" ,
109
132
"WITH MAXROWS=2 AND SKIPROWS=1 AND SKIPCOLS=str_val" );
133
+ assertCommandResultSet (resultSet , true , 2 , 1 , 2 );
110
134
assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key2" , 2 , null );
111
135
assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key3" , 3 , null );
112
136
}
113
137
114
138
@ Test
115
139
void givenTableAndOriginFile_whenExecuteCopyFromCommandWithNullValueOption_executeExpectedStatements ()
116
140
throws SQLException {
117
- executeCopyFromCommand (COPY_CMD_TEST_PARTIAL_TABLE_NAME , "test_partial_import.csv" , "WITH NULLVAL=N/A" );
141
+ final ResultSet resultSet = executeCopyFromCommand (COPY_CMD_TEST_PARTIAL_TABLE_NAME ,
142
+ "test_partial_import.csv" , "WITH NULLVAL=N/A" );
143
+ assertCommandResultSet (resultSet , true , 4 , 1 , 0 );
118
144
assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key1" , 1 , "test1" );
119
145
assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key3" , 3 , null );
120
146
}
121
147
148
+ @ Test
149
+ void givenTableAndOriginFile_whenExecuteCopyFromCommandWithUnsupportedOptions_throwException () {
150
+ final SQLException sqlException = assertThrows (SQLException .class , () ->
151
+ executeCopyFromCommand (COPY_CMD_TEST_TABLE_NAME , "test_simple.csv" , "WITH BADOPTION=1" ));
152
+ assertThat (sqlException .getMessage (),
153
+ containsString ("Command COPY used with unknown or unsupported options: [BADOPTION]" ));
154
+ }
155
+
156
+ @ Test
157
+ void givenTableAndOriginFile_whenExecuteCopyFromCommandWithInvalidIntOption_useDefaultOptionValue ()
158
+ throws SQLException {
159
+ final ResultSet resultSet = executeCopyFromCommand (
160
+ COPY_CMD_TEST_KEYSPACE + "." + COPY_CMD_TEST_PARTIAL_TABLE_NAME , "test_partial_import.csv" ,
161
+ "WITH SKIPROWS=a" );
162
+ assertCommandResultSet (resultSet , true , 4 , 1 , 0 );
163
+ assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key1" , 1 , "test1" );
164
+ assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key2" , 2 , "test2" );
165
+ assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key3" , 3 , "N/A" );
166
+ assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key4" , 4 , "test4" );
167
+ }
168
+
169
+ @ Test
170
+ void givenNonExistingFile_whenExecuteCopyFromCommand_throwException () {
171
+ final SQLException sqlException = assertThrows (SQLException .class , () ->
172
+ executeCopyFromCommand (COPY_CMD_TEST_TABLE_NAME , "bad_test_file.csv" , EMPTY , true ));
173
+ assertThat (sqlException .getMessage (),
174
+ containsString ("Could not find CSV file to import 'bad_test_file.csv'" ));
175
+ }
176
+
177
+ @ Test
178
+ void givenTableAndOriginFile_whenExecuteCopyFromCommandWithSpecificBatchSize_executeExpectedBatches ()
179
+ throws SQLException {
180
+ final ResultSet resultSet = executeCopyFromCommand (COPY_CMD_TEST_PARTIAL_TABLE_NAME ,
181
+ "test_partial_import.csv" , "WITH BATCHSIZE=2" );
182
+ assertCommandResultSet (resultSet , true , 4 , 2 , 0 );
183
+ assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key1" , 1 , "test1" );
184
+ assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key2" , 2 , "test2" );
185
+ assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key3" , 3 , "N/A" );
186
+ assertRowValues (sqlConnection , COPY_CMD_TEST_PARTIAL_TABLE , "key4" , 4 , "test4" );
187
+ }
188
+
189
+ // TODO: test with all types
122
190
}
0 commit comments