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

Change subject: [WIP] Tests for PopulateDatabase
......................................................................

[WIP] Tests for PopulateDatabase

Bug: T184140
Change-Id: I6fec77e85e1cc45e10d855f8edcef2d3e891cf64
---
A tests/phpunit/maintenance/PopulateDatabaseTest.php
M tests/phpunit/maintenance/PurgeScoreCacheTest.php
2 files changed, 363 insertions(+), 6 deletions(-)


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

diff --git a/tests/phpunit/maintenance/PopulateDatabaseTest.php 
b/tests/phpunit/maintenance/PopulateDatabaseTest.php
new file mode 100644
index 0000000..070138b
--- /dev/null
+++ b/tests/phpunit/maintenance/PopulateDatabaseTest.php
@@ -0,0 +1,359 @@
+<?php
+
+namespace ORES\Tests\Maintenance;
+
+use MediaWiki\MediaWikiServices;
+
+use ORES\Maintenance\PopulateDatabase;
+use ORES\ORESService;
+
+use ORES\Tests\TestHelper;
+
+/**
+ * @group ORES
+ * @group Database
+ * @covers ORES\Maintenance\PopulateDatabase
+ */
+class PopulateDatabaseTest extends \MediaWikiTestCase {
+
+       public function setUp() {
+               parent::setUp();
+               $this->tablesUsed = [
+                       'ores_classification',
+                       'ores_model',
+                       'recentchanges',
+               ];
+
+               $this->maintenance = new PopulateDatabase();
+
+               TestHelper::clearOresTables();
+               TestHelper::insertModelData();
+               \wfGetDB( DB_MASTER )->delete( 'recentchanges', '*', __METHOD__ 
);
+
+               $this->setMwGlobals( [
+                       'wgOresWikiId' => 'wiki',
+               ] );
+               $this->mockOresService = $this->getMockBuilder( 
ORESService::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $this->setService( 'ORESService', $this->mockOresService );
+       }
+
+       public function testPopulateDatabase_single() {
+               \wfGetDB( DB_MASTER )->insert( 'recentchanges', [
+                       [ 'rc_this_oldid' => '123', 'rc_user_text' => 
'TestUser' ],
+               ], __METHOD__ );
+               $this->mockOresService->method( 'request' )
+                       ->with(
+                               $this->equalTo( [
+                                       'models' => 
'damaging|goodfaith|reverted|wp10',
+                                       'revids' => '123',
+                               ] ),
+                               $this->equalTo( null )
+                       )->willReturn( [
+                               'wiki' => [
+                                       'models' => [
+                                               'damaging' => [
+                                                       'version' => '0.1',
+                                               ],
+                                       ],
+                                       'scores' => [
+                                               '123' => [
+                                                       'damaging' => [
+                                                               'score' => [
+                                                                       
'prediction' => false,
+                                                                       
'probability' => [
+                                                                               
'false' => 0.1,
+                                                                               
'true' => 0.9,
+                                                                       ],
+                                                               ],
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ] );
+
+               $this->maintenance->loadWithArgv( [ '--quiet' ] );
+               $this->maintenance->execute();
+
+               $scores = \wfGetDB( DB_REPLICA )->select(
+                       [ 'ores_classification' ],
+                       [ 'oresc_rev', 'oresc_class', 'oresc_probability', 
'oresc_model' ],
+                       null,
+                       __METHOD__
+               );
+
+               $this->assertEquals( [ (object)[
+                       'oresc_rev' => '123',
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.900',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ] ], iterator_to_array( $scores, false ) );
+       }
+
+       public function testPopulateDatabase_oneExists() {
+               \wfGetDB( DB_MASTER )->insert( 'recentchanges', [
+                       [ 'rc_this_oldid' => '123', 'rc_user_text' => 
'TestUser' ],
+                       [ 'rc_this_oldid' => '321', 'rc_user_text' => 
'TestUser' ],
+               ], __METHOD__ );
+               TestHelper::insertOresData( 123, [
+                       'damaging' => 0.1,
+               ] );
+               $this->mockOresService->method( 'request' )
+                       ->with(
+                               $this->equalTo( [
+                                       'models' => 
'damaging|goodfaith|reverted|wp10',
+                                       'revids' => '321',
+                               ] ),
+                               $this->equalTo( null )
+                       )->willReturn( [
+                               'wiki' => [
+                                       'models' => [
+                                               'damaging' => [
+                                                       'version' => '0.1',
+                                               ],
+                                       ],
+                                       'scores' => [
+                                               '321' => [
+                                                       'damaging' => [
+                                                               'score' => [
+                                                                       
'prediction' => false,
+                                                                       
'probability' => [
+                                                                               
'false' => 0.1,
+                                                                               
'true' => 0.9,
+                                                                       ],
+                                                               ],
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ] );
+
+               $this->maintenance->loadWithArgv( [ '--quiet' ] );
+               $this->maintenance->execute();
+
+               $scores = \wfGetDB( DB_REPLICA )->select(
+                       [ 'ores_classification' ],
+                       [ 'oresc_rev', 'oresc_class', 'oresc_probability', 
'oresc_model' ],
+                       null,
+                       __METHOD__
+               );
+
+               $this->assertEquals( [ (object)[
+                       'oresc_rev' => '123',
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.100',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ], (object)[
+                       'oresc_rev' => '321',
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.900',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ] ], iterator_to_array( $scores, false ) );
+       }
+
+       public function testPopulateDatabase_limited() {
+               \wfGetDB( DB_MASTER )->insert( 'recentchanges', [
+                       [ 'rc_this_oldid' => '123', 'rc_user_text' => 
'TestUser' ],
+                       [ 'rc_this_oldid' => '321', 'rc_user_text' => 
'TestUser' ],
+               ], __METHOD__ );
+               $this->mockOresService->method( 'request' )
+                       ->with(
+                               $this->equalTo( [
+                                       'models' => 
'damaging|goodfaith|reverted|wp10',
+                                       'revids' => '321',
+                               ] ),
+                               $this->equalTo( null )
+                       )->willReturn( [
+                               'wiki' => [
+                                       'models' => [
+                                               'damaging' => [
+                                                       'version' => '0.1',
+                                               ],
+                                       ],
+                                       'scores' => [
+                                               '321' => [
+                                                       'damaging' => [
+                                                               'score' => [
+                                                                       
'prediction' => false,
+                                                                       
'probability' => [
+                                                                               
'false' => 0.1,
+                                                                               
'true' => 0.9,
+                                                                       ],
+                                                               ],
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ] );
+
+               $this->maintenance->loadWithArgv( [
+                       '--quiet',
+                       '--batch', '1',
+                       '--number', '1',
+               ] );
+               $this->maintenance->execute();
+
+               $scores = \wfGetDB( DB_REPLICA )->select(
+                       [ 'ores_classification' ],
+                       [ 'oresc_rev', 'oresc_class', 'oresc_probability', 
'oresc_model' ],
+                       null,
+                       __METHOD__
+               );
+
+               $this->assertEquals( [ (object)[
+                       'oresc_rev' => '321',
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.900',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ] ], iterator_to_array( $scores, false ) );
+       }
+
+       public function testPopulateDatabase_oneBot() {
+               $this->setMwGlobals( [
+                       'wgOresExcludeBots' => true,
+               ] );
+               \wfGetDB( DB_MASTER )->insert( 'recentchanges', [
+                       [ 'rc_this_oldid' => '123', 'rc_user_text' => 
'TestUser', 'rc_bot' => '0' ],
+                       [ 'rc_this_oldid' => '321', 'rc_user_text' => 
'TestUser', 'rc_bot' => '1' ],
+               ], __METHOD__ );
+               $this->mockOresService->method( 'request' )
+                       ->with(
+                               $this->equalTo( [
+                                       'models' => 
'damaging|goodfaith|reverted|wp10',
+                                       'revids' => '123',
+                               ] ),
+                               $this->equalTo( null )
+                       )->willReturn( [
+                               'wiki' => [
+                                       'models' => [
+                                               'damaging' => [
+                                                       'version' => '0.1',
+                                               ],
+                                       ],
+                                       'scores' => [
+                                               '123' => [
+                                                       'damaging' => [
+                                                               'score' => [
+                                                                       
'prediction' => false,
+                                                                       
'probability' => [
+                                                                               
'false' => 0.8,
+                                                                               
'true' => 0.2,
+                                                                       ],
+                                                               ],
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ] );
+
+               $this->maintenance->loadWithArgv( [ '--quiet' ] );
+               $this->maintenance->execute();
+
+               $scores = \wfGetDB( DB_REPLICA )->select(
+                       [ 'ores_classification' ],
+                       [ 'oresc_rev', 'oresc_class', 'oresc_probability', 
'oresc_model' ],
+                       null,
+                       __METHOD__
+               );
+
+               $this->assertEquals( [ (object)[
+                       'oresc_rev' => '123',
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.200',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ] ], iterator_to_array( $scores, false ) );
+       }
+
+       public function testPopulateDatabase_twoBatches() {
+               \wfGetDB( DB_MASTER )->insert( 'recentchanges', [
+                       [ 'rc_this_oldid' => '123', 'rc_user_text' => 
'TestUser' ],
+                       [ 'rc_this_oldid' => '321', 'rc_user_text' => 
'TestUser' ],
+               ], __METHOD__ );
+               $this->mockOresService->expects( $this->at( 0 ) )
+                       ->method( 'request' )
+                       ->with(
+                               $this->equalTo( [
+                                       'models' => 
'damaging|goodfaith|reverted|wp10',
+                                       'revids' => '123',
+                               ] ),
+                               $this->equalTo( null )
+                       )->willReturn( [
+                               'wiki' => [
+                                       'models' => [
+                                               'damaging' => [
+                                                       'version' => '0.1',
+                                               ],
+                                       ],
+                                       'scores' => [
+                                               '123' => [
+                                                       'damaging' => [
+                                                               'score' => [
+                                                                       
'prediction' => false,
+                                                                       
'probability' => [
+                                                                               
'false' => 0.1,
+                                                                               
'true' => 0.9,
+                                                                       ],
+                                                               ],
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ] );
+               $this->mockOresService->expects( $this->at( 1 ) )
+                       ->method( 'request' )
+                       ->with(
+                               $this->equalTo( [
+                                       'models' => 
'damaging|goodfaith|reverted|wp10',
+                                       'revids' => '321',
+                               ] ),
+                               $this->equalTo( null )
+                       )->willReturn( [
+                               'wiki' => [
+                                       'models' => [
+                                               'damaging' => [
+                                                       'version' => '0.1',
+                                               ],
+                                       ],
+                                       'scores' => [
+                                               '321' => [
+                                                       'damaging' => [
+                                                               'score' => [
+                                                                       
'prediction' => false,
+                                                                       
'probability' => [
+                                                                               
'false' => 0.8,
+                                                                               
'true' => 0.2,
+                                                                       ],
+                                                               ],
+                                                       ],
+                                               ],
+                                       ],
+                               ],
+                       ] );
+
+               $this->maintenance->loadWithArgv( [
+                       '--quiet',
+                       '--batch', '1',
+               ] );
+               $this->maintenance->execute();
+
+               $scores = \wfGetDB( DB_REPLICA )->select(
+                       [ 'ores_classification' ],
+                       [ 'oresc_rev', 'oresc_class', 'oresc_probability', 
'oresc_model' ],
+                       null,
+                       __METHOD__
+               );
+
+               $this->assertEquals( [ (object)[
+                       'oresc_rev' => '123',
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.900',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ], (object)[
+                       'oresc_rev' => '321',
+                       'oresc_class' => '1',
+                       'oresc_probability' => '0.200',
+                       'oresc_model' => (string)TestHelper::DAMAGING,
+               ] ], iterator_to_array( $scores, false ) );
+       }
+}
diff --git a/tests/phpunit/maintenance/PurgeScoreCacheTest.php 
b/tests/phpunit/maintenance/PurgeScoreCacheTest.php
index 9209716..3dafa78 100644
--- a/tests/phpunit/maintenance/PurgeScoreCacheTest.php
+++ b/tests/phpunit/maintenance/PurgeScoreCacheTest.php
@@ -37,12 +37,15 @@
                $this->maintenance = new PurgeScoreCache();
 
                TestHelper::clearOresTables();
+               TestHelper::insertModelData();
 
                // Reset service to purge cached models.
                MediaWikiServices::getInstance()->resetServiceForTesting( 
'ORESModelLookup' );
        }
 
-       public function testPurgeScoreCache_noop() {
+       public function testPurgeScoreCache_emptyDb() {
+               TestHelper::clearOresTables();
+
                // FIXME: Shouldn't be necessary once we capture output.
                $this->maintenance->loadWithArgv( [ '--quiet' ] );
 
@@ -55,7 +58,6 @@
 
        public function testPurgeScoreCache_bad_model() {
                $revId = mt_rand( 1000, 9999 );
-               TestHelper::insertModelData();
                TestHelper::insertOresData( $revId, [
                        'damaging' => 0.1,
                ] );
@@ -81,7 +83,6 @@
 
        public function testPurgeScoreCache_all() {
                $revId = mt_rand( 1000, 9999 );
-               TestHelper::insertModelData();
                TestHelper::insertOresData( $revId, [
                        TestHelper::DAMAGING_OLD => 0.2,
                        'damaging' => 0.1,
@@ -103,7 +104,6 @@
 
        public function testPurgeScoreCache_oldModels() {
                $revId = mt_rand( 1000, 9999 );
-               TestHelper::insertModelData();
                TestHelper::insertOresData( $revId, [
                        TestHelper::DAMAGING_OLD => 0.2,
                        'damaging' => 0.1,
@@ -134,7 +134,6 @@
                $revId = mt_rand( 1000, 9999 );
                $revIdOld = $revId - 1;
 
-               TestHelper::insertModelData();
                TestHelper::insertOresData( $revId, [
                        'damaging' => 0.1,
                ] );
@@ -167,7 +166,6 @@
 
        public function testPurgeScoreCache_oneModel() {
                $revId = mt_rand( 1000, 9999 );
-               TestHelper::insertModelData();
                TestHelper::insertOresData( $revId, [
                        'damaging' => 0.1,
                        'reverted' => 0.3,

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

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