Add serialization tests for ListFacet

This commit is contained in:
Antonin Delpeuch 2018-09-04 10:09:38 +01:00
parent bc81beb933
commit 497eee5a75
2 changed files with 81 additions and 0 deletions

View File

@ -214,6 +214,7 @@ public class ListFacet implements Facet {
}
public void initializeFromConfig(ListFacetConfig config, Project project) {
_config = config;
if (_config.columnName.length() > 0) {
Column column = project.columnModel.getColumnByName(_config.columnName);
if (column != null) {

View File

@ -0,0 +1,80 @@
package com.google.refine.tests.browsing.facets;
import org.json.JSONObject;
import org.testng.annotations.Test;
import com.google.refine.browsing.Engine;
import com.google.refine.browsing.facets.Facet;
import com.google.refine.browsing.facets.ListFacet.ListFacetConfig;
import com.google.refine.model.Project;
import com.google.refine.tests.RefineTest;
import com.google.refine.tests.util.TestUtils;
public class ListFacetTests extends RefineTest {
private static String jsonConfig = "{"
+ "\"type\":\"list\","
+ "\"name\":\"facet A\","
+ "\"columnName\":\"Column A\","
+ "\"expression\":\"value+\\\"bar\\\"\","
+ "\"omitBlank\":false,"
+ "\"omitError\":false,"
+ "\"selection\":[{\"v\":{\"v\":\"foobar\",\"l\":\"true\"}}],"
+ "\"selectBlank\":false,"
+ "\"selectError\":false,"
+ "\"invert\":false"
+ "}";
private static String jsonFacetError = "{"
+ "\"name\":\"facet A\","
+ "\"expression\":\"value+\\\"bar\\\"\","
+ "\"columnName\":\"Column A\","
+ "\"invert\":false,"
+ "\"error\":\"No column named Column A\"}\" are not equal as JSON strings.\n" +
"}";
private static String jsonFacet = "{"
+ "\"name\":\"facet A\","
+ "\"expression\":\"value+\\\"bar\\\"\","
+ "\"columnName\":\"Column A\","
+ "\"invert\":false,"
+ "\"choices\":["
+ " {\"v\":{\"v\":\"foobar\",\"l\":\"foobar\"},\"c\":1,\"s\":true},"
+ " {\"v\":{\"v\":\"barbar\",\"l\":\"barbar\"},\"c\":1,\"s\":false}"
+ "]}";
@Test
public void serializeListFacetConfig() {
ListFacetConfig facetConfig = new ListFacetConfig();
facetConfig.initializeFromJSON(new JSONObject(jsonConfig));
TestUtils.isSerializedTo(facetConfig, jsonConfig);
}
@Test
public void serializeListFacet() {
Project project = createCSVProject("Column A\n" +
"foo\n" +
"bar\n");
Engine engine = new Engine(project);
ListFacetConfig facetConfig = new ListFacetConfig();
facetConfig.initializeFromJSON(new JSONObject(jsonConfig));
Facet facet = facetConfig.apply(project);
facet.computeChoices(project, engine.getAllFilteredRows());
TestUtils.isSerializedTo(facet, jsonFacet);
}
@Test
public void serializeListFacetWithError() {
Project project = createCSVProject("other column\n" +
"foo\n" +
"bar\n");
ListFacetConfig facetConfig = new ListFacetConfig();
facetConfig.initializeFromJSON(new JSONObject(jsonConfig));
Facet facet = facetConfig.apply(project);
TestUtils.isSerializedTo(facet, jsonFacetError);
}
}