Add tests for grouper for list facet
This commit is contained in:
parent
5387e91493
commit
22072c498b
main
src/com/google/refine/browsing/util
tests/server/src/com/google/refine/tests/browsing/util
@ -57,15 +57,6 @@ import com.google.refine.util.StringUtils;
|
|||||||
* from a given expression.
|
* from a given expression.
|
||||||
*/
|
*/
|
||||||
public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor {
|
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
|
* Configuration
|
||||||
*/
|
*/
|
||||||
@ -77,14 +68,29 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
|
|||||||
* Computed results
|
* Computed results
|
||||||
*/
|
*/
|
||||||
final public Map<Object, IndexedNominalFacetChoice> choices = new HashMap<Object, IndexedNominalFacetChoice>();
|
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 blankCount = 0;
|
||||||
public int errorCount = 0;
|
public int errorCount = 0;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Scratch pad variables
|
* Scratch pad variables
|
||||||
*/
|
*/
|
||||||
|
protected boolean hasNumber;
|
||||||
|
protected boolean hasDateTime;
|
||||||
|
protected boolean hasBoolean;
|
||||||
protected boolean hasBlank;
|
protected boolean hasBlank;
|
||||||
protected boolean hasError;
|
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) {
|
public ExpressionNominalValueGrouper(Evaluable evaluable, String columnName, int cellIndex) {
|
||||||
_evaluable = evaluable;
|
_evaluable = evaluable;
|
||||||
@ -104,13 +110,25 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean visit(Project project, int rowIndex, Row row) {
|
public boolean visit(Project project, int rowIndex, Row row) {
|
||||||
|
hasNumber = false;
|
||||||
|
hasDateTime = false;
|
||||||
|
hasBoolean = false;
|
||||||
hasError = false;
|
hasError = false;
|
||||||
hasBlank = false;
|
hasBlank = false;
|
||||||
|
|
||||||
Properties bindings = ExpressionUtils.createBindings(project);
|
Properties bindings = ExpressionUtils.createBindings(project);
|
||||||
|
|
||||||
visitRow(project, rowIndex, row, bindings, rowIndex);
|
visitRow(project, rowIndex, row, bindings, rowIndex);
|
||||||
|
|
||||||
|
if (hasNumber) {
|
||||||
|
numberCount++;
|
||||||
|
}
|
||||||
|
if (hasDateTime) {
|
||||||
|
datetimeCount++;
|
||||||
|
}
|
||||||
|
if (hasBoolean) {
|
||||||
|
booleanCount++;
|
||||||
|
}
|
||||||
if (hasError) {
|
if (hasError) {
|
||||||
errorCount++;
|
errorCount++;
|
||||||
}
|
}
|
||||||
@ -123,6 +141,9 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean visit(Project project, Record record) {
|
public boolean visit(Project project, Record record) {
|
||||||
|
hasNumber = false;
|
||||||
|
hasDateTime = false;
|
||||||
|
hasBoolean = false;
|
||||||
hasError = false;
|
hasError = false;
|
||||||
hasBlank = false;
|
hasBlank = false;
|
||||||
|
|
||||||
@ -133,6 +154,15 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
|
|||||||
visitRow(project, r, row, bindings, record.recordIndex);
|
visitRow(project, r, row, bindings, record.recordIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hasNumber) {
|
||||||
|
numberCount++;
|
||||||
|
}
|
||||||
|
if (hasDateTime) {
|
||||||
|
datetimeCount++;
|
||||||
|
}
|
||||||
|
if (hasBoolean) {
|
||||||
|
booleanCount++;
|
||||||
|
}
|
||||||
if (hasError) {
|
if (hasError) {
|
||||||
errorCount++;
|
errorCount++;
|
||||||
}
|
}
|
||||||
@ -174,6 +204,12 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
|
|||||||
protected void processValue(Object value, int index) {
|
protected void processValue(Object value, int index) {
|
||||||
if (ExpressionUtils.isError(value)) {
|
if (ExpressionUtils.isError(value)) {
|
||||||
hasError = true;
|
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)) {
|
} else if (ExpressionUtils.isNonBlankData(value)) {
|
||||||
String valueString = StringUtils.toString(value);
|
String valueString = StringUtils.toString(value);
|
||||||
IndexedNominalFacetChoice facetChoice = choices.get(valueString);
|
IndexedNominalFacetChoice facetChoice = choices.get(valueString);
|
||||||
@ -236,6 +272,12 @@ public class ExpressionNominalValueGrouper implements RowVisitor, RecordVisitor
|
|||||||
public Integer getChoiceValueCount(Object choiceValue) {
|
public Integer getChoiceValueCount(Object choiceValue) {
|
||||||
if (ExpressionUtils.isError(choiceValue)) {
|
if (ExpressionUtils.isError(choiceValue)) {
|
||||||
return errorCount;
|
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)) {
|
} else if (ExpressionUtils.isNonBlankData(choiceValue)) {
|
||||||
IndexedNominalFacetChoice choice = choices.get(StringUtils.toString(choiceValue));
|
IndexedNominalFacetChoice choice = choices.get(StringUtils.toString(choiceValue));
|
||||||
return choice != null ? choice.count : 0;
|
return choice != null ? choice.count : 0;
|
||||||
|
@ -62,10 +62,9 @@ public class ExpressionNominalValueGrouperTests extends RefineTest {
|
|||||||
private static Properties bindings;
|
private static Properties bindings;
|
||||||
|
|
||||||
private static OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+00:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);
|
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 int integerValue = 1;
|
||||||
private static String integerStringValue = "1";
|
|
||||||
private static String stringStringValue = "a";
|
private static String stringStringValue = "a";
|
||||||
|
private static Boolean booleanValue = true;
|
||||||
|
|
||||||
private static ExpressionNominalValueGrouper grouper;
|
private static ExpressionNominalValueGrouper grouper;
|
||||||
private static Evaluable eval;
|
private static Evaluable eval;
|
||||||
@ -143,11 +142,7 @@ public class ExpressionNominalValueGrouperTests extends RefineTest {
|
|||||||
grouper.end(project);
|
grouper.end(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
Assert.assertEquals(grouper.choices.size(),1);
|
Assert.assertEquals(grouper.choices.size(),0);
|
||||||
|
|
||||||
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
|
@Test
|
||||||
@ -171,10 +166,30 @@ public class ExpressionNominalValueGrouperTests extends RefineTest {
|
|||||||
grouper.end(project);
|
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.size(),0);
|
||||||
Assert.assertEquals(grouper.choices.get(dateTimeStringValue).decoratedValue.label,dateTimeStringValue);
|
|
||||||
Assert.assertEquals(grouper.choices.get(dateTimeStringValue).decoratedValue.value.toString(),dateTimeStringValue);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user