use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class JavaScriptTransformTest method testEmitAlerts.
@Test
public void testEmitAlerts() throws Exception {
JavaScriptTransform.Config config = new JavaScriptTransform.Config("function transform(input, emitter, context) { " + "emitter.emitAlert({'x': input.x + ''});" + " }", null, null);
JavaScriptTransform transform = new JavaScriptTransform(config);
MockTransformContext transformContext = new MockTransformContext();
transform.initialize(transformContext);
Schema schema = Schema.recordOf("x", Schema.Field.of("x", Schema.of(Schema.Type.INT)));
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(StructuredRecord.builder(schema).set("x", 2).build(), emitter);
Assert.assertEquals(ImmutableList.of(ImmutableMap.of("x", "2")), emitter.getAlerts());
}
use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class JavaScriptTransformTest method testDropAndRename.
@Test
public void testDropAndRename() throws Exception {
Schema outputSchema = Schema.recordOf("smallerSchema", Schema.Field.of("x", Schema.of(Schema.Type.INT)), Schema.Field.of("y", Schema.of(Schema.Type.LONG)));
JavaScriptTransform.Config config = new JavaScriptTransform.Config("function transform(input, emitter, context) { var returnMap = {}; " + "returnMap['x'] = input.intField; " + "returnMap['y'] = input.longField; " + "emitter.emit(returnMap); }", outputSchema.toString(), null);
Transform<StructuredRecord, StructuredRecord> transform = new JavaScriptTransform(config);
transform.initialize(new MockTransformContext());
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(RECORD1, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
Assert.assertEquals(outputSchema, output.getSchema());
Assert.assertEquals(28, output.<Integer>get("x").intValue());
Assert.assertEquals(99L, output.<Long>get("y").longValue());
}
use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class JavaScriptTransformTest method testLookup.
@Test
public void testLookup() throws Exception {
JavaScriptTransform.Config config = new JavaScriptTransform.Config("function transform(x, emitter, ctx) { " + "var single = ctx.getLookup('purchases').lookup('abc');" + "var batch = ctx.getLookup('purchases').lookup(['abc', 'sdf']);" + "x.stringField = '1_' + single + ' 2_' + batch['abc'] + batch['sdf'] + '::' + batch.abc;" + "emitter.emit(x);" + "}", null, new LookupConfig(ImmutableMap.of("purchases", new LookupTableConfig(LookupTableConfig.TableType.DATASET))));
Transform<StructuredRecord, StructuredRecord> transform = new JavaScriptTransform(config);
transform.initialize(new MockTransformContext("somestage", new HashMap<String, String>(), new MockLookupProvider(TEST_LOOKUP)));
MockEmitter<StructuredRecord> emitter = new MockEmitter<>();
transform.transform(STRING_RECORD, emitter);
StructuredRecord output = emitter.getEmitted().get(0);
// check record1
Assert.assertEquals(STRING_SCHEMA, output.getSchema());
Assert.assertEquals("1_abc 2_abcsdf::abc", output.get("stringField"));
}
use of io.cdap.cdap.etl.mock.transform.MockTransformContext in project hydrator-plugins by cdapio.
the class ProjectionTransformTest method testSameFieldMultipleConverts.
@Test
public void testSameFieldMultipleConverts() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "x:int,x:long", 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.CONVERT);
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 testConvertToDouble.
@Test
public void testConvertToDouble() throws Exception {
ProjectionTransform.ProjectionTransformConfig config = new ProjectionTransform.ProjectionTransformConfig(null, null, "intField:double,longField:double,floatField:double", 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.DOUBLE)), Schema.Field.of("longField", Schema.of(Schema.Type.DOUBLE)), Schema.Field.of("floatField", Schema.of(Schema.Type.DOUBLE)), 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(28d, output.get("intField"), 0.0001d);
Assert.assertEquals(99d, output.get("longField"), 0.0001d);
Assert.assertTrue(Math.abs(2.71 - (Double) 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"));
}
Aggregations