From 72be6e14a492d02c9ccdfbb739c2123c121ab9e6 Mon Sep 17 00:00:00 2001 From: Owen Stephens Date: Thu, 22 Nov 2018 17:54:14 +0000 Subject: [PATCH 1/3] Update Cross tests to include tests for Wrapped cells/non-strings --- .../expr/functions/CrossFunctionTests.java | 58 ++++++++++++++++++- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/main/tests/server/src/com/google/refine/tests/expr/functions/CrossFunctionTests.java b/main/tests/server/src/com/google/refine/tests/expr/functions/CrossFunctionTests.java index 4145c6ceb..d76a4c178 100644 --- a/main/tests/server/src/com/google/refine/tests/expr/functions/CrossFunctionTests.java +++ b/main/tests/server/src/com/google/refine/tests/expr/functions/CrossFunctionTests.java @@ -1,6 +1,9 @@ package com.google.refine.tests.expr.functions; +import java.io.IOException; +import java.time.OffsetDateTime; +import java.time.format.DateTimeFormatter; import java.util.Calendar; import java.util.Properties; @@ -13,10 +16,12 @@ import org.testng.annotations.Test; import com.google.refine.expr.EvalError; import com.google.refine.expr.HasFieldsListImpl; import com.google.refine.expr.WrappedRow; +import com.google.refine.expr.WrappedCell; import com.google.refine.grel.ControlFunctionRegistry; import com.google.refine.grel.Function; import com.google.refine.model.Project; import com.google.refine.model.Row; +import com.google.refine.model.Cell; import com.google.refine.tests.RefineTest; /** @@ -24,6 +29,9 @@ import com.google.refine.tests.RefineTest; */ public class CrossFunctionTests extends RefineTest { static Properties bindings; + private static OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); + private static int integerValue = 1600; + private static Boolean booleanValue = true; @Override @BeforeTest @@ -45,16 +53,31 @@ public class CrossFunctionTests extends RefineTest { + "john,120 Main St.\n" + "mary,50 Broadway Ave.\n" + "john,999 XXXXXX St.\n" // john's 2nd address - + "anne,17 Morning Crescent\n"; + + "anne,17 Morning Crescent\n" + + "2017-05-12T05:45:00Z,dateTime\n" + + "1600,integer\n" + + "true,boolean\n"; projectAddress = createCSVProject(projectName, input); projectName = "Christmas Gifts"; input = "gift,recipient\n" + "lamp,mary\n" - + "clock,john\n"; + + "clock,john\n" + + "dateTime,2017-05-12T05:45:00Z\n" + + "integer,1600\n" + + "boolean,true\n"; projectGift = createCSVProject(projectName, input); bindings.put("project", projectGift); + + //Add some non-string value cells to each project + projectAddress.rows.get(4).cells.set(0, new Cell(dateTimeValue, null)); + projectAddress.rows.get(5).cells.set(0, new Cell(1600, null)); + projectAddress.rows.get(6).cells.set(0, new Cell(true, null)); + projectGift.rows.get(2).cells.set(1, new Cell(dateTimeValue, null)); + projectGift.rows.get(3).cells.set(1, new Cell(1600, null)); + projectGift.rows.get(4).cells.set(1, new Cell(true, null)); + // add a column address based on column recipient bindings.put("columnName", "recipient"); } @@ -82,6 +105,37 @@ public class CrossFunctionTests extends RefineTest { Assert.assertNull(invoke("cross", "Anne", "My Address Book", "friend")); } + @Test + public void crossFunctionDateTimeTest() throws Exception { + Project project = (Project) bindings.get("project"); + Cell c = project.rows.get(2).cells.get(1); + WrappedCell lookup = new WrappedCell(project, "recipient", c); + Row row = ((Row)((WrappedRow) ((HasFieldsListImpl) invoke("cross", lookup, "My Address Book", "friend")).get(0)).row); + String address = row.getCell(1).value.toString(); + Assert.assertEquals(address, "dateTime"); + } + + @Test + public void crossFunctionIntegerTest() throws Exception { + Project project = (Project) bindings.get("project"); + Cell c = project.rows.get(3).cells.get(1); + WrappedCell lookup = new WrappedCell(project, "recipient", c); + Row row = ((Row)((WrappedRow) ((HasFieldsListImpl) invoke("cross", lookup, "My Address Book", "friend")).get(0)).row); + String address = row.getCell(1).value.toString(); + Assert.assertEquals(address, "integer"); + } + + @Test + public void crossFunctionBooleanTest() throws Exception { + Project project = (Project) bindings.get("project"); + Cell c = project.rows.get(4).cells.get(1); + WrappedCell lookup = new WrappedCell(project, "recipient", c); + Row row = ((Row)((WrappedRow) ((HasFieldsListImpl) invoke("cross", lookup, "My Address Book", "friend")).get(0)).row); + String address = row.getCell(1).value.toString(); + Assert.assertEquals(address, "boolean"); + } + + /** * If no match, return null. * From d1acd9336c0c55ad606df0eb6fc57137f216a344 Mon Sep 17 00:00:00 2001 From: Owen Stephens Date: Thu, 22 Nov 2018 17:54:29 +0000 Subject: [PATCH 2/3] Update Cross function to accept non-string wrapped cells --- main/src/com/google/refine/expr/functions/Cross.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/main/src/com/google/refine/expr/functions/Cross.java b/main/src/com/google/refine/expr/functions/Cross.java index 93189212b..70347a941 100644 --- a/main/src/com/google/refine/expr/functions/Cross.java +++ b/main/src/com/google/refine/expr/functions/Cross.java @@ -67,10 +67,11 @@ public class Cross implements Function { (String) toProjectName, (String) toColumnName ); - - String srcValue = v instanceof String ? (String)v : (String)((WrappedCell) v).cell.value; - - return join.getRows(srcValue); + if(v instanceof String) { + return join.getRows(v); + } else { + return join.getRows(((WrappedCell) v).cell.value); + } } } return new EvalError(ControlFunctionRegistry.getFunctionName(this) + " expects a string or cell, a project name to join with, and a column name in that project"); From 04d2e04b4cda9b1589349b5882143e620eef0d55 Mon Sep 17 00:00:00 2001 From: Owen Stephens Date: Fri, 23 Nov 2018 13:50:34 +0000 Subject: [PATCH 3/3] Tidy up Cross tests --- .../google/refine/tests/expr/functions/CrossFunctionTests.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/main/tests/server/src/com/google/refine/tests/expr/functions/CrossFunctionTests.java b/main/tests/server/src/com/google/refine/tests/expr/functions/CrossFunctionTests.java index d76a4c178..289df274c 100644 --- a/main/tests/server/src/com/google/refine/tests/expr/functions/CrossFunctionTests.java +++ b/main/tests/server/src/com/google/refine/tests/expr/functions/CrossFunctionTests.java @@ -1,7 +1,6 @@ package com.google.refine.tests.expr.functions; -import java.io.IOException; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; import java.util.Calendar; @@ -30,8 +29,6 @@ import com.google.refine.tests.RefineTest; public class CrossFunctionTests extends RefineTest { static Properties bindings; private static OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); - private static int integerValue = 1600; - private static Boolean booleanValue = true; @Override @BeforeTest