Some rudimentary testing of operations and changes
This commit is contained in:
parent
a002468e7d
commit
6b658fe87e
@ -190,4 +190,13 @@ public class WikibaseSchema implements OverlayModel {
|
|||||||
public void dispose(Project project) {
|
public void dispose(Project project) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if(other == null || !WikibaseSchema.class.isInstance(other)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
WikibaseSchema otherSchema = (WikibaseSchema)other;
|
||||||
|
return itemDocumentExprs.equals(otherSchema.getItemDocumentExpressions());
|
||||||
|
}
|
||||||
}
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"op": "wikidata/perform-wikibase-edits",
|
||||||
|
"description": "Perform Wikibase edits",
|
||||||
|
"summary": "test null edit",
|
||||||
|
"engineConfig": {
|
||||||
|
"mode": "row-based",
|
||||||
|
"facets": []
|
||||||
|
}
|
||||||
|
}
|
33
extensions/wikidata/tests/data/operations/save-schema.json
Normal file
33
extensions/wikidata/tests/data/operations/save-schema.json
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
{
|
||||||
|
"op": "wikidata/save-wikibase-schema",
|
||||||
|
"description": "Save Wikibase schema skeleton",
|
||||||
|
"schema": {
|
||||||
|
"itemDocuments": [
|
||||||
|
{
|
||||||
|
"subject": {
|
||||||
|
"label": "University of Oxford",
|
||||||
|
"type": "wbitemconstant",
|
||||||
|
"qid": "Q34433"
|
||||||
|
},
|
||||||
|
"statementGroups": [],
|
||||||
|
"nameDescs": [
|
||||||
|
{
|
||||||
|
"name_type": "LABEL",
|
||||||
|
"value": {
|
||||||
|
"language": {
|
||||||
|
"id": "en",
|
||||||
|
"label": "en",
|
||||||
|
"type": "wblanguageconstant"
|
||||||
|
},
|
||||||
|
"type": "wbmonolingualexpr",
|
||||||
|
"value": {
|
||||||
|
"type": "wbstringconstant",
|
||||||
|
"value": "University of Oxford"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,70 @@
|
|||||||
|
package org.openrefine.wikidata.operations;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.LineNumberReader;
|
||||||
|
import java.io.StringReader;
|
||||||
|
import java.io.StringWriter;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.json.JSONWriter;
|
||||||
|
import org.openrefine.wikidata.testing.JacksonSerializationTest;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import com.google.refine.history.Change;
|
||||||
|
import com.google.refine.model.AbstractOperation;
|
||||||
|
import com.google.refine.model.Project;
|
||||||
|
import com.google.refine.operations.OperationRegistry;
|
||||||
|
import com.google.refine.tests.RefineTest;
|
||||||
|
import com.google.refine.util.Pool;
|
||||||
|
|
||||||
|
import edu.mit.simile.butterfly.ButterflyModule;
|
||||||
|
|
||||||
|
public abstract class OperationTest extends RefineTest {
|
||||||
|
|
||||||
|
protected Project project = null;
|
||||||
|
protected ButterflyModule module = null;
|
||||||
|
protected Pool pool = null;
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setUp() {
|
||||||
|
project = createCSVProject("a,b\nc,d");
|
||||||
|
module = mock(ButterflyModule.class);
|
||||||
|
when(module.getName()).thenReturn("wikidata");
|
||||||
|
pool = new Pool();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void registerOperation(String name, Class klass) {
|
||||||
|
OperationRegistry.registerOperation(module, name, klass);
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract AbstractOperation reconstruct() throws Exception;
|
||||||
|
|
||||||
|
public abstract JSONObject getJson() throws Exception;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testReconstruct() throws Exception {
|
||||||
|
JSONObject json = getJson();
|
||||||
|
AbstractOperation op = reconstruct();
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
JSONWriter jsonWriter = new JSONWriter(writer);
|
||||||
|
op.write(jsonWriter, new Properties());
|
||||||
|
JacksonSerializationTest.assertJsonEquals(json.toString(), writer.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected LineNumberReader makeReader(String input) {
|
||||||
|
StringReader reader = new StringReader(input);
|
||||||
|
return new LineNumberReader(reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected String saveChange(Change change) throws IOException {
|
||||||
|
StringWriter writer = new StringWriter();
|
||||||
|
change.save(writer, new Properties());
|
||||||
|
return writer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
package org.openrefine.wikidata.operations;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.io.LineNumberReader;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.openrefine.wikidata.testing.TestingData;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.google.refine.history.Change;
|
||||||
|
import com.google.refine.model.AbstractOperation;
|
||||||
|
import com.google.refine.model.Recon;
|
||||||
|
|
||||||
|
public class PerformWikibaseEditsOperationTest extends OperationTest {
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void registerOperation() {
|
||||||
|
registerOperation("perform-wikibase-edits", PerformWikibaseEditsOperation.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AbstractOperation reconstruct() throws Exception {
|
||||||
|
JSONObject json = getJson();
|
||||||
|
return PerformWikibaseEditsOperation.reconstruct(project, json);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JSONObject getJson() throws Exception {
|
||||||
|
return TestingData.jsonFromFile("data/operations/perform-edits.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLoadChange() throws Exception {
|
||||||
|
String changeString = "newItems={\"qidMap\":{\"1234\":\"Q789\"}}\n" +
|
||||||
|
"/ec/\n";
|
||||||
|
LineNumberReader reader = makeReader(changeString);
|
||||||
|
Change change = PerformWikibaseEditsOperation.PerformWikibaseEditsChange.load(reader, pool);
|
||||||
|
|
||||||
|
project.rows.get(0).cells.set(0, TestingData.makeNewItemCell(1234L, "my new item"));
|
||||||
|
|
||||||
|
change.apply(project);
|
||||||
|
|
||||||
|
assertEquals(Recon.Judgment.Matched, project.rows.get(0).cells.get(0).recon.judgment);
|
||||||
|
assertEquals("Q789", project.rows.get(0).cells.get(0).recon.match.id);
|
||||||
|
|
||||||
|
change.revert(project);
|
||||||
|
|
||||||
|
assertEquals(Recon.Judgment.New, project.rows.get(0).cells.get(0).recon.judgment);
|
||||||
|
|
||||||
|
assertEquals(changeString, saveChange(change));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
package org.openrefine.wikidata.operations;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNull;
|
||||||
|
|
||||||
|
import java.io.LineNumberReader;
|
||||||
|
|
||||||
|
import org.json.JSONObject;
|
||||||
|
import org.openrefine.wikidata.schema.WikibaseSchema;
|
||||||
|
import org.openrefine.wikidata.testing.TestingData;
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import com.google.refine.history.Change;
|
||||||
|
import com.google.refine.model.AbstractOperation;
|
||||||
|
|
||||||
|
|
||||||
|
public class SaveWikibaseSchemaOperationTest extends OperationTest {
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void registerOperation() {
|
||||||
|
registerOperation("save-wikibase-schema", SaveWikibaseSchemaOperation.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AbstractOperation reconstruct() throws Exception {
|
||||||
|
return SaveWikibaseSchemaOperation.reconstruct(project, getJson());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JSONObject getJson() throws Exception {
|
||||||
|
return TestingData.jsonFromFile("data/operations/save-schema.json");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLoadChange() throws Exception {
|
||||||
|
JSONObject schemaJson = TestingData.jsonFromFile("data/schema/inception.json");
|
||||||
|
String changeString =
|
||||||
|
"newSchema="+schemaJson.toString()+"\n" +
|
||||||
|
"oldSchema=\n" +
|
||||||
|
"/ec/";
|
||||||
|
WikibaseSchema schema = WikibaseSchema.reconstruct(schemaJson);
|
||||||
|
|
||||||
|
LineNumberReader reader = makeReader(changeString);
|
||||||
|
Change change = SaveWikibaseSchemaOperation.WikibaseSchemaChange.load(reader, pool);
|
||||||
|
|
||||||
|
change.apply(project);
|
||||||
|
|
||||||
|
assertEquals(schema, project.overlayModels.get(SaveWikibaseSchemaOperation.WikibaseSchemaChange.overlayModelKey));
|
||||||
|
|
||||||
|
change.revert(project);
|
||||||
|
|
||||||
|
assertNull(project.overlayModels.get(SaveWikibaseSchemaOperation.WikibaseSchemaChange.overlayModelKey));
|
||||||
|
|
||||||
|
saveChange(change); // not checking for equality because JSON serialization varies
|
||||||
|
}
|
||||||
|
}
|
@ -6,7 +6,9 @@ import java.io.IOException;
|
|||||||
|
|
||||||
import org.testng.Assert;
|
import org.testng.Assert;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParseException;
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
@ -16,16 +18,12 @@ public class JacksonSerializationTest {
|
|||||||
public static void testSerialize(Object pojo, String expectedJson) {
|
public static void testSerialize(Object pojo, String expectedJson) {
|
||||||
// Test that the pojo is correctly serialized
|
// Test that the pojo is correctly serialized
|
||||||
try {
|
try {
|
||||||
JsonNode parsedExpectedJson = mapper.readValue(expectedJson, JsonNode.class);
|
|
||||||
String actualJson = mapper.writeValueAsString(pojo);
|
String actualJson = mapper.writeValueAsString(pojo);
|
||||||
JsonNode parsedActualJson = mapper.readValue(actualJson, JsonNode.class);
|
assertJsonEquals(expectedJson, actualJson);
|
||||||
assertEquals(parsedExpectedJson, parsedActualJson);
|
|
||||||
} catch (JsonProcessingException e) {
|
} catch (JsonProcessingException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Assert.fail("Failed to serialize object");
|
Assert.fail("Failed to serialize object");
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Assert.fail("Invalid test JSON provided");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,4 +44,15 @@ public class JacksonSerializationTest {
|
|||||||
testSerialize(pojo, json);
|
testSerialize(pojo, json);
|
||||||
testDeserialize(targetClass, pojo, json);
|
testDeserialize(targetClass, pojo, json);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void assertJsonEquals(String expectedJson, String actualJson) {
|
||||||
|
JsonNode parsedExpectedJson;
|
||||||
|
try {
|
||||||
|
parsedExpectedJson = mapper.readValue(expectedJson, JsonNode.class);
|
||||||
|
JsonNode parsedActualJson = mapper.readValue(actualJson, JsonNode.class);
|
||||||
|
assertEquals(parsedExpectedJson, parsedActualJson);
|
||||||
|
} catch (IOException e) {
|
||||||
|
Assert.fail("Invalid JSON");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user