use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testSameFieldMultipleRenames.
@Test
public void testSameFieldMultipleRenames() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, "x:z,x:y", 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(2, e.getFailures().get(0).getCauses().size());
}
}
use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testConvertToString.
@Test
public void testConvertToString() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "booleanField:string,intField:string,longField:string,floatField:string," + "doubleField:string,bytesField:string,stringField:string", 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.STRING)), Schema.Field.of("intField", Schema.of(Schema.Type.STRING)), Schema.Field.of("longField", Schema.of(Schema.Type.STRING)), Schema.Field.of("floatField", Schema.of(Schema.Type.STRING)), Schema.Field.of("doubleField", Schema.of(Schema.Type.STRING)), Schema.Field.of("bytesField", Schema.of(Schema.Type.STRING)), Schema.Field.of("stringField", Schema.of(Schema.Type.STRING)));
Assert.assertEquals(expectedSchema, output.getSchema());
Assert.assertEquals("true", output.get("booleanField"));
Assert.assertEquals("28", output.get("intField"));
Assert.assertEquals("99", output.get("longField"));
Assert.assertEquals("2.71", output.get("floatField"));
Assert.assertEquals("3.14", output.get("doubleField"));
Assert.assertEquals("foo", 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 testDropRenameConvert.
@Test
public void testDropRenameConvert() throws Exception {
Schema schema = Schema.recordOf("record", Schema.Field.of("x", Schema.of(Schema.Type.INT)), Schema.Field.of("y", Schema.nullableOf(Schema.of(Schema.Type.INT))));
StructuredRecord input = StructuredRecord.builder(schema).set("x", 5).set("y", 10).build();
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig("x", "y:x", "y:string", 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("record.projected", Schema.Field.of("x", Schema.nullableOf(Schema.of(Schema.Type.STRING))));
Assert.assertEquals(expectedSchema, output.getSchema());
Assert.assertEquals("10", output.get("x"));
}
use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testConvertFromString.
@Test
public void testConvertFromString() throws Exception {
Schema schema = Schema.recordOf("record", Schema.Field.of("booleanField", Schema.of(Schema.Type.STRING)), Schema.Field.of("intField", Schema.of(Schema.Type.STRING)), Schema.Field.of("longField", Schema.of(Schema.Type.STRING)), Schema.Field.of("floatField", Schema.of(Schema.Type.STRING)), Schema.Field.of("doubleField", Schema.of(Schema.Type.STRING)), Schema.Field.of("bytesField", Schema.of(Schema.Type.STRING)), Schema.Field.of("stringField", Schema.of(Schema.Type.STRING)));
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "booleanField:boolean,intField:int,longField:long,floatField:float," + "doubleField:double,bytesField:bytes,stringField:string", null);
Transform<StructuredRecord, StructuredRecord> transform = new ProjectionTransform(config);
TransformContext transformContext = new MockTransformContext();
transform.initialize(transformContext);
StructuredRecord input = StructuredRecord.builder(schema).set("booleanField", "true").set("intField", "28").set("longField", "99").set("floatField", "2.71").set("doubleField", "3.14").set("bytesField", "foo").set("stringField", "bar").build();
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(input, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Schema expectedSchema = Schema.recordOf(SIMPLE_TYPES_SCHEMA.getRecordName() + ".projected", SIMPLE_TYPES_SCHEMA.getFields());
Assert.assertEquals(expectedSchema, output.getSchema());
Assert.assertTrue((Boolean) output.get("booleanField"));
Assert.assertEquals(28, output.<Integer>get("intField").intValue());
Assert.assertEquals(99L, output.<Long>get("longField").longValue());
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 testConvertToInt.
@Test
public void testConvertToInt() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "doubleField:int,floatField:int", 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.INT)), Schema.Field.of("longField", Schema.of(Schema.Type.LONG)), Schema.Field.of("floatField", Schema.of(Schema.Type.INT)), Schema.Field.of("doubleField", Schema.of(Schema.Type.INT)), 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(28, output.<Integer>get("intField").intValue());
Assert.assertEquals(99L, output.<Long>get("longField").longValue());
Assert.assertEquals(3, output.<Integer>get("floatField").intValue());
Assert.assertEquals(3, output.<Integer>get("doubleField").intValue());
Assert.assertArrayEquals(Bytes.toBytes("foo"), (byte[]) output.get("bytesField"));
Assert.assertEquals("bar", output.get("stringField"));
}
Aggregations