From 29f6885cccc9c6a28a21f283a6404a38fe06a73f Mon Sep 17 00:00:00 2001 From: Ekta Mishra Date: Mon, 6 Jul 2020 21:57:16 +0530 Subject: [PATCH] MockConstraintFetcher cleanup part-I (#2882) * MockConstraintFetcher cleanup part-I Removed its use from different test files * Removed MockConstraintFetcher's use from various test files --- .../wikidata/qa/MockConstraintFetcher.java | 256 ------------------ .../ConflictsWithScrutinizerTest.java | 22 +- .../EntityTypeScrutinizerTest.java | 12 +- .../qa/scrutinizers/ScrutinizerTest.java | 4 - 4 files changed, 19 insertions(+), 275 deletions(-) delete mode 100644 extensions/wikidata/tests/src/org/openrefine/wikidata/qa/MockConstraintFetcher.java diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/MockConstraintFetcher.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/MockConstraintFetcher.java deleted file mode 100644 index 194a57b41..000000000 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/MockConstraintFetcher.java +++ /dev/null @@ -1,256 +0,0 @@ -/******************************************************************************* - * MIT License - * - * Copyright (c) 2018 Antonin Delpeuch - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - ******************************************************************************/ -package org.openrefine.wikidata.qa; - -import org.wikidata.wdtk.datamodel.helpers.Datamodel; -import org.wikidata.wdtk.datamodel.interfaces.Claim; -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.Reference; -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.StatementRank; -import org.wikidata.wdtk.datamodel.interfaces.Value; - -import java.math.BigDecimal; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -public class MockConstraintFetcher implements ConstraintFetcher { - - public static PropertyIdValue pidWithInverse = Datamodel.makeWikidataPropertyIdValue("P350"); - public static PropertyIdValue inversePid = Datamodel.makeWikidataPropertyIdValue("P57"); - public static PropertyIdValue symmetricPid = Datamodel.makeWikidataPropertyIdValue("P783"); - public static PropertyIdValue allowedQualifierPid = Datamodel.makeWikidataPropertyIdValue("P34"); - public static PropertyIdValue mandatoryQualifierPid = Datamodel.makeWikidataPropertyIdValue("P97"); - - public static PropertyIdValue mainSnakPid = Datamodel.makeWikidataPropertyIdValue("P1234"); - public static PropertyIdValue qualifierPid = Datamodel.makeWikidataPropertyIdValue("P987"); - public static PropertyIdValue referencePid = Datamodel.makeWikidataPropertyIdValue("P384"); - - public static PropertyIdValue allowedValuesPid = Datamodel.makeWikidataPropertyIdValue("P8121"); - public static ItemIdValue allowedValueQid = Datamodel.makeWikidataItemIdValue("Q389"); - public static PropertyIdValue forbiddenValuesPid = Datamodel.makeWikidataPropertyIdValue("P8141"); - public static ItemIdValue forbiddenValueQid = Datamodel.makeWikidataItemIdValue("Q378"); - - public static PropertyIdValue allowedUnitsPid = Datamodel.makeWikidataPropertyIdValue("P34787"); - public static ItemIdValue allowedUnit = Datamodel.makeWikidataItemIdValue("Q7887"); - public static PropertyIdValue noUnitsPid = Datamodel.makeWikidataPropertyIdValue("P334211"); - - public static PropertyIdValue noBoundsPid = Datamodel.makeWikidataPropertyIdValue("P8932"); - public static PropertyIdValue integerPid = Datamodel.makeWikidataPropertyIdValue("P389"); - - public static PropertyIdValue propertyOnlyPid = Datamodel.makeWikidataPropertyIdValue("P372"); - - public static PropertyIdValue differenceWithinRangePid = Datamodel.makeWikidataPropertyIdValue("P570"); - public static PropertyIdValue lowerBoundPid = Datamodel.makeWikidataPropertyIdValue("P569"); - public static QuantityValue minValuePid = Datamodel.makeQuantityValue(new BigDecimal(0)); - public static QuantityValue maxValuePid = Datamodel.makeQuantityValue(new BigDecimal(150)); - - public static PropertyIdValue conflictsWithPid = Datamodel.makeWikidataPropertyIdValue("P2002"); - public static Value conflictsWithStatementValue = Datamodel.makeWikidataItemIdValue("Q36322"); - public static PropertyIdValue conflictingStatement1Pid = Datamodel.makeWikidataPropertyIdValue("P31"); - public static Value conflictingStatement1Value = Datamodel.makeWikidataItemIdValue("Q4167836"); - public static PropertyIdValue conflictingStatement2Pid = Datamodel.makeWikidataPropertyIdValue("P553"); - public static Value conflictingStatement2Value = Datamodel.makeWikidataItemIdValue("Q918"); - - @Override - public String getFormatRegex(PropertyIdValue pid) { - return "[1-9]\\d+"; - } - - /** - * This constraint is purposely left inconsistent (the inverse constraint holds - * only on one side). - */ - @Override - public PropertyIdValue getInversePid(PropertyIdValue pid) { - if (pidWithInverse.equals(pid)) { - return inversePid; - } - return null; - } - - @Override - public boolean allowedAsValue(PropertyIdValue pid) { - return (!qualifierPid.equals(pid) && !referencePid.equals(pid)); - } - - @Override - public boolean allowedAsQualifier(PropertyIdValue pid) { - return (!mainSnakPid.equals(pid) && !referencePid.equals(pid)); - } - - @Override - public boolean allowedAsReference(PropertyIdValue pid) { - return (!mainSnakPid.equals(pid) && !qualifierPid.equals(pid)); - } - - @Override - public Set allowedQualifiers(PropertyIdValue pid) { - return Arrays.asList(allowedQualifierPid, mandatoryQualifierPid).stream().collect(Collectors.toSet()); - } - - @Override - public Set mandatoryQualifiers(PropertyIdValue pid) { - return Collections.singleton(mandatoryQualifierPid); - } - - @Override - public boolean hasSingleValue(PropertyIdValue pid) { - return true; - } - - @Override - public boolean hasSingleBestValue(PropertyIdValue pid) { - return false; - } - - @Override - public boolean hasDistinctValues(PropertyIdValue pid) { - return true; - } - - @Override - public boolean hasMultiValue(PropertyIdValue pid) { - return true; - } - - @Override - public boolean isSymmetric(PropertyIdValue pid) { - return pid.equals(symmetricPid); - } - - @Override - public Set allowedValues(PropertyIdValue pid) { - if (allowedValuesPid.equals(pid)) { - return Arrays.asList(allowedValueQid, null).stream().collect(Collectors.toSet()); - } - return null; - } - - @Override - public Set disallowedValues(PropertyIdValue pid) { - if (forbiddenValuesPid.equals(pid)) { - return Collections.singleton(forbiddenValueQid); - } - return null; - } - - @Override - public boolean boundsAllowed(PropertyIdValue pid) { - return !noBoundsPid.equals(pid); - } - - @Override - public boolean integerValued(PropertyIdValue pid) { - return integerPid.equals(pid); - } - - @Override - public Set allowedUnits(PropertyIdValue pid) { - if(allowedUnitsPid.equals(pid)) { - return Collections.singleton(allowedUnit); - } else if(noUnitsPid.equals(pid)) { - return Collections.singleton(null); - } - return null; - } - - @Override - public boolean usableOnItems(PropertyIdValue pid) { - return !propertyOnlyPid.equals(pid); - } - - @Override - public QuantityValue getMinimumValue(PropertyIdValue pid) { - if (differenceWithinRangePid.equals(pid)) { - return minValuePid; - } - return null; - } - - @Override - public QuantityValue getMaximumValue(PropertyIdValue pid) { - if (differenceWithinRangePid.equals(pid)) { - return maxValuePid; - } - return null; - } - - @Override - public PropertyIdValue getLowerPropertyId(PropertyIdValue pid) { - if (differenceWithinRangePid.equals(pid)){ - return lowerBoundPid; - } - return null; - } - - @Override - public boolean hasDiffWithinRange(PropertyIdValue pid) { - return true; - } - - @Override - public List getConstraintsByType(PropertyIdValue pid, String qid) { - EntityIdValue entityIdValue = Datamodel.makeWikidataItemIdValue("Q21502838"); - PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P2302"); - Snak snak = Datamodel.makeValueSnak(propertyIdValue,entityIdValue); - - PropertyIdValue property = Datamodel.makeWikidataPropertyIdValue("P2306"); - Value propertyValue = Datamodel.makeWikidataPropertyIdValue("P31"); - Snak snak1 = Datamodel.makeValueSnak(property, propertyValue); - List group1 = Collections.singletonList(snak1); - - PropertyIdValue item = Datamodel.makeWikidataPropertyIdValue("P2305"); - Value itemValue = Datamodel.makeWikidataItemIdValue("Q5"); - Snak snak2 = Datamodel.makeValueSnak(item, itemValue); - List group2 = Collections.singletonList(snak2); - - SnakGroup snakGroup1 = Datamodel.makeSnakGroup(group1); - SnakGroup snakGroup2 = Datamodel.makeSnakGroup(group2); - - List listSnakGroup = Arrays.asList(snakGroup1, snakGroup2); - Claim claim = Datamodel.makeClaim(entityIdValue, snak, listSnakGroup); - - Reference reference = Datamodel.makeReference(listSnakGroup); - List referenceList = Collections.singletonList(reference); - - Statement statement = Datamodel.makeStatement(claim, referenceList, StatementRank.NORMAL, "P2302$77BD7FE4-C051-4776-855C-543F0CE697D0"); - List statements = Collections.singletonList(statement); - - return statements; - } - - @Override - public List findValues(List groups, String pid) { - return null; - } -} diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizerTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizerTest.java index 666f134b8..c003b0707 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizerTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ConflictsWithScrutinizerTest.java @@ -1,7 +1,6 @@ package org.openrefine.wikidata.qa.scrutinizers; import org.openrefine.wikidata.qa.ConstraintFetcher; -import org.openrefine.wikidata.qa.MockConstraintFetcher; import org.openrefine.wikidata.testing.TestingData; import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdateBuilder; @@ -24,21 +23,24 @@ import java.util.List; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.openrefine.wikidata.qa.scrutinizers.ConflictsWithScrutinizer.CONFLICTS_WITH_CONSTRAINT_QID; +import static org.openrefine.wikidata.qa.scrutinizers.ConflictsWithScrutinizer.CONFLICTS_WITH_PROPERTY_PID; +import static org.openrefine.wikidata.qa.scrutinizers.ConflictsWithScrutinizer.ITEM_OF_PROPERTY_CONSTRAINT_PID; public class ConflictsWithScrutinizerTest extends ScrutinizerTest { - public static PropertyIdValue conflictsWithPid = MockConstraintFetcher.conflictsWithPid; - public static Value conflictsWithValue = MockConstraintFetcher.conflictsWithStatementValue; - public static PropertyIdValue propertyWithConflictsPid1 = MockConstraintFetcher.conflictingStatement1Pid; - public static Value conflictingValue1 = MockConstraintFetcher.conflictingStatement1Value; - public static PropertyIdValue propertyWithConflictsPid2 = MockConstraintFetcher.conflictingStatement2Pid; - public static Value conflictingValue2 = MockConstraintFetcher.conflictingStatement2Value; + public static PropertyIdValue conflictsWithPid = Datamodel.makeWikidataPropertyIdValue("P2002"); + public static Value conflictsWithValue = Datamodel.makeWikidataItemIdValue("Q36322"); + public static PropertyIdValue propertyWithConflictsPid1 = Datamodel.makeWikidataPropertyIdValue("P31"); + public static Value conflictingValue1 = Datamodel.makeWikidataItemIdValue("Q4167836"); + public static PropertyIdValue propertyWithConflictsPid2 = Datamodel.makeWikidataPropertyIdValue("P553"); + public static Value conflictingValue2 = Datamodel.makeWikidataItemIdValue("Q918"); - public static ItemIdValue entityIdValue = Datamodel.makeWikidataItemIdValue("Q21502838"); - public static PropertyIdValue propertyParameterPID = Datamodel.makeWikidataPropertyIdValue("P2306"); + public static ItemIdValue entityIdValue = Datamodel.makeWikidataItemIdValue(CONFLICTS_WITH_CONSTRAINT_QID); + public static PropertyIdValue propertyParameterPID = Datamodel.makeWikidataPropertyIdValue(CONFLICTS_WITH_PROPERTY_PID); public static Value conflictingPropertyValue1 = Datamodel.makeWikidataPropertyIdValue("P31"); public static Value conflictingPropertyValue2 = Datamodel.makeWikidataPropertyIdValue("P553"); - public static PropertyIdValue itemParameterPID = Datamodel.makeWikidataPropertyIdValue("P2305"); + public static PropertyIdValue itemParameterPID = Datamodel.makeWikidataPropertyIdValue(ITEM_OF_PROPERTY_CONSTRAINT_PID); public static Value conflictingItemValue1 = Datamodel.makeWikidataItemIdValue("Q4167836"); public static Value conflictingItemValue2 = Datamodel.makeWikidataItemIdValue("Q918"); diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/EntityTypeScrutinizerTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/EntityTypeScrutinizerTest.java index e8f7688ba..671b59160 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/EntityTypeScrutinizerTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/EntityTypeScrutinizerTest.java @@ -1,7 +1,6 @@ package org.openrefine.wikidata.qa.scrutinizers; import org.openrefine.wikidata.qa.ConstraintFetcher; -import org.openrefine.wikidata.qa.MockConstraintFetcher; import org.openrefine.wikidata.testing.TestingData; import org.openrefine.wikidata.updates.ItemUpdate; import org.openrefine.wikidata.updates.ItemUpdateBuilder; @@ -21,16 +20,19 @@ import java.util.List; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import static org.openrefine.wikidata.qa.scrutinizers.EntityTypeScrutinizer.ALLOWED_ENTITY_TYPES_PID; +import static org.openrefine.wikidata.qa.scrutinizers.EntityTypeScrutinizer.ALLOWED_ENTITY_TYPES_QID; +import static org.openrefine.wikidata.qa.scrutinizers.EntityTypeScrutinizer.ALLOWED_ITEM_TYPE_QID; public class EntityTypeScrutinizerTest extends StatementScrutinizerTest { public static PropertyIdValue propertyIdValue = Datamodel.makeWikidataPropertyIdValue("P2302"); - public static Value propertyValue = MockConstraintFetcher.conflictsWithStatementValue; + public static Value propertyValue = Datamodel.makeWikidataItemIdValue("Q36322"); - public static ItemIdValue entityIdValue = Datamodel.makeWikidataItemIdValue("Q52004125"); - public static PropertyIdValue itemParameterPID = Datamodel.makeWikidataPropertyIdValue("P2305"); + public static ItemIdValue entityIdValue = Datamodel.makeWikidataItemIdValue(ALLOWED_ENTITY_TYPES_QID); + public static PropertyIdValue itemParameterPID = Datamodel.makeWikidataPropertyIdValue(ALLOWED_ENTITY_TYPES_PID); public static Value itemValue = Datamodel.makeWikidataItemIdValue("Q29934218"); - public static Value allowedValue = Datamodel.makeWikidataItemIdValue("Q29934200"); + public static Value allowedValue = Datamodel.makeWikidataItemIdValue(ALLOWED_ITEM_TYPE_QID); diff --git a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ScrutinizerTest.java b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ScrutinizerTest.java index e5cd8b0fb..3d3de0fe5 100644 --- a/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ScrutinizerTest.java +++ b/extensions/wikidata/tests/src/org/openrefine/wikidata/qa/scrutinizers/ScrutinizerTest.java @@ -24,7 +24,6 @@ package org.openrefine.wikidata.qa.scrutinizers; import org.openrefine.wikidata.qa.ConstraintFetcher; -import org.openrefine.wikidata.qa.MockConstraintFetcher; import org.openrefine.wikidata.qa.QAWarning; import org.openrefine.wikidata.qa.QAWarningStore; import org.openrefine.wikidata.updates.ItemUpdate; @@ -57,15 +56,12 @@ public abstract class ScrutinizerTest { private EditScrutinizer scrutinizer; private QAWarningStore store; - private ConstraintFetcher fetcher; @BeforeMethod public void setUp() { store = new QAWarningStore(); - fetcher = new MockConstraintFetcher(); scrutinizer = getScrutinizer(); scrutinizer.setStore(store); - scrutinizer.setFetcher(fetcher); } public void scrutinize(ItemUpdate... updates) {