Implemented SingleValue Scrutinizer tests using mocks (#2818)

* Implemented SingleValue Scrutinizer tests using mocks

Updated test class & added inner class to the scrutinizer

* tests updated

* Updated SingleValueConstraint class
This commit is contained in:
Ekta Mishra 2020-06-29 19:29:53 +05:30 committed by GitHub
parent 2a34c8b5e6
commit 46c510b5e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 13 deletions

View File

@ -26,7 +26,7 @@ public class Constraint {
ItemIdValue constraintStatus;
Set<EntityIdValue> constraintExceptions;
Constraint(Statement statement) {
public Constraint(Statement statement) {
constraintExceptions = new HashSet<>();
List<SnakGroup> snakGroupList = statement.getClaim().getQualifiers();
for(SnakGroup group : snakGroupList) {

View File

@ -23,14 +23,15 @@
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers;
import java.util.HashSet;
import java.util.Set;
import org.openrefine.wikidata.qa.QAWarning;
import org.openrefine.wikidata.updates.ItemUpdate;
import org.wikidata.wdtk.datamodel.interfaces.PropertyIdValue;
import org.wikidata.wdtk.datamodel.interfaces.Statement;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* For now this scrutinizer only checks for uniqueness at the item level (it
* ignores qualifiers and references).
@ -44,6 +45,8 @@ import org.wikidata.wdtk.datamodel.interfaces.Statement;
public class SingleValueScrutinizer extends EditScrutinizer {
public static final String type = "single-valued-property-added-more-than-once";
public static String SINGLE_VALUE_CONSTRAINT_QID = "Q19474404";
public static String SINGLE_BEST_VALUE_CONSTRAINT_QID = "Q52060874";
@Override
public void scrutinize(ItemUpdate update) {
@ -51,13 +54,14 @@ public class SingleValueScrutinizer extends EditScrutinizer {
for (Statement statement : update.getAddedStatements()) {
PropertyIdValue pid = statement.getClaim().getMainSnak().getPropertyId();
List<Statement> constraintStatementList1 = _fetcher.getConstraintsByType(pid, SINGLE_VALUE_CONSTRAINT_QID);
List<Statement> constraintStatementList2 = _fetcher.getConstraintsByType(pid, SINGLE_BEST_VALUE_CONSTRAINT_QID);
if (seenSingleProperties.contains(pid)) {
QAWarning issue = new QAWarning(type, pid.getId(), QAWarning.Severity.WARNING, 1);
issue.setProperty("property_entity", pid);
issue.setProperty("example_entity", update.getItemId());
addIssue(issue);
} else if (_fetcher.hasSingleValue(pid) || _fetcher.hasSingleBestValue(pid)) {
} else if (!constraintStatementList1.isEmpty() || !constraintStatementList2.isEmpty()){
seenSingleProperties.add(pid);
}
}

View File

@ -23,14 +23,36 @@
******************************************************************************/
package org.openrefine.wikidata.qa.scrutinizers;
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 org.wikidata.wdtk.datamodel.interfaces.Value;
import java.util.Collections;
import java.util.List;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class SingleValueScrutinizerTest extends ScrutinizerTest {
public static PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P21");
public static Value value1 = Datamodel.makeWikidataItemIdValue("Q6581072");
public static Value value2 = Datamodel.makeWikidataItemIdValue("Q6581097");
public static ItemIdValue entityIdValue = Datamodel.makeWikidataItemIdValue("Q19474404");
public static PropertyIdValue constraintException = Datamodel.makeWikidataPropertyIdValue("P2303");
public static Value exceptionValue = Datamodel.makeWikidataItemIdValue("Q7409772");
@Override
public EditScrutinizer getScrutinizer() {
return new SingleValueScrutinizer();
@ -39,9 +61,21 @@ public class SingleValueScrutinizerTest extends ScrutinizerTest {
@Test
public void testTrigger() {
ItemIdValue idA = TestingData.existingId;
ItemIdValue idB = TestingData.matchedId;
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(TestingData.generateStatement(idA, idB))
.addStatement(TestingData.generateStatement(idA, idB)).build();
Snak snak1 = Datamodel.makeValueSnak(propertyIdValue, value1);
Snak snak2 = Datamodel.makeValueSnak(propertyIdValue, value2);
Statement statement1 = new StatementImpl("P21", snak1, idA);
Statement statement2 = new StatementImpl("P21", snak2, idA);
ItemUpdate update = new ItemUpdateBuilder(idA).addStatement(statement1).addStatement(statement2).build();
Snak snak = Datamodel.makeValueSnak(constraintException, exceptionValue);
List<Snak> snakList = Collections.singletonList(snak);
SnakGroup snakGroup = Datamodel.makeSnakGroup(snakList);
List<SnakGroup> snakGroupList = Collections.singletonList(snakGroup);
List<Statement> statementList = constraintParameterStatementList(entityIdValue, snakGroupList);
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
when(fetcher.getConstraintsByType(propertyIdValue, "Q19474404")).thenReturn(statementList);
setFetcher(fetcher);
scrutinize(update);
assertWarningsRaised(SingleValueScrutinizer.type);
}
@ -49,10 +83,20 @@ public class SingleValueScrutinizerTest extends ScrutinizerTest {
@Test
public void testNoIssue() {
ItemIdValue idA = TestingData.existingId;
ItemIdValue idB = TestingData.matchedId;
ItemUpdate updateA = new ItemUpdateBuilder(idA).addStatement(TestingData.generateStatement(idA, idB)).build();
ItemUpdate updateB = new ItemUpdateBuilder(idB).addStatement(TestingData.generateStatement(idB, idB)).build();
scrutinize(updateA, updateB);
Snak snak1 = Datamodel.makeValueSnak(propertyIdValue, value1);
Statement statement1 = new StatementImpl("P21", snak1, idA);
ItemUpdate updateA = new ItemUpdateBuilder(idA).addStatement(statement1).build();
Snak snak = Datamodel.makeValueSnak(constraintException, exceptionValue);
List<Snak> snakList = Collections.singletonList(snak);
SnakGroup snakGroup = Datamodel.makeSnakGroup(snakList);
List<SnakGroup> snakGroupList = Collections.singletonList(snakGroup);
List<Statement> statementList = constraintParameterStatementList(entityIdValue, snakGroupList);
ConstraintFetcher fetcher = mock(ConstraintFetcher.class);
when(fetcher.getConstraintsByType(propertyIdValue, "Q19474404")).thenReturn(statementList);
setFetcher(fetcher);
scrutinize(updateA);
assertNoWarningRaised();
}
}