Merge pull request #1852 from ostephens/fix-cross

Enable Cross to work with non-string wrapped cells
This commit is contained in:
Antonin Delpeuch 2018-11-24 15:28:05 +00:00 committed by GitHub
commit 0d3c7c7af4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 6 deletions

View File

@ -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");

View File

@ -1,6 +1,8 @@
package com.google.refine.tests.expr.functions;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Properties;
@ -13,10 +15,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 +28,7 @@ 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);
@Override
@BeforeTest
@ -45,16 +50,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 +102,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.
*