Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,25 @@
*/
package org.apache.beam.examples.cookbook;

import static org.hamcrest.MatcherAssert.assertThat;

import com.google.api.services.bigquery.model.TableRow;
import java.util.List;
import java.util.Arrays;
import org.apache.beam.examples.cookbook.CombinePerKeyExamples.ExtractLargeWordsFn;
import org.apache.beam.examples.cookbook.CombinePerKeyExamples.FormatShakespeareOutputFn;
import org.apache.beam.sdk.transforms.DoFnTester;
import org.apache.beam.sdk.testing.PAssert;
import org.apache.beam.sdk.testing.TestPipeline;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.values.KV;
import org.hamcrest.CoreMatchers;
import org.apache.beam.sdk.values.PCollection;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/** Unit tests for {@link CombinePerKeyExamples}. */
@RunWith(JUnit4.class)
public class CombinePerKeyExamplesTest {
@Rule public TestPipeline p = TestPipeline.create();

private static final TableRow row1 =
new TableRow().set("corpus", "king_lear").set("word", "snuffleupaguses");
Expand Down Expand Up @@ -66,21 +69,19 @@ public class CombinePerKeyExamplesTest {
.set("all_plays", "king_lear,macbeth");

@Test
public void testExtractLargeWordsFn() throws Exception {
DoFnTester<TableRow, KV<String, String>> extractLargeWordsFn =
DoFnTester.of(new ExtractLargeWordsFn());
List<KV<String, String>> results = extractLargeWordsFn.processBundle(ROWS_ARRAY);
assertThat(results, CoreMatchers.hasItem(tuple1));
assertThat(results, CoreMatchers.hasItem(tuple2));
assertThat(results, CoreMatchers.hasItem(tuple3));
public void testExtractLargeWordsFn() {
PCollection<KV<String, String>> results =
p.apply(Create.of(Arrays.asList(ROWS_ARRAY))).apply(ParDo.of(new ExtractLargeWordsFn()));
PAssert.that(results).containsInAnyOrder(tuple1, tuple2, tuple3);
p.run().waitUntilFinish();
}

@Test
public void testFormatShakespeareOutputFn() throws Exception {
DoFnTester<KV<String, String>, TableRow> formatShakespeareOutputFn =
DoFnTester.of(new FormatShakespeareOutputFn());
List<TableRow> results = formatShakespeareOutputFn.processBundle(COMBINED_TUPLES_ARRAY);
assertThat(results, CoreMatchers.hasItem(resultRow1));
assertThat(results, CoreMatchers.hasItem(resultRow2));
public void testFormatShakespeareOutputFn() {
PCollection<TableRow> results =
p.apply(Create.of(Arrays.asList(COMBINED_TUPLES_ARRAY)))
.apply(ParDo.of(new FormatShakespeareOutputFn()));
PAssert.that(results).containsInAnyOrder(resultRow1, resultRow2);
p.run().waitUntilFinish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Data processing is tricky, full of corner cases, and difficult to debug, because
* Leaks of `Closeable`/`AutoCloseable` resources in failure cases
* Common corner cases when developing sources: complicated arithmetic in `BoundedSource.split` (e.g. splitting key or offset ranges), iteration over empty data sources or composite data sources that have some empty components.
* Mock out the interactions with third-party systems, or better, use ["fake"](https://martinfowler.com/articles/mocksArentStubs.html) implementations when available. Make sure that the mocked-out interactions are representative of all interesting cases of the actual behavior of these systems.
* To unit test `DoFn`s, `CombineFn`s, and `BoundedSource`s, consider using `DoFnTester`, `CombineFnTester`, and `SourceTestUtils` respectively which can exercise the code in non-trivial ways to flesh out potential bugs.
* To unit test `DoFn`s, run them in a `TestPipeline` and check their output with `PAssert`. For `CombineFn`s and `BoundedSource`s, consider using `CombineFnTester` and `SourceTestUtils` respectively, which can exercise the code in non-trivial ways to flesh out potential bugs.
* For transforms that work over unbounded collections, test their behavior in the presence of late or out-of-order data using `TestStream`.
* Tests must pass 100% of the time, including in hostile, CPU- or network-constrained environments (continuous integration servers). Never put timing-dependent code (e.g. sleeps) into tests. Experience shows that no reasonable amount of sleeping is enough - code can be suspended for more than several seconds.
* For detailed instructions on test code organization, see the [Beam Testing Guide](https://cwiki.apache.org/confluence/display/BEAM/Contribution+Testing+Guide).
Expand Down
Loading