use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testMultipleRenamesToSameField.
@Test
public void testMultipleRenamesToSameField() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, "x:z,y:z", null, null);
Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
TransformContext transformContext = new MockTransformContext();
try {
transform.initialize(transformContext);
Assert.fail();
} catch (ValidationException e) {
Assert.assertEquals(1, e.getFailures().size());
Assert.assertEquals(1, e.getFailures().get(0).getCauses().size());
Cause expectedCause = new Cause();
expectedCause.addAttribute(CauseAttributes.STAGE_CONFIG, ProjectionTransform.ProjectionTransformConfig.RENAME);
expectedCause.addAttribute(STAGE, MOCK_STAGE);
Assert.assertEquals(expectedCause, e.getFailures().get(0).getCauses().get(0));
}
}
use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testConvertNullField.
@Test
public void testConvertNullField() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "x:long", null);
Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
TransformContext transformContext = new MockTransformContext();
transform.initialize(transformContext);
Schema inputSchema = Schema.recordOf("record", Schema.Field.of("x", Schema.nullableOf(Schema.of(Schema.Type.INT))));
StructuredRecord input = StructuredRecord.builder(inputSchema).build();
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(input, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Schema expectedSchema = Schema.recordOf("record.projected", Schema.Field.of("x", Schema.nullableOf(Schema.of(Schema.Type.LONG))));
Assert.assertEquals(expectedSchema, output.getSchema());
Assert.assertNull(output.get("x"));
}
use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testRenameFields.
@Test
public void testRenameFields() throws Exception {
Schema schema = Schema.recordOf("three", Schema.Field.of("x", Schema.of(Schema.Type.INT)), Schema.Field.of("y", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("z", Schema.arrayOf(Schema.of(Schema.Type.INT))));
StructuredRecord input = StructuredRecord.builder(schema).set("x", 1).set("y", 3.14).set("z", new int[] { 1, 2, 3 }).build();
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, "x:y,y:z,z:x", null, null);
Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
TransformContext transformContext = new MockTransformContext();
transform.initialize(transformContext);
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(input, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Schema expectedSchema = Schema.recordOf("three.projected", Schema.Field.of("y", Schema.of(Schema.Type.INT)), Schema.Field.of("z", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("x", Schema.arrayOf(Schema.of(Schema.Type.INT))));
Assert.assertEquals(expectedSchema, output.getSchema());
Assert.assertEquals(1, output.<Integer>get("y").intValue());
Assert.assertTrue(Math.abs(3.14 - (Double) output.get("z")) < 0.000001);
Assert.assertArrayEquals(new int[] { 1, 2, 3 }, (int[]) output.get("x"));
}
use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testConvertToFloat.
@Test
public void testConvertToFloat() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "intField:float,longField:float", null);
Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
TransformContext transformContext = new MockTransformContext();
transform.initialize(transformContext);
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(SIMPLE_TYPES_RECORD, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Schema expectedSchema = Schema.recordOf("record.projected", Schema.Field.of("booleanField", Schema.of(Schema.Type.BOOLEAN)), Schema.Field.of("intField", Schema.of(Schema.Type.FLOAT)), Schema.Field.of("longField", Schema.of(Schema.Type.FLOAT)), Schema.Field.of("floatField", Schema.of(Schema.Type.FLOAT)), Schema.Field.of("doubleField", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("bytesField", Schema.of(Schema.Type.BYTES)), Schema.Field.of("stringField", Schema.of(Schema.Type.STRING)));
Assert.assertEquals(expectedSchema, output.getSchema());
Assert.assertTrue((Boolean) output.get("booleanField"));
Assert.assertEquals(28f, output.get("intField"), 0.0001f);
Assert.assertEquals(99f, output.get("longField"), 0.0001f);
Assert.assertTrue(Math.abs(2.71f - (Float) output.get("floatField")) < 0.000001);
Assert.assertTrue(Math.abs(3.14 - (Double) output.get("doubleField")) < 0.000001);
Assert.assertArrayEquals(Bytes.toBytes("foo"), (byte[]) output.get("bytesField"));
Assert.assertEquals("bar", output.get("stringField"));
}
use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testDropFields.
@Test
public void testDropFields() throws Exception {
Schema schema = Schema.recordOf("three", Schema.Field.of("x", Schema.of(Schema.Type.INT)), Schema.Field.of("y", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("z", Schema.arrayOf(Schema.of(Schema.Type.INT))));
StructuredRecord input = StructuredRecord.builder(schema).set("x", 1).set("y", 3.14).set("z", new int[] { 1, 2, 3 }).build();
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig("y, z", null, null, null);
Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
TransformContext transformContext = new MockTransformContext();
transform.initialize(transformContext);
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(input, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Schema expectedSchema = Schema.recordOf("three.projected", Schema.Field.of("x", Schema.of(Schema.Type.INT)));
Assert.assertEquals(expectedSchema, output.getSchema());
Assert.assertEquals(1, output.<Integer>get("x").intValue());
}
Aggregations