Add JSON serialization tests for refine.history and refine.browsing

This commit is contained in:
Antonin Delpeuch 2018-08-18 20:19:36 +01:00
parent a2c0ac9bcc
commit 2dde302a90
4 changed files with 156 additions and 6 deletions

View File

@ -0,0 +1,17 @@
package com.google.refine.tests.browsing;
import java.time.OffsetDateTime;
import org.testng.annotations.Test;
import com.google.refine.browsing.DecoratedValue;
import com.google.refine.tests.util.TestUtils;
public class DecoratedValueTests {
@Test
public void serializeDecoratedValue() {
OffsetDateTime date = OffsetDateTime.parse("2017-03-04T12:56:32Z");
DecoratedValue dv = new DecoratedValue(date, "[date 2017-03-04T12:56:32Z]");
TestUtils.isSerializedTo(dv, "{\"v\":\"2017-03-04T12:56:32Z\",\"l\":\"[date 2017-03-04T12:56:32Z]\"}");
}
}

View File

@ -0,0 +1,20 @@
package com.google.refine.tests.browsing;
import static org.mockito.Mockito.mock;
import org.testng.annotations.Test;
import com.google.refine.browsing.Engine;
import com.google.refine.model.Project;
import com.google.refine.tests.util.TestUtils;
// TODO Engine and engine config should be separated
// create an EngineConfig class that can be used in operations directly (to avoid manipulating JSONObject)
public class EngineTests {
@Test
public void serializeEngine() {
Project project = mock(Project.class);
Engine engine = new Engine(project);
TestUtils.isSerializedTo(engine, "{\"mode\":\"row-based\",\"facets\":[]}");
}
}

View File

@ -0,0 +1,66 @@
package com.google.refine.tests.history;
import static org.mockito.Mockito.mock;
import java.util.Properties;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.google.refine.history.HistoryEntry;
import com.google.refine.model.Project;
import com.google.refine.operations.OperationRegistry;
import com.google.refine.operations.column.ColumnAdditionOperation;
import com.google.refine.tests.RefineTest;
import com.google.refine.tests.util.TestUtils;
public class HistoryEntryTests extends RefineTest {
Project project;
@BeforeTest
public void register() {
OperationRegistry.registerOperation(getCoreModule(), "column-addition", ColumnAdditionOperation.class);
}
@BeforeMethod
public void setUp() {
project = mock(Project.class);
}
@Test
public void serializeHistoryEntry() throws Exception {
String json = "{\"id\":1533651837506,"
+ "\"description\":\"Discard recon judgment for single cell on row 76, column organization_name, containing \\\"Catholic University Leuven\\\"\","
+ "\"time\":\"2018-08-07T14:18:29Z\"}";
TestUtils.isSerializedTo(HistoryEntry.load(project, json), json);
}
@Test
public void serializeHistoryEntryWithOperation() throws Exception {
String json = "{"
+ "\"id\":1533633623158,"
+ "\"description\":\"Create new column uri based on column country by filling 269 rows with grel:\\\"https://www.wikidata.org/wiki/\\\"+cell.recon.match.id\","
+ "\"time\":\"2018-08-07T09:06:37Z\","
+ "\"operation\":{\"op\":\"core/column-addition\","
+ " \"description\":\"Create column uri at index 2 based on column country using expression grel:\\\"https://www.wikidata.org/wiki/\\\"+cell.recon.match.id\","
+ " \"engineConfig\":{\"mode\":\"row-based\",\"facets\":[]},"
+ " \"newColumnName\":\"uri\","
+ " \"columnInsertIndex\":2,"
+ " \"baseColumnName\":\"country\","
+ " \"expression\":\"grel:\\\"https://www.wikidata.org/wiki/\\\"+cell.recon.match.id\","
+ " \"onError\":\"set-to-blank\"}"
+ "}";
String jsonSimple = "{"
+ "\"id\":1533633623158,"
+ "\"description\":\"Create new column uri based on column country by filling 269 rows with grel:\\\"https://www.wikidata.org/wiki/\\\"+cell.recon.match.id\","
+ "\"time\":\"2018-08-07T09:06:37Z\"}";
HistoryEntry historyEntry = HistoryEntry.load(project, json);
TestUtils.isSerializedTo(historyEntry, jsonSimple);
Properties options = new Properties();
options.setProperty("mode", "save");
TestUtils.isSerializedTo(historyEntry, json, options);
}
}

