Implemented DistinctValueScrutinizer tests using mockito (#2833)
* Implemented DistinctValueScrutinizer tests using mcokito Added inner class to the scrutinizer and updated the tests using mocks. * Tests updated-testNoIssue added * all tests updated & working fine
This commit is contained in:
parent
46c510b5e2
commit
bc672047f6
@ -23,15 +23,16 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.openrefine.wikidata.qa.scrutinizers;
|
package org.openrefine.wikidata.qa.scrutinizers;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.openrefine.wikidata.qa.QAWarning;
|
import org.openrefine.wikidata.qa.QAWarning;
|
||||||
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.Statement;
|
import org.wikidata.wdtk.datamodel.interfaces.Statement;
|
||||||
import org.wikidata.wdtk.datamodel.interfaces.Value;
|
import org.wikidata.wdtk.datamodel.interfaces.Value;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A scrutinizer that checks for properties using the same value on different
|
* A scrutinizer that checks for properties using the same value on different
|
||||||
* items.
|
* items.
|
||||||
@ -42,6 +43,7 @@ import org.wikidata.wdtk.datamodel.interfaces.Value;
|
|||||||
public class DistinctValuesScrutinizer extends StatementScrutinizer {
|
public class DistinctValuesScrutinizer extends StatementScrutinizer {
|
||||||
|
|
||||||
public final static String type = "identical-values-for-distinct-valued-property";
|
public final static String type = "identical-values-for-distinct-valued-property";
|
||||||
|
public static String DISTINCT_VALUES_CONSTRAINT_QID = "Q21502410";
|
||||||
|
|
||||||
private Map<PropertyIdValue, Map<Value, EntityIdValue>> _seenValues;
|
private Map<PropertyIdValue, Map<Value, EntityIdValue>> _seenValues;
|
||||||
|
|
||||||
@ -52,7 +54,8 @@ public class DistinctValuesScrutinizer extends StatementScrutinizer {
|
|||||||
@Override
|
@Override
|
||||||
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
|
public void scrutinize(Statement statement, EntityIdValue entityId, boolean added) {
|
||||||
PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId();
|
PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId();
|
||||||
if (_fetcher.hasDistinctValues(pid)) {
|
List<Statement> statementList = _fetcher.getConstraintsByType(pid, DISTINCT_VALUES_CONSTRAINT_QID);
|
||||||
|
if (!statementList.isEmpty()) {
|
||||||
Value mainSnakValue = statement.getClaim().getMainSnak().getValue();
|
Value mainSnakValue = statement.getClaim().getMainSnak().getValue();
|
||||||
Map<Value, EntityIdValue> seen = _seenValues.get(pid);
|
Map<Value, EntityIdValue> seen = _seenValues.get(pid);
|
||||||
if (seen == null) {
|
if (seen == null) {
|
||||||
|
@ -23,14 +23,34 @@
|
|||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
package org.openrefine.wikidata.qa.scrutinizers;
|
package org.openrefine.wikidata.qa.scrutinizers;
|
||||||
|
|
||||||
|
import org.openrefine.wikidata.qa.ConstraintFetcher;
|
||||||
import org.openrefine.wikidata.testing.TestingData;
|
import org.openrefine.wikidata.testing.TestingData;
|
||||||
import org.openrefine.wikidata.updates.ItemUpdate;
|
import org.openrefine.wikidata.updates.ItemUpdate;
|
||||||
import org.openrefine.wikidata.updates.ItemUpdateBuilder;
|
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.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 java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
public class DistinctValuesScrutinizerTest extends StatementScrutinizerTest {
|
public class DistinctValuesScrutinizerTest extends StatementScrutinizerTest {
|
||||||
|
|
||||||
|
public static PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P163");
|
||||||
|
public static Value value1 = Datamodel.makeWikidataItemIdValue("Q41673");
|
||||||
|
public static Value value2 = Datamodel.makeWikidataItemIdValue("Q43175");
|
||||||
|
|
||||||
|
public static ItemIdValue entityIdValue = Datamodel.makeWikidataItemIdValue("Q19474404");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public EditScrutinizer getScrutinizer() {
|
public EditScrutinizer getScrutinizer() {
|
||||||
return new DistinctValuesScrutinizer();
|
return new DistinctValuesScrutinizer();
|
||||||
@ -39,10 +59,41 @@ public class DistinctValuesScrutinizerTest extends StatementScrutinizerTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testTrigger() {
|
public void testTrigger() {
|
||||||
ItemIdValue idA = TestingData.existingId;
|
ItemIdValue idA = TestingData.existingId;
|
||||||
ItemIdValue idB = TestingData.matchedId;
|
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, value1);
|
||||||
ItemUpdate updateA = new ItemUpdateBuilder(idA).addStatement(TestingData.generateStatement(idA, idB)).build();
|
Statement statement1 = new StatementImpl("P163", mainSnak, idA);
|
||||||
ItemUpdate updateB = new ItemUpdateBuilder(idB).addStatement(TestingData.generateStatement(idB, idB)).build();
|
Statement statement2 = new StatementImpl("P163", mainSnak, idA);
|
||||||
scrutinize(updateA, updateB);
|
|
||||||
|
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement1).addStatement(statement2).build();
|
||||||
|
|
||||||
|
List<SnakGroup> snakGroupList = new ArrayList<>();
|
||||||
|
List<Statement> statementList = constraintParameterStatementList(entityIdValue, snakGroupList);
|
||||||
|
|
||||||
|
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||||
|
when(fetcher.getConstraintsByType(propertyIdValue, "Q21502410")).thenReturn(statementList);
|
||||||
|
setFetcher(fetcher);
|
||||||
|
|
||||||
|
scrutinize(update);
|
||||||
assertWarningsRaised(DistinctValuesScrutinizer.type);
|
assertWarningsRaised(DistinctValuesScrutinizer.type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoIssue() {
|
||||||
|
ItemIdValue idA = TestingData.existingId;
|
||||||
|
Snak snak1 = Datamodel.makeValueSnak(propertyIdValue, value1);
|
||||||
|
Snak snak2 = Datamodel.makeValueSnak(propertyIdValue, value2);
|
||||||
|
Statement statement1 = new StatementImpl("P163", snak1, idA);
|
||||||
|
Statement statement2 = new StatementImpl("P163", snak2, idA);
|
||||||
|
|
||||||
|
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement1).addStatement(statement2).build();
|
||||||
|
|
||||||
|
List<SnakGroup> snakGroupList = new ArrayList<>();
|
||||||
|
List<Statement> statementList = constraintParameterStatementList(entityIdValue, snakGroupList);
|
||||||
|
|
||||||
|
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||||
|
when(fetcher.getConstraintsByType(propertyIdValue, "Q21502410")).thenReturn(statementList);
|
||||||
|
setFetcher(fetcher);
|
||||||
|
|
||||||
|
scrutinize(update);
|
||||||
|
assertNoWarningRaised();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user