Add tests for grouper for list facet

This commit is contained in:
Owen Stephens 2018-06-28 06:07:41 +01:00
parent 5387e91493
commit 22072c498b
2 changed files with 78 additions and 21 deletions

View File

@ -57,15 +57,6 @@ import com.google.refine.util.StringUtils;
* from a given expression.
*/
public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor {
static public class IndexedNominalFacetChoice extends NominalFacetChoice {
int _latestIndex;
public IndexedNominalFacetChoice(DecoratedValue decoratedValue, int latestIndex) {
super(decoratedValue);
_latestIndex = latestIndex;
}
}
/*
* Configuration
*/
@ -77,14 +68,29 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
* Computed results
*/
final public Map<Object, IndexedNominalFacetChoice> choices = new HashMap<Object, IndexedNominalFacetChoice>();
public int numberCount = 0;
public int datetimeCount = 0;
public int booleanCount = 0;
public int blankCount = 0;
public int errorCount = 0;
/*
* Scratch pad variables
*/
protected boolean hasNumber;
protected boolean hasDateTime;
protected boolean hasBoolean;
protected boolean hasBlank;
protected boolean hasError;
static public class IndexedNominalFacetChoice extends NominalFacetChoice {
int _latestIndex;
public IndexedNominalFacetChoice(DecoratedValue decoratedValue, int latestIndex) {
super(decoratedValue);
_latestIndex = latestIndex;
}
}
public ExpressionNominalValueGrouper(Evaluable evaluable, String columnName, int cellIndex) {
_evaluable = evaluable;
@ -104,13 +110,25 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
@Override
public boolean visit(Project project, int rowIndex, Row row) {
hasNumber = false;
hasDateTime = false;
hasBoolean = false;
hasError = false;
hasBlank = false;
Properties bindings = ExpressionUtils.createBindings(project);
visitRow(project, rowIndex, row, bindings, rowIndex);
if (hasNumber) {
numberCount++;
}
if (hasDateTime) {
datetimeCount++;
}
if (hasBoolean) {
booleanCount++;
}
if (hasError) {
errorCount++;
}
@ -123,6 +141,9 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
@Override
public boolean visit(Project project, Record record) {
hasNumber = false;
hasDateTime = false;
hasBoolean = false;
hasError = false;
hasBlank = false;
@ -133,6 +154,15 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
visitRow(project, r, row, bindings, record.recordIndex);
}
if (hasNumber) {
numberCount++;
}
if (hasDateTime) {
datetimeCount++;
}
if (hasBoolean) {
booleanCount++;
}
if (hasError) {
errorCount++;
}
@ -174,6 +204,12 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
protected void processValue(Object value, int index) {
if (ExpressionUtils.isError(value)) {
hasError = true;
} else if (ExpressionUtils.isNumber(value)) {
hasNumber = true;
} else if (ExpressionUtils.isDateTime(value)) {
hasDateTime = true;
} else if (ExpressionUtils.isBoolean(value)) {
hasBoolean = true;
} else if (ExpressionUtils.isNonBlankData(value)) {
String valueString = StringUtils.toString(value);
IndexedNominalFacetChoice facetChoice = choices.get(valueString);
@ -236,6 +272,12 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
public Integer getChoiceValueCount(Object choiceValue) {
if (ExpressionUtils.isError(choiceValue)) {
return errorCount;
} else if (ExpressionUtils.isNumber(choiceValue)) {
return numberCount;
} else if (ExpressionUtils.isDateTime(choiceValue)) {
return datetimeCount;
} else if (ExpressionUtils.isBoolean(choiceValue)) {
return booleanCount;
} else if (ExpressionUtils.isNonBlankData(choiceValue)) {
IndexedNominalFacetChoice choice = choices.get(StringUtils.toString(choiceValue));
return choice != null ? choice.count : 0;

View File

@ -62,10 +62,9 @@ public class ExpressionNominalValueGrouperTests extends RefineTest {
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 Boolean booleanValue = true;
private static ExpressionNominalValueGrouper grouper;
private static Evaluable eval;
@ -143,11 +142,7 @@ public class ExpressionNominalValueGrouperTests extends RefineTest {
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);
Assert.assertEquals(grouper.choices.size(),0);
}
@Test
@ -171,10 +166,30 @@ public class ExpressionNominalValueGrouperTests extends RefineTest {
grouper.end(project);
}
Assert.assertEquals(grouper.choices.size(),1);
Assert.assertEquals(grouper.choices.size(),0);
}
@Test
public void expressionNominalValueGrouperBooleans() throws Exception {
//populate project
for (int i = 0; i < numberOfRows; i++) {
Row row = new Row(1);
row.setCell(0, new Cell(booleanValue, 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.assertTrue(grouper.choices.containsKey(dateTimeStringValue));
Assert.assertEquals(grouper.choices.get(dateTimeStringValue).decoratedValue.label,dateTimeStringValue);
Assert.assertEquals(grouper.choices.get(dateTimeStringValue).decoratedValue.value.toString(),dateTimeStringValue);
Assert.assertEquals(grouper.choices.size(),0);
}
}