From e1014bfeed6e698384438a5e592be2389a004f86 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Sat, 27 Oct 2018 21:43:22 +0200 Subject: [PATCH] Fix rendering of integers in Wikidata extension. Closes #1775 --- .../wikidata/schema/WbStringVariable.java | 6 +++++- .../wikidata/schema/WbStringVariableTest.java | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java index cd02e7883..7385d35eb 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/schema/WbStringVariable.java @@ -58,7 +58,11 @@ public class WbStringVariable extends WbVariableExpr { public StringValue fromCell(Cell cell, ExpressionContext ctxt) throws SkipSchemaExpressionException { if (!cell.value.toString().isEmpty()) { - return Datamodel.makeStringValue(cell.value.toString()); + String stringValue = cell.value.toString(); + if (cell.value instanceof Double && ((Double)cell.value) % 1 == 0) { + stringValue = Integer.toString(((Double)cell.value).intValue()); + } + return Datamodel.makeStringValue(stringValue); } throw new SkipSchemaExpressionException(); } diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbStringVariableTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbStringVariableTest.java index 036c9bd10..3cace300c 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbStringVariableTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/schema/WbStringVariableTest.java @@ -27,6 +27,8 @@ import org.testng.annotations.Test; import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.interfaces.StringValue; +import com.google.refine.model.Cell; + public class WbStringVariableTest extends WbVariableTest { @Override @@ -52,6 +54,23 @@ public class WbStringVariableTest extends WbVariableTest { public void testTrailingWhitespace() { evaluatesTo(Datamodel.makeStringValue("dirty \t"), "dirty \t"); } + + /** + * Test that integers are correctly converted to strings + */ + @Test + public void testInteger() { + evaluatesTo(Datamodel.makeStringValue("45"), new Cell(45,null)); + } + + /** + * Test that floating point numbers with no decimal part are also converted + */ + @Test + public void testDoubleInteger() { + evaluatesTo(Datamodel.makeStringValue("45"), new Cell(45.0,null)); + } + @Test public void testLeadingWhitespace() {