InputT - the type of the DoFn's (main) input elementsOutputT - the type of the DoFn's (main) output elementspublic class DoFnTester<InputT,OutputT> extends Object
DoFn.
For example:
DoFn<InputT, OutputT> fn = ...;
DoFnTester<InputT, OutputT> fnTester = DoFnTester.of(fn);
// Set arguments shared across all bundles:
fnTester.setSideInputs(...); // If fn takes side inputs.
fnTester.setSideOutputTags(...); // If fn writes to side outputs.
// Process a bundle containing a single input element:
Input testInput = ...;
List<OutputT> testOutputs = fnTester.processBundle(testInput);
Assert.assertThat(testOutputs, Matchers.hasItems(...));
// Process a bigger bundle:
Assert.assertThat(fnTester.processBundle(i1, i2, ...), Matchers.hasItems(...));
| Modifier and Type | Class and Description |
|---|---|
static class |
DoFnTester.CloningBehavior
Whether or not a
DoFnTester should clone the DoFn under test. |
| Modifier and Type | Method and Description |
|---|---|
void |
clearOutputElements()
Clears the record of the elements output so far to the main output.
|
<T> void |
clearSideOutputElements(TupleTag<T> tag)
Clears the record of the elements output so far to the side
output with the given tag.
|
void |
finishBundle()
Calls
DoFn.finishBundle(org.apache.beam.sdk.transforms.DoFn<InputT, OutputT>.Context) of the DoFn under test. |
<AggregateT> |
getAggregatorValue(Aggregator<?,AggregateT> agg)
Returns the value of the provided
Aggregator. |
DoFnTester.CloningBehavior |
getCloningBehavior()
Indicates whether this
DoFnTester will clone the DoFn under test. |
static <InputT,OutputT> |
of(DoFn<InputT,OutputT> fn)
Returns a
DoFnTester supporting unit-testing of the given
DoFn. |
static <InputT,OutputT> |
of(DoFnWithContext<InputT,OutputT> fn)
Returns a
DoFnTester supporting unit-testing of the given
DoFn. |
List<OutputT> |
peekOutputElements()
Returns the elements output so far to the main output.
|
List<TimestampedValue<OutputT>> |
peekOutputElementsInWindow(BoundedWindow window)
Returns the elements output so far to the main output in the provided window with associated
timestamps.
|
List<TimestampedValue<OutputT>> |
peekOutputElementsInWindow(TupleTag<OutputT> tag,
BoundedWindow window)
Returns the elements output so far to the specified output in the provided window with
associated timestamps.
|
List<TimestampedValue<OutputT>> |
peekOutputElementsWithTimestamp()
Returns the elements output so far to the main output with associated timestamps.
|
<T> List<T> |
peekSideOutputElements(TupleTag<T> tag)
Returns the elements output so far to the side output with the
given tag.
|
List<OutputT> |
processBundle(InputT... inputElements)
A convenience method for testing
DoFns with bundles of elements. |
List<OutputT> |
processBundle(Iterable<? extends InputT> inputElements)
A convenience operation that first calls
startBundle(),
then calls processElement(InputT) on each of the input elements, then
calls finishBundle(), then returns the result of
takeOutputElements(). |
void |
processElement(InputT element)
Calls
DoFn.processElement(org.apache.beam.sdk.transforms.DoFn<InputT, OutputT>.ProcessContext) on the DoFn under test, in a
context where DoFn.ProcessContext.element() returns the
given element. |
void |
setCloningBehavior(DoFnTester.CloningBehavior newValue)
Instruct this
DoFnTester whether or not to clone the DoFn under test. |
<T> void |
setSideInput(PCollectionView<T> sideInput,
BoundedWindow window,
T value)
Registers the values of a side input
PCollectionView to pass to the DoFn under
test. |
void |
setSideInputs(Map<PCollectionView<?>,Map<BoundedWindow,?>> sideInputs)
Registers the tuple of values of the side input
PCollectionViews to
pass to the DoFn under test. |
void |
startBundle()
Calls
DoFn.startBundle(org.apache.beam.sdk.transforms.DoFn<InputT, OutputT>.Context) on the DoFn under test. |
List<OutputT> |
takeOutputElements()
Returns the elements output so far to the main output.
|
List<TimestampedValue<OutputT>> |
takeOutputElementsWithTimestamp()
Returns the elements output so far to the main output with associated timestamps.
|
<T> List<T> |
takeSideOutputElements(TupleTag<T> tag)
Returns the elements output so far to the side output with the given tag.
|
public static <InputT,OutputT> DoFnTester<InputT,OutputT> of(DoFn<InputT,OutputT> fn)
DoFnTester supporting unit-testing of the given
DoFn.public static <InputT,OutputT> DoFnTester<InputT,OutputT> of(DoFnWithContext<InputT,OutputT> fn)
DoFnTester supporting unit-testing of the given
DoFn.public void setSideInputs(Map<PCollectionView<?>,Map<BoundedWindow,?>> sideInputs)
PCollectionViews to
pass to the DoFn under test.
Resets the state of this DoFnTester.
If this isn't called, DoFnTester assumes the
DoFn takes no side inputs.
public <T> void setSideInput(PCollectionView<T> sideInput, BoundedWindow window, T value)
PCollectionView to pass to the DoFn under
test.
The provided value is the final value of the side input in the specified window, not the value of the input PCollection in that window.
If this isn't called, DoFnTester will return the default value for any side input
that is used.
public void setCloningBehavior(DoFnTester.CloningBehavior newValue)
DoFnTester whether or not to clone the DoFn under test.public DoFnTester.CloningBehavior getCloningBehavior()
DoFnTester will clone the DoFn under test.public List<OutputT> processBundle(Iterable<? extends InputT> inputElements) throws Exception
startBundle(),
then calls processElement(InputT) on each of the input elements, then
calls finishBundle(), then returns the result of
takeOutputElements().Exception@SafeVarargs public final List<OutputT> processBundle(InputT... inputElements) throws Exception
DoFns with bundles of elements.
Logic proceeds as follows:
startBundle().processElement(InputT) on each of the arguments.finishBundle().takeOutputElements().Exceptionpublic void startBundle()
throws Exception
DoFn.startBundle(org.apache.beam.sdk.transforms.DoFn<InputT, OutputT>.Context) on the DoFn under test.
If needed, first creates a fresh instance of the DoFn under test.
Exceptionpublic void processElement(InputT element) throws Exception
DoFn.processElement(org.apache.beam.sdk.transforms.DoFn<InputT, OutputT>.ProcessContext) on the DoFn under test, in a
context where DoFn.ProcessContext.element() returns the
given element.
Will call startBundle() automatically, if it hasn't
already been called.
IllegalStateException - if the DoFn under test has already
been finishedExceptionpublic void finishBundle()
throws Exception
DoFn.finishBundle(org.apache.beam.sdk.transforms.DoFn<InputT, OutputT>.Context) of the DoFn under test.
Will call startBundle() automatically, if it hasn't
already been called.
IllegalStateException - if the DoFn under test has already
been finishedExceptionpublic List<OutputT> peekOutputElements()
takeOutputElements(),
clearOutputElements()@Experimental public List<TimestampedValue<OutputT>> peekOutputElementsWithTimestamp()
public List<TimestampedValue<OutputT>> peekOutputElementsInWindow(BoundedWindow window)
public List<TimestampedValue<OutputT>> peekOutputElementsInWindow(TupleTag<OutputT> tag, BoundedWindow window)
public void clearOutputElements()
peekOutputElements()public List<OutputT> takeOutputElements()
peekOutputElements()@Experimental public List<TimestampedValue<OutputT>> takeOutputElementsWithTimestamp()
public <T> List<T> peekSideOutputElements(TupleTag<T> tag)
public <T> void clearSideOutputElements(TupleTag<T> tag)
public <T> List<T> takeSideOutputElements(TupleTag<T> tag)
public <AggregateT> AggregateT getAggregatorValue(Aggregator<?,AggregateT> agg)
Aggregator.