jenkins-bot has submitted this change and it was merged.

Change subject: QUnit tests for client banner allocations
......................................................................


QUnit tests for client banner allocations

Change-Id: I58eee6f15f0d01f7d8e4d57020d4148ddd8d5152
---
M tests/data/AllocationsFixtures.json
A 
tests/qunit/ext.centralNotice.bannerController.lib/bannerController.lib.tests.js
M tests/qunit/ext.centralNotice.bannerController/bannerController.tests.js
3 files changed, 115 insertions(+), 7 deletions(-)

Approvals:
  Ejegg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/tests/data/AllocationsFixtures.json 
b/tests/data/AllocationsFixtures.json
index cdf4ba6..d19c821 100644
--- a/tests/data/AllocationsFixtures.json
+++ b/tests/data/AllocationsFixtures.json
@@ -78,8 +78,9 @@
         "choices": [
             {
                 "name": "c1",
-                "throttle": 60,
                 "geotargetted": false,
+                "preferred": 3,
+                "throttle": 60,
                 "banners": [
                     {
                         "name": "c1b1",
@@ -95,6 +96,8 @@
             },
             {
                 "name": "c2",
+                "preferred": 1,
+                "throttle": 100,
                 "banners": [
                     {
                         "name": "c2b1",
@@ -179,6 +182,7 @@
         },
         "choices": [
             {
+                "throttle": 10,
                 "banners": [
                     { "name": "b1" }
                 ]
@@ -210,8 +214,20 @@
             ]
         },
         "choices": [
-            { "name": "c1" },
-            { "name": "c2" }
+            {
+                "name": "c1",
+                "preferred": 1,
+                "banners": [
+                    { "name": "c1b1" }
+                ]
+            },
+            {
+                "name": "c2",
+                "preferred": 2,
+                "banners": [
+                    { "name": "c2b1" }
+                ]
+            }
         ],
         "allocations": {
             "c1b1": 0.000,
diff --git 
a/tests/qunit/ext.centralNotice.bannerController.lib/bannerController.lib.tests.js
 
b/tests/qunit/ext.centralNotice.bannerController.lib/bannerController.lib.tests.js
new file mode 100644
index 0000000..e647a6b
--- /dev/null
+++ 
b/tests/qunit/ext.centralNotice.bannerController.lib/bannerController.lib.tests.js
@@ -0,0 +1,86 @@
+( function( mw, $ ) {
+       'use strict';
+
+       var defaultCampaignData = {
+               banners: [],
+               bucket_count: 1,
+               countries: [],
+               geotargetted: 0,
+               preferred: 1,
+               throttle: 100
+       }, defaultBannerData = {
+               bucket: 0,
+               devices: [ 'desktop' ],
+               weight: 25
+       };
+
+       QUnit.module( 'ext.centralNotice.bannerController.lib', 
QUnit.newMwEnvironment( {
+               setup: function() {
+                       mw.centralNotice.data.country = 'XX';
+                       mw.centralNotice.data.device = 'desktop';
+                       mw.centralNotice.data.bucket = 0;
+               }
+       } ) );
+
+       QUnit.asyncTest( 'allocations test cases', function( assert ) {
+               $.ajax( {
+                       url: mw.config.get( 'wgExtensionAssetsPath' )
+                               + 
'/CentralNotice/tests/data/AllocationsFixtures.json'
+               } ).done( function( testCases ) {
+                       // Declare the number of test cases
+                       assert.ok( testCases.length );
+                       QUnit.expect( testCases.length + 1 );
+
+                       $.each( testCases, function( index, testCaseInputs ) {
+                               var testCase = testCaseInputs[0],
+                                       lib = mw.cnBannerControllerLib,
+                                       choices,
+                                       i,
+                                       allocatedBanner;
+
+                               // Flesh out choice data with some default 
values
+                               // BOOM on priority case
+                               choices = $.map( testCase.choices, function( 
campaign ) {
+                                       return $.extend( {}, 
defaultCampaignData, campaign, {
+                                               banners: $.map( 
campaign.banners, function( banner ) {
+                                                       return $.extend( {}, 
defaultBannerData, banner );
+                                               } )
+                                       } );
+                               } );
+
+                               // TODO: would like to declare individual tests 
here, but I
+                               // haven't been able to make that work, yet.
+                               lib.setChoiceData( choices );
+                               lib.filterChoiceData();
+                               lib.calculateBannerAllocations();
+
+                               // TODO: the errors will not reveal anything 
useful about
+                               // which case this is, and what happened.  So 
we throw
+                               // exceptions manually.  The horror!
+                               try {
+                                       if ( lib.possibleBanners.length !== 
Object.keys( testCase.allocations ).length ) {
+                                               throw 'Wrong number of banners 
allocated in "' + testCase.title + '".';
+                                       }
+                                       for ( i = 0; i < 
lib.possibleBanners.length; i++ ) {
+                                               allocatedBanner = 
lib.possibleBanners[i];
+                                               if ( Math.abs( 
allocatedBanner.allocation - testCase.allocations[allocatedBanner.name] ) > 
0.001 ) {
+                                                       throw 'Banner ' + 
allocatedBanner.name + ' was misallocated in "' + testCase.title + '".';
+                                               }
+                                       }
+                               } catch ( error ) {
+                                       assert.ok( false, error
+                                               + " expected: " + 
QUnit.jsDump.parse( testCase.allocations )
+                                               + ", actual: " + 
QUnit.jsDump.parse( lib.possibleBanners )
+                                       );
+                                       return;
+                               }
+                               assert.ok( true, 'Allocations match in "' + 
testCase.title + '"' );
+                       } );
+
+                       QUnit.start();
+               } );
+       } );
+
+       // TODO: chooser tests
+
+} ( mediaWiki, jQuery ) );
diff --git 
a/tests/qunit/ext.centralNotice.bannerController/bannerController.tests.js 
b/tests/qunit/ext.centralNotice.bannerController/bannerController.tests.js
index 6273eef..91aa12d 100644
--- a/tests/qunit/ext.centralNotice.bannerController/bannerController.tests.js
+++ b/tests/qunit/ext.centralNotice.bannerController/bannerController.tests.js
@@ -22,13 +22,15 @@
                        // Prevent banner load during initialize().
                        mw.centralNotice.loadBanner = function () {};
 
-                       $.extend( mw.centralNotice.data.getVars, {
-                               // Suppress GeoIP call
-                               country: 'US',
+                       // Force to the first bucket.
+                       mw.centralNotice.getBucket = function() { return 0; };
 
-                               // Boring defaults, assumed by test fixtures
+                       $.extend( mw.centralNotice.data.getVars, {
+                               // Boring defaults, assumed by test fixtures.
                                // FIXME: move to tests that actually assume 
this.  Move the
                                // initialize() call as well.
+                               // FIXME: see below
+                               country: 'XX',
                                uselang: 'en',
                                project: 'wikipedia',
                                anonymous: true
@@ -38,6 +40,10 @@
                        // Do it before initialize, so nothing 
                        $( '#siteNotice' ).remove();
 
+                       // Sigh.  Suppress the GeoIP call, and prevent any 
other side-
+                       // effects, unless $.ajax is explictly mocked by a test 
case.
+                       $.ajax = function() { return $.Deferred(); };
+
                        mw.centralNotice.initialize();
 
                        mw.centralNotice.loadBanner = realLoadBanner;

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I58eee6f15f0d01f7d8e4d57020d4148ddd8d5152
Gerrit-PatchSet: 18
Gerrit-Project: mediawiki/extensions/CentralNotice
Gerrit-Branch: master
Gerrit-Owner: Awight <[email protected]>
Gerrit-Reviewer: AndyRussG <[email protected]>
Gerrit-Reviewer: Awight <[email protected]>
Gerrit-Reviewer: Ejegg <[email protected]>
Gerrit-Reviewer: Katie Horn <[email protected]>
Gerrit-Reviewer: Mwalker <[email protected]>
Gerrit-Reviewer: Ssmith <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to