Skip to content

Commit b59a742

Browse files
committed
[BEAM-2682] Deletes AvroIOTransformTest
Instead, merges the little test coverage it provided into AvroIOTest.
1 parent ec052bb commit b59a742

2 files changed

Lines changed: 97 additions & 350 deletions

File tree

sdks/java/core/src/test/java/org/apache/beam/sdk/io/AvroIOTest.java

Lines changed: 97 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,105 @@ public GenericClass apply(GenericRecord input) {
165165
}
166166
}
167167

168+
private static final String SCHEMA_STRING =
169+
"{\"namespace\": \"example.avro\",\n"
170+
+ " \"type\": \"record\",\n"
171+
+ " \"name\": \"AvroGeneratedUser\",\n"
172+
+ " \"fields\": [\n"
173+
+ " {\"name\": \"name\", \"type\": \"string\"},\n"
174+
+ " {\"name\": \"favorite_number\", \"type\": [\"int\", \"null\"]},\n"
175+
+ " {\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]}\n"
176+
+ " ]\n"
177+
+ "}";
178+
179+
private static final Schema SCHEMA = new Schema.Parser().parse(SCHEMA_STRING);
180+
168181
@Test
169182
@Category(NeedsRunner.class)
170-
public void testAvroIOWriteAndReadAndParseASingleFile() throws Throwable {
183+
public void testAvroIOWriteAndReadJavaClass() throws Throwable {
184+
List<GenericClass> values =
185+
ImmutableList.of(new GenericClass(3, "hi"), new GenericClass(5, "bar"));
186+
File outputFile = tmpFolder.newFile("output.avro");
187+
188+
writePipeline
189+
.apply(Create.of(values))
190+
.apply(
191+
AvroIO.write(GenericClass.class)
192+
.to(writePipeline.newProvider(outputFile.getAbsolutePath()))
193+
.withoutSharding());
194+
writePipeline.run().waitUntilFinish();
195+
196+
PAssert.that(
197+
readPipeline.apply(
198+
"Read",
199+
AvroIO.read(GenericClass.class)
200+
.from(readPipeline.newProvider(outputFile.getAbsolutePath()))))
201+
.containsInAnyOrder(values);
202+
203+
readPipeline.run();
204+
}
205+
206+
@Test
207+
@Category(NeedsRunner.class)
208+
public void testAvroIOWriteAndReadGeneratedClassWithSchema() throws Throwable {
209+
File outputFile = tmpFolder.newFile("output.avro");
210+
211+
List<GenericRecord> values =
212+
ImmutableList.<GenericRecord>of(
213+
new AvroGeneratedUser("Bob", 256, null),
214+
new AvroGeneratedUser("Alice", 128, null),
215+
new AvroGeneratedUser("Ted", null, "white"));
216+
217+
writePipeline
218+
.apply(Create.of(values))
219+
.apply(
220+
AvroIO.writeGenericRecords(SCHEMA)
221+
.to(writePipeline.newProvider(outputFile.getAbsolutePath()))
222+
.withoutSharding());
223+
writePipeline.run().waitUntilFinish();
224+
225+
PAssert.that(
226+
readPipeline.apply(
227+
"Read",
228+
AvroIO.readGenericRecords(SCHEMA)
229+
.from(readPipeline.newProvider(outputFile.getAbsolutePath()))))
230+
.containsInAnyOrder(values);
231+
232+
readPipeline.run();
233+
}
234+
235+
@Test
236+
@Category(NeedsRunner.class)
237+
public void testAvroIOWriteAndReadGeneratedClassWithSchemaString() throws Throwable {
238+
File outputFile = tmpFolder.newFile("output.avro");
239+
240+
List<GenericRecord> values =
241+
ImmutableList.<GenericRecord>of(
242+
new AvroGeneratedUser("Bob", 256, null),
243+
new AvroGeneratedUser("Alice", 128, null),
244+
new AvroGeneratedUser("Ted", null, "white"));
245+
246+
writePipeline
247+
.apply(Create.of(values))
248+
.apply(
249+
AvroIO.writeGenericRecords(SCHEMA.toString())
250+
.to(writePipeline.newProvider(outputFile.getAbsolutePath()))
251+
.withoutSharding());
252+
writePipeline.run().waitUntilFinish();
253+
254+
PAssert.that(
255+
readPipeline.apply(
256+
"Read",
257+
AvroIO.readGenericRecords(SCHEMA.toString())
258+
.from(readPipeline.newProvider(outputFile.getAbsolutePath()))))
259+
.containsInAnyOrder(values);
260+
261+
readPipeline.run();
262+
}
263+
264+
@Test
265+
@Category(NeedsRunner.class)
266+
public void testAvroIOWriteAndReadAndParseJavaClassSingleFile() throws Throwable {
171267
List<GenericClass> values =
172268
ImmutableList.of(new GenericClass(3, "hi"), new GenericClass(5, "bar"));
173269
File outputFile = tmpFolder.newFile("output.avro");
@@ -220,31 +316,6 @@ public void testAvroIOWriteAndReadAndParseASingleFile() throws Throwable {
220316
readPipeline.run();
221317
}
222318

223-
@Test
224-
@Category(NeedsRunner.class)
225-
public void testAvroIOWriteAndReadViaValueProvider() throws Throwable {
226-
List<GenericClass> values =
227-
ImmutableList.of(new GenericClass(3, "hi"), new GenericClass(5, "bar"));
228-
File outputFile = tmpFolder.newFile("output.avro");
229-
230-
writePipeline
231-
.apply(Create.of(values))
232-
.apply(
233-
AvroIO.write(GenericClass.class)
234-
.to(writePipeline.newProvider(outputFile.getAbsolutePath()))
235-
.withoutSharding());
236-
writePipeline.run().waitUntilFinish();
237-
238-
PAssert.that(
239-
readPipeline.apply(
240-
"Read",
241-
AvroIO.read(GenericClass.class)
242-
.from(readPipeline.newProvider(outputFile.getAbsolutePath()))))
243-
.containsInAnyOrder(values);
244-
245-
readPipeline.run();
246-
}
247-
248319
@Test
249320
@Category(NeedsRunner.class)
250321
public void testAvroIOWriteAndReadMultipleFilepatterns() throws Throwable {

0 commit comments

Comments
 (0)