Repository: incubator-datafu
Updated Branches:
  refs/heads/master d4a5c5d43 -> b2134e660


DATAFU-33 add unit test for alias eval func udf

https://issues.apache.org/jira/browse/DATAFU-33

Signed-off-by: Matthew Hayes <matthew.terence.ha...@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-datafu/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-datafu/commit/b2134e66
Tree: http://git-wip-us.apache.org/repos/asf/incubator-datafu/tree/b2134e66
Diff: http://git-wip-us.apache.org/repos/asf/incubator-datafu/diff/b2134e66

Branch: refs/heads/master
Commit: b2134e66063b06ab1fd7813368faa30ecf945f78
Parents: d4a5c5d
Author: Jian J. Wang <wj...@dogfavorshot-lm.peking.corp.yahoo.com>
Authored: Sun Mar 2 20:32:47 2014 +0800
Committer: Matthew Hayes <matthew.terence.ha...@gmail.com>
Committed: Sun Mar 2 17:06:09 2014 -0800

----------------------------------------------------------------------
 .../datafu/test/pig/util/AliasEvalFuncTest.java | 95 ++++++++++++++++++++
 1 file changed, 95 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-datafu/blob/b2134e66/test/pig/datafu/test/pig/util/AliasEvalFuncTest.java
----------------------------------------------------------------------
diff --git a/test/pig/datafu/test/pig/util/AliasEvalFuncTest.java 
b/test/pig/datafu/test/pig/util/AliasEvalFuncTest.java
new file mode 100644
index 0000000..3b23d35
--- /dev/null
+++ b/test/pig/datafu/test/pig/util/AliasEvalFuncTest.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package datafu.test.pig.util;
+
+import static org.testng.Assert.*;
+
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.io.IOException;
+
+import org.apache.pig.data.Tuple;
+import org.apache.pig.data.DataBag;
+import org.apache.pig.data.TupleFactory;
+import org.apache.pig.data.BagFactory;
+import org.apache.pig.pigunit.PigTest;
+import org.apache.pig.data.DataType;
+import org.apache.pig.impl.logicalLayer.schema.Schema;
+import org.apache.pig.impl.logicalLayer.schema.Schema.FieldSchema;
+
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+import datafu.test.pig.PigTests;
+import datafu.pig.util.AliasableEvalFunc;
+
+public class AliasEvalFuncTest extends PigTests
+{
+  static class ReportBuilder extends AliasableEvalFunc<DataBag> {
+    static final String ORDERED_ROUTES = "orderedRoutes";
+
+    public DataBag exec(Tuple input) throws IOException {
+       DataBag inputBag = getBag(input, ORDERED_ROUTES);
+       DataBag outputBag = BagFactory.getInstance().newDefaultBag();
+       for(Iterator<Tuple> tupleIter = inputBag.iterator(); 
tupleIter.hasNext(); ) {
+           outputBag.add(tupleIter.next());
+       }
+       return outputBag;
+    }
+
+    public Schema getOutputSchema(Schema input) {
+       try {
+            Schema bagSchema = input.getField(0).schema;
+            Schema outputSchema = new Schema(new 
Schema.FieldSchema(getSchemaName(this.getClass()
+                                                                    .getName()
+                                                                    
.toLowerCase(), input),
+                                                                    bagSchema,
+                                                                    
DataType.BAG));
+            return outputSchema;
+       } catch (Exception ex) {
+            return null;
+       }
+    }
+  }
+
+  @Test
+  public void getBagTest() throws Exception
+  {
+     ReportBuilder udf = new ReportBuilder();
+     udf.setUDFContextSignature("test");
+     List<Schema.FieldSchema> fieldSchemaList = new 
ArrayList<Schema.FieldSchema>();
+     fieldSchemaList.add(new Schema.FieldSchema("msisdn", DataType.LONG));
+     fieldSchemaList.add(new Schema.FieldSchema("ts", DataType.INTEGER));
+     fieldSchemaList.add(new Schema.FieldSchema("center_lon", 
DataType.DOUBLE));
+     fieldSchemaList.add(new Schema.FieldSchema("center_lat", 
DataType.DOUBLE));
+     Schema schemaTuple = new Schema(fieldSchemaList);
+     Schema schemaBag = new Schema(new 
Schema.FieldSchema(ReportBuilder.ORDERED_ROUTES, schemaTuple, DataType.BAG));
+     udf.outputSchema(schemaBag);
+
+     Tuple inputTuple = TupleFactory.getInstance().newTuple();
+     DataBag inputBag = BagFactory.getInstance().newDefaultBag();
+     
inputBag.add(TupleFactory.getInstance().newTuple(Arrays.asList(71230000000L, 
1382351612, 10.697, 20.713)));
+     inputTuple.append(inputBag);
+     DataBag outputBag = udf.exec(inputTuple);
+     Assert.assertEquals(inputBag, outputBag);
+  }
+}

Reply via email to