Implemented EntityTypeScrutinizer tests usings mocks (#2839)

Updates all the testcases in EntityTypeScrutinizerTest
This commit is contained in:
Ekta Mishra 2020-07-01 02:29:43 +05:30 committed by GitHub
parent 54291ef441
commit cef2e84e7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 106 additions and 11 deletions

View File

@ -26,6 +26,7 @@ package org.openrefine.wikidata.qa;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.QuantityValue; import org.wikidata.wdtk.datamodel.interfaces.QuantityValue;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement; import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value; import org.wikidata.wdtk.datamodel.interfaces.Value;
@ -154,6 +155,17 @@ public interface ConstraintFetcher {
*/ */
List<Statement> getConstraintsByType(PropertyIdValue pid, String qid); List<Statement> getConstraintsByType(PropertyIdValue pid, String qid);
/**
* Returns the values of a given property in qualifiers
*
* @param groups
* the qualifiers
* @param pid
* the property to filter on
* @return
*/
List<Value> findValues(List<SnakGroup> groups, String pid);
/** /**
* Retrieves the lower bound of the range * Retrieves the lower bound of the range
* required in difference-within-range constraint * required in difference-within-range constraint

View File

@ -293,7 +293,7 @@ public class WikidataConstraintFetcher implements ConstraintFetcher {
/** /**
* Gets the list of constraints of a particular type for a property * Gets the list of constraints of a particular type for a property
* *
* @param pid * @param pid
* the property to retrieve the constraints for * the property to retrieve the constraints for
* @param qid * @param qid
@ -336,7 +336,8 @@ public class WikidataConstraintFetcher implements ConstraintFetcher {
* the property to filter on * the property to filter on
* @return * @return
*/ */
protected List<Value> findValues(List<SnakGroup> groups, String pid) { @Override
public List<Value> findValues(List<SnakGroup> groups, String pid) {
List<Value> results = new ArrayList<>(); List<Value> results = new ArrayList<>();
for (SnakGroup group : groups) { for (SnakGroup group : groups) {
if (group.getProperty().getId().equals(pid)) { if (group.getProperty().getId().equals(pid)) {

View File

@ -1,23 +1,40 @@
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.qa.QAWarning; import org.openrefine.wikidata.qa.QAWarning;
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue; import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue; import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak; import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import java.util.List;
public class EntityTypeScrutinizer extends SnakScrutinizer { public class EntityTypeScrutinizer extends SnakScrutinizer {
public final static String type = "invalid-entity-type"; public final static String type = "invalid-entity-type";
public static String ALLOWED_ENTITY_TYPES_QID = "Q52004125";
public static String ALLOWED_ITEM_TYPE_QID = "Q29934200";
public static String ALLOWED_ENTITY_TYPES_PID = "P2305";
@Override @Override
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) { public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
PropertyIdValue pid = snak.getPropertyId(); PropertyIdValue pid = snak.getPropertyId();
if(!_fetcher.usableOnItems(pid)) { List<Statement> statementList = _fetcher.getConstraintsByType(pid, ALLOWED_ENTITY_TYPES_QID);
QAWarning issue = new QAWarning(type, null, QAWarning.Severity.WARNING, 1); if(!statementList.isEmpty()) {
issue.setProperty("property_entity", pid); List<SnakGroup> constraint = statementList.get(0).getClaim().getQualifiers();
issue.setProperty("example_entity", entityId); boolean isUsable = true;
addIssue(issue); if (constraint != null) {
isUsable = _fetcher.findValues(constraint, ALLOWED_ENTITY_TYPES_PID).contains(
Datamodel.makeWikidataItemIdValue(ALLOWED_ITEM_TYPE_QID));
}
if (!isUsable) {
QAWarning issue = new QAWarning(type, null, QAWarning.Severity.WARNING, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("example_entity", entityId);
addIssue(issue);
}
} }
} }
} }

View File

@ -248,4 +248,9 @@ public class MockConstraintFetcher implements ConstraintFetcher {
return statements; return statements;
} }
@Override
public List<Value> findValues(List<SnakGroup> groups, String pid) {
return null;
}
} }

View File

@ -1,14 +1,38 @@
package org.openrefine.wikidata.qa.scrutinizers; package org.openrefine.wikidata.qa.scrutinizers;
import org.openrefine.wikidata.qa.ConstraintFetcher;
import org.openrefine.wikidata.qa.MockConstraintFetcher; import org.openrefine.wikidata.qa.MockConstraintFetcher;
import org.openrefine.wikidata.testing.TestingData; import org.openrefine.wikidata.testing.TestingData;
import org.openrefine.wikidata.updates.ItemUpdate;
import org.openrefine.wikidata.updates.ItemUpdateBuilder;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import org.wikidata.wdtk.datamodel.helpers.Datamodel; import org.wikidata.wdtk.datamodel.helpers.Datamodel;
import org.wikidata.wdtk.datamodel.implementation.StatementImpl;
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue; import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Snak;
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import org.wikidata.wdtk.datamodel.interfaces.Value;
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
import java.util.Collections;
import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class EntityTypeScrutinizerTest extends StatementScrutinizerTest { public class EntityTypeScrutinizerTest extends StatementScrutinizerTest {
private static ItemIdValue qid = Datamodel.makeWikidataItemIdValue("Q343"); public static PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P2302");
public static Value propertyValue = MockConstraintFetcher.conflictsWithStatementValue;
public static ItemIdValue entityIdValue = Datamodel.makeWikidataItemIdValue("Q52004125");
public static PropertyIdValue itemParameterPID = Datamodel.makeWikidataPropertyIdValue("P2305");
public static Value itemValue = Datamodel.makeWikidataItemIdValue("Q29934218");
public static Value allowedValue = Datamodel.makeWikidataItemIdValue("Q29934200");
@Override @Override
public EditScrutinizer getScrutinizer() { public EditScrutinizer getScrutinizer() {
@ -17,13 +41,49 @@ public class EntityTypeScrutinizerTest extends StatementScrutinizerTest {
@Test @Test
public void testAllowed() { public void testAllowed() {
scrutinize(TestingData.generateStatement(qid, qid)); ItemIdValue idA = TestingData.existingId;
ValueSnak mainValueSnak = Datamodel.makeValueSnak(propertyIdValue, propertyValue);
Statement statement = new StatementImpl("P2302", mainValueSnak, idA);
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, itemValue);
List<Snak> qualifierSnakList = Collections.singletonList(qualifierSnak);
SnakGroup qualifierSnakGroup = Datamodel.makeSnakGroup(qualifierSnakList);
List<SnakGroup> snakGroupList = Collections.singletonList(qualifierSnakGroup);
List<Statement> statementList = constraintParameterStatementList(entityIdValue, snakGroupList);
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
when(fetcher.getConstraintsByType(propertyIdValue,"Q52004125")).thenReturn(statementList);
when(fetcher.findValues(snakGroupList, "P2305")).thenReturn(Collections.singletonList(allowedValue));
setFetcher(fetcher);
scrutinize(update);
assertNoWarningRaised(); assertNoWarningRaised();
} }
@Test @Test
public void testDisallowed() { public void testDisallowed() {
scrutinize(TestingData.generateStatement(qid, MockConstraintFetcher.propertyOnlyPid, qid)); ItemIdValue idA = TestingData.existingId;
ValueSnak mainValueSnak = Datamodel.makeValueSnak(propertyIdValue, propertyValue);
Statement statement = new StatementImpl("P2302", mainValueSnak, idA);
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, itemValue);
List<Snak> qualifierSnakList = Collections.singletonList(qualifierSnak);
SnakGroup qualifierSnakGroup = Datamodel.makeSnakGroup(qualifierSnakList);
List<SnakGroup> snakGroupList = Collections.singletonList(qualifierSnakGroup);
List<Statement> statementList = constraintParameterStatementList(entityIdValue, snakGroupList);
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
when(fetcher.getConstraintsByType(propertyIdValue,"Q52004125")).thenReturn(statementList);
when(fetcher.findValues(snakGroupList, "P2305")).thenReturn(Collections.singletonList(itemValue));
setFetcher(fetcher);
scrutinize(update);
assertWarningsRaised(EntityTypeScrutinizer.type); assertWarningsRaised(EntityTypeScrutinizer.type);
} }
} }