Implemented InverseScrutinizer tests using Mocks (#2855)
* Implemented InverseScrutinizer tests using Mocks updated testcases and added InverseConstraint Class * Test cases updated & working fine
This commit is contained in:
parent
2d1d740b44
commit
67bc8581ce
@ -24,10 +24,16 @@
|
||||
package org.openrefine.wikidata.qa.scrutinizers;
|
||||
|
||||
import org.openrefine.wikidata.qa.QAWarning;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.*;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.EntityIdValue;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.Statement;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.Value;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
@ -41,6 +47,24 @@ import java.util.Set;
|
||||
public class InverseConstraintScrutinizer extends StatementScrutinizer {
|
||||
|
||||
public static final String type = "missing-inverse-statements";
|
||||
public static String INVERSE_CONSTRAINT_QID = "Q21510855";
|
||||
public static String INVERSE_PROPERTY_PID = "P2306";
|
||||
public static String SYMMETRIC_CONSTRAINT_QID = "Q21510862";
|
||||
|
||||
class InverseConstraint {
|
||||
PropertyIdValue propertyParameterValue;
|
||||
|
||||
InverseConstraint(Statement statement) {
|
||||
List<SnakGroup> specs = statement.getClaim().getQualifiers();
|
||||
|
||||
if (specs != null) {
|
||||
List<Value> inverses = _fetcher.findValues(specs, INVERSE_PROPERTY_PID);
|
||||
if (!inverses.isEmpty()) {
|
||||
propertyParameterValue = (PropertyIdValue) inverses.get(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Map<PropertyIdValue, PropertyIdValue> _inverse;
|
||||
private Map<PropertyIdValue, Map<EntityIdValue, Set<EntityIdValue>>> _statements;
|
||||
@ -54,8 +78,13 @@ public class InverseConstraintScrutinizer extends StatementScrutinizer {
|
||||
if (_inverse.containsKey(pid)) {
|
||||
return _inverse.get(pid);
|
||||
} else {
|
||||
PropertyIdValue inversePid = _fetcher.getInversePid(pid);
|
||||
if (inversePid == null && _fetcher.isSymmetric(pid)) {
|
||||
PropertyIdValue inversePid = null;
|
||||
List<Statement> statementList = _fetcher.getConstraintsByType(pid, INVERSE_CONSTRAINT_QID);
|
||||
if (!statementList.isEmpty()) {
|
||||
InverseConstraint constraint = new InverseConstraint(statementList.get(0));
|
||||
inversePid = constraint.propertyParameterValue;
|
||||
}
|
||||
if (inversePid == null && !_fetcher.getConstraintsByType(pid, SYMMETRIC_CONSTRAINT_QID).isEmpty()) {
|
||||
inversePid = pid;
|
||||
}
|
||||
_inverse.put(pid, inversePid);
|
||||
|
@ -23,21 +23,36 @@
|
||||
******************************************************************************/
|
||||
package org.openrefine.wikidata.qa.scrutinizers;
|
||||
|
||||
import org.openrefine.wikidata.qa.MockConstraintFetcher;
|
||||
import org.openrefine.wikidata.qa.ConstraintFetcher;
|
||||
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.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.PropertyIdValue;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.Snak;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.Statement;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class InverseConstaintScrutinizerTest extends StatementScrutinizerTest {
|
||||
|
||||
private ItemIdValue idA = TestingData.existingId;
|
||||
private ItemIdValue idB = TestingData.newIdB;
|
||||
private PropertyIdValue pidWithInverse = MockConstraintFetcher.pidWithInverse;
|
||||
private PropertyIdValue inversePid = MockConstraintFetcher.inversePid;
|
||||
private PropertyIdValue symmetricPid = MockConstraintFetcher.symmetricPid;
|
||||
public static PropertyIdValue propertyId = Datamodel.makeWikidataPropertyIdValue("P25");
|
||||
public static ItemIdValue propertyValue = Datamodel.makeWikidataItemIdValue("Q345");
|
||||
public static PropertyIdValue inversePropertyID = Datamodel.makeWikidataPropertyIdValue("P40");
|
||||
public static PropertyIdValue symmetricPropertyID = Datamodel.makeWikidataPropertyIdValue("P3373");
|
||||
public static ItemIdValue symmetricPropertyValue = Datamodel.makeWikidataItemIdValue("Q9545711");
|
||||
|
||||
public static ItemIdValue inverseEntityIdValue = Datamodel.makeWikidataItemIdValue("Q21510855");
|
||||
public static ItemIdValue symmetricEntityIdValue = Datamodel.makeWikidataItemIdValue("Q21510862");
|
||||
public static PropertyIdValue propertyParameter = Datamodel.makeWikidataPropertyIdValue("P2306");
|
||||
|
||||
@Override
|
||||
public EditScrutinizer getScrutinizer() {
|
||||
@ -46,24 +61,63 @@ public class InverseConstaintScrutinizerTest extends StatementScrutinizerTest {
|
||||
|
||||
@Test
|
||||
public void testTrigger() {
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA)
|
||||
.addStatement(TestingData.generateStatement(idA, pidWithInverse, idB)).build();
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyId, propertyValue);
|
||||
Statement statement = new StatementImpl("P25", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
Snak qualifierSnak = Datamodel.makeValueSnak(propertyParameter, inverseEntityIdValue);
|
||||
List<Snak> qualifierSnakList = Collections.singletonList(qualifierSnak);
|
||||
SnakGroup qualifierSnakGroup = Datamodel.makeSnakGroup(qualifierSnakList);
|
||||
List<SnakGroup> snakGroupList = Collections.singletonList(qualifierSnakGroup);
|
||||
List<Statement> statementList = constraintParameterStatementList(inverseEntityIdValue, snakGroupList);
|
||||
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyId, "Q21510855")).thenReturn(statementList);
|
||||
when(fetcher.findValues(snakGroupList, "P2306")).thenReturn(Collections.singletonList(inversePropertyID));
|
||||
setFetcher(fetcher);
|
||||
scrutinize(update);
|
||||
assertWarningsRaised(InverseConstraintScrutinizer.type);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testSymmetric() {
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA)
|
||||
.addStatement(TestingData.generateStatement(idA, symmetricPid, idB)).build();
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(symmetricPropertyID, symmetricPropertyValue);
|
||||
Statement statement = new StatementImpl("P3373", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
Snak qualifierSnak = Datamodel.makeValueSnak(symmetricPropertyID, symmetricEntityIdValue);
|
||||
List<Snak> qualifierSnakList = Collections.singletonList(qualifierSnak);
|
||||
SnakGroup qualifierSnakGroup = Datamodel.makeSnakGroup(qualifierSnakList);
|
||||
List<SnakGroup> snakGroupList = Collections.singletonList(qualifierSnakGroup);
|
||||
List<Statement> statementList = constraintParameterStatementList(symmetricEntityIdValue, snakGroupList);
|
||||
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(symmetricPropertyID, "Q21510862")).thenReturn(statementList);
|
||||
when(fetcher.findValues(snakGroupList, "P2306")).thenReturn(Collections.singletonList(symmetricPropertyID));
|
||||
setFetcher(fetcher);
|
||||
scrutinize(update);
|
||||
assertWarningsRaised(InverseConstraintScrutinizer.type);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSymmetricClosure() {
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(TestingData.generateStatement(idA, inversePid, idB))
|
||||
.build();
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeSomeValueSnak(propertyId);
|
||||
Statement statement = new StatementImpl("P25", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
Snak qualifierSnak = Datamodel.makeValueSnak(propertyParameter, inverseEntityIdValue);
|
||||
List<Snak> qualifierSnakList = Collections.singletonList(qualifierSnak);
|
||||
SnakGroup qualifierSnakGroup = Datamodel.makeSnakGroup(qualifierSnakList);
|
||||
List<SnakGroup> snakGroupList = Collections.singletonList(qualifierSnakGroup);
|
||||
List<Statement> statementList = constraintParameterStatementList(inverseEntityIdValue, snakGroupList);
|
||||
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyId, "Q21510855")).thenReturn(statementList);
|
||||
when(fetcher.findValues(snakGroupList, "P2306")).thenReturn(Collections.singletonList(inversePropertyID));
|
||||
setFetcher(fetcher);
|
||||
scrutinize(update);
|
||||
assertNoWarningRaised();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user