commit
0af6f5ed08
@ -33,12 +33,14 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
package com.google.refine.browsing;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONWriter;
|
||||
|
||||
import com.google.refine.Jsonizable;
|
||||
import com.google.refine.util.StringUtils;
|
||||
|
||||
/**
|
||||
* Store a value and its text label, in case the value is not a string itself.
|
||||
@ -52,8 +54,12 @@ public class DecoratedValue implements Jsonizable {
|
||||
final public String label;
|
||||
|
||||
public DecoratedValue(Object value, String label) {
|
||||
this.value = value;
|
||||
this.label = label;
|
||||
if (value instanceof OffsetDateTime) {
|
||||
this.value = StringUtils.toString(value);
|
||||
} else {
|
||||
this.value = value;
|
||||
}
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -59,6 +59,7 @@ import com.google.refine.model.changes.CellChange;
|
||||
import com.google.refine.operations.EngineDependentMassCellOperation;
|
||||
import com.google.refine.operations.OperationRegistry;
|
||||
import com.google.refine.util.ParsingUtilities;
|
||||
import com.google.refine.util.StringUtils;
|
||||
|
||||
public class MassEditOperation extends EngineDependentMassCellOperation {
|
||||
final protected String _expression;
|
||||
@ -259,7 +260,7 @@ public class MassEditOperation extends EngineDependentMassCellOperation {
|
||||
newCell = new Cell(fromErrorTo, (cell != null) ? cell.recon : null);
|
||||
}
|
||||
} else if (ExpressionUtils.isNonBlankData(v)) {
|
||||
String from = v.toString();
|
||||
String from = StringUtils.toString(v);
|
||||
Serializable to = fromTo.get(from);
|
||||
if (to != null) {
|
||||
newCell = new Cell(to, (cell != null) ? cell.recon : null);
|
||||
|
@ -1,11 +1,8 @@
|
||||
package com.google.refine.util;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
|
||||
public class StringUtils {
|
||||
private static String DEFAULT_PATTERN = "dd-MMM-yyyy";
|
||||
|
||||
/**
|
||||
* String formatting method that knows how to format dates (using the default locale's date formatter)
|
||||
* @param o object to be converted to a string
|
||||
@ -15,7 +12,7 @@ public class StringUtils {
|
||||
// to replace the DateFormat with java.time.format.DateTimeFormatter
|
||||
if (o instanceof OffsetDateTime) {
|
||||
OffsetDateTime odt = (OffsetDateTime)o;
|
||||
return odt.format(DateTimeFormatter.ofPattern(DEFAULT_PATTERN));
|
||||
return ParsingUtilities.dateToString((OffsetDateTime) odt);
|
||||
} else if (o == null) {
|
||||
return "";
|
||||
} else {
|
||||
@ -23,4 +20,3 @@ public class StringUtils {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,180 @@
|
||||
/*
|
||||
|
||||
Copyright 2018, Owen Stephens
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use in source and binary forms, with or without
|
||||
modification, are permitted provided that the following conditions are
|
||||
met:
|
||||
|
||||
* Redistributions of source code must retain the above copyright
|
||||
notice, this list of conditions and the following disclaimer.
|
||||
* Redistributions in binary form must reproduce the above
|
||||
copyright notice, this list of conditions and the following disclaimer
|
||||
in the documentation and/or other materials provided with the
|
||||
distribution.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
|
||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
*/
|
||||
|
||||
package com.google.refine.tests.browsing.util;
|
||||
|
||||
import com.google.refine.expr.MetaParser;
|
||||
import org.testng.annotations.AfterMethod;
|
||||
import org.testng.Assert;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Properties;
|
||||
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import com.google.refine.browsing.util.ExpressionNominalValueGrouper;
|
||||
import com.google.refine.expr.Evaluable;
|
||||
import com.google.refine.model.Cell;
|
||||
import com.google.refine.model.ModelException;
|
||||
import com.google.refine.model.Project;
|
||||
import com.google.refine.model.Row;
|
||||
import com.google.refine.tests.RefineTest;
|
||||
|
||||
|
||||
public class ExpressionNominalValueGrouperTests extends RefineTest {
|
||||
// dependencies
|
||||
//Variables
|
||||
private static Project project;
|
||||
private static Properties bindings;
|
||||
|
||||
private static OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
||||
private static String dateTimeStringValue = "2017-05-12T05:45:00Z";
|
||||
private static int integerValue = 1;
|
||||
private static String integerStringValue = "1";
|
||||
private static String stringStringValue = "a";
|
||||
|
||||
private static ExpressionNominalValueGrouper grouper;
|
||||
private static Evaluable eval;
|
||||
private static final int cellIndex = 0;
|
||||
private static final String columnName = "Col1";
|
||||
private static final int numberOfRows = 5;
|
||||
private static final String projectName = "ExpressionNominalValueGrouper";
|
||||
|
||||
|
||||
@Override
|
||||
@BeforeTest
|
||||
public void init() {
|
||||
logger = LoggerFactory.getLogger(this.getClass());
|
||||
}
|
||||
|
||||
@BeforeMethod
|
||||
public void setUp() throws JSONException, IOException, ModelException {
|
||||
project = createProjectWithColumns(projectName, columnName);
|
||||
bindings = new Properties();
|
||||
bindings.put("project", project);
|
||||
}
|
||||
|
||||
@AfterMethod
|
||||
public void tearDown() {
|
||||
project = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expressionNominalValueGrouperStrings() throws Exception {
|
||||
//populate project
|
||||
// Five rows of a's
|
||||
for (int i = 0; i < numberOfRows; i++) {
|
||||
Row row = new Row(1);
|
||||
row.setCell(0, new Cell(stringStringValue, null));
|
||||
project.rows.add(row);
|
||||
}
|
||||
//create grouper
|
||||
eval = MetaParser.parse("value");
|
||||
grouper = new ExpressionNominalValueGrouper(eval, columnName, cellIndex);
|
||||
try {
|
||||
grouper.start(project);
|
||||
for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
|
||||
Row row = project.rows.get(rowIndex);
|
||||
grouper.visit(project, rowIndex, row);
|
||||
}
|
||||
} finally {
|
||||
grouper.end(project);
|
||||
}
|
||||
|
||||
Assert.assertEquals(grouper.choices.size(),1);
|
||||
|
||||
Assert.assertTrue(grouper.choices.containsKey(stringStringValue));
|
||||
Assert.assertEquals(grouper.choices.get(stringStringValue).decoratedValue.label,stringStringValue);
|
||||
Assert.assertEquals(grouper.choices.get(stringStringValue).decoratedValue.value.toString(),stringStringValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expressionNominalValueGrouperInts() throws Exception {
|
||||
//populate project
|
||||
for (int i = 0; i < numberOfRows; i++) {
|
||||
Row row = new Row(1);
|
||||
row.setCell(0, new Cell(integerValue, null));
|
||||
project.rows.add(row);
|
||||
}
|
||||
//create grouper
|
||||
eval = MetaParser.parse("value");
|
||||
grouper = new ExpressionNominalValueGrouper(eval, columnName, cellIndex);
|
||||
try {
|
||||
grouper.start(project);
|
||||
for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
|
||||
Row row = project.rows.get(rowIndex);
|
||||
grouper.visit(project, rowIndex, row);
|
||||
}
|
||||
} finally {
|
||||
grouper.end(project);
|
||||
}
|
||||
|
||||
Assert.assertEquals(grouper.choices.size(),1);
|
||||
|
||||
Assert.assertTrue(grouper.choices.containsKey(integerStringValue));
|
||||
Assert.assertEquals(grouper.choices.get(integerStringValue).decoratedValue.label,integerStringValue);
|
||||
Assert.assertEquals(grouper.choices.get(integerStringValue).decoratedValue.value.toString(),integerStringValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expressionNominalValueGrouperDates() throws Exception {
|
||||
//populate project
|
||||
for (int i = 0; i < numberOfRows; i++) {
|
||||
Row row = new Row(1);
|
||||
row.setCell(0, new Cell(dateTimeValue, null));
|
||||
project.rows.add(row);
|
||||
}
|
||||
//create grouper
|
||||
eval = MetaParser.parse("value");
|
||||
grouper = new ExpressionNominalValueGrouper(eval, columnName, cellIndex);
|
||||
try {
|
||||
grouper.start(project);
|
||||
for (int rowIndex = 0; rowIndex < numberOfRows; rowIndex++) {
|
||||
Row row = project.rows.get(rowIndex);
|
||||
grouper.visit(project, rowIndex, row);
|
||||
}
|
||||
} finally {
|
||||
grouper.end(project);
|
||||
}
|
||||
|
||||
Assert.assertEquals(grouper.choices.size(),1);
|
||||
|
||||
Assert.assertTrue(grouper.choices.containsKey(dateTimeStringValue));
|
||||
Assert.assertEquals(grouper.choices.get(dateTimeStringValue).decoratedValue.label,dateTimeStringValue);
|
||||
Assert.assertEquals(grouper.choices.get(dateTimeStringValue).decoratedValue.value.toString(),dateTimeStringValue);
|
||||
}
|
||||
}
|
@ -119,7 +119,7 @@ public class ToFromConversionTests extends RefineTest {
|
||||
|
||||
String inputDate = "2013-06-01";
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate)),
|
||||
"01-Jun-2013");
|
||||
"2013-06-01T00:00:00Z");
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate), "yyyy-MM-dd"),
|
||||
"2013-06-01");
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate), "yyyy/dd/MM"), "2013/01/06");
|
||||
@ -127,7 +127,7 @@ public class ToFromConversionTests extends RefineTest {
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDate), "yyyy-MM-dd hh:mm:ss"), "2013-06-01 12:00:00");
|
||||
|
||||
String inputDateTime = "2013-06-01 13:12:11";
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime)), "01-Jun-2013");
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime)), "2013-06-01T13:12:11Z");
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime), "yyyy-MM-dd"), "2013-06-01");
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime), "yyyy-MM-dd hh:mm:ss"),"2013-06-01 01:12:11");
|
||||
Assert.assertEquals(invoke("toString", CalendarParser.parseAsOffsetDateTime(inputDateTime), "yyyy-MM-dd HH:mm:ss"),"2013-06-01 13:12:11");
|
||||
|
@ -68,6 +68,18 @@ public class MassOperationTests extends RefineTest {
|
||||
Assert.assertFalse(editList.get(0).fromError);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReconstructEditDate() throws Exception {
|
||||
editsString = "[{\"from\":[\"2018-10-04T00:00:00Z\"],\"to\":\"newString\",\"type\":\"text\"}]";
|
||||
|
||||
editList = MassEditOperation.reconstructEdits(ParsingUtilities.evaluateJsonStringToArray(editsString));
|
||||
|
||||
Assert.assertEquals(editList.get(0).from.get(0), "2018-10-04T00:00:00Z");
|
||||
Assert.assertEquals(editList.get(0).to,"newString" );
|
||||
Assert.assertFalse(editList.get(0).fromBlank);
|
||||
Assert.assertFalse(editList.get(0).fromError);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testReconstructEditEmpty() throws Exception {
|
||||
editsString = "[{\"from\":[\"\"],\"to\":\"newString\",\"type\":\"text\"}]";
|
||||
@ -83,6 +95,5 @@ public class MassOperationTests extends RefineTest {
|
||||
}
|
||||
|
||||
//Not yet testing for mass edit from OR Error
|
||||
//Not yet testing for mass edit from OR Date
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user