Implemented QuantityScrutinizer tests using Mocks (#2862)
* Implemented QunatityScrutinizer tests using Mockito Updated test cases and added AllowedUnitsConstraint class * Test cases updated
This commit is contained in:
parent
9edb1e514d
commit
c68047a614
@ -1,9 +1,18 @@
|
||||
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.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.Value;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Scrutinizer checking for units and bounds in quantities.
|
||||
@ -18,27 +27,51 @@ public class QuantityScrutinizer extends SnakScrutinizer {
|
||||
public static final String invalidUnitType = "invalid-unit";
|
||||
public static final String noUnitProvidedType = "no-unit-provided";
|
||||
|
||||
public static String NO_BOUNDS_CONSTRAINT_QID = "Q51723761";
|
||||
public static String INTEGER_VALUED_CONSTRAINT_QID = "Q52848401";
|
||||
|
||||
public static String ALLOWED_UNITS_CONSTRAINT_QID = "Q21514353";
|
||||
public static String ALLOWED_UNITS_CONSTRAINT_PID = "P2305";
|
||||
|
||||
class AllowedUnitsConstraint {
|
||||
Set<ItemIdValue> allowedUnits;
|
||||
AllowedUnitsConstraint(Statement statement) {
|
||||
List<SnakGroup> specs = statement.getClaim().getQualifiers();
|
||||
if (specs != null) {
|
||||
List<Value> properties = _fetcher.findValues(specs, ALLOWED_UNITS_CONSTRAINT_PID);
|
||||
allowedUnits = properties.stream()
|
||||
.map(e -> e == null ? null : (ItemIdValue) e)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scrutinize(Snak snak, EntityIdValue entityId, boolean added) {
|
||||
if (snak.getValue() instanceof QuantityValue && added) {
|
||||
PropertyIdValue pid = snak.getPropertyId();
|
||||
QuantityValue value = (QuantityValue)snak.getValue();
|
||||
|
||||
if(!_fetcher.boundsAllowed(pid) && (value.getUpperBound() != null || value.getLowerBound() != null)) {
|
||||
|
||||
if(!_fetcher.getConstraintsByType(pid, NO_BOUNDS_CONSTRAINT_QID).isEmpty() && (value.getUpperBound() != null || value.getLowerBound() != null)) {
|
||||
QAWarning issue = new QAWarning(boundsDisallowedType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
|
||||
issue.setProperty("property_entity", pid);
|
||||
issue.setProperty("example_value", value.getNumericValue().toString());
|
||||
issue.setProperty("example_item_entity", entityId);
|
||||
addIssue(issue);
|
||||
}
|
||||
if(_fetcher.integerValued(pid) && value.getNumericValue().scale() > 0) {
|
||||
if(!_fetcher.getConstraintsByType(pid, INTEGER_VALUED_CONSTRAINT_QID).isEmpty() && value.getNumericValue().scale() > 0) {
|
||||
QAWarning issue = new QAWarning(integerConstraintType, pid.getId(), QAWarning.Severity.IMPORTANT, 1);
|
||||
issue.setProperty("property_entity", pid);
|
||||
issue.setProperty("example_value", value.getNumericValue().toString());
|
||||
issue.setProperty("example_item_entity", entityId);
|
||||
addIssue(issue);
|
||||
}
|
||||
Set<ItemIdValue> allowedUnits = _fetcher.allowedUnits(pid);
|
||||
List<Statement> statementList = _fetcher.getConstraintsByType(pid, ALLOWED_UNITS_CONSTRAINT_QID);
|
||||
Set<ItemIdValue> allowedUnits = null;
|
||||
if (!statementList.isEmpty()) {
|
||||
AllowedUnitsConstraint allowedUnitsConstraint = new AllowedUnitsConstraint(statementList.get(0));
|
||||
allowedUnits = allowedUnitsConstraint.allowedUnits;
|
||||
}
|
||||
ItemIdValue currentUnit = null;
|
||||
if (value.getUnitItemId() != null) {
|
||||
currentUnit = value.getUnitItemId();
|
||||
|
@ -1,11 +1,32 @@
|
||||
package org.openrefine.wikidata.qa.scrutinizers;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
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.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.Value;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.openrefine.wikidata.qa.scrutinizers.QuantityScrutinizer.ALLOWED_UNITS_CONSTRAINT_QID;
|
||||
import static org.openrefine.wikidata.qa.scrutinizers.QuantityScrutinizer.INTEGER_VALUED_CONSTRAINT_QID;
|
||||
import static org.openrefine.wikidata.qa.scrutinizers.QuantityScrutinizer.NO_BOUNDS_CONSTRAINT_QID;
|
||||
|
||||
public class QuantityScrutinizerTest extends ValueScrutinizerTest{
|
||||
|
||||
@ -27,7 +48,15 @@ public class QuantityScrutinizerTest extends ValueScrutinizerTest{
|
||||
new BigDecimal("1.234"), "Q346721");
|
||||
|
||||
private QuantityValue goodUnitValue = Datamodel.makeQuantityValue(
|
||||
new BigDecimal("1.234"), MockConstraintFetcher.allowedUnit.getIri());
|
||||
new BigDecimal("1.234"), (ItemIdValue) allowedUnit);
|
||||
|
||||
|
||||
public static PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P1083");
|
||||
public static ItemIdValue noBoundsEntity = Datamodel.makeWikidataItemIdValue(NO_BOUNDS_CONSTRAINT_QID);
|
||||
public static ItemIdValue integerValueEntity = Datamodel.makeWikidataItemIdValue(INTEGER_VALUED_CONSTRAINT_QID);
|
||||
public static ItemIdValue allowedUnitEntity = Datamodel.makeWikidataItemIdValue(ALLOWED_UNITS_CONSTRAINT_QID);
|
||||
public static PropertyIdValue itemParameterPID = Datamodel.makeWikidataPropertyIdValue("P2305");
|
||||
public static Value allowedUnit = Datamodel.makeWikidataItemIdValue("Q5");
|
||||
|
||||
@Override
|
||||
public EditScrutinizer getScrutinizer() {
|
||||
@ -36,67 +65,189 @@ public class QuantityScrutinizerTest extends ValueScrutinizerTest{
|
||||
|
||||
@Test
|
||||
public void testBoundsAllowed() {
|
||||
scrutinize(valueWithBounds);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, valueWithBounds);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, NO_BOUNDS_CONSTRAINT_QID)).thenReturn(new ArrayList<>());
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertNoWarningRaised();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBoundsDisallowed() {
|
||||
scrutinize(MockConstraintFetcher.noBoundsPid, valueWithBounds);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, valueWithBounds);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
List<Statement> constraintDefinitions = constraintParameterStatementList(noBoundsEntity, new ArrayList<>());
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, NO_BOUNDS_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertWarningsRaised(QuantityScrutinizer.boundsDisallowedType);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFractionalAllowed() {
|
||||
scrutinize(exactValue);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, exactValue);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, INTEGER_VALUED_CONSTRAINT_QID)).thenReturn(new ArrayList<>());
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertNoWarningRaised();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFractionalDisallowed() {
|
||||
scrutinize(MockConstraintFetcher.integerPid, exactValue);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, exactValue);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
List<Statement> constraintDefinitions = constraintParameterStatementList(integerValueEntity, new ArrayList<>());
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, INTEGER_VALUED_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertWarningsRaised(QuantityScrutinizer.integerConstraintType);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testTrailingZeros() {
|
||||
scrutinize(MockConstraintFetcher.integerPid, trailingZeros);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, trailingZeros);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
List<Statement> constraintDefinitions = constraintParameterStatementList(integerValueEntity, new ArrayList<>());
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, INTEGER_VALUED_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertWarningsRaised(QuantityScrutinizer.integerConstraintType);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testInteger() {
|
||||
scrutinize(MockConstraintFetcher.integerPid, integerValue);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, integerValue);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
List<Statement> constraintDefinitions = constraintParameterStatementList(integerValueEntity, new ArrayList<>());
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, INTEGER_VALUED_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertNoWarningRaised();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUnitReqired() {
|
||||
scrutinize(MockConstraintFetcher.allowedUnitsPid, integerValue);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, integerValue);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, allowedUnit);
|
||||
List<Snak> qualifierSnakList = Collections.singletonList(qualifierSnak);
|
||||
SnakGroup snakGroup1 = Datamodel.makeSnakGroup(qualifierSnakList);
|
||||
List<SnakGroup> constraintQualifiers = Collections.singletonList(snakGroup1);
|
||||
List<Statement> constraintDefinitions = constraintParameterStatementList(allowedUnitEntity, constraintQualifiers);
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, ALLOWED_UNITS_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertWarningsRaised(QuantityScrutinizer.noUnitProvidedType);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testWrongUnit() {
|
||||
scrutinize(MockConstraintFetcher.allowedUnitsPid, wrongUnitValue);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, wrongUnitValue);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, allowedUnit);
|
||||
List<Snak> qualifierSnakList = Collections.singletonList(qualifierSnak);
|
||||
SnakGroup snakGroup1 = Datamodel.makeSnakGroup(qualifierSnakList);
|
||||
List<SnakGroup> constraintQualifiers = Collections.singletonList(snakGroup1);
|
||||
List<Statement> constraintDefinitions = constraintParameterStatementList(allowedUnitEntity, constraintQualifiers);
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, ALLOWED_UNITS_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertWarningsRaised(QuantityScrutinizer.invalidUnitType);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testGoodUnit() {
|
||||
scrutinize(MockConstraintFetcher.allowedUnitsPid, goodUnitValue);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, goodUnitValue);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
Snak qualifierSnak = Datamodel.makeValueSnak(itemParameterPID, allowedUnit);
|
||||
List<Snak> qualifierSnakList = Collections.singletonList(qualifierSnak);
|
||||
SnakGroup snakGroup1 = Datamodel.makeSnakGroup(qualifierSnakList);
|
||||
List<SnakGroup> constraintQualifiers = Collections.singletonList(snakGroup1);
|
||||
List<Statement> constraintDefinitions = constraintParameterStatementList(allowedUnitEntity, constraintQualifiers);
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, ALLOWED_UNITS_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||
when(fetcher.findValues(constraintQualifiers, "P2305")).thenReturn(Collections.singletonList(allowedUnit));
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertNoWarningRaised();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testUnitForbidden() {
|
||||
scrutinize(MockConstraintFetcher.noUnitsPid, goodUnitValue);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, goodUnitValue);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
List<Statement> constraintDefinitions = constraintParameterStatementList(allowedUnitEntity, new ArrayList<>());
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, ALLOWED_UNITS_CONSTRAINT_QID)).thenReturn(constraintDefinitions);
|
||||
when(fetcher.findValues(any(), eq("P2305"))).thenReturn(new ArrayList<>());
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertWarningsRaised(QuantityScrutinizer.invalidUnitType);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testNoUnit() {
|
||||
scrutinize(MockConstraintFetcher.noUnitsPid, integerValue);
|
||||
ItemIdValue idA = TestingData.existingId;
|
||||
Snak mainSnak = Datamodel.makeValueSnak(propertyIdValue, integerValue);
|
||||
Statement statement = new StatementImpl("P1083", mainSnak, idA);
|
||||
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement).build();
|
||||
|
||||
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
|
||||
when(fetcher.getConstraintsByType(propertyIdValue, ALLOWED_UNITS_CONSTRAINT_QID)).thenReturn(new ArrayList<>());
|
||||
when(fetcher.findValues(any(), eq("P2305"))).thenReturn(new ArrayList<>());
|
||||
setFetcher(fetcher);
|
||||
|
||||
scrutinize(update);
|
||||
assertNoWarningRaised();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user