derrickaw commented on code in PR #34411: URL: https://github.com/apache/beam/pull/34411#discussion_r2040242315
########## sdks/java/core/src/test/java/org/apache/beam/sdk/io/TFRecordSchemaTransformProviderTest.java: ########## @@ -0,0 +1,605 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.io; + +import static org.apache.beam.sdk.io.Compression.AUTO; +import static org.apache.beam.sdk.io.Compression.DEFLATE; +import static org.apache.beam.sdk.io.Compression.GZIP; +import static org.apache.beam.sdk.io.Compression.UNCOMPRESSED; +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.startsWith; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.in; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; +import static org.junit.internal.matchers.ThrowableMessageMatcher.hasMessage; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.ServiceLoader; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; +import org.apache.beam.sdk.io.TFRecordReadSchemaTransformProvider.TFRecordReadSchemaTransform; +import org.apache.beam.sdk.io.TFRecordWriteSchemaTransformProvider.TFRecordWriteSchemaTransform; +import org.apache.beam.sdk.schemas.Schema; +import org.apache.beam.sdk.schemas.transforms.SchemaTransform; +import org.apache.beam.sdk.schemas.transforms.SchemaTransformProvider; +import org.apache.beam.sdk.testing.NeedsRunner; +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.values.PCollection; +import org.apache.beam.sdk.values.PCollectionRowTuple; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Lists; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.Sets; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.BaseEncoding; +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.io.ByteStreams; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.rules.ExpectedException; +import org.junit.rules.TemporaryFolder; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for TFRecordIO Read and Write transforms. */ +@RunWith(JUnit4.class) +public class TFRecordSchemaTransformProviderTest { + + /* + From https://github.com/apache/beam/blob/master/sdks/python/apache_beam/io/tfrecordio_test.py + Created by running following code in python: + >>> import tensorflow as tf + >>> import base64 + >>> writer = tf.python_io.TFRecordWriter('/tmp/python_foo.tfrecord') + >>> writer.write('foo') + >>> writer.close() + >>> with open('/tmp/python_foo.tfrecord', 'rb') as f: + ... data = base64.b64encode(f.read()) + ... print data + */ + private static final String FOO_RECORD_BASE64 = "AwAAAAAAAACwmUkOZm9vYYq+/g=="; + + // Same as above but containing two records ['foo', 'bar'] + private static final String FOO_BAR_RECORD_BASE64 = + "AwAAAAAAAACwmUkOZm9vYYq+/gMAAAAAAAAAsJlJDmJhckYA5cg="; + private static final String BAR_FOO_RECORD_BASE64 = + "AwAAAAAAAACwmUkOYmFyRgDlyAMAAAAAAAAAsJlJDmZvb2GKvv4="; + + private static final String[] FOO_RECORDS = {"foo"}; + private static final String[] FOO_BAR_RECORDS = {"foo", "bar"}; + + private static final Iterable<String> EMPTY = Collections.emptyList(); + private static final Iterable<String> SMALL = makeLines(1, 4); + private static final Iterable<String> LARGE = makeLines(1000, 4); + private static final Iterable<String> LARGE_RECORDS = makeLines(100, 100000); + + @Rule public TemporaryFolder tempFolder = new TemporaryFolder(); + + @Rule public TestPipeline readPipeline = TestPipeline.create(); + + @Rule public TestPipeline writePipeline = TestPipeline.create(); + + @Rule public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void testReadInvalidConfigurations() { + String filePattern = "foo.*"; + String compression = "AUTO"; + + // Invalid filepattern + assertThrows( + IllegalStateException.class, + () -> { + TFRecordReadSchemaTransformConfiguration.builder() + .setValidate(true) + .setCompression(compression) + .setFilePattern(filePattern) + .build() + .validate(); + }); + + // Filepattern unset + assertThrows( + IllegalStateException.class, + () -> { + TFRecordReadSchemaTransformConfiguration.builder() + .setValidate(true) + .setCompression(compression) + // .setFilePattern(StaticValueProvider.of("vegetable")) File pattern is mandatory + .build() + .validate(); + }); + + // Validate unset + assertThrows( + IllegalStateException.class, + () -> { + TFRecordReadSchemaTransformConfiguration.builder() + // .setValidate(true) // Validate is mandatory + .setCompression(compression) + .setFilePattern(filePattern) + .build() + .validate(); + }); + + // Compression unset + assertThrows( + IllegalStateException.class, + () -> { + TFRecordReadSchemaTransformConfiguration.builder() + .setValidate(false) + // .setCompression(Compression.AUTO) // Compression is mandatory + .setFilePattern(filePattern) + .build() + .validate(); + }); + } + + @Test + public void testWriteInvalidConfigurations() throws Exception { + String fileName = "foo"; + String nonExistentPath = "abc"; + String filenameSuffix = "bar"; + String shardTemplate = "xyz"; + String compression = "AUTO"; + Integer numShards = 10; + + // Invalid outputPrefix + assertThrows( + IllegalStateException.class, + () -> { + TFRecordWriteSchemaTransformConfiguration.builder() + .setOutputPrefix(tempFolder.getRoot().toPath().toString() + nonExistentPath) + .setFilenameSuffix(filenameSuffix) + .setShardTemplate(shardTemplate) + .setNumShards(numShards) + .setCompression(compression) + .setNoSpilling(true) + .build() + .validate(); + }); + + // NumShards unset + assertThrows( + IllegalStateException.class, + () -> { + TFRecordWriteSchemaTransformConfiguration.builder() + .setOutputPrefix(tempFolder.getRoot().toPath().toString() + fileName) + .setFilenameSuffix(filenameSuffix) + .setShardTemplate(shardTemplate) + // .setNumShards(numShards) // NumShards is mandatory + .setCompression(compression) + .setNoSpilling(true) + .build() + .validate(); + }); + + // Compression unset + assertThrows( + IllegalStateException.class, + () -> { + TFRecordWriteSchemaTransformConfiguration.builder() + .setOutputPrefix(tempFolder.getRoot().toPath().toString() + fileName) + .setFilenameSuffix(filenameSuffix) + .setShardTemplate(shardTemplate) + .setNumShards(numShards) + // .setCompression(compression) // Compression is mandatory + .setNoSpilling(true) + .build() + .validate(); + }); + + // NoSpilling unset + assertThrows( + IllegalStateException.class, + () -> { + TFRecordWriteSchemaTransformConfiguration.builder() + .setOutputPrefix(tempFolder.getRoot().toPath().toString() + fileName) + .setFilenameSuffix(filenameSuffix) + .setShardTemplate(shardTemplate) + .setNumShards(numShards) + .setCompression(compression) + // .setNoSpilling(true) // NoSpilling is mandatory Review Comment: Validate was also throwing an error, which allowed it to pass. Will update. Thanks. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@beam.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org