Awight has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/403450 )
Change subject: Move some sharable methods into a test helper
......................................................................
Move some sharable methods into a test helper
Change-Id: I98b57fdf743cd31048de436a42d8f37c7c1c8679
---
M extension.json
M tests/phpunit/includes/Api/ApiIntegrationTest.php
A tests/phpunit/includes/TestHelper.php
3 files changed, 83 insertions(+), 61 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ORES
refs/changes/50/403450/1
diff --git a/extension.json b/extension.json
index 9745ffb..d7f8e5d 100644
--- a/extension.json
+++ b/extension.json
@@ -12,7 +12,8 @@
"MediaWiki": ">= 1.31.0"
},
"AutoloadNamespaces": {
- "ORES\\": "includes/"
+ "ORES\\": "includes/",
+ "ORES\\Tests\\": "tests/phpunit/includes/"
},
"APIMetaModules": {
"ores": "ORES\\ApiQueryORES"
diff --git a/tests/phpunit/includes/Api/ApiIntegrationTest.php
b/tests/phpunit/includes/Api/ApiIntegrationTest.php
index 14e605d..a1d1b65 100644
--- a/tests/phpunit/includes/Api/ApiIntegrationTest.php
+++ b/tests/phpunit/includes/Api/ApiIntegrationTest.php
@@ -2,16 +2,10 @@
namespace ORES\Tests\Api;
-use ContentHandler;
-use MediaWiki\Linker\LinkTarget;
-use MediaWiki\MediaWikiServices;
use ORES\Storage\HashModelLookup;
-use ORES\Storage\ModelLookup;
-use Revision;
-use Title;
use TitleValue;
-use User;
-use WikiPage;
+
+use ORES\Tests\TestHelper;
/**
* @group API
@@ -26,10 +20,7 @@
public function __construct( $name = null, array $data = [], $dataName
= '' ) {
parent::__construct( $name, $data, $dataName );
- $this->tablesUsed[] = 'recentchanges';
- $this->tablesUsed[] = 'page';
- $this->tablesUsed[] = 'ores_model';
- $this->tablesUsed[] = 'ores_classification';
+ $this->tablesUsed = TestHelper::getTablesUsed();
}
protected function setUp() {
@@ -37,9 +28,8 @@
self::$users['ORESApiIntegrationTestUser'] =
$this->getMutableTestUser();
$this->doLogin( 'ORESApiIntegrationTestUser' );
- wfGetDB( DB_MASTER )->delete( 'recentchanges', '*', __METHOD__
);
- wfGetDB( DB_MASTER )->delete( 'ores_model', '*', __METHOD__ );
- wfGetDB( DB_MASTER )->delete( 'ores_classification', '*',
__METHOD__ );
+
+ TestHelper::clearOresTables();
$this->setMwGlobals(
[
@@ -112,8 +102,8 @@
public function testListRecentChanges_getOresScores() {
$target = new TitleValue( 0, 'ORESApiIntegrationTestPage' );
- $status = $this->doPageEdit( $this->getLoggedInTestUser(),
$target, 'Create the page' );
- $this->insertOresData(
+ $status = TestHelper::doPageEdit( $this->getLoggedInTestUser(),
$target, 'Create the page' );
+ TestHelper::insertOresData(
$status->getValue()['revision'],
[ 'damaging' => 0.4, 'goodfaith' => 0.7 ]
);
@@ -131,43 +121,8 @@
$this->assertEquals(
$result[0]['query']['recentchanges'][0]['oresscores'], $expected );
}
- private function doPageEdit( User $user, LinkTarget $target, $summary )
{
- static $i = 0;
-
- $title = Title::newFromLinkTarget( $target );
- $page = WikiPage::factory( $title );
- $status = $page->doEditContent(
- ContentHandler::makeContent( __CLASS__ . $i++, $title ),
- $summary,
- 0,
- false,
- $user
- );
- if ( !$status->isOK() ) {
- $this->fail();
- }
- return $status;
- }
-
private function getLoggedInTestUser() {
return self::$users['ORESApiIntegrationTestUser']->getUser();
- }
-
- private function insertOresData( Revision $revision, $scores ) {
- /** @var ModelLookup $modelLookup */
- $modelLookup = MediaWikiServices::getInstance()->getService(
'ORESModelLookup' );
- // TODO: Use ScoreStorage
- $dbData = [];
- foreach ( $scores as $modelName => $score ) {
- $dbData[] = [
- 'oresc_rev' => $revision->getId(),
- 'oresc_model' => $modelLookup->getModelId(
$modelName ),
- 'oresc_class' => 1,
- 'oresc_probability' => $score,
- 'oresc_is_predicted' => 0
- ];
- }
- wfGetDB( DB_MASTER )->insert( 'ores_classification', $dbData );
}
private function doListRecentChangesRequest( array $params = [] ) {
@@ -183,8 +138,8 @@
public function testListRecentChanges_showOresReview() {
$target = new TitleValue( 0, 'ORESApiIntegrationTestPage' );
- $status = $this->doPageEdit( $this->getLoggedInTestUser(),
$target, 'Create the page' );
- $this->insertOresData(
+ $status = TestHelper::doPageEdit( $this->getLoggedInTestUser(),
$target, 'Create the page' );
+ TestHelper::insertOresData(
$status->getValue()['revision'],
[ 'damaging' => 0.6, 'goodfaith' => 0.3 ]
);
@@ -204,8 +159,8 @@
public function testListRecentChanges_showOresReviewNotNeedingReview() {
$target = new TitleValue( 0, 'ORESApiIntegrationTestPage' );
- $status = $this->doPageEdit( $this->getLoggedInTestUser(),
$target, 'Create the page' );
- $this->insertOresData(
+ $status = TestHelper::doPageEdit( $this->getLoggedInTestUser(),
$target, 'Create the page' );
+ TestHelper::insertOresData(
$status->getValue()['revision'],
[ 'damaging' => 0.4, 'goodfaith' => 0.7 ]
);
@@ -219,8 +174,8 @@
public function testListRecentChanges_showNotOresReview() {
$target = new TitleValue( 0, 'ORESApiIntegrationTestPage' );
- $status = $this->doPageEdit( $this->getLoggedInTestUser(),
$target, 'Create the page' );
- $this->insertOresData(
+ $status = TestHelper::doPageEdit( $this->getLoggedInTestUser(),
$target, 'Create the page' );
+ TestHelper::insertOresData(
$status->getValue()['revision'],
[ 'damaging' => 0.6, 'goodfaith' => 0.3 ]
);
@@ -234,8 +189,8 @@
public function
testListRecentChanges_showNotOresReviewNotNeedingReview() {
$target = new TitleValue( 0, 'ORESApiIntegrationTestPage' );
- $status = $this->doPageEdit( $this->getLoggedInTestUser(),
$target, 'Create the page' );
- $this->insertOresData(
+ $status = TestHelper::doPageEdit( $this->getLoggedInTestUser(),
$target, 'Create the page' );
+ TestHelper::insertOresData(
$status->getValue()['revision'],
[ 'damaging' => 0.4, 'goodfaith' => 0.7 ]
);
diff --git a/tests/phpunit/includes/TestHelper.php
b/tests/phpunit/includes/TestHelper.php
new file mode 100644
index 0000000..53321b6
--- /dev/null
+++ b/tests/phpunit/includes/TestHelper.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace ORES\Tests;
+
+use ContentHandler;
+use MediaWiki\Linker\LinkTarget;
+use MediaWiki\MediaWikiServices;
+use Revision;
+use Title;
+use User;
+use WikiPage;
+
+class TestHelper {
+
+ public static function getTablesUsed() {
+ $tablesUsed = [
+ 'recentchanges',
+ 'page',
+ 'ores_model',
+ 'ores_classification',
+ ];
+ return $tablesUsed;
+ }
+
+ public static function clearOresTables() {
+ wfGetDB( DB_MASTER )->delete( 'recentchanges', '*', __METHOD__
);
+ wfGetDB( DB_MASTER )->delete( 'ores_model', '*', __METHOD__ );
+ wfGetDB( DB_MASTER )->delete( 'ores_classification', '*',
__METHOD__ );
+ }
+
+ public static function insertOresData( Revision $revision, $scores ) {
+ /** @var ModelLookup $modelLookup */
+ $modelLookup = MediaWikiServices::getInstance()->getService(
'ORESModelLookup' );
+ // TODO: Use ScoreStorage
+ $dbData = [];
+ foreach ( $scores as $modelName => $score ) {
+ $dbData[] = [
+ 'oresc_rev' => $revision->getId(),
+ 'oresc_model' => $modelLookup->getModelId(
$modelName ),
+ 'oresc_class' => 1,
+ 'oresc_probability' => $score,
+ 'oresc_is_predicted' => 0
+ ];
+ }
+ wfGetDB( DB_MASTER )->insert( 'ores_classification', $dbData );
+ }
+
+ public static function doPageEdit( User $user, LinkTarget $target,
$summary ) {
+ static $i = 0;
+
+ $title = Title::newFromLinkTarget( $target );
+ $page = WikiPage::factory( $title );
+ $status = $page->doEditContent(
+ ContentHandler::makeContent( __CLASS__ . $i++, $title ),
+ $summary,
+ 0,
+ false,
+ $user
+ );
+ if ( !$status->isOK() ) {
+ throw new RuntimeException( 'Test failed, couldn\'t
perform page edit.' );
+ }
+ return $status;
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/403450
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I98b57fdf743cd31048de436a42d8f37c7c1c8679
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ORES
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits