Test (and fix) ReconEntityIdValue

This commit is contained in:
Antonin Delpeuch 2018-02-26 17:43:09 +00:00
parent b48c78a617
commit 7cae9455da
3 changed files with 133 additions and 5 deletions

View File

@ -117,7 +117,8 @@ public abstract class ReconEntityIdValue implements PrefetchedEntityIdValue {
// This ensures compliance with OR's notion of new items
// (it is possible that two cells are reconciled to the same
// new item, in which case they share the same internal recon id).
return getRecon().judgmentHistoryEntry == reconOther.getRecon().judgmentHistoryEntry;
return (getReconInternalId() == reconOther.getReconInternalId() &&
getEntityType().equals(reconOther.getEntityType()));
}
}
@ -125,8 +126,15 @@ public abstract class ReconEntityIdValue implements PrefetchedEntityIdValue {
return getIri().equals(otherNew.getIri());
}
/**
* Returns the integer used internally in OpenRefine to identify the new
* item.
*
* @return
* the judgment history entry id of the reconciled cell
*/
public long getReconInternalId() {
return getRecon().id;
return getRecon().judgmentHistoryEntry;
}
@Override
@ -134,10 +142,16 @@ public abstract class ReconEntityIdValue implements PrefetchedEntityIdValue {
if (isMatched()) {
return Hash.hashCode(this);
} else {
return (int) _recon.id;
return (int) getReconInternalId();
}
}
/**
* Returns the reconciliation object corresponding to this entity.
*
* @return
* the full reconciliation metadata of the corresponding cell
*/
protected Recon getRecon() {
return _recon;
}

View File

@ -0,0 +1,97 @@
package org.openrefine.wikidata.schema.entityvalues;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import java.util.Collections;
import org.openrefine.wikidata.testing.TestingDataGenerator;
import org.testng.annotations.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import com.google.refine.model.Recon;
public class ReconEntityIdValueTest {
private ReconEntityIdValue newItem = TestingDataGenerator.makeNewItemIdValue(1234L, "new item");
private ReconEntityIdValue sameNewItem = TestingDataGenerator.makeNewItemIdValue(1234L, "different text");
private ReconEntityIdValue differentNewItem = TestingDataGenerator.makeNewItemIdValue(7890L, "new item");
private ReconEntityIdValue newProp = TestingDataGenerator.makeNewPropertyIdValue(1234L, "new prop");
private ReconEntityIdValue existingProp = TestingDataGenerator.makeMatchedPropertyIdValue("P53", "new prop");
private ReconEntityIdValue existingItem = TestingDataGenerator.makeMatchedItemIdValue("Q42", "existing item");
@Test
public void testIsNew() {
assertTrue(newItem.isNew());
assertFalse(existingItem.isNew());
}
@Test
public void testGetLabel() {
assertEquals("new item", newItem.getLabel());
assertEquals("existing item", existingItem.getLabel());
}
@Test
public void testGetTypes() {
String[] types = {"Q5"};
Recon matchedRecon = TestingDataGenerator.makeMatchedRecon("Q453", "other item", types);
ReconEntityIdValue existingIdWithTypes = new ReconItemIdValue(matchedRecon, "cell content");
assertEquals(Collections.singletonList("Q5"), existingIdWithTypes.getTypes());
assertEquals(Collections.emptyList(), existingItem.getTypes());
assertEquals(Collections.emptyList(), newItem.getTypes());
}
@Test
public void testGetId() {
assertEquals("Q42", existingItem.getId());
assertEquals("Q0", newItem.getId());
assertEquals("P53", existingProp.getId());
assertEquals("P0", newProp.getId());
}
@Test
public void testGetIri() {
assertEquals("http://www.wikidata.org/entity/Q42", existingItem.getIri());
assertEquals("http://www.wikidata.org/entity/Q0", newItem.getIri());
}
@Test
public void testGetSiteIri() {
assertEquals("http://www.wikidata.org/entity/", existingItem.getSiteIri());
assertEquals("http://www.wikidata.org/entity/", newItem.getSiteIri());
}
@Test
public void testEquality() {
// simple cases
assertEquals(newItem, newItem);
assertEquals(existingItem, existingItem);
assertNotEquals(newItem, existingItem);
assertNotEquals(existingItem, newItem);
// a matched cell is equal to the canonical entity id of its item
assertEquals(Datamodel.makeWikidataItemIdValue("Q42"), existingItem);
// just checking this is symmetrical
assertEquals(existingItem, Datamodel.makeWikidataItemIdValue("Q42"));
// new cell equality relies on the judgmentHistoryEntry parameter
assertEquals(newItem, sameNewItem);
assertNotEquals(newItem, differentNewItem);
// and on datatype
assertNotEquals(newProp, newItem);
}
@Test
public void testHashCode() {
assertEquals(newItem.hashCode(), sameNewItem.hashCode());
assertEquals(existingItem.hashCode(), Datamodel.makeWikidataItemIdValue("Q42").hashCode());
}
@Test
public void testGetRecon() {
assertEquals(newItem.getReconInternalId(), newItem.getRecon().judgmentHistoryEntry);
}
}

View File

@ -4,6 +4,7 @@ import java.util.Collections;
import org.openrefine.wikidata.schema.entityvalues.ReconEntityIdValue;
import org.openrefine.wikidata.schema.entityvalues.ReconItemIdValue;
import org.openrefine.wikidata.schema.entityvalues.ReconPropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import com.google.refine.model.Cell;
@ -18,14 +19,18 @@ public class TestingDataGenerator {
return recon;
}
public static Recon makeMatchedRecon(String qid, String name) {
public static Recon makeMatchedRecon(String qid, String name, String[] types) {
Recon recon = Recon.makeWikidataRecon(123456L);
recon.match = new ReconCandidate(qid, name, new String[0], 100.0);
recon.match = new ReconCandidate(qid, name, types, 100.0);
recon.candidates = Collections.singletonList(recon.match);
recon.judgment = Recon.Judgment.Matched;
return recon;
}
public static Recon makeMatchedRecon(String qid, String name) {
return makeMatchedRecon(qid, name, new String[0]);
}
public static Cell makeNewItemCell(long judgementId, String name) {
return new Cell(name, makeNewItemRecon(judgementId));
}
@ -37,4 +42,16 @@ public class TestingDataGenerator {
public static ReconEntityIdValue makeNewItemIdValue(long judgementId, String name) {
return new ReconItemIdValue(makeNewItemRecon(judgementId), name);
}
public static ReconEntityIdValue makeMatchedItemIdValue(String qid, String name) {
return new ReconItemIdValue(makeMatchedRecon(qid, name), name);
}
public static ReconEntityIdValue makeNewPropertyIdValue(long judgmentId, String name) {
return new ReconPropertyIdValue(makeNewItemRecon(judgmentId), name);
}
public static ReconEntityIdValue makeMatchedPropertyIdValue(String pid, String name) {
return new ReconPropertyIdValue(makeMatchedRecon(pid, name), name);
}
}