/** * Constructs an {@link IterableAssert} for the value of the provided {@link PCollection} which * must contain a single {@code Iterable<T>} value. */ public static <T> IterableAssert<T> thatSingletonIterable( PCollection<? extends Iterable<T>> actual) { return thatSingletonIterable(actual.getName(), actual); }
@Test @Category(NeedsRunner.class) @SuppressWarnings("unchecked") public void testTop() { PCollection<String> input = p.apply(Create.of(Arrays.asList(COLLECTION)).withCoder(StringUtf8Coder.of())); PCollection<List<String>> top1 = input.apply(Top.of(1, new OrderByLength())); PCollection<List<String>> top2 = input.apply(Top.largest(2)); PCollection<List<String>> top3 = input.apply(Top.smallest(3)); PCollection<KV<String, Integer>> inputTable = createInputTable(p); PCollection<KV<String, List<Integer>>> largestPerKey = inputTable.apply(Top.largestPerKey(2)); PCollection<KV<String, List<Integer>>> smallestPerKey = inputTable.apply(Top.smallestPerKey(2)); PAssert.thatSingletonIterable(top1).containsInAnyOrder(Arrays.asList("bb")); PAssert.thatSingletonIterable(top2).containsInAnyOrder("z", "c"); PAssert.thatSingletonIterable(top3).containsInAnyOrder("a", "bb", "c"); PAssert.that(largestPerKey) .containsInAnyOrder(KV.of("a", Arrays.asList(3, 2)), KV.of("b", Arrays.asList(100, 10))); PAssert.that(smallestPerKey) .containsInAnyOrder(KV.of("a", Arrays.asList(1, 2)), KV.of("b", Arrays.asList(1, 10))); p.run(); }
@Test @Category(NeedsRunner.class) @SuppressWarnings("unchecked") public void testTopZero() { PCollection<String> input = p.apply(Create.of(Arrays.asList(COLLECTION)).withCoder(StringUtf8Coder.of())); PCollection<List<String>> top1 = input.apply(Top.of(0, new OrderByLength())); PCollection<List<String>> top2 = input.apply(Top.largest(0)); PCollection<List<String>> top3 = input.apply(Top.smallest(0)); PCollection<KV<String, Integer>> inputTable = createInputTable(p); PCollection<KV<String, List<Integer>>> largestPerKey = inputTable.apply(Top.largestPerKey(0)); PCollection<KV<String, List<Integer>>> smallestPerKey = inputTable.apply(Top.smallestPerKey(0)); PAssert.thatSingletonIterable(top1).empty(); PAssert.thatSingletonIterable(top2).empty(); PAssert.thatSingletonIterable(top3).empty(); PAssert.that(largestPerKey) .containsInAnyOrder(KV.of("a", Arrays.asList()), KV.of("b", Arrays.asList())); PAssert.that(smallestPerKey) .containsInAnyOrder(KV.of("a", Arrays.asList()), KV.of("b", Arrays.asList())); p.run(); }
@Test @Category(NeedsRunner.class) @SuppressWarnings("unchecked") public void testTopEmpty() { PCollection<String> input = p.apply(Create.of(Arrays.asList(EMPTY_COLLECTION)).withCoder(StringUtf8Coder.of())); PCollection<List<String>> top1 = input.apply(Top.of(1, new OrderByLength())); PCollection<List<String>> top2 = input.apply(Top.largest(2)); PCollection<List<String>> top3 = input.apply(Top.smallest(3)); PCollection<KV<String, Integer>> inputTable = createEmptyInputTable(p); PCollection<KV<String, List<Integer>>> largestPerKey = inputTable.apply(Top.largestPerKey(2)); PCollection<KV<String, List<Integer>>> smallestPerKey = inputTable.apply(Top.smallestPerKey(2)); PAssert.thatSingletonIterable(top1).empty(); PAssert.thatSingletonIterable(top2).empty(); PAssert.thatSingletonIterable(top3).empty(); PAssert.that(largestPerKey).empty(); PAssert.that(smallestPerKey).empty(); p.run(); }
@Test @Category(NeedsRunner.class) public void testSampleEmpty() { PCollection<Integer> input = pipeline.apply(Create.empty(BigEndianIntegerCoder.of())); PCollection<Iterable<Integer>> output = input.apply(Sample.fixedSizeGlobally(3)); PAssert.thatSingletonIterable(output).satisfies(new VerifyCorrectSample<>(0, EMPTY)); pipeline.run(); }
@Test @Category(NeedsRunner.class) public void testSample() { PCollection<Integer> input = pipeline.apply( Create.of(ImmutableList.copyOf(DATA)).withCoder(BigEndianIntegerCoder.of())); PCollection<Iterable<Integer>> output = input.apply(Sample.fixedSizeGlobally(3)); PAssert.thatSingletonIterable(output).satisfies(new VerifyCorrectSample<>(3, DATA)); pipeline.run(); }
@Test @Category(NeedsRunner.class) public void testSampleZero() { PCollection<Integer> input = pipeline.apply( Create.of(ImmutableList.copyOf(DATA)).withCoder(BigEndianIntegerCoder.of())); PCollection<Iterable<Integer>> output = input.apply(Sample.fixedSizeGlobally(0)); PAssert.thatSingletonIterable(output).satisfies(new VerifyCorrectSample<>(0, DATA)); pipeline.run(); }
@Test @Category(NeedsRunner.class) public void testSampleInsufficientElements() { PCollection<Integer> input = pipeline.apply( Create.of(ImmutableList.copyOf(DATA)).withCoder(BigEndianIntegerCoder.of())); PCollection<Iterable<Integer>> output = input.apply(Sample.fixedSizeGlobally(10)); PAssert.thatSingletonIterable(output).satisfies(new VerifyCorrectSample<>(5, DATA)); pipeline.run(); }
@Test @Category(NeedsRunner.class) public void testSampleMultiplicity() { PCollection<Integer> input = pipeline.apply( Create.of(ImmutableList.copyOf(REPEATED_DATA)).withCoder(BigEndianIntegerCoder.of())); // At least one value must be selected with multiplicity. PCollection<Iterable<Integer>> output = input.apply(Sample.fixedSizeGlobally(6)); PAssert.thatSingletonIterable(output).satisfies(new VerifyCorrectSample<>(6, REPEATED_DATA)); pipeline.run(); }
PAssert.thatSingletonIterable(frontOfList).containsInAnyOrder(expectedFrontOfList); PAssert.thatSingletonIterable(backOfList).containsInAnyOrder(expectedBackOfList);