Lucas Werkmeister (WMDE) has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/405032 )
Change subject: Rewrite ValueCountChecker tests
......................................................................
Rewrite ValueCountChecker tests
The tests for SingleValueChecker and MultiValueChecker used to load
entities from JSON files and then additionally construct the Statement
for the context independently of them, which was risky because the
constructed Statement was not entirely equivalent to the deserialized
one (its GUID was never set). Since this pattern breaks with the
upcoming changes to ValueCountCheckerHelper (change TODO, for T175566),
rewrite the tests to use NewItem and NewStatement instead.
And on that opportunity, also do some other cleanups: most importantly,
create the mock Constraint object in the constructor, since we never
passed anything other than [] into the constraint parameters anyways
(and there was also a typo on the mocked method name – the real method
name is getConstraintParameters, plural), and also skip the mocking of
methods (never called anyways).
Note: Q1.json and Q6.json are used by UniqueValueCheckerTest and
therefore can’t be deleted yet.
Bug: T168240
Change-Id: I0fced666d5e49d7c7cada49eab19bff972214c9b
---
M tests/phpunit/Checker/ValueCountChecker/MultiValueCheckerTest.php
D tests/phpunit/Checker/ValueCountChecker/Q2.json
D tests/phpunit/Checker/ValueCountChecker/Q3.json
D tests/phpunit/Checker/ValueCountChecker/Q4.json
D tests/phpunit/Checker/ValueCountChecker/Q5.json
M tests/phpunit/Checker/ValueCountChecker/SingleValueCheckerTest.php
6 files changed, 64 insertions(+), 326 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikibaseQualityConstraints
refs/changes/32/405032/1
diff --git a/tests/phpunit/Checker/ValueCountChecker/MultiValueCheckerTest.php
b/tests/phpunit/Checker/ValueCountChecker/MultiValueCheckerTest.php
index ea18498..ea41c31 100644
--- a/tests/phpunit/Checker/ValueCountChecker/MultiValueCheckerTest.php
+++ b/tests/phpunit/Checker/ValueCountChecker/MultiValueCheckerTest.php
@@ -20,7 +20,7 @@
*
* @group WikibaseQualityConstraints
*
- * @author BP2014N1
+ * @author Lucas Werkmeister
* @license GNU GPL v2+
*/
class MultiValueCheckerTest extends \MediaWikiTestCase {
@@ -28,97 +28,75 @@
use ResultAssertions;
/**
- * @var PropertyId
+ * @var Constraint
*/
- private $multiPropertyId;
+ private $constraint;
/**
* @var MultiValueChecker
*/
private $checker;
- /**
- * @var JsonFileEntityLookup
- */
- private $lookup;
-
protected function setUp() {
parent::setUp();
- $this->multiPropertyId = new PropertyId( 'P161' );
+ $this->constraint = $this->getMockBuilder( Constraint::class )
+ ->disableOriginalConstructor()
+ ->getMock();
$this->checker = new MultiValueChecker();
- $this->lookup = new JsonFileEntityLookup( __DIR__ );
}
- public function testMultiValueConstraintOne() {
- $entity = $this->lookup->getEntity( new ItemId( 'Q4' ) );
- $statement = new Statement( new PropertyValueSnak(
$this->multiPropertyId, new EntityIdValue( new ItemId( 'Q207' ) ) ) );
- $constraint = $this->getConstraintMock( [] );
+ public function testMultiValueConstraint_One() {
+ $statement = NewStatement::noValueFor( 'P1'
)->withSomeGuid()->build();
+ $item = NewItem::withStatement( $statement )->build();
+ $context = new MainSnakContext( $item, $statement );
- $checkResult = $this->checker->checkConstraint( new
MainSnakContext( $entity, $statement ), $constraint );
+ $checkResult = $this->checker->checkConstraint( $context,
$this->constraint );
$this->assertViolation( $checkResult,
'wbqc-violation-message-multi-value' );
}
- public function testMultiValueConstraintTwo() {
- $entity = $this->lookup->getEntity( new ItemId( 'Q5' ) );
- $statement = new Statement( new PropertyValueSnak(
$this->multiPropertyId, new EntityIdValue( new ItemId( 'Q207' ) ) ) );
- $constraint = $this->getConstraintMock( [] );
+ public function testMultiValueConstraint_Two() {
+ $statement1 = NewStatement::noValueFor( 'P1'
)->withSomeGuid()->build();
+ $statement2 = NewStatement::noValueFor( 'P1'
)->withSomeGuid()->build();
+ $item = NewItem::withStatement( $statement1 )->andStatement(
$statement2 )->build();
+ $context = new MainSnakContext( $item, $statement1 );
- $checkResult = $this->checker->checkConstraint( new
MainSnakContext( $entity, $statement ), $constraint );
+ $checkResult = $this->checker->checkConstraint( $context,
$this->constraint );
$this->assertCompliance( $checkResult );
}
- public function testMultiValueConstraintTwoButOneDeprecated() {
- $entity = $this->lookup->getEntity( new ItemId( 'Q6' ) );
- $statement = new Statement( new PropertyValueSnak(
$this->multiPropertyId, new EntityIdValue( new ItemId( 'Q409' ) ) ) );
- $constraint = $this->getConstraintMock( [] );
+ public function testMultiValueConstraint_TwoButOneDeprecated() {
+ $statement1 = NewStatement::noValueFor( 'P1'
)->withSomeGuid()->build();
+ $statement2 = NewStatement::noValueFor( 'P1' )
+ ->withDeprecatedRank()
+ ->withSomeGuid()->build();
+ $item = NewItem::withStatement( $statement1 )->andStatement(
$statement2 )->build();
+ $context = new MainSnakContext( $item, $statement1 );
- $checkResult = $this->checker->checkConstraint( new
MainSnakContext( $entity, $statement ), $constraint );
+ $checkResult = $this->checker->checkConstraint( $context,
$this->constraint );
$this->assertViolation( $checkResult,
'wbqc-violation-message-multi-value' );
}
- public function testMultiValueConstraintDeprecatedStatement() {
+ public function testSingleValueConstraintDeprecatedStatement() {
$statement = NewStatement::noValueFor( 'P1' )
- ->withDeprecatedRank()
- ->build();
- $constraint = $this->getConstraintMock( [] );
+ ->withDeprecatedRank()
+ ->withSomeGuid()->build();
$entity = NewItem::withId( 'Q1' )
- ->build();
+ ->build();
+ $context = new MainSnakContext( $entity, $statement );
- $checkResult = $this->checker->checkConstraint( new
MainSnakContext( $entity, $statement ), $constraint );
+ $checkResult = $this->checker->checkConstraint( $context,
$this->constraint );
$this->assertDeprecation( $checkResult );
}
public function testCheckConstraintParameters() {
- $constraint = $this->getConstraintMock( [] );
-
- $result = $this->checker->checkConstraintParameters(
$constraint );
+ $result = $this->checker->checkConstraintParameters(
$this->constraint );
$this->assertEmpty( $result );
- }
-
- /**
- * @param string[] $parameters
- *
- * @return Constraint
- */
- private function getConstraintMock( array $parameters ) {
- $mock = $this
- ->getMockBuilder( Constraint::class )
- ->disableOriginalConstructor()
- ->getMock();
- $mock->expects( $this->any() )
- ->method( 'getConstraintParameter' )
- ->will( $this->returnValue( $parameters ) );
- $mock->expects( $this->any() )
- ->method( 'getConstraintTypeItemId' )
- ->will( $this->returnValue( 'Multi value' ) );
-
- return $mock;
}
}
diff --git a/tests/phpunit/Checker/ValueCountChecker/Q2.json
b/tests/phpunit/Checker/ValueCountChecker/Q2.json
deleted file mode 100644
index 2555c74..0000000
--- a/tests/phpunit/Checker/ValueCountChecker/Q2.json
+++ /dev/null
@@ -1,59 +0,0 @@
-{
- "pageid": 3706,
- "ns": 120,
- "title": "Item:Q2",
- "lastrevid": 3787,
- "modified": "2015-02-20T15:37:14Z",
- "id": "Q2",
- "type": "item",
- "labels": {
- "en": {
- "language": "en",
- "value": "TestItemSingleValue"
- }
- },
- "descriptions": {
- "en": {
- "language": "en",
- "value": "Used for tests"
- }
- },
- "claims": {
- "P36": [
- {
- "id": "Q2$2c2d17d2-442e-3a3b-b3d6-cff3906c97da",
- "mainsnak": {
- "snaktype": "value",
- "property": "P36",
- "datatype": "wikibase-item",
- "datavalue": {
- "value": {
- "entity-type": "item",
- "numeric-id": 1384
- },
- "type": "wikibase-entityid"
- }
- },
- "type": "statement",
- "rank": "normal"
- },
- {
- "id": "Q2$ce7bfae6-4ded-f8f5-e475-94e906037f02",
- "mainsnak": {
- "snaktype": "value",
- "property": "P36",
- "datatype": "wikibase-item",
- "datavalue": {
- "value": {
- "entity-type": "item",
- "numeric-id": 64
- },
- "type": "wikibase-entityid"
- }
- },
- "type": "statement",
- "rank": "normal"
- }
- ]
- }
-}
\ No newline at end of file
diff --git a/tests/phpunit/Checker/ValueCountChecker/Q3.json
b/tests/phpunit/Checker/ValueCountChecker/Q3.json
deleted file mode 100644
index d704b34..0000000
--- a/tests/phpunit/Checker/ValueCountChecker/Q3.json
+++ /dev/null
@@ -1,59 +0,0 @@
-{
- "pageid": 3706,
- "ns": 120,
- "title": "Item:Q3",
- "lastrevid": 3787,
- "modified": "2015-02-20T15:37:14Z",
- "id": "Q3",
- "type": "item",
- "labels": {
- "en": {
- "language": "en",
- "value": "TestItemSingleValue"
- }
- },
- "descriptions": {
- "en": {
- "language": "en",
- "value": "Used for tests"
- }
- },
- "claims": {
- "P36": [
- {
- "id": "Q3$2c2d17d2-442e-3a3b-b3d6-cff3906c97da",
- "mainsnak": {
- "snaktype": "value",
- "property": "P36",
- "datatype": "wikibase-item",
- "datavalue": {
- "value": {
- "entity-type": "item",
- "numeric-id": 1384
- },
- "type": "wikibase-entityid"
- }
- },
- "type": "statement",
- "rank": "normal"
- },
- {
- "id": "Q3$ce7bfae6-4ded-f8f5-e475-94e906037f02",
- "mainsnak": {
- "snaktype": "value",
- "property": "P36",
- "datatype": "wikibase-item",
- "datavalue": {
- "value": {
- "entity-type": "item",
- "numeric-id": 64
- },
- "type": "wikibase-entityid"
- }
- },
- "type": "statement",
- "rank": "deprecated"
- }
- ]
- }
-}
\ No newline at end of file
diff --git a/tests/phpunit/Checker/ValueCountChecker/Q4.json
b/tests/phpunit/Checker/ValueCountChecker/Q4.json
deleted file mode 100644
index 779ce36..0000000
--- a/tests/phpunit/Checker/ValueCountChecker/Q4.json
+++ /dev/null
@@ -1,42 +0,0 @@
-{
- "pageid": 3707,
- "ns": 120,
- "title": "Item:Q4",
- "lastrevid": 3790,
- "modified": "2015-02-20T15:58:27Z",
- "id": "Q4",
- "type": "item",
- "labels": {
- "en": {
- "language": "en",
- "value": "TestItemMultiValue"
- }
- },
- "descriptions": {
- "en": {
- "language": "en",
- "value": "Used for tests"
- }
- },
- "claims": {
- "P161": [
- {
- "id": "Q4$b2624617-47d1-b842-e637-86d80294e892",
- "mainsnak": {
- "snaktype": "value",
- "property": "P161",
- "datatype": "wikibase-item",
- "datavalue": {
- "value": {
- "entity-type": "item",
- "numeric-id": 207
- },
- "type": "wikibase-entityid"
- }
- },
- "type": "statement",
- "rank": "normal"
- }
- ]
- }
-}
\ No newline at end of file
diff --git a/tests/phpunit/Checker/ValueCountChecker/Q5.json
b/tests/phpunit/Checker/ValueCountChecker/Q5.json
deleted file mode 100644
index c1f3337..0000000
--- a/tests/phpunit/Checker/ValueCountChecker/Q5.json
+++ /dev/null
@@ -1,59 +0,0 @@
-{
- "pageid": 3707,
- "ns": 120,
- "title": "Item:Q5",
- "lastrevid": 3791,
- "modified": "2015-02-20T16:03:38Z",
- "id": "Q5",
- "type": "item",
- "labels": {
- "en": {
- "language": "en",
- "value": "TestItemMultiValue"
- }
- },
- "descriptions": {
- "en": {
- "language": "en",
- "value": "Used for tests"
- }
- },
- "claims": {
- "P161": [
- {
- "id": "Q5$b2624617-47d1-b842-e637-86d80294e892",
- "mainsnak": {
- "snaktype": "value",
- "property": "P161",
- "datatype": "wikibase-item",
- "datavalue": {
- "value": {
- "entity-type": "item",
- "numeric-id": 207
- },
- "type": "wikibase-entityid"
- }
- },
- "type": "statement",
- "rank": "normal"
- },
- {
- "id": "Q5$dc567e08-44d6-3b38-b380-8c4d934e56a0",
- "mainsnak": {
- "snaktype": "value",
- "property": "P161",
- "datatype": "wikibase-item",
- "datavalue": {
- "value": {
- "entity-type": "item",
- "numeric-id": 449
- },
- "type": "wikibase-entityid"
- }
- },
- "type": "statement",
- "rank": "normal"
- }
- ]
- }
-}
\ No newline at end of file
diff --git a/tests/phpunit/Checker/ValueCountChecker/SingleValueCheckerTest.php
b/tests/phpunit/Checker/ValueCountChecker/SingleValueCheckerTest.php
index 83744b2..3c894e8 100644
--- a/tests/phpunit/Checker/ValueCountChecker/SingleValueCheckerTest.php
+++ b/tests/phpunit/Checker/ValueCountChecker/SingleValueCheckerTest.php
@@ -12,6 +12,7 @@
use WikibaseQuality\ConstraintReport\Constraint;
use
WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\SingleValueChecker;
use WikibaseQuality\ConstraintReport\ConstraintCheck\Context\MainSnakContext;
+use WikibaseQuality\ConstraintReport\ConstraintRepository;
use WikibaseQuality\ConstraintReport\Tests\ResultAssertions;
use WikibaseQuality\Tests\Helper\JsonFileEntityLookup;
@@ -20,7 +21,7 @@
*
* @group WikibaseQualityConstraints
*
- * @author BP2014N1
+ * @author Lucas Werkmeister
* @license GNU GPL v2+
*/
class SingleValueCheckerTest extends \MediaWikiTestCase {
@@ -28,97 +29,75 @@
use ResultAssertions;
/**
- * @var PropertyId
+ * @var Constraint
*/
- private $singlePropertyId;
+ private $constraint;
/**
* @var SingleValueChecker
*/
private $checker;
- /**
- * @var JsonFileEntityLookup
- */
- private $lookup;
-
protected function setUp() {
parent::setUp();
- $this->singlePropertyId = new PropertyId( 'P36' );
+ $this->constraint = $this->getMockBuilder( Constraint::class )
+ ->disableOriginalConstructor()
+ ->getMock();
$this->checker = new SingleValueChecker();
- $this->lookup = new JsonFileEntityLookup( __DIR__ );
}
- public function testSingleValueConstraintOne() {
- $entity = $this->lookup->getEntity( new ItemId( 'Q1' ) );
- $statement = new Statement( new PropertyValueSnak(
$this->singlePropertyId, new EntityIdValue( new ItemId( 'Q1384' ) ) ) );
- $constraint = $this->getConstraintMock( [] );
+ public function testSingleValueConstraint_One() {
+ $statement = NewStatement::noValueFor( 'P1'
)->withSomeGuid()->build();
+ $item = NewItem::withStatement( $statement )->build();
+ $context = new MainSnakContext( $item, $statement );
- $checkResult = $this->checker->checkConstraint( new
MainSnakContext( $entity, $statement ), $constraint );
+ $checkResult = $this->checker->checkConstraint( $context,
$this->constraint );
$this->assertCompliance( $checkResult );
}
- public function testSingleValueConstraintTwo() {
- $entity = $this->lookup->getEntity( new ItemId( 'Q2' ) );
- $statement = new Statement( new PropertyValueSnak(
$this->singlePropertyId, new EntityIdValue( new ItemId( 'Q1384' ) ) ) );
- $constraint = $this->getConstraintMock( [] );
+ public function testSingleValueConstraint_Two() {
+ $statement1 = NewStatement::noValueFor( 'P1'
)->withSomeGuid()->build();
+ $statement2 = NewStatement::noValueFor( 'P1'
)->withSomeGuid()->build();
+ $item = NewItem::withStatement( $statement1 )->andStatement(
$statement2 )->build();
+ $context = new MainSnakContext( $item, $statement1 );
- $checkResult = $this->checker->checkConstraint( new
MainSnakContext( $entity, $statement ), $constraint );
+ $checkResult = $this->checker->checkConstraint( $context,
$this->constraint );
$this->assertViolation( $checkResult,
'wbqc-violation-message-single-value' );
}
- public function testSingleValueConstraintTwoButOneDeprecated() {
- $entity = $this->lookup->getEntity( new ItemId( 'Q3' ) );
- $statement = new Statement( new PropertyValueSnak(
$this->singlePropertyId, new EntityIdValue( new ItemId( 'Q1384' ) ) ) );
- $constraint = $this->getConstraintMock( [] );
+ public function testSingleValueConstraint_TwoButOneDeprecated() {
+ $statement1 = NewStatement::noValueFor( 'P1'
)->withSomeGuid()->build();
+ $statement2 = NewStatement::noValueFor( 'P1' )
+ ->withDeprecatedRank()
+ ->withSomeGuid()->build();
+ $item = NewItem::withStatement( $statement1 )->andStatement(
$statement2 )->build();
+ $context = new MainSnakContext( $item, $statement1 );
- $checkResult = $this->checker->checkConstraint( new
MainSnakContext( $entity, $statement ), $constraint );
+ $checkResult = $this->checker->checkConstraint( $context,
$this->constraint );
$this->assertCompliance( $checkResult );
}
public function testSingleValueConstraintDeprecatedStatement() {
$statement = NewStatement::noValueFor( 'P1' )
- ->withDeprecatedRank()
- ->build();
- $constraint = $this->getConstraintMock( [] );
+ ->withDeprecatedRank()
+ ->withSomeGuid()->build();
$entity = NewItem::withId( 'Q1' )
- ->build();
+ ->build();
+ $context = new MainSnakContext( $entity, $statement );
- $checkResult = $this->checker->checkConstraint( new
MainSnakContext( $entity, $statement ), $constraint );
+ $checkResult = $this->checker->checkConstraint( $context,
$this->constraint );
$this->assertDeprecation( $checkResult );
}
public function testCheckConstraintParameters() {
- $constraint = $this->getConstraintMock( [] );
-
- $result = $this->checker->checkConstraintParameters(
$constraint );
+ $result = $this->checker->checkConstraintParameters(
$this->constraint );
$this->assertEmpty( $result );
- }
-
- /**
- * @param string[] $parameters
- *
- * @return Constraint
- */
- private function getConstraintMock( array $parameters ) {
- $mock = $this
- ->getMockBuilder( Constraint::class )
- ->disableOriginalConstructor()
- ->getMock();
- $mock->expects( $this->any() )
- ->method( 'getConstraintParameter' )
- ->will( $this->returnValue( $parameters ) );
- $mock->expects( $this->any() )
- ->method( 'getConstraintTypeItemId' )
- ->will( $this->returnValue( 'Single value' ) );
-
- return $mock;
}
}
--
To view, visit https://gerrit.wikimedia.org/r/405032
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0fced666d5e49d7c7cada49eab19bff972214c9b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits