/** * Constructs a {@link SingletonAssert} for the value of the provided {@code PCollection * PCollection<T>}, which must be a singleton. */ public static <T> SingletonAssert<T> thatSingleton(PCollection<T> actual) { return thatSingleton(actual.getName(), actual); }
public static void addCountingAsserts(PCollection<Long> input, long numElements) { // Count == numElements PAssert.thatSingleton(input.apply("Count", Count.globally())).isEqualTo(numElements); // Unique count == numElements PAssert.thatSingleton(input.apply(Distinct.create()).apply("UniqueCount", Count.globally())) .isEqualTo(numElements); // Min == 0 PAssert.thatSingleton(input.apply("Min", Min.globally())).isEqualTo(0L); // Max == numElements-1 PAssert.thatSingleton(input.apply("Max", Max.globally())).isEqualTo(numElements - 1); }
public static void addCountingAsserts( PCollection<Long> input, long count, long uniqueCount, long min, long max) { PAssert.thatSingleton(input.apply("Count", Count.globally())).isEqualTo(count); PAssert.thatSingleton(input.apply(Distinct.create()).apply("UniqueCount", Count.globally())) .isEqualTo(uniqueCount); PAssert.thatSingleton(input.apply("Min", Min.globally())).isEqualTo(min); PAssert.thatSingleton(input.apply("Max", Max.globally())).isEqualTo(max); }
public static void addCountingAsserts(PCollection<Long> input, long start, long end) { // Count == numElements PAssert.thatSingleton(input.apply("Count", Count.globally())).isEqualTo(end - start); // Unique count == numElements PAssert.thatSingleton(input.apply(Distinct.create()).apply("UniqueCount", Count.globally())) .isEqualTo(end - start); // Min == start PAssert.thatSingleton(input.apply("Min", Min.globally())).isEqualTo(start); // Max == end-1 PAssert.thatSingleton(input.apply("Max", Max.globally())).isEqualTo(end - 1); }
/** * Test that {@code PAssert.thatSingleton().hashCode()} is unsupported. See {@link * #testPAssertEqualsSingletonUnsupported}. */ @SuppressWarnings("deprecation") // test of deprecated function @Test public void testPAssertHashCodeSingletonUnsupported() throws Exception { thrown.expect(UnsupportedOperationException.class); thrown.expectMessage(".hashCode() is not supported."); PCollection<Integer> pcollection = pipeline.apply(Create.of(42)); PAssert.thatSingleton(pcollection).hashCode(); }
/** Test that we throw an error for false assertion on singleton. */ @Test @Category({ValidatesRunner.class, UsesFailureMessage.class}) public void testPAssertEqualsSingletonFalseDefaultReasonString() throws Exception { PCollection<Integer> pcollection = pipeline.apply(Create.of(42)); PAssert.thatSingleton(pcollection).isEqualTo(44); Throwable thrown = runExpectingAssertionFailure(pipeline); String message = thrown.getMessage(); assertThat(message, containsString("Create.Values/Read(CreateSource).out")); assertThat(message, containsString("Expected: <44>")); assertThat(message, containsString("but: was <42>")); }
/** Test that we throw an error for false assertion on singleton. */ @Test @Category({ValidatesRunner.class, UsesFailureMessage.class}) public void testPAssertEqualsSingletonFalse() throws Exception { PCollection<Integer> pcollection = pipeline.apply(Create.of(42)); PAssert.thatSingleton("The value was not equal to 44", pcollection).isEqualTo(44); Throwable thrown = runExpectingAssertionFailure(pipeline); String message = thrown.getMessage(); assertThat(message, containsString("The value was not equal to 44")); assertThat(message, containsString("Expected: <44>")); assertThat(message, containsString("but: was <42>")); }
/** * Test that we throw an error at pipeline construction time when the user mistakenly uses {@code * PAssert.thatSingleton().equals()} instead of the test method {@code .isEqualTo}. */ @SuppressWarnings({ "deprecation", // test of deprecated function "EqualsIncompatibleType" }) @Test public void testPAssertEqualsSingletonUnsupported() throws Exception { thrown.expect(UnsupportedOperationException.class); thrown.expectMessage("isEqualTo"); PCollection<Integer> pcollection = pipeline.apply(Create.of(42)); PAssert.thatSingleton(pcollection).equals(42); }
private void runApproximateUniqueWithDuplicates( final int elementCount, final int uniqueCount, final int sampleSize) { assert elementCount >= uniqueCount; final List<Double> elements = Lists.newArrayList(); for (int i = 0; i < elementCount; i++) { elements.add(1.0 / (i % uniqueCount + 1)); } Collections.shuffle(elements); final PCollection<Double> input = p.apply(Create.of(elements)); final PCollection<Long> estimate = input.apply(ApproximateUnique.globally(sampleSize)); PAssert.thatSingleton(estimate).satisfies(new VerifyEstimateFn(uniqueCount, sampleSize)); p.run(); }
/** Basic test for {@code notEqualTo}. */ @Test @Category(ValidatesRunner.class) public void testNotEqualTo() throws Exception { PCollection<Integer> pcollection = pipeline.apply(Create.of(43)); PAssert.thatSingleton(pcollection).notEqualTo(42); pipeline.run(); }
@Test @Category(NeedsRunner.class) public void testApproximateUniqueWithSmallInput() { final PCollection<Integer> input = p.apply(Create.of(Arrays.asList(1, 2, 3, 3))); final PCollection<Long> estimate = input.apply(ApproximateUnique.globally(1000)); PAssert.thatSingleton(estimate).isEqualTo(3L); p.run(); }
/** Basic test for {@code isEqualTo}. */ @Test @Category(ValidatesRunner.class) public void testIsEqualTo() throws Exception { PCollection<Integer> pcollection = pipeline.apply(Create.of(43)); PAssert.thatSingleton(pcollection).isEqualTo(43); pipeline.run(); }
@Test public void testParseDamagedPdfFile() throws IOException { String path = getClass().getResource("/damaged.pdf").getPath(); PCollection<ParseResult> res = p.apply("ParseInvalidPdfFile", TikaIO.parse().filepattern(path)); PAssert.thatSingleton(res) .satisfies( input -> { assertEquals(path, input.getFileLocation()); assertFalse(input.isSuccess()); assertTrue(input.getError() instanceof TikaException); return null; }); p.run(); }
@Test public void countAssertsSucceeds() { PCollection<Integer> create = pipeline.apply("FirstCreate", Create.of(1, 2, 3)); PAssert.that(create).containsInAnyOrder(1, 2, 3); PAssert.thatSingleton(create.apply(Sum.integersGlobally())).isEqualTo(6); PAssert.thatMap(pipeline.apply("CreateMap", Create.of(KV.of(1, 2)))) .isEqualTo(Collections.singletonMap(1, 2)); assertThat(PAssert.countAsserts(pipeline), equalTo(3)); }
@Test public void countAssertsMultipleCallsIndependent() { PCollection<Integer> create = pipeline.apply("FirstCreate", Create.of(1, 2, 3)); PAssert.that(create).containsInAnyOrder(1, 2, 3); PAssert.thatSingleton(create.apply(Sum.integersGlobally())).isEqualTo(6); assertThat(PAssert.countAsserts(pipeline), equalTo(2)); PAssert.thatMap(pipeline.apply("CreateMap", Create.of(KV.of(1, 2)))) .isEqualTo(Collections.singletonMap(1, 2)); assertThat(PAssert.countAsserts(pipeline), equalTo(3)); } }
@Test public void testRead() throws Exception { SolrIOTestUtils.insertTestDocuments(SOLR_COLLECTION, NUM_DOCS, solrClient); PCollection<SolrDocument> output = pipeline.apply( SolrIO.read() .withConnectionConfiguration(connectionConfiguration) .from(SOLR_COLLECTION) .withBatchSize(101)); PAssert.thatSingleton(output.apply("Count", Count.globally())).isEqualTo(NUM_DOCS); pipeline.run(); }
void testRead() throws Exception { if (!useAsITests) { ElasticSearchIOTestUtils.insertTestDocuments(connectionConfiguration, numDocs, restClient); } PCollection<String> output = pipeline.apply( ElasticsearchIO.read() .withConnectionConfiguration(connectionConfiguration) //set to default value, useful just to test parameter passing. .withScrollKeepalive("5m") //set to default value, useful just to test parameter passing. .withBatchSize(100L)); PAssert.thatSingleton(output.apply("Count", Count.globally())).isEqualTo(numDocs); pipeline.run(); }
@Test public void testReadWithQuery() throws Exception { SolrIOTestUtils.insertTestDocuments(SOLR_COLLECTION, NUM_DOCS, solrClient); PCollection<SolrDocument> output = pipeline.apply( SolrIO.read() .withConnectionConfiguration(connectionConfiguration) .from(SOLR_COLLECTION) .withQuery("scientist:Franklin")); PAssert.thatSingleton(output.apply("Count", Count.globally())) .isEqualTo(NUM_DOCS / NUM_SCIENTISTS); pipeline.run(); }
private void runBigQueryIOReadPipeline() { Pipeline p = Pipeline.create(options); PCollection<Long> count = p.apply("Read", BigQueryIO.read().from(options.getInputTable())) .apply("Count", Count.globally()); PAssert.thatSingleton(count).isEqualTo(options.getNumRecords()); p.run().waitUntilFinish(); }
private void testOutputAfterCheckpoint(IsBounded bounded) { PCollection<Integer> outputs = p.apply(Create.of("foo")) .apply(ParDo.of(sdfWithMultipleOutputsPerBlock(bounded, 3))) .apply(Window.<Integer>configure().triggering(Never.ever()).discardingFiredPanes()); PAssert.thatSingleton(outputs.apply(Count.globally())) .isEqualTo((long) SDFWithMultipleOutputsPerBlockBase.MAX_INDEX); p.run(); }