Awight has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/403839 )

Change subject: Add a maintenance script test
......................................................................

Add a maintenance script test

TODO: See T184775, we could get rid of the --quiet flag, captureu and verify
output.

Bug: T184140
Change-Id: I1deeb809e27c2daddf9f221fb0ea2cbd36aa166b
---
M maintenance/PurgeScoreCache.php
A tests/phpunit/maintenance/PurgeScoreCacheTest.php
2 files changed, 198 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ORES 
refs/changes/39/403839/1

diff --git a/maintenance/PurgeScoreCache.php b/maintenance/PurgeScoreCache.php
index b7f2480..357fd7a 100644
--- a/maintenance/PurgeScoreCache.php
+++ b/maintenance/PurgeScoreCache.php
@@ -43,10 +43,10 @@
                                $description = 'old rows';
                        } elseif ( $this->hasOption( 'all' ) ) {
                                $deletedRows = $this->purge( $model, true, 
$this->mBatchSize );
-                               $description = 'old model versions';
+                               $description = 'scores from all model versions';
                        } else {
                                $deletedRows = $this->purge( $model, false, 
$this->mBatchSize );
-                               $description = 'all rows';
+                               $description = 'scores from old model versions';
                        }
                        if ( $deletedRows ) {
                                $this->output( "   ...purging $description from 
'$model' model': deleted $deletedRows rows\n" );
@@ -120,7 +120,7 @@
                array $tables,
                array $conditions,
                array $join_conds,
-               $batchSize = 1000
+               $batchSize
        ) {
                $dbr = \wfGetDB( DB_REPLICA );
                $dbw = \wfGetDB( DB_MASTER );
diff --git a/tests/phpunit/maintenance/PurgeScoreCacheTest.php 
b/tests/phpunit/maintenance/PurgeScoreCacheTest.php
new file mode 100644
index 0000000..9209716
--- /dev/null
+++ b/tests/phpunit/maintenance/PurgeScoreCacheTest.php
@@ -0,0 +1,195 @@
+<?php
+
+namespace ORES\Tests\Maintenance;
+
+use MediaWiki\MediaWikiServices;
+
+use ORES\Maintenance\PurgeScoreCache;
+
+use ORES\Tests\TestHelper;
+
+/*
+ * TODO: It would be ideal to extend a core class like \MaintenanceTest to take
+ * care of capturing output and so on, but this doesn't seem to exist yet?
+ * See https://phabricator.wikimedia.org/T184775
+ *
+ * This ficitious test class would have to be autoloadable, otherwise:
+ *
+ * require_once getenv( 'MW_INSTALL_PATH' ) !== false
+ *     ? getenv( 'MW_INSTALL_PATH' ) . 
'/tests/phpunit/maintenance/MaintenanceTest.php'
+ *     : __DIR__ . 
'/../../../../../tests/phpunit/maintenance/MaintenanceTest.php';
+ */
+
+/**
+ * @group ORES
+ * @group Database
+ * @covers ORES\Maintenance\PurgeScoreCache
+ */
+class PurgeScoreCacheTest extends \MediaWikiTestCase {
+
+       public function setUp() {
+               parent::setUp();
+               $this->tablesUsed = [
+                       'ores_classification',
+                       'ores_model',
+               ];
+
+               $this->maintenance = new PurgeScoreCache();
+
+               TestHelper::clearOresTables();
+
+               // Reset service to purge cached models.
+               MediaWikiServices::getInstance()->resetServiceForTesting( 
'ORESModelLookup' );
+       }
+
+       public function testPurgeScoreCache_noop() {
+               // FIXME: Shouldn't be necessary once we capture output.
+               $this->maintenance->loadWithArgv( [ '--quiet' ] );
+
+               $this->maintenance->execute();
+
+               // Well, this is dirty but the point I want to demonstrate is 
that
+               // the previous function didn't crash.
+               $this->assertTrue( true );
+       }
+
+       public function testPurgeScoreCache_bad_model() {
+               $revId = mt_rand( 1000, 9999 );
+               TestHelper::insertModelData();
+               TestHelper::insertOresData( $revId, [
+                       'damaging' => 0.1,
+               ] );
+
+               $this->maintenance->loadWithArgv( [ '--quiet', '--model', 
'not_a_thing' ] );
+
+               $this->maintenance->execute();
+
+               $remainingScores = \wfGetDB( DB_REPLICA )->select(
+                       [ 'ores_classification' ],
+                       [ 'oresc_rev', 'oresc_class', 'oresc_probability', 
'oresc_model' ],
+                       [ 'oresc_rev' => $revId ],
+                       __METHOD__
+               );
+
+               $this->assertEquals( [ (object)[
+                       'oresc_rev' => (string)$revId,
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.100',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ] ], iterator_to_array( $remainingScores, false ) );
+       }
+
+       public function testPurgeScoreCache_all() {
+               $revId = mt_rand( 1000, 9999 );
+               TestHelper::insertModelData();
+               TestHelper::insertOresData( $revId, [
+                       TestHelper::DAMAGING_OLD => 0.2,
+                       'damaging' => 0.1,
+               ] );
+
+               $this->maintenance->loadWithArgv( [ '--quiet', '--all' ] );
+
+               $this->maintenance->execute();
+
+               $remainingScores = \wfGetDB( DB_REPLICA )->select(
+                       [ 'ores_classification' ],
+                       [ 'oresc_rev', 'oresc_class', 'oresc_probability', 
'oresc_model' ],
+                       [ 'oresc_rev' => $revId ],
+                       __METHOD__
+               );
+
+               $this->assertEquals( [], iterator_to_array( $remainingScores, 
false ) );
+       }
+
+       public function testPurgeScoreCache_oldModels() {
+               $revId = mt_rand( 1000, 9999 );
+               TestHelper::insertModelData();
+               TestHelper::insertOresData( $revId, [
+                       TestHelper::DAMAGING_OLD => 0.2,
+                       'damaging' => 0.1,
+               ] );
+
+               $this->maintenance->loadWithArgv( [ '--quiet' ] );
+
+               $this->maintenance->execute();
+
+               $remainingScores = \wfGetDB( DB_REPLICA )->select(
+                       [ 'ores_classification' ],
+                       [ 'oresc_rev', 'oresc_class', 'oresc_probability', 
'oresc_model' ],
+                       [ 'oresc_rev' => $revId ],
+                       __METHOD__
+               );
+
+               $this->assertEquals( [ (object)[
+                       'oresc_rev' => (string)$revId,
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.100',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ] ], iterator_to_array( $remainingScores, false ) );
+       }
+
+       public function testPurgeScoreCache_nonRecent() {
+               $this->tablesUsed[] = 'recentchanges';
+
+               $revId = mt_rand( 1000, 9999 );
+               $revIdOld = $revId - 1;
+
+               TestHelper::insertModelData();
+               TestHelper::insertOresData( $revId, [
+                       'damaging' => 0.1,
+               ] );
+               TestHelper::insertOresData( $revIdOld, [
+                       'damaging' => 0.2,
+               ] );
+               \wfGetDB( DB_MASTER )->insert( 'recentchanges', [
+                       'rc_this_oldid' => $revId,
+                       'rc_user_text' => 'TestUser',
+               ], __METHOD__ );
+
+               $this->maintenance->loadWithArgv( [ '--quiet', '--old' ] );
+
+               $this->maintenance->execute();
+
+               $remainingScores = \wfGetDB( DB_REPLICA )->select(
+                       [ 'ores_classification' ],
+                       [ 'oresc_rev', 'oresc_class', 'oresc_probability', 
'oresc_model' ],
+                       [ 'oresc_rev' => $revId ],
+                       __METHOD__
+               );
+
+               $this->assertEquals( [ (object)[
+                       'oresc_rev' => (string)$revId,
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.100',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ] ], iterator_to_array( $remainingScores, false ) );
+       }
+
+       public function testPurgeScoreCache_oneModel() {
+               $revId = mt_rand( 1000, 9999 );
+               TestHelper::insertModelData();
+               TestHelper::insertOresData( $revId, [
+                       'damaging' => 0.1,
+                       'reverted' => 0.3,
+               ] );
+
+               $this->maintenance->loadWithArgv( [ '--quiet', '--model', 
'reverted', '--all' ] );
+
+               $this->maintenance->execute();
+
+               $remainingScores = \wfGetDB( DB_REPLICA )->select(
+                       [ 'ores_classification' ],
+                       [ 'oresc_rev', 'oresc_class', 'oresc_probability', 
'oresc_model' ],
+                       [ 'oresc_rev' => $revId ],
+                       __METHOD__
+               );
+
+               $this->assertEquals( [ (object)[
+                       'oresc_rev' => (string)$revId,
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.100',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ] ], iterator_to_array( $remainingScores, false ) );
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/403839
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1deeb809e27c2daddf9f221fb0ea2cbd36aa166b
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

Reply via email to