View File

@ -49,9 +49,12 @@ import org.testng.annotations.Test;
import com.google.refine.ProjectManager;
import com.google.refine.history.History;
import com.google.refine.history.HistoryEntry;
import com.google.refine.history.HistoryEntryManager;
import com.google.refine.model.Project;
import com.google.refine.model.metadata.ProjectMetadata;
import com.google.refine.tests.RefineTest;
import com.google.refine.tests.util.TestUtils;
import com.google.refine.history.Change;
public class HistoryTests extends RefineTest {
@ -66,13 +69,23 @@ public class HistoryTests extends RefineTest {
//dependencies
Project proj;
ProjectMetadata projectMetadata;
ProjectManager projectManager;
HistoryEntryManager historyEntryManager;
@BeforeMethod
public void SetUp(){
projectManager = mock(ProjectManager.class);
historyEntryManager = mock(HistoryEntryManager.class);
ProjectManager.singleton = projectManager;
proj = new Project();
projectMetadata = mock(ProjectMetadata.class);
when(projectManager.getProject(Mockito.anyLong())).thenReturn(proj);
when(projectManager.getProjectMetadata(Mockito.anyLong())).thenReturn(projectMetadata);
when(projectManager.getHistoryEntryManager()).thenReturn(historyEntryManager);
SUT = new History(proj);
}
@ -86,17 +99,51 @@ public class HistoryTests extends RefineTest {
public void canAddEntry(){
//local dependencies
HistoryEntry entry = mock(HistoryEntry.class);
Project project = mock(Project.class);
ProjectMetadata projectMetadata = mock(ProjectMetadata.class);
when(projectManager.getProject(Mockito.anyLong())).thenReturn(project);
when(projectManager.getProjectMetadata(Mockito.anyLong())).thenReturn(projectMetadata);
SUT.addEntry(entry);
verify(projectManager, times(1)).getProject(Mockito.anyLong());
verify(entry, times(1)).apply(project);
verify(entry, times(1)).apply(proj);
verify(projectMetadata, times(1)).updateModified();
Assert.assertEquals(SUT.getLastPastEntries(1).get(0), entry);
}
@Test
public void serializeHistory() throws Exception {
String json1 = "{\"id\":1533650900300,"
+ "\"description\":\"Reconcile cells in column organization_name to type Q43229\","
+ "\"time\":\"2018-08-07T13:57:17Z\","
+ "\"operation\":{"
+ " \"op\":\"core/recon\","
+ " \"description\":\"Reconcile cells in column organization_name to type Q43229\","
+ " \"columnName\":\"organization_name\","
+ " \"config\":{"
+ " \"mode\":\"standard-service\","
+ " \"service\":\"https://tools.wmflabs.org/openrefine-wikidata/en/api\","
+ " \"identifierSpace\":\"http://www.wikidata.org/entity/\","
+ " \"schemaSpace\":\"http://www.wikidata.org/prop/direct/\","
+ " \"type\":{\"id\":\"Q43229\",\"name\":\"organization\"},"
+ " \"autoMatch\":true,"
+ " \"columnDetails\":[],"
+ " \"limit\":0},"
+ "\"engineConfig\":{\"mode\":\"row-based\",\"facets\":[]}}}";
String json1simple = "{\"id\":1533650900300,"
+ "\"description\":\"Reconcile cells in column organization_name to type Q43229\","
+ "\"time\":\"2018-08-07T13:57:17Z\"}";
String json2 = "{\"id\":1533651586483,"
+ "\"description\":\"Edit single cell on row 94, column organization_id\","
+ "\"time\":\"2018-08-07T14:18:21Z\"}";
String targetJson = "{\"past\":["+json1simple+","+json2+"],\"future\":[]}";
com.google.refine.history.Change dummyChange = mock(Change.class);
HistoryEntry firstEntry = HistoryEntry.load(proj, json1);
firstEntry.setChange(dummyChange);
HistoryEntry secondEntry = HistoryEntry.load(proj, json2);
secondEntry.setChange(dummyChange);
SUT.addEntry(firstEntry);
SUT.addEntry(secondEntry);
TestUtils.isSerializedTo(SUT, targetJson);
}
}