Implemented Difference-within-range Scrutinizer tests using mocks (#2864)
Updated all test cases and added DifferenceWithinRangeConstraint class.
This commit is contained in:
parent
a4b7a00c70
commit
9edb1e514d
@ -5,11 +5,39 @@ import org.openrefine.wikidata.updates.ItemUpdate;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DifferenceWithinRangeScrutinizer extends EditScrutinizer {
|
||||
|
||||
public static final String type = "difference-of-the-properties-is-not-within-the-specified-range";
|
||||
public static String DIFFERENCE_WITHIN_RANGE_CONSTRAINT_QID = "Q21510854";
|
||||
public static String DIFFERENCE_WITHIN_RANGE_CONSTRAINT_PID = "P2306";
|
||||
public static String MINIMUM_VALUE_PID = "P2313";
|
||||
public static String MAXIMUM_VALUE_PID = "P2312";
|
||||
|
||||
class DifferenceWithinRangeConstraint {
|
||||
PropertyIdValue lowerPropertyIdValue;
|
||||
QuantityValue minRangeValue, maxRangeValue;
|
||||
|
||||
DifferenceWithinRangeConstraint(Statement statement) {
|
||||
List<SnakGroup> specs = statement.getClaim().getQualifiers();
|
||||
if (specs != null) {
|
||||
List<Value> lowerValueProperty = _fetcher.findValues(specs, DIFFERENCE_WITHIN_RANGE_CONSTRAINT_PID);
|
||||
List<Value> minValue = _fetcher.findValues(specs, MINIMUM_VALUE_PID);
|
||||
List<Value> maxValue = _fetcher.findValues(specs, MAXIMUM_VALUE_PID);
|
||||
if (!lowerValueProperty.isEmpty()) {
|
||||
lowerPropertyIdValue = (PropertyIdValue) lowerValueProperty.get(0);
|
||||
}
|
||||
if (!minValue.isEmpty()) {
|
||||
minRangeValue = (QuantityValue) minValue.get(0);
|
||||
}
|
||||
if (!maxValue.isEmpty()) {
|
||||
maxRangeValue = (QuantityValue) maxValue.get(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scrutinize(ItemUpdate update) {
|
||||
@ -21,15 +49,16 @@ public class DifferenceWithinRangeScrutinizer extends EditScrutinizer {
|
||||
}
|
||||
|
||||
for(PropertyIdValue propertyId : propertyIdValueValueMap.keySet()){
|
||||
if (_fetcher.hasDiffWithinRange(propertyId)){
|
||||
PropertyIdValue lowerPropertyId = _fetcher.getLowerPropertyId(propertyId);
|
||||
QuantityValue minRangeValue = _fetcher.getMinimumValue(propertyId);
|
||||
QuantityValue maxRangeValue = _fetcher.getMaximumValue(propertyId);
|
||||
List<Statement> statementList = _fetcher.getConstraintsByType(propertyId, DIFFERENCE_WITHIN_RANGE_CONSTRAINT_QID);
|
||||
if (!statementList.isEmpty()){
|
||||
DifferenceWithinRangeConstraint constraint = new DifferenceWithinRangeConstraint(statementList.get(0));
|
||||
PropertyIdValue lowerPropertyId = constraint.lowerPropertyIdValue;
|
||||
QuantityValue minRangeValue = constraint.minRangeValue;
|
||||
QuantityValue maxRangeValue = constraint.maxRangeValue;
|
||||
|
||||
if (propertyIdValueValueMap.containsKey(lowerPropertyId)){
|
||||
Value startingValue = propertyIdValueValueMap.get(lowerPropertyId);
|
||||
Value endingValue = propertyIdValueValueMap.get(propertyId);
|
||||
|
||||
if (startingValue instanceof TimeValue && endingValue instanceof TimeValue){
|
||||
TimeValue lowerDate = (TimeValue)startingValue;
|
||||
TimeValue upperDate = (TimeValue)endingValue;
|
||||
|
@ -1,6 +1,6 @@
|
||||
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;
|
||||
@ -8,9 +8,34 @@ import org.testng.annotations.Test;
|
||||
import org.wikidata.wdtk.datamodel.helpers.Datamodel;
|
||||
import org.wikidata.wdtk.datamodel.implementation.StatementImpl;
|
||||
import org.wikidata.wdtk.datamodel.implementation.TimeValueImpl;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.*;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.ItemIdValue;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.QuantityValue;
|
||||
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.TimeValue;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.ValueSnak;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class DifferenceWithinScrutinizerTest extends ScrutinizerTest{
|
||||
|
||||
public static PropertyIdValue upperBoundPid = Datamodel.makeWikidataPropertyIdValue("P570");
|
||||
public static PropertyIdValue lowerBoundPid = Datamodel.makeWikidataPropertyIdValue("P569");
|
||||
public static QuantityValue minValue = Datamodel.makeQuantityValue(new BigDecimal(0));
|
||||
public static QuantityValue maxValue = Datamodel.makeQuantityValue(new BigDecimal(150));
|
||||
public static ItemIdValue entityIdValue = Datamodel.makeWikidataItemIdValue("Q21510854");
|
||||
|
||||
public static PropertyIdValue propertyParameterPID = Datamodel.makeWikidataPropertyIdValue("P2306");
|
||||
public static PropertyIdValue minimumValuePID = Datamodel.makeWikidataPropertyIdValue("P2313");
|
||||
public static PropertyIdValue maximumValuePID = Datamodel.makeWikidataPropertyIdValue("P2312");
|
||||
|
||||
@Override
|
||||
public EditScrutinizer getScrutinizer() {
|
||||
return new DifferenceWithinRangeScrutinizer();
|
||||
@ -19,19 +44,27 @@ public class DifferenceWithinScrutinizerTest extends ScrutinizerTest{
|
||||
@Test
|
||||
public void testTrigger() {
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
PropertyIdValue lowerBoundPid = MockConstraintFetcher.lowerBoundPid;
|
||||
PropertyIdValue upperBoundPid = MockConstraintFetcher.differenceWithinRangePid;
|
||||
|
||||
TimeValue lowerYear = new TimeValueImpl(1800, (byte)10, (byte)15, (byte)0, (byte)0, (byte)0, (byte)11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
|
||||
TimeValue upperYear = new TimeValueImpl(2020, (byte)10, (byte)15, (byte)0, (byte)0, (byte)0, (byte)11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
|
||||
|
||||
ValueSnak value1 = Datamodel.makeValueSnak(lowerBoundPid, lowerYear);
|
||||
ValueSnak value2 = Datamodel.makeValueSnak(upperBoundPid, upperYear);
|
||||
|
||||
Statement statement1 = new StatementImpl("P569", value1,idA);
|
||||
Statement statement2 = new StatementImpl("P570", value2,idA);
|
||||
|
||||
ItemUpdate updateA = new ItemUpdateBuilder(idA).addStatement(statement1).addStatement(statement2).build();
|
||||
|
||||
Snak propertyQualifier = Datamodel.makeValueSnak(propertyParameterPID, lowerBoundPid);
|
||||
Snak minValueQualifier = Datamodel.makeValueSnak(minimumValuePID, minValue);
|
||||
Snak maxValueQualifier = Datamodel.makeValueSnak(maximumValuePID, maxValue);
|
||||
List<SnakGroup> constraintQualifiers = makeSnakGroupList(propertyQualifier, minValueQualifier, maxValueQualifier);
|
||||
List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
|
||||
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(upperBoundPid, "Q21510854")).thenReturn(constraintDefinitions);
|
||||
when(fetcher.findValues(constraintQualifiers, "P2306")).thenReturn(Collections.singletonList(lowerBoundPid));
|
||||
when(fetcher.findValues(constraintQualifiers, "P2313")).thenReturn(Collections.singletonList(minValue));
|
||||
when(fetcher.findValues(constraintQualifiers, "P2312")).thenReturn(Collections.singletonList(maxValue));
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(updateA);
|
||||
assertWarningsRaised(DifferenceWithinRangeScrutinizer.type);
|
||||
}
|
||||
@ -39,19 +72,27 @@ public class DifferenceWithinScrutinizerTest extends ScrutinizerTest{
|
||||
@Test
|
||||
public void testNoIssue() {
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
PropertyIdValue lowerBoundPid = MockConstraintFetcher.lowerBoundPid;
|
||||
PropertyIdValue upperBoundPid = MockConstraintFetcher.differenceWithinRangePid;
|
||||
|
||||
TimeValue lowerYear = new TimeValueImpl(2000, (byte)10, (byte)15, (byte)0, (byte)0, (byte)0, (byte)11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
|
||||
TimeValue upperYear = new TimeValueImpl(2020, (byte)10, (byte)15, (byte)0, (byte)0, (byte)0, (byte)11, 0, 0, 0, TimeValue.CM_GREGORIAN_PRO);
|
||||
|
||||
ValueSnak value1 = Datamodel.makeValueSnak(lowerBoundPid, lowerYear);
|
||||
ValueSnak value2 = Datamodel.makeValueSnak(upperBoundPid, upperYear);
|
||||
|
||||
Statement statement1 = new StatementImpl("P569", value1,idA);
|
||||
Statement statement2 = new StatementImpl("P570", value2,idA);
|
||||
|
||||
ItemUpdate updateA = new ItemUpdateBuilder(idA).addStatement(statement1).addStatement(statement2).build();
|
||||
|
||||
Snak propertyQualifier = Datamodel.makeValueSnak(propertyParameterPID, lowerBoundPid);
|
||||
Snak minValueQualifier = Datamodel.makeValueSnak(minimumValuePID, minValue);
|
||||
Snak maxValueQualifier = Datamodel.makeValueSnak(maximumValuePID, maxValue);
|
||||
List<SnakGroup> constraintQualifiers = makeSnakGroupList(propertyQualifier, minValueQualifier, maxValueQualifier);
|
||||
List<Statement> constraintDefinitions = constraintParameterStatementList(entityIdValue, constraintQualifiers);
|
||||
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(upperBoundPid, "Q21510854")).thenReturn(constraintDefinitions);
|
||||
when(fetcher.findValues(constraintQualifiers, "P2306")).thenReturn(Collections.singletonList(lowerBoundPid));
|
||||
when(fetcher.findValues(constraintQualifiers, "P2313")).thenReturn(Collections.singletonList(minValue));
|
||||
when(fetcher.findValues(constraintQualifiers, "P2312")).thenReturn(Collections.singletonList(maxValue));
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(updateA);
|
||||
assertNoWarningRaised();
|
||||
}
|
||||
|
@ -39,9 +39,12 @@ import org.wikidata.wdtk.datamodel.interfaces.SnakGroup;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.Statement;
|
||||
import org.wikidata.wdtk.datamodel.interfaces.StatementRank;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -110,4 +113,26 @@ public abstract class ScrutinizerTest {
|
||||
return statements;
|
||||
}
|
||||
|
||||
public List<SnakGroup> makeSnakGroupList(Snak... snaks) {
|
||||
Map<PropertyIdValue, List<Snak>> propertySnakMap = new HashMap<>();
|
||||
for (Snak snak : snaks) {
|
||||
PropertyIdValue pid = snak.getPropertyId();
|
||||
List<Snak> snakList;
|
||||
if (propertySnakMap.containsKey(pid)) {
|
||||
snakList = propertySnakMap.get(pid);
|
||||
} else {
|
||||
snakList = new ArrayList<>();
|
||||
}
|
||||
snakList.add(snak);
|
||||
propertySnakMap.put(pid, snakList);
|
||||
}
|
||||
|
||||
List<SnakGroup> snakGroupList = new ArrayList<>();
|
||||
for (List<Snak> snakList : propertySnakMap.values()) {
|
||||
snakGroupList.add(Datamodel.makeSnakGroup(snakList));
|
||||
}
|
||||
|
||||
return snakGroupList;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user