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

Change subject: Tests for ScoreFetcher
......................................................................

Tests for ScoreFetcher

By mocking ORES service reponse, now it's possible to easily test it

Bug: T184142
Change-Id: I5707e71891167938d831c9bceb91faf33827ca91
---
M tests/phpunit/includes/ScoreFetcherTest.php
1 file changed, 109 insertions(+), 4 deletions(-)


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

diff --git a/tests/phpunit/includes/ScoreFetcherTest.php 
b/tests/phpunit/includes/ScoreFetcherTest.php
index 23356cd..095b9dc 100644
--- a/tests/phpunit/includes/ScoreFetcherTest.php
+++ b/tests/phpunit/includes/ScoreFetcherTest.php
@@ -2,6 +2,7 @@
 
 namespace ORES\Tests;
 
+use ORES\ORESService;
 use ORES\ScoreFetcher;
 use ORES\Storage\HashModelLookup;
 
@@ -24,7 +25,110 @@
                        'goodfaith' => [ 'id' => self::GOODFAITH, 'version' => 
'0.0.3' ],
                ];
                $this->setService( 'ORESModelLookup', new HashModelLookup( 
$modelData ) );
+               $this->setService( 'ORESService', $this->getORESServiceMock() );
+               $this->setMwGlobals( [
+                       'wgOresModels' => [ 'damaging' => true ],
+               ] );
                $this->tablesUsed[] = 'ores_model';
+       }
+
+       private function getORESServiceMock() {
+               $mock = $this->getMockBuilder( ORESService::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $mock->expects( $this->any() )
+                       ->method( 'request' )
+                       ->willReturnCallback( [ $this, 'mockORESResponse' ] );
+
+               return $mock;
+       }
+
+       public static function mockORESResponse( array $params, 
$originalRequest = null ) {
+               $models = [];
+               foreach ( explode( '|', $params['models'] ) as $model ) {
+                       $models[$model] = [ 'version' => '0.0.4' ];
+               }
+
+               $scores = [];
+               foreach ( explode( '|', $params['revids'] ) as $revid ) {
+                       $scores[(string)$revid] = self::mockRevisionResponse( 
$revid, array_keys( $models ) );
+               }
+
+               return [ ORESService::getWikiID() => [ 'models' => $models, 
'scores' => $scores ] ];
+       }
+
+       public static function mockRevisionResponse( $revid, $models ) {
+               $result = [];
+               foreach ( $models as $model ) {
+                       $result[$model] = [ 'score' => [] ];
+                       $probability = (float)strrev( substr( $revid, -2 ) ) / 
100;
+                       $result[$model]['score']['probability'] = [
+                               'true' => $probability,
+                               'false' => 1 - $probability
+                       ];
+                       $result[$model]['score']['prediction'] = $probability > 
0.5;
+               }
+               return $result;
+       }
+
+       public function provideTestGetScores() {
+               $firstCase = [
+                       '123' => [
+                               'damaging' => [ 'score' => [
+                                       'prediction' => false,
+                                       'probability' => [ 'true' => 0.32, 
'false' => 0.68 ]
+                               ] ]
+                       ]
+               ];
+               $secondCase = $firstCase;
+               $secondCase['123']['goodfaith'] = [
+                       'score' => [
+                               'prediction' => false,
+                               'probability' => [ 'true' => 0.32, 'false' => 
0.68 ]
+                       ]
+               ];
+               return [
+                       [ $firstCase, 123, 'damaging', true, null ],
+                       [ $secondCase, 123, 'damaging|goodfaith', true, null ],
+                       [ $firstCase, 123, 'damaging', false, null ],
+                       [ $secondCase, 123, 'damaging|goodfaith', false, null ],
+                       [ $firstCase, 123, null, true, null ],
+                       [ $firstCase, 123, null, false, null ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideTestGetScores
+        * @covers ORES\ScoreFetcher::getScores
+        */
+       public function testGetScores( $expected, $revisions, $models, 
$precache, $originalRequest ) {
+               $scoreFetcher = ScoreFetcher::instance();
+               $result = $scoreFetcher->getScores( $revisions, $models, 
$precache, $originalRequest );
+               $this->assertEquals( $expected, $result );
+               $res = wfGetDB( DB_REPLICA )->select(
+                       'ores_model',
+                       [ 'oresm_name', 'oresm_version', 'oresm_is_current' ],
+                       '',
+                       __METHOD__
+               );
+
+               $result = iterator_to_array( $res, false );
+
+               if ( $models === null ) {
+                       global $wgOresModels;
+                       $models = implode( '|', array_keys( array_filter( 
$wgOresModels ) ) );
+               }
+
+               $expected = [];
+               foreach ( explode( '|', $models ) as $model ) {
+                       $expected[] = (object)[
+                               'oresm_name' => $model,
+                               'oresm_version' => '0.0.4',
+                               'oresm_is_current' => '1'
+                       ];
+               }
+               $this->assertEquals( $expected, $result );
        }
 
        public function provideTestCheckModelVersion() {
@@ -39,6 +143,7 @@
 
        /**
         * @dataProvider provideTestCheckModelVersion
+        * @covers ORES\ScoreFetcher::checkModelVersion
         */
        public function testCheckModelVersion( $expected, $model, array 
$response ) {
                $scoreFetcher = ScoreFetcher::instance();
@@ -46,6 +151,9 @@
                $this->assertSame( $expected, $scoreFetcher->checkModelVersion( 
$model, $response ) );
        }
 
+       /**
+        * @covers ORES\ScoreFetcher::updateModelVersion
+        */
        public function testUpdateModelVersion() {
                $dbw = \wfGetDB( DB_MASTER );
                $dbw->insert( 'ores_model',
@@ -66,10 +174,7 @@
                        __METHOD__
                );
 
-               $result = [];
-               foreach ( $res as $row ) {
-                       $result[] = $row;
-               }
+               $result = iterator_to_array( $res, false );
 
                $expected = [
                        (object)[

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5707e71891167938d831c9bceb91faf33827ca91
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ORES
Gerrit-Branch: master
Gerrit-Owner: Ladsgroup <ladsgr...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to