Search in sources :

Example 1 with MockPageOutput

use of org.embulk.spi.TestPageBuilderReader.MockPageOutput in project embulk by embulk.

the class TestFileInputRunner method testTransactionAborted.

@Test
public void testTransactionAborted() {
    Buffer[] buffers = new Buffer[] { runtime.getBufferAllocator().allocate(), runtime.getBufferAllocator().allocate() };
    MockFileInputPlugin fileInputPlugin = new MockFileInputPlugin(new LinkedList<Buffer>(Arrays.asList(buffers)));
    final FileInputRunner runner = new FileInputRunner(fileInputPlugin);
    ConfigSource config = Exec.newConfigSource().set("parser", ImmutableMap.of("type", "mock", "columns", ImmutableList.of(ImmutableMap.of("name", "col1", "type", "boolean", "option", ImmutableMap.of()), ImmutableMap.of("name", "col2", "type", "long", "option", ImmutableMap.of()), ImmutableMap.of("name", "col3", "type", "double", "option", ImmutableMap.of()), ImmutableMap.of("name", "col4", "type", "string", "option", ImmutableMap.of()), ImmutableMap.of("name", "col5", "type", "timestamp", "option", ImmutableMap.of()), ImmutableMap.of("name", "col6", "type", "json", "option", ImmutableMap.of()))));
    final MockPageOutput output = new MockPageOutput();
    MockParserPlugin.raiseException = true;
    try {
        runner.transaction(config, new InputPlugin.Control() {

            public List<TaskReport> run(TaskSource inputTaskSource, Schema schema, int taskCount) {
                List<TaskReport> reports = new ArrayList<>();
                reports.add(runner.run(inputTaskSource, schema, 0, output));
                return reports;
            }
        });
    } catch (RuntimeException re) {
    // Just passing through.
    }
    assertEquals(false, fileInputPlugin.transactionCompleted);
    assertEquals(0, output.pages.size());
}
Also used : MockPageOutput(org.embulk.spi.TestPageBuilderReader.MockPageOutput) ConfigSource(org.embulk.config.ConfigSource) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) TaskSource(org.embulk.config.TaskSource) Test(org.junit.Test)

Example 2 with MockPageOutput

use of org.embulk.spi.TestPageBuilderReader.MockPageOutput in project embulk by embulk.

the class TestFileInputRunner method testMockParserIteration.

@Test
public void testMockParserIteration() {
    Buffer[] buffers = new Buffer[] { runtime.getBufferAllocator().allocate(), runtime.getBufferAllocator().allocate() };
    MockFileInputPlugin fileInputPlugin = new MockFileInputPlugin(new LinkedList<Buffer>(Arrays.asList(buffers)));
    final FileInputRunner runner = new FileInputRunner(fileInputPlugin);
    ConfigSource config = Exec.newConfigSource().set("parser", ImmutableMap.of("type", "mock", "columns", ImmutableList.of(ImmutableMap.of("name", "col1", "type", "boolean", "option", ImmutableMap.of()), ImmutableMap.of("name", "col2", "type", "long", "option", ImmutableMap.of()), ImmutableMap.of("name", "col3", "type", "double", "option", ImmutableMap.of()), ImmutableMap.of("name", "col4", "type", "string", "option", ImmutableMap.of()), ImmutableMap.of("name", "col5", "type", "timestamp", "option", ImmutableMap.of()), ImmutableMap.of("name", "col6", "type", "json", "option", ImmutableMap.of()))));
    final MockPageOutput output = new MockPageOutput();
    runner.transaction(config, new InputPlugin.Control() {

        public List<TaskReport> run(TaskSource inputTaskSource, Schema schema, int taskCount) {
            List<TaskReport> reports = new ArrayList<>();
            reports.add(runner.run(inputTaskSource, schema, 0, output));
            return reports;
        }
    });
    assertEquals(true, fileInputPlugin.transactionCompleted);
    assertEquals(1, output.pages.size());
    Schema schema = config.getNested("parser").loadConfig(MockParserPlugin.PluginTask.class).getSchemaConfig().toSchema();
    List<Object[]> records = Pages.toObjects(schema, output.pages);
    assertEquals(2, records.size());
    for (Object[] record : records) {
        assertEquals(6, record.length);
        assertEquals(true, record[0]);
        assertEquals(2L, record[1]);
        assertEquals(3.0D, (Double) record[2], 0.01D);
        assertEquals("45", record[3]);
        assertEquals(678L, ((Timestamp) record[4]).toEpochMilli());
        assertEquals("{\"_c2\":10,\"_c1\":true,\"_c4\":{\"k\":\"v\"},\"_c3\":\"embulk\"}", record[5].toString());
    }
}
Also used : MockPageOutput(org.embulk.spi.TestPageBuilderReader.MockPageOutput) ConfigSource(org.embulk.config.ConfigSource) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LinkedList(java.util.LinkedList) TaskSource(org.embulk.config.TaskSource) Test(org.junit.Test)

Example 3 with MockPageOutput

use of org.embulk.spi.TestPageBuilderReader.MockPageOutput in project embulk by embulk.

the class TestJsonParserPlugin method createResource.

@Before
public void createResource() {
    config = config();
    plugin = new JsonParserPlugin();
    output = new MockPageOutput();
}
Also used : MockPageOutput(org.embulk.spi.TestPageBuilderReader.MockPageOutput) Before(org.junit.Before)

Example 4 with MockPageOutput

use of org.embulk.spi.TestPageBuilderReader.MockPageOutput in project embulk by embulk.

the class PageTestUtils method buildPage.

public static List<Page> buildPage(BufferAllocator bufferAllocator, Schema schema, Object... values) {
    MockPageOutput output = new MockPageOutput();
    try (PageBuilder builder = new PageBuilder(bufferAllocator, schema, output)) {
        int idx = 0;
        while (idx < values.length) {
            for (int column = 0; column < builder.getSchema().getColumnCount(); ++column) {
                Object value = values[idx++];
                if (value == null) {
                    builder.setNull(column);
                } else if (value instanceof Boolean) {
                    builder.setBoolean(column, (Boolean) value);
                } else if (value instanceof Double) {
                    builder.setDouble(column, (Double) value);
                } else if (value instanceof Long) {
                    builder.setLong(column, (Long) value);
                } else if (value instanceof String) {
                    builder.setString(column, (String) value);
                } else if (value instanceof Timestamp) {
                    builder.setTimestamp(column, (Timestamp) value);
                } else if (value instanceof Value) {
                    builder.setJson(column, (Value) value);
                } else {
                    throw new IllegalStateException("Unsupported type in test utils: " + value.toString());
                }
            }
            builder.addRecord();
        }
        builder.finish();
    }
    return output.pages;
}
Also used : Value(org.msgpack.value.Value) Timestamp(org.embulk.spi.time.Timestamp) MockPageOutput(org.embulk.spi.TestPageBuilderReader.MockPageOutput)

Aggregations

MockPageOutput (org.embulk.spi.TestPageBuilderReader.MockPageOutput)4 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 ConfigSource (org.embulk.config.ConfigSource)2 TaskSource (org.embulk.config.TaskSource)2 Test (org.junit.Test)2 Timestamp (org.embulk.spi.time.Timestamp)1 Before (org.junit.Before)1 Value (org.msgpack.value.Value)1