jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/369909 )
Change subject: Improve rangeParameter helper and use it in RangeCheckerTest
......................................................................
Improve rangeParameter helper and use it in RangeCheckerTest
All the remaining RangeCheckerTest methods that used template-style
constraint parameters are changed to use the rangeParameter helper of
the ConstraintParameter test trait. To simplify the test code, that
helper is improved to support specifying time values without having to
instantiate a TimeValue. (The code to do this is partly taken from
ConstraintParameterParser.)
Bug: T172378
Change-Id: Ie3a32b648f3a5d0fa2d2a741323242258a9398a5
---
M tests/phpunit/Checker/RangeChecker/RangeCheckerTest.php
M tests/phpunit/ConstraintParameters.php
2 files changed, 51 insertions(+), 70 deletions(-)
Approvals:
WMDE-leszek: Looks good to me, approved
jenkins-bot: Verified
diff --git a/tests/phpunit/Checker/RangeChecker/RangeCheckerTest.php
b/tests/phpunit/Checker/RangeChecker/RangeCheckerTest.php
index 737f607..202d47c 100644
--- a/tests/phpunit/Checker/RangeChecker/RangeCheckerTest.php
+++ b/tests/phpunit/Checker/RangeChecker/RangeCheckerTest.php
@@ -67,10 +67,7 @@
public function testRangeConstraintWithinRange() {
$value = new DecimalValue( 3.1415926536 );
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), new QuantityValue( $value, '1', $value, $value ) ) );
- $constraintParameters = [
- 'minimum_quantity' => 0,
- 'maximum_quantity' => 10
- ];
+ $constraintParameters = $this->rangeParameter( 'quantity', 0,
10 );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertCompliance( $checkResult );
}
@@ -78,10 +75,7 @@
public function testRangeConstraintTooSmall() {
$value = new DecimalValue( 42 );
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), new QuantityValue( $value, '1', $value, $value ) ) );
- $constraintParameters = [
- 'minimum_quantity' => 100,
- 'maximum_quantity' => 1000
- ];
+ $constraintParameters = $this->rangeParameter( 'quantity', 100,
1000 );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertViolation( $checkResult,
'wbqc-violation-message-range-quantity-closed' );
}
@@ -89,82 +83,49 @@
public function testRangeConstraintTooBig() {
$value = new DecimalValue( 3.141592 );
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), new QuantityValue( $value, '1', $value, $value ) ) );
- $constraintParameters = [
- 'minimum_quantity' => 0,
- 'maximum_quantity' => 1
- ];
+ $constraintParameters = $this->rangeParameter( 'quantity', 0, 1
);
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertViolation( $checkResult,
'wbqc-violation-message-range-quantity-closed' );
}
public function testRangeConstraintTimeWithinRange() {
- $min = '+00000001960-01-01T00:00:00Z';
- $max = '+00000001980-01-01T00:00:00Z';
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), $this->timeValue ) );
- $constraintParameters = [
- 'minimum_quantity' => $min,
- 'maximum_quantity' => $max
- ];
+ $constraintParameters = $this->rangeParameter( 'date', '1960',
'1980' );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertCompliance( $checkResult );
}
public function testRangeConstraintTimeWithinRangeToNow() {
- $min = '+00000001960-01-01T00:00:00Z';
- $max = 'now';
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), $this->timeValue ) );
- $constraintParameters = [
- 'minimum_quantity' => $min,
- 'maximum_quantity' => $max
- ];
+ $constraintParameters = $this->rangeParameter( 'date', '1960',
'now' );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertCompliance( $checkResult );
}
public function testRangeConstraintTimeWithinYearRange() {
- $min = '1960';
- $max = '1980';
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), $this->timeValue ) );
- $constraintParameters = [
- 'minimum_quantity' => $min,
- 'maximum_quantity' => $max
- ];
+ $constraintParameters = $this->rangeParameter( 'date', '1960',
'1980' );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertCompliance( $checkResult );
}
public function testRangeConstraintTimeWithinYearMonthDayRange() {
- $min = '1969-12-31';
- $max = '1970-01-02';
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), $this->timeValue ) );
- $constraintParameters = [
- 'minimum_quantity' => $min,
- 'maximum_quantity' => $max
- ];
+ $constraintParameters = $this->rangeParameter( 'date',
'1969-12-31', '1970-01-02' );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertCompliance( $checkResult );
}
public function testRangeConstraintTimeTooEarly() {
- $min = '+00000001975-01-01T00:00:00Z';
- $max = '+00000001980-01-01T00:00:00Z';
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), $this->timeValue ) );
- $constraintParameters = [
- 'minimum_quantity' => $min,
- 'maximum_quantity' => $max
- ];
+ $constraintParameters = $this->rangeParameter( 'date', '1975',
'1980' );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertViolation( $checkResult,
'wbqc-violation-message-range-time-closed' );
}
public function testRangeConstraintTimeTooLate() {
- $min = '+00000001960-01-01T00:00:00Z';
- $max = '+00000001965-01-01T00:00:00Z';
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), $this->timeValue ) );
- $constraintParameters = [
- 'minimum_quantity' => $min,
- 'maximum_quantity' => $max
- ];
+ $constraintParameters = $this->rangeParameter( 'date', '1960',
'1965' );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
$this->assertViolation( $checkResult,
'wbqc-violation-message-range-time-closed' );
}
@@ -178,7 +139,7 @@
public function testRangeConstraintLeftOpenWithinRange() {
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), UnboundedQuantityValue::newFromNumber( -10 ) ) );
- $constraintParameters = $this->rangeParameter( 'quantity',
null, UnboundedQuantityValue::newFromNumber( 0 ) );
+ $constraintParameters = $this->rangeParameter( 'quantity',
null, 0 );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
@@ -187,7 +148,7 @@
public function testRangeConstraintLeftOpenTooSmall() {
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), UnboundedQuantityValue::newFromNumber( 10 ) ) );
- $constraintParameters = $this->rangeParameter( 'quantity',
null, UnboundedQuantityValue::newFromNumber( 0 ) );
+ $constraintParameters = $this->rangeParameter( 'quantity',
null, 0 );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
@@ -196,7 +157,7 @@
public function testRangeConstraintRightOpenWithinRange() {
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), UnboundedQuantityValue::newFromNumber( 10 ) ) );
- $constraintParameters = $this->rangeParameter( 'quantity',
UnboundedQuantityValue::newFromNumber( 0 ), null );
+ $constraintParameters = $this->rangeParameter( 'quantity', 0,
null );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
@@ -205,7 +166,7 @@
public function testRangeConstraintRightOpenTooSmall() {
$statement = new Statement( new PropertyValueSnak( new
PropertyId( 'P1457' ), UnboundedQuantityValue::newFromNumber( -10 ) ) );
- $constraintParameters = $this->rangeParameter( 'quantity',
UnboundedQuantityValue::newFromNumber( 0 ), null );
+ $constraintParameters = $this->rangeParameter( 'quantity', 0,
null );
$checkResult = $this->checker->checkConstraint( $statement,
$this->getConstraintMock( $constraintParameters ), $this->getEntity() );
diff --git a/tests/phpunit/ConstraintParameters.php
b/tests/phpunit/ConstraintParameters.php
index c527d10..baf220c 100644
--- a/tests/phpunit/ConstraintParameters.php
+++ b/tests/phpunit/ConstraintParameters.php
@@ -11,9 +11,12 @@
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\Services\EntityId\PlainEntityIdFormatter;
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
+use Wikibase\DataModel\Snak\PropertySomeValueSnak;
use Wikibase\DataModel\Snak\PropertyValueSnak;
+use Wikibase\DataModel\Snak\Snak;
use
WikibaseQuality\ConstraintReport\ConstraintCheck\Helper\ConstraintParameterParser;
use WikibaseQuality\ConstraintReport\ConstraintParameterRenderer;
+use Wikibase\Repo\Parsers\TimeParserFactory;
use Wikibase\Repo\WikibaseRepo;
/**
@@ -114,9 +117,40 @@
}
/**
+ * Convert an abbreviated value for a range endpoint
+ * to a full snak for range constraint parameters.
+ * A numeric argument means a numeric endpoint,
+ * 'now' corresponds to a somevalue snak,
+ * any other string is parsed as a time value,
+ * a DataValue is used directly,
+ * and null corresponds to a novalue snak (open-ended range).
+ *
+ * @param DataValue|int|float|string|null $value
+ * @param string $property property ID serialization
+ * @return Snak
+ */
+ private function rangeEndpoint( $value, $property ) {
+ $propertyId = new PropertyId( $property );
+ if ( $value === null ) {
+ return new PropertyNoValueSnak( $propertyId );
+ } else {
+ if ( is_string( $value ) ) {
+ if ( $value === 'now' ) {
+ return new PropertySomeValueSnak(
$propertyId );
+ }
+ $timeParser = ( new TimeParserFactory()
)->getTimeParser();
+ $value = $timeParser->parse( $value );
+ } elseif ( is_numeric( $value ) ) {
+ $value = UnboundedQuantityValue::newFromNumber(
$value );
+ }
+ return new PropertyValueSnak( $propertyId, $value );
+ }
+ }
+
+ /**
* @param string $type 'quantity' or 'time'
- * @param DataValue|int|float|null $min lower boundary, or null to
signify no lower boundary
- * @param DataValue|int|float|null $max upper boundary, or null to
signfiy no upper boundary
+ * @param DataValue|int|float|string|null $min lower boundary, see
rangeEndpoint() for details
+ * @param DataValue|int|float|string|null $max upper boundary, see
rangeEndpoint() for details
* @return array
*/
public function rangeParameter( $type, $min, $max ) {
@@ -124,22 +158,8 @@
$config = $this->getDefaultConfig();
$minimumId = $config->get( 'WBQualityConstraintsMinimum' .
$configKey . 'Id' );
$maximumId = $config->get( 'WBQualityConstraintsMaximum' .
$configKey . 'Id' );
- if ( $min === null ) {
- $minimumSnak = new PropertyNoValueSnak( new PropertyId(
$minimumId ) );
- } else {
- if ( is_numeric( $min ) ) {
- $min = UnboundedQuantityValue::newFromNumber(
$min );
- }
- $minimumSnak = new PropertyValueSnak( new PropertyId(
$minimumId ), $min );
- }
- if ( $max === null ) {
- $maximumSnak = new PropertyNoValueSnak( new PropertyId(
$maximumId ) );
- } else {
- if ( is_numeric( $max ) ) {
- $max = UnboundedQuantityValue::newFromNumber(
$max );
- }
- $maximumSnak = new PropertyValueSnak( new PropertyId(
$maximumId ), $max );
- }
+ $minimumSnak = $this->rangeEndpoint( $min, $minimumId );
+ $maximumSnak = $this->rangeEndpoint( $max, $maximumId );
$snakSerializer = $this->getSnakSerializer();
return [
$minimumId => [ $snakSerializer->serialize(
$minimumSnak ) ],
--
To view, visit https://gerrit.wikimedia.org/r/369909
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ie3a32b648f3a5d0fa2d2a741323242258a9398a5
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/WikibaseQualityConstraints
Gerrit-Branch: master
Gerrit-Owner: Lucas Werkmeister (WMDE) <[email protected]>
Gerrit-Reviewer: Jonas Kress (WMDE) <[email protected]>
Gerrit-Reviewer: WMDE-leszek <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits