Skip to content

Commit 4fb6f5c

Browse files
committed
Add helper context method to fix test
1 parent af39520 commit 4fb6f5c

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

dumper/app/src/test/java/com/google/edwmigration/dumper/application/dumper/connector/teradata/TeradataLogsConnectorTest.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818

1919
import static com.google.common.collect.ImmutableList.toImmutableList;
2020
import static com.google.common.collect.Iterables.getOnlyElement;
21+
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.AbstractTeradataConnector.DEF_LOG_TABLE;
22+
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.AbstractTeradataConnector.DEF_SQL_TABLE;
2123
import static com.google.edwmigration.dumper.application.dumper.test.DumperTestUtils.assertQueryEquals;
24+
import static java.nio.file.FileSystems.newFileSystem;
2225
import static java.time.ZoneOffset.UTC;
2326
import static org.junit.Assert.assertEquals;
2427
import static org.junit.Assert.assertTrue;
@@ -28,6 +31,7 @@
2831
import com.google.common.collect.ImmutableMap;
2932
import com.google.edwmigration.dumper.application.dumper.ConnectorArguments;
3033
import com.google.edwmigration.dumper.application.dumper.connector.AbstractConnectorExecutionTest;
34+
import com.google.edwmigration.dumper.application.dumper.handle.Handle;
3135
import com.google.edwmigration.dumper.application.dumper.handle.JdbcHandle;
3236
import com.google.edwmigration.dumper.application.dumper.io.FileSystemOutputHandleFactory;
3337
import com.google.edwmigration.dumper.application.dumper.io.OutputHandleFactory;
@@ -37,7 +41,6 @@
3741
import java.io.File;
3842
import java.net.URI;
3943
import java.nio.file.FileSystem;
40-
import java.nio.file.FileSystems;
4144
import java.time.Clock;
4245
import java.time.Instant;
4346
import java.util.ArrayList;
@@ -53,7 +56,8 @@
5356
@RunWith(JUnit4.class)
5457
public class TeradataLogsConnectorTest extends AbstractConnectorExecutionTest {
5558

56-
final TaskRunContext mockContext = mock(TaskRunContext.class);
59+
final TaskRunContextOps mockOptions = mock(TaskRunContextOps.class);
60+
5761
private final TeradataLogsConnector connector = new TeradataLogsConnector();
5862

5963
@Test
@@ -224,37 +228,30 @@ public void addTasksTo_commonConnectorTest_success() throws Exception {
224228

225229
@Test
226230
public void addTasksTo_executeQuery_success() throws Exception {
227-
String name = getClass().getSimpleName();
228-
File dbFile = DumperTestUtils.newJdbcFile(name);
229-
File zipFile = DumperTestUtils.newZipFile(name);
231+
File dbFile = DumperTestUtils.newJdbcFile(getClass().getSimpleName());
232+
File zipFile = DumperTestUtils.newZipFile(getClass().getSimpleName());
230233

231234
dbFile.delete();
232235
zipFile.delete();
233236

234237
URI outputUri = URI.create("jar:" + zipFile.toURI());
235-
238+
ImmutableList<String> scripts =
239+
ImmutableList.of(
240+
"attach ':memory:' as dbc",
241+
String.format(
242+
"create table %s (UserName varchar, errorcode int, StartTime int)", DEF_LOG_TABLE),
243+
String.format("create table %s (QueryID int)", DEF_SQL_TABLE));
236244
// This isn't great because all the column-validity queries fail.
237245
try (JdbcHandle handle = DumperTestUtils.newJdbcHandle(dbFile);
238-
FileSystem fileSystem =
239-
FileSystems.newFileSystem(outputUri, ImmutableMap.of("create", "true"))) {
240-
OutputHandleFactory sinkFactory = new FileSystemOutputHandleFactory(fileSystem, "/");
241-
handle.getJdbcTemplate().execute("attach ':memory:' as dbc");
242-
handle
243-
.getJdbcTemplate()
244-
.execute(
245-
"create table "
246-
+ TeradataLogsConnector.DEF_LOG_TABLE
247-
+ " (UserName varchar, errorcode int, StartTime int)");
248-
handle
249-
.getJdbcTemplate()
250-
.execute("create table " + TeradataLogsConnector.DEF_SQL_TABLE + " (QueryID int)");
251-
246+
FileSystem fileSystem = newFileSystem(outputUri, ImmutableMap.of("create", "true"))) {
247+
scripts.forEach(handle.getJdbcTemplate()::execute);
252248
ConnectorArguments arguments =
253-
new ConnectorArguments("--connector", connector.getName(), "--query-log-days", "1");
249+
new ConnectorArguments("--connector", "teradata-logs", "--query-log-days", "1");
250+
TaskRunContext runContext = testContext(fileSystem, handle, mockOptions, arguments);
254251
List<Task<?>> tasks = new ArrayList<>();
255252
connector.addTasksTo(tasks, arguments);
256253
for (Task<?> task : tasks) {
257-
task.run(mockContext);
254+
task.run(runContext);
258255
}
259256
}
260257
}
@@ -452,4 +449,13 @@ public void values_success() {
452449
Teradata14LogsConnector.EXPRESSIONS_LOG_TBL.toArray(
453450
new String[Teradata14LogsConnector.EXPRESSIONS_LOG_TBL.size()]));
454451
}
452+
453+
static TaskRunContext testContext(
454+
FileSystem fileSystem,
455+
Handle handle,
456+
TaskRunContextOps options,
457+
ConnectorArguments arguments) {
458+
OutputHandleFactory sinkFactory = new FileSystemOutputHandleFactory(fileSystem, "/");
459+
return new TaskRunContext(sinkFactory, handle, 10, options, arguments);
460+
}
455461
}

dumper/app/src/test/java/com/google/edwmigration/dumper/application/dumper/task/DumpMetadataTaskTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public class DumpMetadataTaskTest extends AbstractTaskTest {
3535
public void testTask() throws Exception {
3636
MemoryByteSink sink = new MemoryByteSink();
3737
ConnectorArguments arguments = new ConnectorArguments("--connector", "bigquery-logs");
38-
new DumpMetadataTask(arguments, "test-format")
39-
.doRun(mockContext, sink, HANDLE);
38+
new DumpMetadataTask(arguments, "test-format").doRun(mockContext, sink, HANDLE);
4039
}
4140

4241
@Test

dumper/app/src/test/java/com/google/edwmigration/dumper/application/dumper/task/JdbcSelectTaskTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@ public static void setUpClass() throws Exception {
6969
public void testResultSetHeader() throws Exception {
7070
MemoryByteSink sink = new MemoryByteSink();
7171
try (JdbcHandle handle = DumperTestUtils.newJdbcHandle(FILE)) {
72-
new JdbcSelectTask("(memory)", QUERY)
73-
.doRun(mockContext, sink, handle);
72+
new JdbcSelectTask("(memory)", QUERY).doRun(mockContext, sink, handle);
7473
}
7574
String actualOutput = sink.openStream().toString();
7675
assertEquals("a,b,c\n1,2,3\n", actualOutput);

0 commit comments

Comments
 (0)