jenkins-bot has submitted this change and it was merged.
Change subject: Add error handling to EntitiesByPropertyValue
......................................................................
Add error handling to EntitiesByPropertyValue
Change-Id: I14da12256386e25879954daa0f97b9a4364c7362
---
M Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
M phpunit.xml.dist
M src/Wikibase/Query/Api/EntitiesByPropertyValue.php
3 files changed, 118 insertions(+), 33 deletions(-)
Approvals:
Addshore: Looks good to me, approved
jenkins-bot: Verified
diff --git a/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
b/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
index ff25b78..fb98c80 100644
--- a/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
+++ b/Tests/System/Wikibase/Query/EntitiesByPropertyValueApiTest.php
@@ -23,6 +23,12 @@
*/
class EntitiesByPropertyValueApiTest extends \ApiTestCase {
+ const MODULE_NAME = 'entitiesByPropertyValue';
+ const PROPERTY_ID = 31337;
+ const ITEM_ID = 42;
+ const PROPERTY_ID_STRING = 'p31337';
+ const ITEM_ID_STRING = 'q42';
+
protected function getQueryStore() {
return ExtensionAccess::getWikibaseQuery()->getQueryStore();
}
@@ -38,6 +44,9 @@
public function setUp() {
parent::setUp();
$this->reinitializeStore();
+
+ $this->createNewProperty();
+ $this->insertNewItem();
}
public function tearDown() {
@@ -45,33 +54,9 @@
parent::tearDown();
}
- public function testMakeApiRequest() {
- $this->createNewProperty();
-
- $storeUpdater = $this->getQueryStore()->getUpdater();
-
- $item = $this->newMockItem();
-
- $storeUpdater->insertEntity( $item );
-
- $params = array(
- 'action' => 'entitiesByPropertyValue',
- 'property' => 'p31337',
- 'value' => json_encode(
$this->newMockValue()->toArray() ),
- );
-
- list( $resultArray, ) = $this->doApiRequest( $params );
-
- $this->assertArrayHasKey( 'entities', $resultArray );
-
- $entities = $resultArray['entities'];
-
- $this->assertEquals( array( 'q42' ), $entities );
- }
-
protected function createNewProperty() {
$property = Property::newEmpty();
- $property->setId( new EntityId( 'property', 31337 ) );
+ $property->setId( new EntityId( 'property', self::PROPERTY_ID )
);
$property->setDataTypeId( 'string' );
$propertyContent = PropertyContent::newFromProperty( $property
);
@@ -79,14 +64,22 @@
$propertyContent->save();
}
+ protected function insertNewItem() {
+ $storeUpdater = $this->getQueryStore()->getUpdater();
+
+ $item = $this->newMockItem();
+
+ $storeUpdater->insertEntity( $item );
+ }
+
protected function newMockItem() {
$item = Item::newEmpty();
- $item->setId( new EntityId( 'item', 42 ) );
+ $item->setId( new EntityId( 'item', self::ITEM_ID ) );
$item->addClaim( new Statement(
new PropertyValueSnak(
- new EntityId( 'property', 31337 ),
+ new EntityId( 'property', self::PROPERTY_ID ),
$this->newMockValue()
)
) );
@@ -98,4 +91,58 @@
return new StringValue( 'API tests really suck' );
}
+ protected function newMockValueString() {
+ return '{"value":"API tests really suck","type":"string"}';
+ }
+
+ public function testMakeApiRequest() {
+ $resultArray = $this->getResultForRequestWithValue(
$this->newMockValueString() );
+
+ $this->assertArrayHasKey( 'entities', $resultArray );
+
+ $entities = $resultArray['entities'];
+
+ $this->assertEquals( array( self::ITEM_ID_STRING ), $entities );
+ }
+
+ protected function getResultForRequestWithValue( $value ) {
+ $params = array(
+ 'action' => self::MODULE_NAME,
+ 'property' => self::PROPERTY_ID_STRING,
+ 'value' => $value,
+ );
+
+ return $this->getResultForRequest( $params );
+ }
+
+ protected function getResultForRequest( array $requestParams ) {
+ list( $resultArray, ) = $this->doApiRequest( $requestParams );
+
+ return $resultArray;
+ }
+
+ public function testMakeRequestWithInvalidValue() {
+ $this->setExpectedException(
+ 'UsageException',
+ 'The provided value needs to be a serialization of a
DataValue'
+ );
+
+ $this->getResultForRequestWithValue(
+ 'Im an invalid value in your API request!'
+ );
+ }
+
+ public function testMakeRequestWithUnknownProperty() {
+ $this->setExpectedException(
+ 'UsageException',
+ 'The specified property does not exist'
+ );
+
+ $this->getResultForRequest( array(
+ 'action' => self::MODULE_NAME,
+ 'property' => 'p45831890',
+ 'value' => $this->newMockValueString(),
+ ) );
+ }
+
}
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index 68df2e4..bede6f1 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -16,6 +16,7 @@
<testsuite name="WikibaseQuery">
<directory>Tests/Phpunit</directory>
<directory>Tests/Integration</directory>
+ <directory>Tests/System</directory>
</testsuite>
</testsuites>
</phpunit>
diff --git a/src/Wikibase/Query/Api/EntitiesByPropertyValue.php
b/src/Wikibase/Query/Api/EntitiesByPropertyValue.php
index 7b0ab1a..3909fee 100644
--- a/src/Wikibase/Query/Api/EntitiesByPropertyValue.php
+++ b/src/Wikibase/Query/Api/EntitiesByPropertyValue.php
@@ -3,6 +3,8 @@
namespace Wikibase\Query\Api;
use ApiBase;
+use InvalidArgumentException;
+use Wikibase\Lib\PropertyNotFoundException;
use Wikibase\Query\DIC\ExtensionAccess;
/**
@@ -16,6 +18,9 @@
*/
class EntitiesByPropertyValue extends \ApiBase {
+ const ERR_NO_SUCH_PROPERTY = 'The specified property does not exist';
+ const ERR_INVALID_JSON = 'The provided value needs to be a
serialization of a DataValue';
+
/**
* @see ApiBase::execute
*
@@ -24,13 +29,25 @@
public function execute() {
$entityFinder =
ExtensionAccess::getWikibaseQuery()->getByPropertyValueEntityFinder();
- // TODO: handle exceptions
- $entityIds = $entityFinder->findEntities(
$this->extractRequestParams() );
+ try {
+ $entityIds = $entityFinder->findEntities(
$this->extractRequestParams() );
+ }
+ catch ( InvalidArgumentException $ex ) {
+ $this->dieUsage(
+ self::ERR_INVALID_JSON,
+ 'invalid-json'
+ );
+ }
+ catch ( PropertyNotFoundException $ex ) {
+ $this->dieUsage(
+ self::ERR_NO_SUCH_PROPERTY,
+ 'no-such-property'
+ );
+ }
- $this->getResult()->addValue( null, 'entities', $entityIds );
-
- // TODO: add to API output
- // TODO: system test
+ if ( isset( $entityIds ) ) {
+ $this->getResult()->addValue( null, 'entities',
$entityIds );
+ }
}
/**
@@ -117,6 +134,26 @@
}
/**
+ * @see ApiBase::getPossibleErrors
+ *
+ * @since 0.1
+ *
+ * @return array
+ */
+ public function getPossibleErrors() {
+ return array(
+ array(
+ 'code' => 'invalid-json',
+ 'info' => self::ERR_INVALID_JSON,
+ ),
+ array(
+ 'code' => 'no-such-property',
+ 'info' => self::ERR_NO_SUCH_PROPERTY,
+ )
+ );
+ }
+
+ /**
* @see ApiBase::getHelpUrls
*
* @since 0.1
--
To view, visit https://gerrit.wikimedia.org/r/75108
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I14da12256386e25879954daa0f97b9a4364c7362
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/WikibaseQuery
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits