use of com.google.cloud.teleport.spanner.ExportTransform.CreateDatabaseManifest in project DataflowTemplates by GoogleCloudPlatform.
the class ExportTransformTest method buildDatabaseManifestFile.
@Test
public void buildDatabaseManifestFile() throws InvalidProtocolBufferException {
Map<String, String> tablesAndManifests = ImmutableMap.of("table1", "table1 manifest", "table2", "table2 manifest", "changeStream", "changeStream manifest");
PCollection<List<Export.Table>> metadataTables = pipeline.apply("Initialize table manifests", Create.of(tablesAndManifests)).apply("Combine table manifests", Combine.globally(new CombineTableMetadata()));
ImmutableList<Export.DatabaseOption> databaseOptions = ImmutableList.of(Export.DatabaseOption.newBuilder().setOptionName("version_retention_period").setOptionValue("5d").build());
Ddl.Builder ddlBuilder = Ddl.builder();
ddlBuilder.mergeDatabaseOptions(databaseOptions);
ddlBuilder.createChangeStream("changeStream").endChangeStream();
Ddl ddl = ddlBuilder.build();
PCollectionView<Ddl> ddlView = pipeline.apply(Create.of(ddl)).apply(View.asSingleton());
PCollectionView<Dialect> dialectView = pipeline.apply("CreateSingleton", Create.of(Dialect.GOOGLE_STANDARD_SQL)).apply("As PCollectionView", View.asSingleton());
PCollection<String> databaseManifest = metadataTables.apply("Test adding database option to manifest", ParDo.of(new CreateDatabaseManifest(ddlView, dialectView)).withSideInputs(ddlView, dialectView));
// The output JSON may contain the tables in any order, so a string comparison is not
// sufficient. Have to convert the manifest string to a protobuf. Also for the checker function
// to be serializable, it has to be written as a lambda.
PAssert.thatSingleton(databaseManifest).satisfies((SerializableFunction<String, Void>) input -> {
Builder builder1 = Export.newBuilder();
try {
JsonFormat.parser().merge(input, builder1);
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException(e);
}
Export manifestProto = builder1.build();
assertThat(manifestProto.getTablesCount(), is(2));
assertThat(manifestProto.getDialect(), is(ProtoDialect.GOOGLE_STANDARD_SQL));
String table1Name = manifestProto.getTables(0).getName();
assertThat(table1Name, startsWith("table"));
assertThat(manifestProto.getTables(0).getManifestFile(), is(table1Name + "-manifest.json"));
Export.DatabaseOption dbOptions = manifestProto.getDatabaseOptions(0);
String optionName = dbOptions.getOptionName();
String optionValue = dbOptions.getOptionValue();
assertThat(optionName, is("version_retention_period"));
assertThat(optionValue, is("5d"));
assertThat(manifestProto.getChangeStreamsCount(), is(1));
assertThat(manifestProto.getChangeStreams(0).getName(), is("changeStream"));
assertThat(manifestProto.getChangeStreams(0).getManifestFile(), is("changeStream-manifest.json"));
return null;
});
pipeline.run();
}
Aggregations